Open In App

Image classification using Support Vector Machine (SVM) in Python

Improve
Improve
Like Article
Like
Save
Share
Report

Support Vector Machines (SVMs) are a type of supervised machine learning algorithm that can be used for classification and regression tasks. In this article, we will focus on using SVMs for image classification.

When a computer processes an image, it perceives it as a two-dimensional array of pixels. The size of the array corresponds to the resolution of the image, for example, if the image is 200 pixels wide and 200 pixels tall, the array will have the dimensions 200 x 200 x 3. The first two dimensions represent the width and height of the image, respectively, while the third dimension represents the RGB color channels. The values in the array can range from 0 to 255, which indicates the intensity of the pixel at each point.

In order to classify an image using an SVM, we first need to extract features from the image. These features can be the color values of the pixels, edge detection, or even the textures present in the image. Once the features are extracted, we can use them as input for the SVM algorithm.

The SVM algorithm works by finding the hyperplane that separates the different classes in the feature space. The key idea behind SVMs is to find the hyperplane that maximizes the margin, which is the distance between the closest points of the different classes. The points that are closest to the hyperplane are called support vectors.

One of the main advantages of using SVMs for image classification is that they can effectively handle high-dimensional data, such as images. Additionally, SVMs are less prone to overfitting than other algorithms such as neural networks.

In 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. Import required libraries
  2. Load the image and convert it to a dataframe.
  3.  separate input features and targets.
  4. Split train and test value.
  5. Build and train the model
  6. Model evaluation.
  7. Prediction

Step 1:Import required libraries

Python3




import pandas as pd
import os
from skimage.transform import resize
from skimage.io import imread
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report


Step 2: Load the image and convert it to a data frame.

Download the cat’s vs dog’s dataset from here and labeled it as 0,1 in the following way:

Python3




Categories=['cats','dogs']
flat_data_arr=[] #input array
target_arr=[] #output array
datadir='IMAGES/' 
#path which contains all the categories of images
for 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)


Output:

loading... category : cats
loaded category:Cats successfully
loading... category : dogs
loaded category:Dogs successfully

The above code provided performs a series of essential steps to read, preprocess and organize image data for machine learning. First, the necessary packages are imported, including scikit-image for image processing, pandas for data manipulation, and numpy for mathematical computations. A list of ‘Categories’ is defined to represent the image categories that will be used for training the machine learning model. Two empty arrays are created to store the image data and their corresponding labels. The images are then loaded from the specified path, read and resized to a fixed size of 150×150 pixels with 3 color channels, and flattened to a 1D array. The flattened image data and its corresponding label (0 for ‘cats’ and 1 for ‘dogs’) are added to the arrays. The arrays are converted to a pandas DataFrame, which is then split into input data ‘x’ (all columns except the last one) and output data ‘y’ (the last column). The resulting ‘x’ and ‘y’ data can then be used to train a machine learning model. The variable names used are self-explanatory, making the code easy to follow and understand. Overall, this code provides a clear and concise way of loading, processing, and organizing image data for machine learning.  

Python3




#dataframe
df=pd.DataFrame(flat_data) 
df['Target']=target
df.shape


Output:

(500, 67501)

Step 3: Separate input features and targets.

Separate input and out features from the newly created dataframe

Python3




#input data 
x=df.iloc[:,:-1
#output data
y=df.iloc[:,-1]


Step 4: Separate input features and targets.

Python3




# Splitting the data into training and testing sets
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.20,
                                               random_state=77,
                                               stratify=y)


Step 5: Build and train the model

Model construction

Here the model is a Support vector machine and it will look like this

Python3




# Defining the parameters grid for GridSearchCV
param_grid={'C':[0.1,1,10,100],
            'gamma':[0.0001,0.001,0.1,1],
            'kernel':['rbf','poly']}
  
# Creating a support vector classifier
svc=svm.SVC(probability=True)
  
# Creating a model using GridSearchCV with the parameters grid
model=GridSearchCV(svc,param_grid)


