Transfer Learning in Keras: Exploring Fine Tuning and Feature Extraction

Posted by


Transfer learning is a machine learning technique where a model trained on one task is repurposed for a second related task. This is particularly useful when the second task has limited data available, as the pre-trained model can leverage knowledge gained from the first task to improve performance on the second task.

Transfer learning has gained popularity in recent years due to the rise of deep learning, which requires large amounts of data to train effectively. By using pre-trained models that have been trained on vast datasets, researchers and developers can achieve high accuracy on new tasks with relatively little data.

In this tutorial, we will focus on transfer learning in Keras, one of the most popular deep learning libraries. We will discuss two common strategies for transfer learning in Keras: fine-tuning and feature extraction.

Fine-Tuning:
Fine-tuning involves taking a pre-trained model and modifying a few of its top layers to better suit the new task. The lower layers of the model, which are responsible for learning general features like edges and textures, are kept frozen to preserve the knowledge gained from the original task. The top layers, which are responsible for learning task-specific features, are replaced or re-trained to adapt to the new task.

To demonstrate fine-tuning in Keras, let’s consider an example where we want to classify images of cats and dogs using a pre-trained VGG16 model. First, we load the pre-trained VGG16 model without including the top layers:

from keras.applications import VGG16

base_model = VGG16(weights='imagenet', include_top=False)

Next, we add our own classification layers on top of the VGG16 model:

from keras.models import Model
from keras.layers import Flatten, Dense

x = Flatten()(base_model.output)
x = Dense(256, activation='relu')(x)
predictions = Dense(2, activation='softmax')(x)

model = Model(inputs=base_model.input, outputs=predictions)

We then freeze the layers of the VGG16 model and compile the model for training:

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

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

Finally, we train the model on our cat and dog dataset:

model.fit(X_train, y_train, batch_size=32, epochs=10, validation_data=(X_val, y_val))

By fine-tuning the VGG16 model with our classification layers, we can achieve high accuracy on the cat and dog classification task with relatively little data.

Feature Extraction:
Feature extraction is another transfer learning strategy where we use the pre-trained model as a fixed feature extractor. In this approach, we freeze all the layers of the pre-trained model and only add new classification layers on top to train on the new task.

To demonstrate feature extraction in Keras, let’s consider the same cat and dog classification task using the pre-trained VGG16 model. First, we load the pre-trained VGG16 model without including the top layers:

from keras.applications import VGG16

base_model = VGG16(weights='imagenet', include_top=False)

Next, we freeze the layers of the VGG16 model and add our own classification layers on top:

from keras.models import Model
from keras.layers import Flatten, Dense

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

x = base_model.output
x = Flatten()(x)
x = Dense(256, activation='relu')(x)
predictions = Dense(2, activation='softmax')(x)

model = Model(inputs=base_model.input, outputs=predictions)

We then compile the model for training:

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

Finally, we train the model on our cat and dog dataset:

model.fit(X_train, y_train, batch_size=32, epochs=10, validation_data=(X_val, y_val))

Feature extraction is particularly useful when we have limited data for the new task, as it allows us to leverage the knowledge of the pre-trained model without modifying its weights. By adding our own classification layers on top of the pre-trained model, we can achieve high accuracy on the new task with minimal training.

In conclusion, transfer learning is a powerful technique in deep learning that allows us to leverage pre-trained models to achieve high accuracy on new tasks with limited data. In this tutorial, we discussed two common strategies for transfer learning in Keras: fine-tuning and feature extraction. By understanding these techniques and applying them to our own tasks, we can take advantage of the wealth of knowledge encapsulated in pre-trained models to build highly accurate and efficient deep learning models.

0 0 votes
Article Rating
21 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@dheerajsinghpatel5144
1 month ago

Hi NItish, Can you also cover RCNN, Fast-RCNN, Faster-RCNN & YOLO Algorithm in CNN.
It would be of great help. Thanks!!

@suryashekhawat6965
1 month ago

Mark my word! This channel will be the best channel for data science community. No PR just pure content. People will flow lessely follow this channel just need to here once. Keep up the good work man.

@anuragshrivastava7855
1 month ago

NLP playlist pending

@pkumar0212
1 month ago

👌

@DataVue
1 month ago

thanks Sir
Awesom 🙂

@athleteworld1703
1 month ago

sir ham fine tuning ke sath data augmentation use karsakte ha

@yashmatlani1976
1 month ago

Please make vidoes on transfer learning methods for NLP

@varutriparihar981
1 month ago

no matter how much thankyou i say it will still be less. really sir thankyou so much.

@kindaeasy9797
1 month ago

I guess it is giving good results because it has been trained on cat dog images

@braineaterzombie3981
1 month ago

Bhai dense layer badha k dekh le single se kya hi hoga 2- 3 toh laga

@Sjcube949
1 month ago

can we apply transfer learning on tabular data?

@StoryScapebyShan
1 month ago

completed ,. Awesome content

@aninditasaha0307
1 month ago

Can you apply this on UCIHAR dataset? That's a sensor data which u have worked also in feature selection

@rb4754
1 month ago

Thank you Nitish for "Transferring your Learning" to all the students and learners who wish to be a deep learning expert….

@113_kunalsapkal3
1 month ago

Does transfer learning works if we want to train a language models , is there any pretrained model dataset for a language model.

@AICC2222
1 month ago

Very good explanation sir.

@ShivamPatel-jm2iy
1 month ago

What is the difference between Transfer learning and domain adaptation?

@muhammadaafaq691
1 month ago

Our teacher told us that transfer learning is used in CNNs because initially features are somewhat similar and change in the later layers. So, my question is whether transfer learning can only be used in CNN tasks?
Please Reply

@fahadriaz1357
1 month ago

Sir great explanation!

@iit_adda
1 month ago

SIR HEAD OFF TO YOU THIS WAY YOU UNDERSTAND CONCEPT SO EASILY DIE HEART FAN OF YOU