Day 39 of 100DaysofML

Charan Soneji
100DaysofMLcode
Published in
4 min readJul 25, 2020

Learning to use pre-trained models. I think this is a very important thing to know considering how much easier it makes things as a developer and an ML coder. I do not deny the fact that creating your own model is a bad thing but at the same time, using a pre-trained model with better documentation is something that is going to help you out and make your computation tasks easier.

What exactly is a pre-trained model?
A pre-trained model in simple words is something which already has its weights and biases saved for a given model of implementation. So for instance, if you’re trying to create a model to identify between Cats and Dogs, a pre-trained model is something that would refer to a neural network which would already have its weights and biases saved for the given model. Simply put, a pre-trained model is a model created by someone else to solve a similar problem. Instead of building a model from scratch to solve a similar problem, you use the model trained on other problem as a starting point.

I used Kaggle as reference to learn this if any of y’all are curious. I shall now cover a few lines of a code for an interesting model that I’m going to create.

Let us start by defining the directory in which the images of the dogs are present. The next step involves joining these images into an array using os library in python which is basically used to append the filename to the directory.

from os.path import join

image_dir = '../input/dog-breed-identification/train/'
img_paths = [join(image_dir, filename) for filename in
['0c8fe33bd89646b678f6b2891df8a1c6.jpg',
'0c3b282ecbed1ca9eb17de4cb1b6e326.jpg',
'04fb4d719e9fe2b6ffe32d9ae7be8a22.jpg',
'0e79be614f12deb4f7cae18614b7391b.jpg']]

This is mainly as a demo so we use only a few images. The next step involves pre-processing of these images for which we need to actually load some of these images. We need to specify the image height and the image width as well which we have covered in the below given snippet of code.

import numpy as np
from tensorflow.python.keras.applications.resnet50 import preprocess_input
from tensorflow.python.keras.preprocessing.image import load_img, img_to_array

image_size = 224

def read_and_prep_images(img_paths, img_height=image_size, img_width=image_size):
imgs = [load_img(img_path, target_size=(img_height, img_width)) for img_path in img_paths]
img_array = np.array([img_to_array(img) for img in imgs])
output = preprocess_input(img_array)
return(output)

Using the np.array, we are converting it into an image based array. The output here would be a 4 dimensional tensor or array but that might be a little confusing to understand so I’m just going to continue. Finally, we have used a preprocess_input to preprocess the image.

The next and most important step would be to create the model with pretrained weights and run predictions to that given model.

from tensorflow.python.keras.applications import ResNet50

my_model = ResNet50(weights='../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels.h5')
test_data = read_and_prep_images(img_paths)
preds = my_model.predict(test_data)

Next step is the prediction step whereby keras has this feature called decode_predictions. So this mainly helps in obtaining or extracting the highest probability of each image and we mention the top number of probabilities which should be returned. Have a look at the snippet of the code given below to execute this function:

from learntools.deep_learning.decode_predictions import decode_predictions
from IPython.display import Image, display

most_likely_labels = decode_predictions(preds, top=3, class_list_path='../input/resnet50/imagenet_class_index.json')

for i, img_path in enumerate(img_paths):
display(Image(img_path))
print(most_likely_labels[i])

Now, we will be able to visualize the see the outputs obtained from the given few lines of code. Again, I used Kaggle as reference and the link to where I learned it from is given below.

I’ve mentioned a couple of the outputs obtained below. Do have a look at them and the accuracy in calculating the top 3 probabilities obtained in identifying the breed of dog.

Output of pre-trained model

So, in the following manner, some simplistic code can be used and pre-trained models can be used with Keras or tensorflow to create some amazing new models.

Thanks for reading. Keep Learning.

Cheers.

--

--