How to do it...

Run the following steps to implement fine-tuning (with transfer learning) using keras.

  1. Define the train and test directories along with the size that the input images will get resized to before training:
train_dir = 'images/flower_photos/train'test_dir = 'images/flower_photos/test'image_size = 224
  1. Load the pretrained VGG16 network, but without the top FC layers:
vgg_conv = VGG16(weights='imagenet', include_top=False, input_shape=(image_size, image_size, 3))
  1. Freeze all the convolutional layers except the last two convolutional layers, along with the FC layers, to reuse the pretrained weights for those layers (without retraining them):
for layer in vgg_conv.layers[:-2]:     layer.trainable = False 
  1. Now let's verify the status ...

Get Python Image Processing Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.