Training SVM in Python with pictures - python

I have basic knowledge of SVM, but now I am working with images. I have images in 5 folders, each folder, for example, has images for letters a, b, c, d, e. The folder 'a' has images of handwriting letters for 'a, folder 'b' has images of handwriting letters for 'b' and so on.
Now how can I use the images as my training data in SVM in Python.

as far i understood you want to train your svm to classify these images into the classes named a,b,c,d . For that you can use any of the good image processing techniques to extract features (such as HOG which is nicely implemented in opencv) from your image and then use these features , and the label as the input to your SVM training (the corresponding label for those would be the name of the folders i.e a,b,c,d) you can train your SVM using the features only and during the inference time , you can simply calculate the HOG feature of the image and feed it to your SVM and it will give you the desired output.

Related

Best approach to correlate images, based on their tags

I was trying to tackle an ML problem with tensor flow, but im not sure what algorithm should I use. I have tagged images on my dataset. When a new image comes on, i want the to correlate the images I have, based on the tags. Where should I start? O.o
What do you mean by correlate the images? Are you attempting to cluster the images based on their tags?
If so, you could train an encoder that runs over your images, produces a feature vector and cluster those feature vectors based on their image tags. So for example, consider you had multiple images of tags: cars & cats. You could run an encoder (consisting of convolutional layers), flatten the final layer to get a feature vector and run a clustering algorithm like K-means (with K=2, since you only have 2 tags -cars & cats).
Depending on the size and nature of the images in your dataset you might have to play around with the encoder architecture, collect more data, use alternate clustering algorithms etc.
In the event your image feature vector can belong to multiple classes and you would like to return possible tags, you'll have to opt for soft clustering algorithms such as GMMs (Gaussian Mixture Models) or FCMs (Fuzzy C Means). These algorithms don't specifically output class but outputs a class score for each data point. So if you want the top 5 tags of a new image, you could:
Run an encoder to get a feature vector
Perform soft clustering on the feature vectors
Get the 5 highest scoring classes

How to train OpenCV SVM classifier on images

The purpose of the task is to classify images by means of SVM. The variable 'images' is supposed to contain the image information and correspondingly labels contains image labels. How can I build (what format and dimensions) should the images and labels have? I tried unsuccesfully images to be a Python array (appending flattened images) and then, in another attempt, Numpy arrays:
images=np.zeros((number_of_images, image_size))
labels=np.zeros((number_of_images, 1))
svm=cv2.SVM()
svm.train(images, labels)
Is it a right approach to the problem and if so, what is the correct way for training the classifier?
I don't think that you can use raw image data to train SVM model. Ok-ok, you can, but it won't be very fruitful.
The basic approach is to extract some features from each image and to use these features for training your model. A set of features forms a dictionary of words, each of which describes your image. Due to the fact that you are using the same set of words to describe each image, you can compare features corresponding to different images. This link introduces more details, check it.
Whats next?
Choose a feature extractor for your algo - HOG, SURF, SIFT (link)
Extract features from each image. You'll get an array of the same length as images array.
Initialize bag-of-words (BoG) model
Train SVM with BoG
Useful links:
C++ vey detailed example
Documentation for existing BOG classifier

Image classification using SVM Python

I am currently working on a projet to perform image recognition. There is a big set of images and I have to predict whether or not an image contains given characteristics.
For example, the output could be whether or not there is a banana in the picture.
I would like to implement a classifier using SVM with output yes or no the image contains the given characteristics. What is the simplest way to train a SVM classifier on images with 2 outputs? Is there any template to use in Python? Thanks a lot.
With SVM you can classify set of images.For example You can train svm with set of car and plane images.Once you trained it can predict the class of an unknown images as whether it is car or plane.There is also multiclass SVM.
In your case,Make two sets of images for training SVM
Set of images that contain given characteristics(banana)
Set of images that doesn't contain that characteristics
Once your training phase completed it will output to which class the given image belong.If its in banana class you can output as Yes otherwise No.
Usefull links
Hand written Digit Recognition using python opencv
Squirrel and Bird Classifier using java
Edit
Fruit classifier using python

What features of images will produce good result used in SVM multiclass image classification?

I am using opencv2.4 and python 2.7.What features of images can be used for svm classification.I have gone through surf and sift but as a beginner it seems very difficult to me.What are the other feature extraction techniques?
If you are looking for simplest representation then this will help you.These two are very simple compared to other SIFT and SURF
Bitmap representation
HOG-Histogram of Gradients
SVM is a machine learning model for data classification.I have built a simple svm classifier.If you have two folder of images,birds and squireels.The steps i followed are
Extracted Hog features of Images and append that in a list
for file in listing1:
img = cv2.imread(path1 + file)
res=cv2.resize(img,(250,250))
h=hog(res)
training_set.append(h)
append the labels also
training_labels.append(1)
convert both lists to numpy array.
trainData=np.float32(training_set)
responses=np.float32(training_labels)
Train SVM
svm.train(trainData,responses, params=svm_params)
Test SVM
result = svm.predict_all(testData)
print result

What it mean by Training SVM

I am new to image processing.As my project i am doing "image classifier using SVM".I have the idea of my final software "I select some image and give it as input to my software and it will classify that image .if i give the image of an animal it will classify it to cat or snake suitably"
When I google about it.it says "First you need to train SVM"
What it mean by Training SVM?
What is the actual input to SVM in my case(image classification)?
SVM is just a classifier how it classify images.Is it necessary for me to covert image to any particular format?.please help.
Support Vector Machine (SVM) is a machine learning model for supervised data classification. SVMs essentially learn a hyper-plane which separates the data space into 2 regions (in 2 class case). In your case, suppose you have images of snakes and cats and you need to classify them. The steps you'll need to follow are
Extract 'features' from the images.
These 'features' may be functions of appearance of snake/cat in your case e.g colour of the animal, shape of the animal etc. By concatenating these features you can get a multi-dimensional feature vector.
Train an SVM classifier
Training essentially learns a separating hyper-plane between the feature vectors of snake class and cat class . For example, if your feature vector is 2-dimensional, training an SVM classifier would amount to 'learning' a line which best separates your labeled-data/training-data.
You could use any of the multitude of freely available libraries of machine learning. In case you speak python, you could use sklearn for the task.
This task of learning (hyper-plane in linear SVM) is referred to training.
Classify the images.
Once you have trained your model, you could then use it classify images whose class is not known.
Note: I am simplifying a lot of details/issues involved in this answer. I suggest you should read-up about SVM

Categories