
In this tutorial, I will show how to process image and extract features using pre-trained models in keras.
Step 1: Load sample image
# load sample image file
img_file = "sample-image.jpg"
img = keras.utils.load_img(img_file, target_size=(224,224))
plt.imshow(img)

Step 2: Load pre-trained model weights. Keras offers number of pre-trained models, which can be found at — https://keras.io/api/applications/
Most popular models for image classification include: Xception, VGG16, Resnet50, MobileNet, InceptionV3 ..
# load pre-trained model
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
base_model = VGG16(weights='imagenet')
In this example, I have used VGG16 model.
Step 3: This model has 23 layers. First layer takes the input image as numpy array, and last one produces class predictions as the final output. To extract image features, we use some intermediate layer.
model = keras.Model(inputs=base_model.input, outputs=base_model.get_layer('block2_pool').output)
So when we pass our sample image through the model, it will generate features from this intermediate layer, rather than final class predictions.
Step 4: Run sample image through the model.
x = preprocess_input(img)
y = model.predict(np.array([x]))
Step 5: Now we can visualize feature maps — which is the output of intermediate layer on sample image
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.axis('off')
plt.imshow(y[0,:,:,i], cmap="berlin")

If you extract features from another layer e.g. block1_pool, you can see different feature maps.

Now, let’s try feature extraction using ResNet50 model.
Step 6: All we have to do is to load the ResNet50 model, and extract output from intermediate layer.
from keras.applications.resnet50 import ResNet50
from keras.applications.resnet50 import preprocess_input
base_model = ResNet50(weights='imagenet')
You can check how many layers model has with len(base_model.layers()). As you can see, this model is more complex than earlier VGG16, with 170+ layers. We’ll extract features from intermediate layer.
model = keras.Model(inputs=base_model.input, outputs=base_model.get_layer('conv2_block2_2_bn').output)

That’s it, folks.. Isn’t that nice? Feel free to follow my git repo for more — https://github.com/pamruta/Keras/