Image Classification Using Machine Learning-Support Vector Machine(SVM)
Python
Introduction
Machine learning is an application of artificial intelligence, which allows the computer to operate in a self-learning mode, without being explicitly programmed. It is a very interesting and complex topic, which could drive the future of technology.
There are copious applications of Machine learning, out of which Image Classification is one. To classify images, here we are using SVM. Scikit-learn is a free software machine learning library for the Python programming language and Support vector machine(SVM) is subsumed under Scikit-learn.
Tools Used
→ Python syntax
→ Pandas library for data frame
→ Support vector Machine(svm) from sklearn (a.k.a scikit-learn) library
→ GridSearchCV
→ skimage library for reading the image
→ matplotlib for visualization purpose
First, let’s understand the concept and dive into the coding part 😉
Support Vector Machine(SVM)
“Support Vector Machine” (SVM) is a supervised machine learning algorithm that can be used for both classification or regression challenges. However, it is mostly used in classification problems. In this SVM algorithm, we plot each data item as a point in n-dimensional space (where n is the number of features you have) with the value of each feature being the value of a particular coordinate. Then, we perform classification by finding the hyper-plane that differentiates the two classes very well.
Some of the key parameters in SVM are:
→ Gamma : defines how far the influence of single training examples reaches values leads to biased results.
→ C : Controls the cost of miscalculations
Small C — makes the cost of misclassification LOW
Large C — makes the cost of misclassification HIGH
→ Kernel : SVM algorithms use a set of mathematical functions that are defined as the kernel.
Types of Kernels are: Linear, RBF(Radial Basis Function), Polynomial Kernel
More about SVM can be learned from here.
How Does the Computer Read The Image? 🤔
The main task of image Classification is to read the input image, the computer sees the image quite differently:
The computer sees the image as an array of pixels, if the size of the image is 200 X 200, the size of the array will be 200 X 200 X 3 wherein the first 200 is the width and second 200 is height and the next 3 is RGB channel values. The values in the array would range from 0–255 which describes the intensity of the pixel at each point.
GridSearchCV
It is a library function that is a member of sklearn’s model_selection package. It helps to loop through predefined hyperparameters and fit your estimator (model) on your training set. So, in the end, you can select the best parameters from the listed hyperparameters.
Enough of theory, let’s get started with the coding part.
Process
It is one of the ways of machine learning where the model is trained by input data and expected output data.
To create such a model, it is necessary to go through the following phases:
1.Taking input
2. Model construction
3. Model training
4. Model testing
5.Model evaluation
Taking input: 3 Different categories of images(Cars, Ice cream cone, Cricket ball) are read and labeled as 0,1,2 in the following way:
import pandas as pd
import os
from skimage.transform import resize
from skimage.io import imread
import numpy as np
import matplotlib.pyplot as pltCategories=['Cars','Ice cream cone','Cricket ball']flat_data_arr=[] #input arraytarget_arr=[] #output arraydatadir='/content/drive/MyDrive/ML'
#path which contains all the categories of imagesfor i in Categories:
print(f'loading... category : {i}') path=os.path.join(datadir,i) for img in os.listdir(path): img_array=imread(os.path.join(path,img)) img_resized=resize(img_array,(150,150,3)) flat_data_arr.append(img_resized.flatten()) target_arr.append(Categories.index(i)) print(f'loaded category:{i} successfully')flat_data=np.array(flat_data_arr)target=np.array(target_arr)df=pd.DataFrame(flat_data) #dataframedf['Target']=targetx=df.iloc[:,:-1] #input data y=df.iloc[:,-1] #output data
Since SVM receives inputs of the same size, all images need to be resized to a fixed size before inputting them to the SVM. df is the data frame created using pandas and x and y are input and output data respectively
Model construction: In this project case, the model is Support vector machine.
The algorithm for model construction looks like this:
1. Create a support vector classifier:
→ svc=svm.SVC()
2. With the help of GridSearchCV and parameters grid, create a model: →model=GridSearchCV(svc,parameters_grid)
from sklearn import svmfrom sklearn.model_selection import GridSearchCVparam_grid={'C':[0.1,1,10,100],'gamma':[0.0001,0.001,0.1,1],'kernel':['rbf','poly']}svc=svm.SVC(probability=True)model=GridSearchCV(svc,param_grid)
Model training: The data is split into two categories: training data and testing data. Training data is used to train the model whereas testing data is used to test the trained model.
For splitting the data into training and testing, train_test_split() from sklearn library is used.
Model is trained using training data in this way
→ model.fit(training_data,expected_output)
from sklearn.model_selection import train_test_splitx_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.20,random_state=77,stratify=y)print('Splitted Successfully')model.fit(x_train,y_train)print('The Model is trained well with the given images')# model.best_params_ contains the best parameters obtained from GridSearchCV
Model testing: Now the model is tested using testing data in this way
→ model.predict(testing_data)
The accuracy of the model can be calculated using the accuracy_score() method from sklearn.metrics
y_pred=model.predict(x_test)print("The predicted Data is :")print(y_pred)print("The actual data is:")print(np.array(y_test))print(f"The model is {accuracy_score(y_pred,y_test)*100}% accurate")
Finally, in the Model evaluation phase, the model generated can be used to evaluate new data.
url=input('Enter URL of Image :')img=imread(url)plt.imshow(img)plt.show()img_resize=resize(img,(150,150,3))l=[img_resize.flatten()]probability=model.predict_proba(l)for ind,val in enumerate(Categories): print(f'{val} = {probability[0][ind]*100}%')print("The predicted image is : "+Categories[model.predict(l)[0]])
The final output would be like this:
The whole code for this project can be found at :
https://github.com/ShanmukhVegi/Image-Classification
Conclusion:
In this work, I assembled and trained the SVM model to classify images of ice cream cone, cricket ball, and cars. I used GridSearchCV to find out the best parameters for SVM to classify the images and have measured the accuracy of the model.