Understanding SVMs’: For Image Classification

--

Hello friends! Welcome back… In this fourth tutorial we are going to understand Support Vector Machines. An algorithm that intuitively works on creating linear decision boundaries to classify multiple classes.

Definition

Support vector machines are supervised learning models with associated learning algorithms that analyze data used for classification and regression analysis. Yess, you read it right… It can also be used for regression problems. We will look at the power of SVMs for classification.

Some theory before hand!

Degree of confidence measure the probability of misclassification. Hence we define terms functional margin and geometric margin. A functional margin tells you about the accuracy of classification of a point. Geometric margin on the other hand, is the normalised version of funcional margin and tells us about the euclidean distance between the hyperplane(or linear classifier) and the data points. For more theory, I suggest going through Christopher M Bishop’s book on Pattern Recognition and Machine Learning.

Coding Time

In machine learning, the dataset entirely decides the fate of the algorithms. SVM being a supervised learning algorithm requires clean, annotated data. So do we have to depend on others to provide datasets? Absolutely not. Creating dataset using Bing/ Google Image search APIS and then labelling them using Dataturks tool simplifies the entire process, and adds flexibility to the process of machine learning.

You can download pre-exiting datasets of various use cases like cancer detection to characters in Game of Thrones. Here is various image classification datasets.

Or if you have your unique use case, you can create your very own dataset for it. You can download images from the web and to make a big dataset in no time, use an annotation tool like Dataturks, where you upload the images and tag images manually in a ziffy.

Let’s import an annotated dataset from dataturks website. We can download the dataset in the form of a JSON file, which has the image URL and its label as its parameters. Let’s extract the images by running the following code.

Once we have imported the dataset, let’s classify the images using SVMs. The speciality of CNNS is that feature extraction seems to be a cakewalk, as convolution takes care of the process of feature extraction with pooling. Don’t worry if these terms feel new to you! We have a detailed tutorial on CNNs. Feature extraction in the case of SVMs is really important. Thus, we start off initially with feature extraction.

Feature Extraction:

The feature extraction is an important engineering process, for it is the main information about the raw data, that the algorithm identifies. Selecting the most meaningful features is a crucial step in the process of classification problems because:

  1. It is necessary to find all possible feature subsets that can be formed from the initial set of data
  2. Every feature is meaningful for at least some of discriminations, and
  3. Variations within intraclass and between inter-class is not too much high.

The selected set of features should be a small set whose values efficiently discriminate among patterns of different classes, but are similar for patterns within the same class. Features can be classified into two categories:

  1. Local features, which are usually geometric
  2. Global features, which are usually topological or statistical.

Feature Extraction algorithms can be classified into three categories.

  1. Statistical Features: The features are derived from statistical distribution of points, resulting in high speed and lower complexity features. There are various statistical features like zoning, characteristic loci and crossing and distance.
  2. Global Transformation and Series Expansion Features: These features are invariant to global deformations like translation and rotation. A continuous signal contains sufficient information to be classified linearly.
    1. Fourier Transform
    2. Rapid Transform
    3. Hough Transform
    4. Gabor Transform
    5.Wavelets
    6. Moments
  3. Geometrical and Topological Features: These features may represent global and local properties of characters and have high tolerances to distortions and style variations. These topological features may encode some knowledge about the contour of the object or may require some knowledge as to what sort of components make up that object.

Code Timeee!!!

Let’s use Global Features for our task. To do so, we have inbuilt functions under OpenCV, mahotas and sklearn libraries.

def fd_hu_moments(image):
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
feature = cv2.HuMoments(cv2.moments(image)).flatten()
return feature
def fd_haralick(image): # convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# compute the haralick texture feature vector
haralick = mahotas.features.haralick(gray).mean(axis=0)
return haralick

def fd_histogram(image, mask=None):
# convert the image to HSV color-space
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# compute the color histogram
hist = cv2.calcHist([image], [0, 1, 2], None, [bins, bins, bins], [0, 256, 0, 256, 0, 256])
# normalize the histogram
cv2.normalize(hist, hist)
hist.flatten()

Once you have all calculates features for all images in your dataset, concatenate all the features obtained. This process of concatenation reduces the correlation between features thus making linear classification more efficient.

global_feature = np.hstack([fd_histogram(image), fd_haralick(image), fd_hu_moments(image)])
scaler = MinMaxScaler(feature_range=(0, 1))
#Normalize The feature vectors...
rescaled_features = scaler.fit_transform(global_features)

Lets create a SVM Classifier…

from sklearn.svm import SVC
clf = models.append(('SVM', SVC(random_state=9)))
prediction= clf.fit(global_feature.reshape(1,-1))[0]

This is just a pseudo code, and the main part of the code is importing images and labels, which we have handled in the CNN tutorial. So you see, feature extraction is the main part of traditional ML algorithms, and training these is just one line of code. Go ahead and try your own… Do let me know your results at lalith@datatuks.com.

This is the fourth blog in the five series tutorial. Here is the previous post in the series on word embeddings. The last one is on Reinforcement Learning. Have fun learning! By now, you have implemented CNNs, Word Embeddings and SVMs… So we have a feel for computer vision and natural language processing. It’s your turn to try them out…

--

--

DataTurks: Data Annotations Made Super Easy

Data Annotation Platform. Image Bounding, Document Annotation, NLP and Text Annotations. #HumanInTheLoop #AI, #TrainingData for #MachineLearning.