A Comprehensive Guide for Image Classification Part -1

Mdabdullahalhasib
3 min readSep 11, 2022

Machine Learning and Deep learning plays a vital role in our day to day life. Today’s machine can automatically detect or classify images. Here, I want to show the step by step process of image classification and improve the accuracy of the model and you can understand how to experiment with data with a huge ways.

Photo by Kevin Ku on Unsplash

Here, you can see the image classification by Machine learning Algorithms like Logistic Regression, KNN, Random Forest Classifier, Adaboost, Neural Networks (Convolution Neural Networks-CNN), transfer learning algorithms( Resnet50, VGG-16, VGG-19 and Others). I will implement all the algorithms step by step so that you can realize and implement any classification problem.

Data Preprocessing

Data Preprocessing is a major part before train the data by your choosing model. I run my code on google colab pro version. You can run it on google colab general version too. Here, I am using kaggle dataset of Gender Classification

Mount data from Google colab and Unzip the file

from google.colab import drive
drive.mount('/content/drive')
# Path of the file
path = '/content/drive/MyDrive/Colab Notebooks/Abdullah Al Hasib/image.zip'
# Extract the file
from zipfile import ZipFile
with ZipFile(path,'r') as zip:
zip.extractall('gender')

Here I took validation data for training and testing because validation data contains almost 11000 images. That’s enough for Machine learning Algorithms, After when I execute deep learning algorithm, I will experiment all the data because Deep learning can deal with big data so much.

# number of images in each sector
import os
print(len(os.listdir('/content/gender/Validation/female')))
print(len(os.listdir('/content/gender/Validation/male')))

There are 5841 female images and 5808 male images. Now take the images into one list and take the label also. I resized the images into (48,48,3) shape and for male image, label is 1 and for female image, label is 0.

Now Normalize the data (data value range 0 to 1) and reshape the image from 2D image to 1D vector. Then split the data for train and testing by sklearn library.

Image Classification Using Logistic Regression(LR)

Now fit the training data with LR model. Then predict the test data by trained model and see, how much can learn the model by accuracy score.

Accuracy is : 0.9007153075822604

For a better Understanding, you can see the classification report of the model and Confusion matrix also.

             precision    recall  f1-score   support         
male 0.90 0.90 0.90 1725
female 0.90 0.90 0.90 1770
accuracy 0.90 3495
macro avg 0.90 0.90 0.90 3495
weighted avg 0.90 0.90 0.90 3495
Confusion Matrix

That’s all for the first part. After part I will write the other machine learning algorithms and Deep learning Algorithms and Transfer Learning and many more.

Happy Learning :)

--

--