Site icon Machine Learning Projects

Monkey Breed Classification using Transfer Learning – with source code – easiest code explanation – easy implementation – 2023

Machine Learning Projects

In today’s blog, we will be using Transfer learning to implement our today’s project which is Monkey Breed Classification. Transfer Learning is simply when we are using an already trained model for our current use case.

In this case, we will be using Mobilenet, pre-trained on Imagenet. So without any further due, Let’s do it…

Step 1 – Importing required libraries for Monkey Breed Classification.

from tensorflow.keras.applications.mobilenet import MobileNet
from tensorflow.keras.layers import Dense,Dropout,GlobalAveragePooling2D,Conv2D,MaxPooling2D,Activation
from tensorflow.keras.models import Model
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import EarlyStopping,ModelCheckpoint
from tensorflow.keras.optimizers import RMSprop

Step 2 – Instantiating Mobilenet with Imagenet weights.

height = 224
width = 224

mobilenet = MobileNet(weights='imagenet',
                     include_top=False,
                     input_shape=(height,width,3))

for layer in mobilenet.layers:
    layer.trainable = False

for i,layers in enumerate(mobilenet.layers):
    print(f'Layer {i+1} --> ',layers.__class__.__name__,layers.trainable)

Step 3 – Creating a top model for the Monkey Breed Classification model.

def add_top_layers_to_MobileNet(bottom_model,num_classes):
    top_model = bottom_model.output
    top_model = GlobalAveragePooling2D()(top_model)
    top_model = Dense(1024,activation='relu')(top_model)
    top_model = Dense(1024,activation='relu')(top_model)
    top_model = Dense(512,activation='relu')(top_model)
    top_model = Dense(num_classes,activation='softmax')(top_model)
    return top_model

Step 4 – Join the bottom and top models.

self_made_layers = add_top_layers_to_MobileNet(mobilenet,10)
model = Model(inputs = mobilenet.input , outputs = self_made_layers)

model.compile(loss='categorical_crossentropy',
             optimizer=RMSprop(learning_rate=0.001),
             metrics=['accuracy'])

print(model.summary())

Step 5 – Instantiate ImageDataGenerators for augmenting the data.

train_dir = 'monkey_breed\\train' 
validation_dir = 'monkey_breed\\validation'

train_gen = ImageDataGenerator(rescale=1./255,
                              rotation_range=45,
                              height_shift_range=0.3,
                              width_shift_range=0.3,
                              horizontal_flip=True,
                              fill_mode='nearest')

validation_gen = ImageDataGenerator(rescale=1./255)

Step 6 – Augmenting the data for Monkey Breed Classification.

training_data = train_gen.flow_from_directory(train_dir,
                                                       batch_size=1,
                                                       shuffle=True,
                                                       class_mode='categorical',
                                                       target_size=(rows,columns))

valiadtion_data = validation_gen.flow_from_directory(validation_dir,
                                                       batch_size=1,
                                                       shuffle=False,
                                                       class_mode='categorical',
                                                       target_size=(rows,columns))

Step 7 – Creating callbacks for the Monkey Breed Classification model.

checkpoint = ModelCheckpoint('model/monkey_breed_MobileNet_V1.h5',
                             verbose=1,
                             mode='min',
                             monitor='val_loss',
                             save_best_only=True)

earlystopping = EarlyStopping(verbose=1,
                              monitor='val_loss',
                              restore_best_weights=True,
                              patience=3,
                              min_delta=0)

callbacks = [earlystopping,checkpoint] 

Step 8 – Declare some constants.

train_samples = 1098
validation_samples = 272
epochs = 5
batch_size = 1

Step 9 – Finally train the model for Monkey Breed Classification.

history = model.fit(training_data,
                   epochs=epochs,
                   steps_per_epoch=train_samples//batch_size,
                   validation_data=valiadtion_data,
                   callbacks=callbacks,
                   validation_steps=validation_samples//batch_size)

Step 10 – Live prediction on 10 random images.

import os
import cv2
import numpy as np
import random
from keras.models import load_model

model = load_model('model\\monkey_breed_MobileNet_V1.h5')
acc=[]
monkey_breed_dict={'[0]':'mantled_howler',
                   '[1]':'patas_monkey',
                   '[2]':'bald_uakari',
                   '[3]':'japanese_macaque',
                   '[4]':'pygmy_marmoset',
                   '[5]':'white_headed_capuchin',
                   '[6]':'silvery_marmoset',
                   '[7]':'common_squirrel_monkey',
                   '[8]':'black_headed_night_monkey',
                   '[9]':'nilgiri_langur'}

for i in range(10):
    monkey_class = np.random.randint(0,9)
    all_class_list    = os.listdir('monkey_breed\\validation')
    select_one_class = all_class_list[monkey_class] 
    pick_one_pic = random.choice(os.listdir('monkey_breed\\validation\\'+str(select_one_class)+'\\'))
    org_img = cv2.imread('monkey_breed\\validation\\'+str(select_one_class)+'\\'+pick_one_pic)
    org_img = cv2.resize(org_img,None,fx=0.5,fy=0.5,interpolation=cv2.INTER_AREA)
    
    img = org_img.copy()
    show_img = org_img.copy()
    
    show_img = cv2.resize(show_img,(500,500),interpolation=cv2.INTER_LINEAR)
    
    img = cv2.resize(img,(224,224),interpolation=cv2.INTER_LINEAR)
    img = img/255
    img = img.reshape(1,224,224,3)
    
    true = monkey_breed_dict[str([monkey_class])]
    predicted = monkey_breed_dict[str([np.argmax(model.predict(img,1,verbose=0))])]
    
    cv2.putText(show_img,'true - '+true,(10,40),cv2.FONT_HERSHEY_SIMPLEX,0.8,(255,255,255),2)
    cv2.putText(show_img,'predicted - '+predicted,(10,60),cv2.FONT_HERSHEY_SIMPLEX,0.8,(255,255,255),2)
    
    cv2.imshow('random img',show_img)

    
    acc.append(np.equal(monkey_class,np.argmax(model.predict(img,1,verbose=0))))
    cv2.waitKey(0)
    
cv2.destroyAllWindows()
print('\n')
print('Accuracy ---> ',str(round(np.mean(acc)*100,2))+' %')
https://machinelearningprojects.net/wp-content/uploads/2022/10/Monkey-Breed-Classification-using-Transfer-Learning.mp4

Download Source Code…

Download Data…

NOTE – When you will download the data from this link you will see 2 folders train and validation. Inside the train, there will be again a folder named train and the same for validation. Do one thing. Cut out this inner train folder and bring that out in the monkey_breed folder. Do the same for the validation folder.

Do let me know if there’s any query regarding Monkey Breed Classification by contacting me on email or LinkedIn.

So this is all for this blog folks, thanks for reading it and I hope you are taking something with you after reading this and till the next time ?…

Read my previous post: AGE DETECTION USING CNN WITH KERAS

Check out my other machine learning projectsdeep learning projectscomputer vision projectsNLP projectsFlask projects at machinelearningprojects.net.

Exit mobile version