Show List

Fine-tuning Pre-trained Models

Fine-tuning pre-trained models is a common technique used in neural networks for transfer learning. It involves taking a pre-trained model, removing the last few layers, and replacing them with new layers that are trained on a new dataset. This allows the model to be re-used for a different task without having to train it from scratch.

Here's an example of how to fine-tune a pre-trained model in Python using the Keras library:

python
Copy code
from keras.applications import VGG16 from keras.models import Model from keras.layers import Dense, Flatten from keras.optimizers import Adam from keras.preprocessing.image import ImageDataGenerator # load the pre-trained model base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) # add new layers to the model x = base_model.output x = Flatten()(x) x = Dense(256, activation='relu')(x) predictions = Dense(10, activation='softmax')(x) # create the new model model = Model(inputs=base_model.input, outputs=predictions) # freeze the weights of the pre-trained layers for layer in base_model.layers: layer.trainable = False # compile the model model.compile(optimizer=Adam(lr=0.0001), loss='categorical_crossentropy', metrics=['accuracy']) # load the new dataset train_datagen = ImageDataGenerator(rescale=1./255) train_generator = train_datagen.flow_from_directory('new_data/train', target_size=(224, 224), batch_size=32, class_mode='categorical') # fine-tune the model model.fit_generator(train_generator, steps_per_epoch=train_generator.n // train_generator.batch_size, epochs=10) # unfreeze some of the pre-trained layers for layer in model.layers[:15]: layer.trainable = False for layer in model.layers[15:]: layer.trainable = True # compile the model again model.compile(optimizer=Adam(lr=0.0001), loss='categorical_crossentropy', metrics=['accuracy']) # fine-tune the model again model.fit_generator(train_generator, steps_per_epoch=train_generator.n // train_generator.batch_size, epochs=10)

In this example, we first load the pre-trained VGG16 model from the Keras library and remove the last few layers. We add a new fully-connected layer with 256 units and a softmax output layer with 10 units for the new classification task. We then freeze the weights of all the pre-trained layers so that only the new layers are trained.

We compile the model with the Adam optimizer and a learning rate of 0.0001, and use categorical cross-entropy loss and accuracy as metrics. We then load the new dataset using the ImageDataGenerator and fit the model to the data for 10 epochs.

After the initial training, we unfreeze some of the pre-trained layers and re-compile the model. We then fine-tune the model again on the new dataset for another 10 epochs.

Note that the exact number of layers to freeze and the number of epochs for fine-tuning may vary depending on the specific task and dataset. It's important to monitor the performance of the model during training and adjust the hyperparameters as needed.


    Leave a Comment


  • captcha text