In this blog post, we are going to explore how Transfer Learning technique helps us to overcome the computation challenges for building a neural network from scratch and training it with images.
Generally, its computationally hard and expensive to train images, which requires GPU support.
But Transfer Learning is a technique which makes this training computation simple, super cool and handy.
Oxford’s Visual Geometry Group developed and trained the so called (VGG16) model with Imagenet database, which contains hundreds and thousands of images.
Lets dive into transfer learning,
Lets begin with importing the VGG16 model and keras layers for building the fully connected layers.
from keras.layers import Dense,Conv2D,MaxPooling2D,Dropout,Flatten,Input
from keras.models import Sequential, Model
from keras.preprocessing.image import ImageDataGenerator
from keras.applications.vgg16 import VGG16
In this classification, we are going to classify three classes of traffic light signals – Red, Green and Yellow. The shape of the input images is expected to be 224,224 with RGB color channel at the last.
image_input = Input(shape=(224, 224, 3))
model = VGG16(input_tensor=image_input, include_top=False, weights= 'imagenet')
As you can see, we are not including the top layers (fully connected layers) and we are using the imagenet weights for our VGG16 model, which reduces the training computation.
Now, we need to build our own new network layers to append it on top of the base VGG16 model for our classification problem.
top_model = Sequential()
top_model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=model.output_shape[1:],padding='same'))
top_model.add(Conv2D(64, kernel_size= (3,3),activation='relu',padding='same'))
Since, the base VGG16 model is already trained, it is good at extracting the patterns, edges and textures from the images, so we don’t need to train the base VGG16 model, so we are freezing the base model and training only the newly appended fully connected layers.
or layer in top_model.layers[:-8]:
layer.trainable = False
So, after building the model, its time to fit our training and test data to evaluate our model’s accuracy. We are using “Accuracy” as our evaluation metric and “Adam” , “categorical_crossentropy” as optimisers and loss metrics respectively.
top_model.compile(optimizer = 'Adam', loss='categorical_crossentropy', metrics=['accuracy'])
from keras.callbacks import EarlyStopping
earlystop = EarlyStopping(monitor='val_acc', min_delta=0.0001, patience=3,
callbacks_list = [earlystop]
data_generator = ImageDataGenerator(rescale=1./255)
data_generator_with_aug = ImageDataGenerator(horizontal_flip=True,
train_generator = data_generator_with_aug.flow_from_directory(r'E:\Thesis\Try1\Train',
validation_generator = data_generator.flow_from_directory(r'E:\Thesis\Try1\Test',
history = top_model.fit_generator(train_generator,
validation_data = validation_generator,
validation_steps = 1,
“Early Stopping” – is a callback function, which is used to reduce overfitting by monitoring the “validation loss“. If the validation loss doesn’t reduces for 2 or 3 iterations, the this Early Stopping stops the training process.
Hope, u guys have learnt something about Transfer Learning.