In the above code snippet provided, we define the parameter grid for GridSearchCV. The parameter grid specifies the hyperparameters that we want to tune, including C, gamma, and kernel. C is the penalty parameter of the error term, gamma is the kernel coefficient, and the kernel is the kernel type. We provide a range of values for each hyperparameter, and GridSearchCV will perform an exhaustive search over all possible combinations of hyperparameters to find the optimal values.

Next, we create an instance of the SVM classifier with the “probability” parameter set to True. This is because we will use the “predict_proba()” method of the classifier to get the class probabilities later on. We then pass the SVM classifier and the parameter grid to GridSearchCV to create a model that will find the optimal hyperparameters for the SVM algorithm.

By using GridSearchCV, we can find the best combination of hyperparameters that will result in the highest accuracy of the model. This will help us to get the best possible performance from our SVM model.

Model training

we split the data into training and testing sets and then trained the model using the training data.

Python3




# Training the model using the training data
model.fit(x_train,y_train)


After preprocessing the dataset and creating the SVM model using GridSearchCV, we can split the dataset into training and testing sets using the train_test_split function from the scikit-learn library. This function randomly splits the data into training and testing sets based on the specified test size and random state. In this case, we have set the test size to 0.20, which means that 20% of the data will be used for testing, and the random state to 77 for reproducibility.

After splitting the data, we can train the model using the training data by calling the fit method on the model object that we created using GridSearchCV. This will train the model using the best combination of hyperparameters obtained from GridSearchCV.

We can print a message to indicate that the model has been trained successfully using the given images.

We can also print the best parameters obtained from GridSearchCV using the best_params_ attribute of the model object. This will display the optimal values for the C, gamma, and kernel parameters that we defined in the parameter grid.

we can evaluate the performance of the SVM model on unseen data. This helps us to ensure that the model generalizes well and is not overfitting to the training data.

Step 6: Model evaluation

Now the model is tested using testing data in this way model.predict() and the accuracy of the model can be calculated using the accuracy_score() method from sklearn.metrics

Python3




# Testing the model using the testing data
y_pred = model.predict(x_test)
  
# Calculating the accuracy of the model
accuracy = accuracy_score(y_pred, y_test)
  
# Print the accuracy of the model
print(f"The model is {accuracy*100}% accurate")


Output:

The model is 59.0% accurate

After training the SVM model, we need to test the model to see how well it performs on new, unseen data. To test the model, we will use the testing data which we split earlier using the train_test_split function from the scikit-learn library.

We use the predict method of the trained model to predict the class labels for the testing data. The predicted labels are stored in the y_pred variable.

To evaluate the performance of the model, we calculate the accuracy of the model using the accuracy_score method from the scikit-learn metrics module. The accuracy score measures the percentage of correctly classified data points out of all the data points. The accuracy score is calculated by comparing the predicted labels with the actual labels and then dividing the number of correct predictions by the total number of data points.

We print the predicted and actual labels for the testing data, followed by the accuracy of the model on the testing data.

Classification Report 

Now we can use the classification_report function from scikit-learn to generate a classification report for your SVM model. Here is an example code snippet:

Python3




print(classification_report(y_test, y_pred, target_names=['cat', 'dog']))


Output:

              precision    recall  f1-score   support

         cat       0.57      0.72      0.64        50
         dog       0.62      0.46      0.53        50

    accuracy                           0.59       100
   macro avg       0.60      0.59      0.58       100
weighted avg       0.60      0.59      0.58       100

Finally, we mention that the trained SVM model can be used to predict the class labels of new, unseen data.

Step 7: Prediction

Now we will give a new image to our model and it will predict whether the given image is of cat or dog

Python3




path='dataset/test_set/dogs/dog.4001.jpg'
img=imread(path)
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]])


Output:

Model evaluation

 

Our model has an accuracy of 0.59, which means it correctly classified 59% of the images in the test set. The F1-score for both classes is between 0.5 and 0.7, which suggests that the model’s performance is moderate.

Conclusion:

The goal of this article was to create and train a Support Vector Machine (SVM) model to accurately classify images of cats and dogs. The best parameters for the SVM model were determined using GridSearchCV, and the model’s accuracy was measured.



Last Updated : 21 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads