Build your First Multi-Label Image Classification Model with Neural Network.

guntapalli suresh
4 min readMay 23, 2020

--

Acculturate first image classification model with python

What is Multi-Label Image Classification?

Let’s understand the concept of multi-label image classification with an intuitive example. Check out the below image:

Image 1
Image 2

The object in image 1 is a Bike. That was a no-brainer. Whereas, there is no car in image 2 — only a house. Can you see where we are going with this? We have classified the images into two classes, i.e., Bike or non-Bike.

How is Multi-Label Image Classification different from Multi-Class Image Classification?

Suppose we are given images of animals to be classified into their corresponding categories. For ease of understanding, let’s assume there are a total of 4 categories (cat, dog, rabbit and parrot) in which a given image can be classified. Now, there can be two scenarios:

  1. Each image contains only a single object (either of the above 4 categories) and hence, it can only be classified in one of the 4 categories
  2. The image might contain more than one object (from the above 4 categories) and hence the image will belong to more than one category

Let’s understand each scenario through examples, starting with the first one:

Here, we have images which contain only a single object. The keen-eyed among you will have noticed there are 4 different types of objects (animals) in this collection.

Each image here can only be classified either as a cat, dog, parrot or rabbit. There are no instances where a single image will belong to more than one category.

Steps to Build your Multi-Label Image Classification Model

Now that we have an intuition about multi-label image classification,

problem : we have some image data of Natural Scenes around the world. we need to classify according in to their labels .

let’s dive into the steps you should follow to solve such a problem.

Step 1:

Let’s import some basics python packages for images processing .

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from
random import randint
import cv2
import os

Step 2:

load the train and test data and resize size into 75x75 pixels

file_path='train-scene classification'
train=pd.read_csv(os.path.join(file_path,'train.csv'))
test=pd.read_csv(os.path.join(file_path,'test.csv'))
temp=[]
for img_name in train.image_name:
img_path=os.path.join(file_path,'train',img_name)
img=cv2.imread(img_path)
img=cv2.resize(img,(75,75))
temp.append(img)

train_x=np.asarray(temp)
temp = []
for img_name in test.image_name:
img_path = os.path.join(file_path, 'train', img_name)
img = cv2.imread(img_path)
img = cv2.resize(img, (75, 75))



temp.append(img)

test_x = np.asarray(temp)

step 3:

Normalize the image data

train_img=train_x/255.0
test_img=test_x/255.0

step 4:

let’s visualize some images

step 5:

Let’s convert the labels into onehot encoder

from keras.utils import to_categorical
y=to_categorical(train.label)

let’s split the train into training and validation.

from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(train_img,y,test_size=0.25,random_state=42)

step 6:

let’s import the keras necessary libraries for models building .

import tensorflow.keras.layers as Layers
import tensorflow.keras.activations as Actications
import tensorflow.keras.models as Models
import tensorflow.keras.optimizers as Optimizer
import tensorflow.keras.metrics as Metrics
import tensorflow.keras.utils as Utils
from keras.utils.vis_utils import model_to_dot
from keras import regularizers
from keras.layers import BatchNormalization

step 7:

model building :

let’s add the CNN layers

model = Models.Sequential()

model.add(Layers.Conv2D(200,kernel_size=(3,3),activation='relu',input_shape=(75,75,3)))
model.add(Layers.BatchNormalization())
model.add(Layers.Conv2D(180,kernel_size=(3,3),activation='relu',kernel_regularizer=regularizers.l1_l2( l2=0.01)))
model.add(Layers.MaxPool2D(2,2))
model.add(Layers.Conv2D(180,kernel_size=(3,3),activation='relu'))
model.add(Layers.Conv2D(140,kernel_size=(3,3),activation='relu'))
model.add(Layers.MaxPool2D(2,2))
model.add(Layers.Conv2D(100,kernel_size=(3,3),activation='relu'))
model.add(Layers.Conv2D(50,kernel_size=(3,3),activation='relu'))
model.add(Layers.MaxPool2D(2,2))
model.add(Layers.Flatten())
model.add(Layers.Dense(180,activation='relu'))
model.add(Layers.Dropout(rate=0.5))
model.add(Layers.Dense(100,activation='relu'))
model.add(Layers.Dropout(rate=0.5))
model.add(Layers.Dense(50,activation='relu',kernel_regularizer=regularizers.l1_l2( l2=0.01)))
model.add(Layers.Dropout(rate=0.5))
model.add(Layers.Dense(6,activation='softmax'))
model.compile(optimizer=Optimizer.Adam(lr=0.0001),loss='categorical_crossentropy',metrics=['accuracy'])

let’s train the model

trained=model.fit(X_train,y_train,epochs=10,validation_data=(X_test,y_test))

I trained model I got 87% accuracy.

If we want to increase your accuracy

fellow this blogs :

--

--