Transfer Learning using MobileNet
What is Transfer Learning?
Transfer learning is the reuse of a pre-trained model on a new problem. It's currently very popular in deep learning because it can train deep neural networks with comparatively little data.
Ever thought of training a model with dense architecture and with little data and on top of that making it more dense ?
Transfer Learning helps us training a pre-trained model with some extra added layers.
Some of the top models used in this are
- VGG16
- MobileNet
- ResNet etc
In this article I will be using MobileNet for Transfer Learning
What is MobileNet?
MobileNetV2 is a convolutional neural network architecture that seeks to perform well on mobile devices. It is based on an inverted residual structure where the residual connections are between the bottleneck layers. This CNN architecture conceived to optimize the number of operations
So Lets start
First we will load the MobileNet model without the top layers as we will be adding the top layer to the model. to achieve this we will freeze the top four layers of the MobileNet model. Then we display the all the layers to get some idea of the architecture of the MobileNet model. Almost there are 55+ layers in the MobileNet model
from keras.applications import MobileNet # MobileNet was designed to work on 224 x 224 pixel input images sizes img_rows, img_cols = 224, 224 # Re-loads the MobileNet model without the top or FC layers MobileNet = MobileNet(weights = 'imagenet', include_top = False, input_shape = (img_rows, img_cols, 3)) # Here we freeze the last 4 layers # Layers are set to trainable as True by default for layer in MobileNet.layers: layer.trainable = False # Let's print our layers for (i,layer) in enumerate(MobileNet.layers): print(str(i) + " "+ layer.__class__.__name__, layer.trainable)
Now we will make a function called "lw"
This function will take the bottom layers and return the function head ie the top layers
def lw(bottom_model, num_classes): """creates the top or head of the model that will be placed ontop of the bottom layers""" 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
Let's add our FC Head back onto MobileNet
here we define the number of classes and form the final model
from keras.models import Sequential from keras.layers import Dense, Dropout, Activation, Flatten, GlobalAveragePooling2D from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D from keras.layers.normalization import BatchNormalization from keras.models import Model # Set our class number to 3 (Young, Middle, Old) num_classes = 17 FC_Head = lw(MobileNet, num_classes) model = Model(inputs = MobileNet.input, outputs = FC_Head) print(model.summary())
Now we will load the dataset
Firstly the with help of gdown command we will load the dataset into the colab and then unzip it. Then we will allot the train and test data to respective variables
from keras.preprocessing.image import ImageDataGenerator from google.colab import drive !gdown --id 1HgJaB_RtxDR0TZYCInnUnfBB9TL_h9os !unzip 17_flowers.zip train_data_dir = '17_flowers/17_flowers/train/' validation_data_dir = '17_flowers/17_flowers/validation/' train_datagen = ImageDataGenerator( rescale=1./255, rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, horizontal_flip=True, fill_mode='nearest') validation_datagen = ImageDataGenerator(rescale=1./255) # Change the batchsize according to your system RAM train_batchsize = 16 val_batchsize = 10 train_generator = train_datagen.flow_from_directory( train_data_dir, target_size=(img_rows, img_cols), batch_size=train_batchsize, class_mode='categorical') validation_generator = validation_datagen.flow_from_directory( validation_data_dir, target_size=(img_rows, img_cols), batch_size=val_batchsize, class_mode='categorical', shuffle=False)
Now we will our top layers which we added. We fit the data into the model and train. and save the file which has the weights which we will further used in prediction of the future data.
from keras.optimizers import RMSprop from keras.callbacks import ModelCheckpoint, EarlyStopping checkpoint = ModelCheckpoint("flowers_vgg.h5", monitor="val_loss", mode="min", save_best_only = True, verbose=1) earlystop = EarlyStopping(monitor = 'val_loss', min_delta = 0, patience = 3, verbose = 1, restore_best_weights = True) # we put our call backs into a callback list callbacks = [earlystop, checkpoint] # Note we use a very small learning rate model.compile(loss = 'categorical_crossentropy', optimizer = RMSprop(lr = 0.001), metrics = ['accuracy']) nb_train_samples = 1190 nb_validation_samples = 170 epochs = 5 batch_size = 16 history = model.fit( train_generator, steps_per_epoch = nb_train_samples // batch_size, epochs = epochs, callbacks = callbacks, validation_data = validation_generator, validation_steps = nb_validation_samples // batch_size) model.save("flowers_mobileNet.h5")
The file "flowers_mobileNet.h5" can be loaded and used for classification.
Hence used transfer learning to train a model.
Thanks for reading
Feel free to ask any doubts
Name: Syed Faheemuddin
Email: [email protected]
GitHub: