Image classification using Support Vector Machine (SVM) in Python

Last Updated : 26 Jun, 2026

Image classification using Support Vector Machine (SVM) is a machine learning technique where images are assigned to specific categories based on extracted features. SVM is a supervised algorithm that separates classes by finding an optimal decision boundary in feature space.

  • Images are converted into numerical feature vectors by resizing and flattening pixel values for model input.
  • SVM classifies data by maximising the margin between different classes using a separating hyperplane.

Implementation

Let’s consider a dataset containing images of cats and dogs, where each image is assigned a label based on its category. The implementation of Image Classification using Support Vector Machine (SVM) in Python follows a structured machine learning workflow, starting from data preprocessing to final prediction.

Step 1: Import required libraries

Python
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 Images and Convert to DataFrame

Loading images from folders, processes them, and converts them into numerical features for model training.

  • Images are resized and flattened into feature vectors.
  • Labels are assigned based on categories (cats = 0, dogs = 1).
Python
Categories=['cats','dogs']
flat_data_arr=[] 
target_arr=[] 
datadir='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)

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

Output:

loading... category : cats
loaded category:Cats successfully
loading... category : dogs
loaded category:Dogs successfully
(500, 67501)

Step 3: Separate Input Features and Targets

Splitting the dataset into input features (image data) and output labels for model training.

Python
x=df.iloc[:,:-1] 
y=df.iloc[:,-1] 

Step 4: Train-Test Split

Dividing the dataset into training and testing sets to evaluate model performance on unseen data.

Python
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

Creating an SVM classifier and tuning its hyperparameters using GridSearchCV to achieve better accuracy.

  • GridSearchCV is used to select the best combination of hyperparameters (C, gamma, kernel).
  • The model is trained on the training data using the optimal parameters obtained.
Python
param_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.fit(x_train, y_train)

Step 6: Model evaluation

Evaluating the trained SVM model using accuracy score and a classification report to measure its performance on test data.

  • Accuracy measures overall correct predictions of the model.
  • Classification report gives precision, recall, and F1-score for each class.
Python
y_pred = model.predict(x_test)

accuracy = accuracy_score(y_pred, y_test)

print(f"The model is {accuracy*100}% accurate")
print(classification_report(y_test, y_pred, target_names=['cat', 'dog']))

Output:

The model is 59.0% accurate

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

Step 7: Prediction

Giving a new image as input to the trained SVM model to predict whether it belongs to the cat or dog category.

Python
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

The model has an accuracy of 0.59, indicating that it correctly classified 59% of the images in the test set. The F1-score for both classes lies between 0.5 and 0.7, suggesting a moderate level of model performance.

Comment