Transfer learning using MobileNet on Custom Data

TLTransfer learning:

Transfer learning is an advanced concept in machine learning techniques. Which helped to reuse the already developed model as starting for a second task model.

Transfer learning is the most famous methodology in deep learning. In this, we utilize pre-prepared models as the beginning stage of computer vision. Likewise, natural language processing assignments are given immense figure and time assets. Despite the fact that we need to developed neural network models.

Transfer learning is identified with numerous issues. For example, perform multiple tasks learning and idea float. In spite of the fact that it isn’t solely a space of study for deep learning.


We have implemented a mobile net on custom face image data.  Here we used the transfer learning technique by freezing half and full layers to check the face detection. For this purpose, we first used a hard cascade classifier to get cropped faces. There is already a built-in library for haar cascade which is in the form of the XML file  you can download   .

For the face, you have to download harcascade_frontal face, as there are many files for other parts of the body. As we can also detect facial landmarks to get more information about the facial attributes.

Import required libraries:

import numpy as np
import matplotlib.pyplot as plt
import glob
import cv2
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.python.keras.layers import Input, Dense

import tensorflow
from tensorflow import keras
from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dense,GlobalAveragePooling2D,Dropout,SeparableConv2D,BatchNormalization, Activation
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications import imagenet_utils
from tensorflow.keras import layers
from tensorflow.keras.models import Model
from sklearn.metrics import confusion_matrix
from IPython.display import Image

We have defined the number of classes, batch sizes, image size, and learning rate basic parameters. In my case 3 classes …

n_classes = 4
batch_size = 12
img_size = 128 # img width & heaight
input_shape = (img_size, img_size, 3) # input shape for model (width, height, channel)
seed = random.randint(1, 1000) # Taking random seed 1~1000 for shuffling and transformations.
learning_rate = 0.0001
epochs = 5

We can used other architecture like MobileNet, vgg16, resnet etc.

Your data folder, which should be accurate.

main_folder=’C:/Users/2109902/My program/face_feature_Randomforest/images/main/pre_data/’


This will organized data in an accurate manner. So it will help to divide data into train and test itself.

def orginize_data():
# Organize data into train, valid, test dirs
if os.path.isdir(‘train/0/’) is False:

for i in range(0, 4):
shutil.move(f'{i}’, ‘train’)

valid_samples = random.sample(os.listdir(f’train/{i}’),
for j in valid_samples:
shutil.move(f’train/{i}/{j}’, f’valid/{i}’)

test_samples = random.sample(os.listdir(f’train/{i}’), 4) 
for k in test_samples:
shutil.move(f’train/{i}/{k}’, f’test/{i}’)

you can just call the data organized function and save in variable train_path1, test_path1 and valid_path1 for further used.

train_path1 = (main_folder + ‘/training’)
valid_path1 = (main_folder + ‘/validation’)
test_path1 = (main_folder + ‘/testing’)

You also need to prepared data by using preprocessing technique  to avoid any problem . You can change parameter according to your requirement.

def data_preprocessing():
global train_batches, valid_batches, test_batches, test_label
# train datagen do only train data augmentation
# with width, height, shear & zoom parameters change 0.0 ~ 0.1 ranges
train_datagen = ImageDataGenerator(
# build-in mobilenet.preprocess_input function for converting color imgs to Numpy tensor
# No need to use data augmentation for test and validation data
test_datagen = ImageDataGenerator(
# build-in mobilenet.preprocess_input function for converting color imgs to Numpy tensor
preprocessing_function = tf.keras.applications.mobilenet.preprocess_input
train_batches = train_datagen.flow_from_directory( # taking these data and then doing data augmentation using train_datagen
directory=train_path, # our train path
target_size=(img_size,img_size), # img width & height
batch_size=batch_size, # The batch size is the amount of samples you feed each time in your network
seed=seed, # Optional random seed for shuffling and transformations.
class_mode=’categorical’) # As we have 10 classes our class_mode is categorical
valid_batches = test_datagen.flow_from_directory( # taking these data and then using test_datagen
directory=valid_path, target_size=(img_size,img_size), batch_size=batch_size,
seed=seed, class_mode=’categorical’)
test_batches = test_datagen.flow_from_directory( # taking these data and then using test_datagen
directory=test_path, target_size=(img_size,img_size), batch_size=batch_size,
seed=seed, class_mode=’categorical’, shuffle=False)


We just call  the MobileNet , it is already in keras. Add some more layer  with 3 classes

base_model = mobile.layers[-6].output # taking all mobilenet layers except last 5 layers

output = Dense(units=n_classes, activation=’softmax’)(base_model ) 
model = Model(inputs=mobile.input, outputs=output)

we have freez all layers except last 8 layers from MobileNet

for layer in model.layers[:-8]:
layer.trainable = False



Compile model by using adam optimizer and it is categorical classification, so we used softmax instead of sigmoid.


The finally predict the result of MobileNet

predictions = model.predict(x=test_batches, steps=len(test_batches), verbose=1


Add a Comment

Your email address will not be published.