Transfer Learning with Keras - Deep Learning tutorial | EduGrad

Transfer Learning Introduction

Humans have an intrinsic ability to transfer knowledge across tasks.

What we acquire as knowledge or gain while learning about one task, we utilize in the same way to solve related tasks. The more tasks are related, the easier it is for anyone to transfer, or cross-utilize our knowledge.

Some simple transfer learning examples  –

  • If we know how to ride a motorbike, we can easily learn how to ride a car
  • Also, if we know how to play classical piano, then we can easily learn how to play jazz piano.

In the above examples, we don’t learn everything from scratch for new aspects or topics. We transfer and leverage our knowledge from learning from the past and apply it to newer aspects.

Deep learning with keras live classes | EduGrad

Conventional ML or machine learning and deep learning algorithms, so far, have been traditionally designed to work in seclusion. It means that they need to be designed completely for a new problem. The models have to be rebuilt from scratch once the features change.

Transfer learning is the idea to overcome the isolated paradigm of learning and utilizing knowledge acquired for one task to solve related ones. Transfer learning Keras makes use of the knowledge gained while solving one problem and applying it to a different but related problem.

For example, the knowledge gained while learning to recognize cars can be used to some extent to recognize buses.

So, it is a machine learning method where a model developed for a task is reused as the starting point for a model on a second task. It is a popular approach in learning where previously trained models are used as the starting point on some tasks given the vast compute and time resources required to develop neural network models on these problems and from the huge jumps in the skill that they provide on related problems.

If I say what is Transfer Learning? Transfer learning is related to problems such as multi-task classification learning and is not exclusively an area of study for deep learning.

Transfer learning is popular in deep learning where the enormous resources are required to train deep neural network models or on the large and challenging datasets on which neural network models are being trained.

Transfer learning only works in deep learning if the model features learned from the first task are general.

Transfer Learning model | EduGrad

The steps to use transfer learning Keras on any of the modeling problems are:-

  • A pre-trained source model is chosen from all the available models.
  • The pre-trained model can then be used as a starting point for a model on the second task of interest. This may involve using all or parts of the model, depending on the modeling technique used.
  • If required, the model may need to be refined on the input-output pair data available for the task of interest.

A few steps should be kept in mind while working on Transfer Learning examples. We can give the new dataset to fine-tune the pre-trained CNN deep learning model. Keep in mind that the new dataset is almost similar to the initial original dataset used for pre-training. Since the new dataset is similar, the same weights can be used for extracting the features from the new dataset.

  • If the new incoming dataset is very small, it’s better to train only the final layers of the network to avoid overfitting, keeping all other layers fixed. To remove the final layers of the pre-trained network, add new layers. Retrain only the new layers.
  • If the new incoming dataset is very much large, retrain the whole network with initial weights from the pre-trained model.

Transfer learning has benefits of decreasing the training time for a neural network model and it also results in lower generalization error. The weights of the previously trained model can be used to solve a new problem.

The use of a pre-trained model is dependent upon our use-case. For example, a pre-trained model may be downloaded and used as it is, such as embedded into an application and used to classify new images or, models may be downloaded and can be used in feature extraction models. Here, the output of the model from a previous layer to the output layer of the model is used as input to a new model for classification.

The pre-trained models can be used in several ways.

  • The pre-trained model is used directly to classify new images.
  • The complete pre-trained model, or some portion of the model, is used to pre-process images and extract relevant features.
  • The pre-trained model, or some portion of the model, is integrated into a new model, but layers of the pre-trained model are fixed during training.
  • The pre-trained model, or some portion of the model, is integrated into a new model, and the layers of the pre-trained model are trained along with the new model.

Each approach can be effective and save significant time in developing a neural network model.

Pre-Trained models for Transfer Learning 

There are several pre-trained models that can be used for any of the above tasks.

These models are trained with millions of images and several classes and it takes several hours of training and a huge computation level.

The most popular models used in transfer learning are:-

  • VGG (VGG16 or VGG19)
  • Google-net (InceptionV3)
  • Residual Network (ResNet50)
  • Mobile-Net

These models are widely used for transfer learning because of their high performance.

We will use Keras which is a library for deep learning and it can be used above TensorFlow.

Keras provides access to a number of top-performing pre-trained models that were developed for image recognition tasks. They are available via the Applications API, and include functions to load a model with or without the pre-trained weights, and prepare data in a way that a given model may expect.

The first time a pre-trained model is loaded, Keras will download the required model weights enclosed in the h5 file, which may take some time given the speed of your internet connection.

To gain a practical intuition we will use Transfer Learning to classify objects using a pre-trained model named VGG16.

VGG is a model usually refers to a deep convolutional network for object recognition developed and trained by Oxford’s renowned Visual Geometry Group (VGG), which achieved very good performance on the ImageNet dataset.

The VGG model can be loaded and used in the Keras deep learning algorithms.

Keras provides an Application interface or API for loading and using pre-trained models.

VGG16 is a Deep CNN (convolutional neural network model).

The model achieves 92.7% top-5 test accuracy in ImageNet, which is a dataset of over 14 million images belonging to 1000 classes.

ImageNet is a dataset of over 15 million labeled high-resolution images belonging to roughly 22,000 categories. The images were collected from the web and labeled by human labelers using Amazon’s Mechanical Turk crowd-sourcing tool. In all, there are roughly 1.2 million training images, 50,000 validation images, and 150,000 testing images.

ImageNet consists of variable-resolution images. Therefore, the images have been down-sampled to a fixed resolution of 256×256. Given a rectangular image, the image is rescaled and cropped out the central 256×256 patch from the resulting image.

VGG16 was trained for weeks and was using NVIDIA Titan Black GPUs. It is a very large neural network.

Pretrained models for Transfer Learning | EduGrad

Code Implementation – 

from keras.applications.vgg16 import VGG16
model = VGG16()

Using TensorFlow backend.

We will load the VGG16 model present in keras.applications.vgg16 and instantiate a variable called model with VGG16.
The first time we run this code, Keras will download the weight files from the Internet and store them.

Model: "vgg16"
Layer (type)                 Output Shape              Param #   
input_1 (InputLayer)         (None, 224, 224, 3)       0         
block1_conv1 (Conv2D)        (None, 224, 224, 64)      1792      
block1_conv2 (Conv2D)        (None, 224, 224, 64)      36928     
block1_pool (MaxPooling2D)   (None, 112, 112, 64)      0         
block2_conv1 (Conv2D)        (None, 112, 112, 128)     73856     
block2_conv2 (Conv2D)        (None, 112, 112, 128)     147584    
block2_pool (MaxPooling2D)   (None, 56, 56, 128)       0         
block3_conv1 (Conv2D)        (None, 56, 56, 256)       295168    
block3_conv2 (Conv2D)        (None, 56, 56, 256)       590080    
block3_conv3 (Conv2D)        (None, 56, 56, 256)       590080    
block3_pool (MaxPooling2D)   (None, 28, 28, 256)       0         
block4_conv1 (Conv2D)        (None, 28, 28, 512)       1180160   
block4_conv2 (Conv2D)        (None, 28, 28, 512)       2359808   
block4_conv3 (Conv2D)        (None, 28, 28, 512)       2359808   
block4_pool (MaxPooling2D)   (None, 14, 14, 512)       0         
block5_conv1 (Conv2D)        (None, 14, 14, 512)       2359808   
block5_conv2 (Conv2D)        (None, 14, 14, 512)       2359808   
block5_conv3 (Conv2D)        (None, 14, 14, 512)       2359808   
block5_pool (MaxPooling2D)   (None, 7, 7, 512)         0         
flatten (Flatten)            (None, 25088)             0         
fc1 (Dense)                  (None, 4096)              102764544 
fc2 (Dense)                  (None, 4096)              16781312  
predictions (Dense)          (None, 1000)              4097000   
Total params: 138,357,544
Trainable params: 138,357,544
Non-trainable params: 0

Next, we can load the image as pixel data and prepare it to be presented to the network.
First, we can use the load_img() function to load the image and resize it to the required size of 224×224 pixels.

import matplotlib.pyplot as plt


Next, we can convert the pixels to a NumPy array so that we can work with it in Keras.
We can use the img_to_array() function for this.

image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))

The network expects one or more images as input, which means the input array will need to be 4-dimensional
number of samples, rows in each sample, columns in each sample, number of channels in each sample

We only have one sample (one image).
We can reshape the array by calling reshape() and adding the extra dimension.

from keras.applications.vgg16 import preprocess_input
image = preprocess_input(image)

We prepare the image for the VGG model

yhat = model.predict(image)

We use predict method under model to predict the class of the image

[[1.03606967e-09 1.08159781e-09 2.53284893e-11 2.97262742e-10
  3.09796078e-10 1.57357150e-09 5.11866244e-11 2.53958543e-09
  3.27615979e-09 7.59504548e-09 2.57676502e-09 9.96576688e-09
  7.47107887e-09 1.36965339e-09 2.22952075e-08 5.03811837e-09
  3.97759692e-09 5.95944094e-09 2.11307523e-08 3.06816039e-09
  5.66863445e-10 1.85850724e-09 2.06719397e-09 6.57562138e-09
  8.06918032e-10 8.52711068e-10 7.70898234e-10 1.37483385e-08
  4.25743663e-09 1.06223528e-08 2.34334951e-09 4.13444523e-09
  5.16156007e-09 1.28189057e-10 1.81323928e-10 3.97921890e-10
  4.22195084e-10 2.19581561e-10 9.17171894e-10 7.29479310e-10
  1.62820468e-09 7.46298578e-10 1.08838771e-09 1.07948328e-09
  5.75435921e-09 5.43187939e-09 3.48165186e-09 1.62538727e-09
  6.61260768e-10 3.70706660e-10 3.37206013e-10 4.40126824e-10
  7.63808927e-09 9.94789584e-09 2.40241405e-09 5.55572122e-09
  2.17054108e-09 3.49941243e-10 8.31060054e-10 2.74118594e-08
  3.04164294e-09 1.29462163e-09 1.74358222e-10 2.23840058e-08
  1.88190730e-09 2.20732232e-09 3.56219143e-09 1.51154655e-09
  1.16777199e-09 5.07918285e-09 6.48883214e-09 9.06863917e-09
  2.16750440e-09 3.36387140e-09 5.59715663e-10 1.72174950e-08
  9.51304724e-09 8.46868442e-09 8.38333420e-08 1.42402747e-08
  2.13235429e-09 3.77105763e-10 6.93486291e-09 1.22913879e-09
  7.10541070e-10 2.58688129e-08 6.71509248e-09 1.36398581e-09
  7.83726972e-10 2.06676032e-09 1.71718408e-08 1.89844429e-09
  2.70663003e-09 2.36778153e-09 2.48434029e-09 1.02419839e-09
  3.67635411e-09 1.50083015e-10 2.45639814e-10 4.91261143e-10
  3.50433016e-09 3.86520504e-08 1.92348426e-09 3.66836206e-09
  9.44192835e-10 1.45443602e-09 4.18455315e-09 9.34874400e-10
  1.39892023e-07 1.38405509e-09 1.18224064e-09 1.18543014e-07
  1.58959050e-08 4.44405535e-09 1.90462544e-08 6.89406809e-10
  9.20580723e-10 8.21732071e-10 6.57904664e-09 7.59159757e-09
  3.57083052e-09 5.48323964e-09 2.89255908e-08 1.27333371e-08
  4.68237005e-08 3.48848483e-09 3.72404885e-09 8.52850957e-10
  2.47669329e-10 1.98232056e-10 3.69417963e-09 2.76006618e-09
  7.76096165e-09 1.60352431e-09 3.47960522e-10 1.87325249e-08
  1.45285850e-09 1.81135962e-09 1.00963593e-09 5.68086522e-10
  1.12125031e-09 8.41706871e-10 3.37126244e-10 5.40116230e-10
  1.53682581e-10 8.15588521e-08 2.12934248e-09 3.70865089e-10
  3.84596532e-09 5.77256076e-10 4.60433247e-10 1.11386500e-08
  2.42758809e-08 5.53248825e-09 1.51054440e-08 1.39353007e-09
  8.02993727e-10 4.25333768e-09 1.97645100e-09 3.41651862e-09
  4.49040671e-09 2.60190314e-09 1.01314301e-09 4.55120475e-09
  3.06777426e-09 3.99506916e-09 1.08471321e-09 2.76427570e-09
  4.76326711e-09 1.08466358e-09 1.06616849e-09 2.94781399e-09
  9.08149556e-10 3.46343065e-09 2.89304719e-10 7.20965820e-09
  9.22014631e-10 3.30476579e-09 2.46233323e-09 2.33042341e-09
  1.63545977e-09 2.15748552e-09 3.79196372e-08 2.09588862e-07
  1.14883228e-07 2.85230222e-08 1.65857816e-08 1.87246663e-08
  4.13276027e-08 3.98050588e-08 5.61448381e-08 6.10571576e-08
  1.96869654e-09 3.23935634e-09 1.83424664e-09 6.70447031e-09
  6.90756297e-09 7.70575248e-09 2.43587657e-08 7.09850623e-09
  2.37160513e-09 7.80090992e-10 2.84778459e-08 6.48573861e-09
  2.78674817e-09 3.19962679e-09 4.29226032e-09 5.22396404e-10
  4.19624646e-09 5.30448174e-09 2.39867792e-09 9.13624465e-09
  7.09620085e-10 3.86891319e-09 1.95682914e-09 5.91160620e-10
  1.52772561e-09 1.85090143e-09 1.04304321e-09 1.38522771e-09
  1.94810106e-08 1.17266294e-08 2.54792565e-09 5.91818683e-09
  3.32438299e-09 1.32371336e-09 3.64328767e-09 3.23734284e-09
  5.30708977e-10 9.37968370e-09 1.00505737e-09 4.25144764e-09
  4.02730960e-09 2.93163342e-08 1.92975702e-09 7.50897622e-10
  1.42393175e-09 3.16066151e-09 3.86453225e-09 1.18813392e-09
  9.41540712e-09 2.36285502e-09 1.07893983e-09 3.39334028e-09
  1.22737009e-09 3.03061709e-09 1.30326827e-09 4.69059791e-10
  1.46746026e-09 1.12576981e-09 7.26743443e-10 5.27969135e-09
  7.62013670e-08 5.24058308e-09 1.23011041e-08 1.92096089e-10
  5.07476727e-09 3.89175858e-10 1.31313915e-09 6.84990020e-09
  1.55147895e-09 3.71673053e-10 3.02101085e-07 1.10859544e-09
  3.21477289e-09 6.54269394e-09 5.85131321e-09 3.06824233e-09
  8.79415385e-09 1.83810600e-09 6.61901867e-10 1.00373165e-09
  5.18411147e-10 1.63056346e-09 4.50466192e-10 2.38797204e-10
  2.76666384e-10 7.05112468e-10 7.41714967e-10 1.44019185e-09
  1.37321621e-09 2.07728079e-09 1.01041620e-09 5.26712851e-09
  4.37625630e-10 2.45865053e-08 1.01458703e-10 2.57063881e-09
  4.87516194e-10 3.47232548e-10 5.18011856e-10 7.51701534e-10
  8.99293251e-10 1.19794596e-09 4.03762135e-10 5.12348053e-10
  3.70533465e-10 1.28468158e-09 2.71376503e-08 1.77142656e-08
  1.48031942e-08 8.62833416e-10 2.59878998e-08 2.58790767e-08
  1.93569205e-09 7.91424437e-09 9.70834293e-08 2.45978904e-09
  4.15710799e-09 9.09907649e-10 5.02720443e-09 1.70085557e-09
  3.85188947e-09 1.38724243e-09 3.35387718e-09 1.64972114e-09
  1.88197902e-09 4.43563519e-10 2.06681450e-10 7.90336463e-10
  1.22080479e-09 7.97360933e-09 1.04455633e-09 1.99948924e-09
  5.23371790e-10 8.45523540e-10 3.59435059e-09 1.37955081e-09
  1.54383362e-09 2.81654966e-09 2.40259723e-09 2.30099428e-09
  9.48481516e-10 7.02138792e-09 3.99736938e-10 2.96264968e-09
  1.77746695e-09 2.14073270e-09 2.18436789e-08 2.08555326e-10
  3.83728049e-10 7.99187660e-10 1.07818487e-09 4.03881728e-09
  5.29148392e-10 1.27679856e-09 1.29663988e-10 6.13269380e-10
  1.60324909e-09 1.71325432e-09 1.99044941e-08 1.27041222e-09
  1.28913169e-09 6.56039723e-10 9.22167620e-10 1.14736720e-09
  4.08521217e-08 6.11624928e-09 5.18999110e-09 3.54221785e-09
  8.08677569e-10 1.49147734e-08 2.16224496e-08 2.02041339e-09
  9.59089141e-10 3.04073655e-10 3.26468208e-09 2.15436446e-09
  2.30917174e-09 2.56753219e-09 3.27682836e-09 4.15528456e-09
  9.87023618e-09 3.91300325e-09 1.11357557e-09 1.36142819e-09
  1.66612979e-09 3.11590642e-09 5.06060660e-09 3.50323437e-09
  4.65081440e-09 1.12116261e-09 1.48919965e-09 1.40102974e-09
  1.22842927e-08 7.01663572e-09 1.50771118e-09 1.45904000e-09
  3.26456834e-10 2.40717862e-10 2.44225458e-08 1.32391031e-09
  4.10201251e-09 8.25846058e-09 1.93417060e-10 6.00061334e-10
  7.34627359e-10 2.65632905e-10 8.84785467e-10 2.30917738e-08
  8.24722157e-09 1.09354037e-08 4.61450700e-09 1.07626942e-10
  3.34247630e-10 2.34267894e-09 8.01452960e-10 8.30553015e-10
  5.53280533e-09 2.17618094e-07 6.27647032e-11 6.63167121e-09
  1.57225236e-06 2.54915555e-09 2.27810393e-08 4.01227638e-08
  1.84909821e-09 1.52717288e-08 1.66228745e-07 4.19518642e-07
  1.07361886e-09 9.29978372e-10 1.38676626e-09 1.73038170e-08
  1.80902560e-09 9.19082366e-10 9.85263071e-09 3.46372030e-06
  1.45542183e-08 1.23544321e-08 9.42775280e-09 2.41346410e-09
  1.03162805e-07 6.15080742e-09 2.05186224e-09 8.78344242e-09
  1.47285184e-10 1.13576848e-09 1.73559347e-05 1.36654961e-07
  9.80084042e-06 7.94193329e-05 5.47236301e-09 1.71046910e-09
  8.77183604e-10 9.18761678e-10 8.95531826e-08 1.40482894e-06
  2.25081065e-09 1.23938346e-10 3.67905290e-10 3.90493078e-08
  1.33087275e-09 1.81670867e-09 1.89103222e-09 1.34131739e-07
  1.96025085e-09 3.37741248e-08 5.90835159e-09 2.75224910e-09
  7.07511827e-10 1.27866606e-08 1.66183245e-09 1.95817720e-05
  3.97243230e-06 2.00730064e-08 3.88726995e-10 1.09458942e-09
  4.81311302e-09 1.83957582e-05 2.42596725e-05 9.70828307e-09
  3.01377201e-10 5.73858756e-07 1.60156810e-09 1.39804595e-08
  2.18553259e-10 8.59396607e-08 3.59368073e-08 6.79462744e-08
  7.87620191e-10 1.40619463e-08 1.44154541e-07 3.91798460e-09
  1.11668029e-10 3.68255293e-08 4.43435200e-09 3.00976694e-07
  1.08704853e-07 1.50693069e-07 9.96770488e-09 2.40246116e-08
  9.90479920e-10 1.45988899e-09 7.81904230e-09 1.87423432e-09
  2.05891881e-09 3.18767984e-10 1.76672532e-09 1.66545476e-07
  6.17002727e-10 1.11478904e-09 1.45169921e-07 6.08718983e-05
  8.95975828e-01 9.26581677e-04 1.02153145e-07 4.47771498e-07
  1.56623905e-08 5.13903498e-09 3.67309494e-09 2.19915197e-10
  1.59391686e-06 4.42186270e-08 1.14257702e-07 6.68532918e-09
  1.06121263e-08 5.24239274e-09 4.33081482e-08 3.67483288e-09
  1.13920456e-10 2.41723865e-06 2.04137809e-08 1.27613946e-08
  5.78858383e-09 4.50261717e-10 9.56825819e-09 3.13524708e-08
  6.97125699e-08 6.93872648e-09 4.19336232e-07 7.84805138e-07
  8.60803695e-09 8.88330121e-09 4.76821205e-09 1.14090177e-08
  6.16754736e-09 1.20734855e-09 2.22297514e-10 2.70462763e-09
  1.66717251e-09 3.72115551e-07 5.90474647e-09 2.23125625e-08
  5.90173499e-07 5.25372412e-09 1.05112612e-08 1.02786918e-10
  8.14308454e-10 1.12421616e-09 7.01498429e-05 3.20610707e-06
  5.80453774e-09 6.97618463e-10 1.42188539e-09 7.70449926e-10
  3.26726712e-09 3.43985396e-10 6.67488065e-09 1.75454862e-09
  1.57218025e-08 9.52993684e-09 6.69405642e-09 1.50000815e-07
  1.11018604e-10 5.94614535e-09 2.97093585e-08 5.64473126e-08
  7.13686776e-10 3.02897019e-09 1.14326149e-07 2.02642330e-08
  2.37239728e-05 5.27335109e-09 7.67157218e-08 1.03951792e-09
  5.50575807e-10 7.43270290e-09 2.86373192e-09 1.41994483e-09
  5.44290168e-10 2.15784368e-09 2.46109959e-08 7.26613159e-10
  3.38306506e-07 2.74276294e-06 5.36399758e-10 1.44224671e-07
  3.64137037e-07 7.03597820e-08 2.68276281e-08 1.31681355e-09
  5.63736116e-08 6.25198666e-08 2.38265918e-09 2.29668840e-09
  5.96505032e-08 8.95666830e-08 1.82699111e-09 5.42606005e-09
  7.13636155e-07 3.64848013e-10 5.17284160e-10 2.98241820e-10
  3.78212590e-08 1.14948215e-07 7.98896949e-09 3.59579744e-09
  3.32223404e-09 2.59540389e-10 6.09421988e-08 3.18334936e-09
  7.30727034e-10 4.77833808e-08 1.45890089e-09 1.89991695e-07
  4.78829207e-08 1.16469625e-08 1.08483533e-07 3.04207965e-08
  1.34227900e-08 3.05369330e-09 1.69017494e-06 1.48551322e-07
  4.85328755e-09 4.17503226e-10 3.63460344e-06 9.61888458e-10
  2.85844948e-09 6.05679361e-07 3.71212039e-09 3.86521460e-05
  3.36791629e-07 3.29204386e-06 8.66871519e-10 3.03746901e-07
  7.87931995e-08 2.97317326e-09 2.72756324e-08 1.27191058e-09
  1.30600225e-10 3.19824913e-08 5.43168011e-10 2.65827289e-08
  6.44333866e-08 5.82559723e-10 4.54093874e-09 4.07533662e-04
  1.60990545e-08 1.29812596e-08 2.10928806e-06 4.34822951e-08
  4.75636730e-09 5.87369686e-06 3.03911452e-09 5.16814758e-10
  3.54425711e-10 2.08963558e-09 4.03799518e-08 2.47416409e-07
  1.52750923e-10 5.90363181e-09 1.51397316e-07 7.40805861e-10
  3.44575901e-09 2.45485715e-10 1.35959044e-06 1.36748746e-08
  8.50816362e-11 2.82159591e-11 4.59633054e-10 5.37898970e-09
  4.28377694e-10 1.68831008e-07 1.43780188e-08 1.35575062e-09
  1.11533247e-07 1.17446547e-07 1.06668622e-07 6.32979891e-08
  5.70095244e-06 1.03727096e-07 3.29417427e-09 1.44218887e-07
  6.93886193e-08 1.09451905e-08 7.45278021e-06 4.77529016e-10
  4.31399405e-09 7.04091063e-10 2.31773531e-10 4.03550047e-08
  3.03302585e-07 8.24990742e-10 7.49203338e-11 3.16389759e-07
  1.97231785e-07 4.93839913e-10 4.90340879e-10 3.96462454e-08
  4.40645351e-07 3.58522850e-10 6.83467949e-10 1.03149123e-09
  1.38676896e-07 5.99338712e-09 2.62188027e-09 9.04933906e-09
  4.91122947e-08 6.76955425e-09 3.39695418e-07 2.96307257e-06
  2.80157986e-09 1.27040012e-09 8.28255679e-07 7.94340149e-09
  1.07995901e-09 2.56497845e-08 1.98958672e-09 1.01087991e-07
  2.15908221e-06 4.33927116e-09 5.40749703e-08 1.43797940e-09
  2.31759079e-09 2.17744545e-03 6.14381601e-10 8.82767637e-10
  2.94139042e-08 2.01247485e-09 1.44004619e-09 3.72865614e-08
  2.10045087e-07 6.39138875e-09 2.61434818e-10 6.43105402e-10
  4.13360945e-09 1.55859084e-06 1.03494403e-05 8.25760253e-07
  4.70373642e-08 1.56672147e-10 3.38320061e-09 1.98792538e-09
  7.60130536e-09 4.14458512e-08 4.49943030e-07 5.00705653e-07
  2.07224815e-08 5.96686007e-08 1.42655429e-10 7.70209319e-09
  3.41035977e-09 7.23166305e-10 2.86685264e-07 1.25353206e-09
  4.11765512e-07 8.13333623e-10 2.68021054e-08 1.55815098e-06
  7.82530929e-09 3.30011432e-07 2.51994834e-08 1.34121625e-09
  2.09422413e-09 1.32717002e-08 3.76826970e-09 2.13196401e-07
  1.67207759e-08 3.89126527e-08 2.23529328e-09 1.28568411e-09
  1.08198279e-07 3.06817237e-05 3.54515990e-08 5.47133006e-09
  9.44460368e-08 2.62971458e-08 1.67135052e-08 1.29279254e-10
  8.55872151e-10 2.42642484e-09 3.87106125e-09 5.97892091e-09
  6.93503850e-08 6.31920756e-08 6.31401491e-08 6.80615475e-09
  5.92064509e-10 7.74730669e-10 1.29971596e-07 5.49719870e-09
  1.10335023e-07 2.32781572e-09 2.19348095e-09 6.28414343e-09
  1.64741714e-08 8.80388829e-09 3.91183175e-09 1.11730673e-10
  2.43337572e-10 3.93400370e-08 2.39404191e-10 5.30565314e-10
  2.42499232e-06 4.28021840e-09 1.05879048e-08 2.58421684e-09
  4.54064386e-09 2.04186817e-05 2.77386714e-09 4.63598191e-08
  8.56607585e-09 2.41021894e-07 3.76229992e-10 8.89467389e-10
  1.93728411e-09 2.25257146e-09 1.92981233e-07 8.29666735e-09
  9.51035806e-10 1.50250357e-09 2.65596545e-09 9.26691598e-07
  1.66628951e-08 2.79161561e-09 4.12191838e-07 1.14972444e-07
  6.81781614e-07 1.21764665e-09 1.97007877e-09 1.83409266e-09
  2.73173595e-10 2.88168178e-09 2.03994599e-08 8.63975025e-09
  7.78125411e-07 6.22812649e-07 1.55862290e-05 9.12943321e-09
  2.56477293e-08 1.56884017e-08 1.19742118e-08 7.83849874e-09
  2.55279620e-08 3.18592654e-08 1.14810284e-07 6.03336714e-10
  3.24171943e-08 4.89209451e-05 5.55184627e-08 1.08181712e-08
  4.63196272e-07 1.08494859e-10 1.15174865e-08 1.38399378e-06
  1.33858462e-08 1.89457383e-09 2.25044161e-09 1.52832769e-07
  1.62433125e-08 3.28043193e-08 4.04831155e-07 8.01144151e-10
  7.87378784e-09 1.16081456e-09 2.10231335e-08 1.37193334e-09
  7.59152883e-08 7.77288345e-10 9.73782566e-09 7.75833842e-10
  4.01884002e-08 4.80246687e-09 8.58058763e-11 9.35583078e-09
  1.38229082e-08 1.47986201e-08 5.94561200e-09 1.20104895e-08
  8.64629091e-10 9.20571896e-10 5.22296304e-08 2.83462596e-05
  5.80753556e-10 7.74568587e-09 4.49677495e-09 7.44563966e-10
  4.17740020e-09 5.41252954e-09 1.26700261e-09 4.10582679e-08
  4.91882046e-09 5.18733181e-08 5.01432174e-10 8.28540403e-10
  1.91878314e-07 1.24918875e-08 9.85550723e-05 1.37265329e-03
  1.94412557e-08 6.64370600e-03 1.10246526e-06 4.08694012e-09
  7.45800158e-11 1.55603322e-10 3.67177799e-09 2.93087624e-06
  1.88695348e-09 1.16958205e-07 9.77972491e-07 1.92481746e-08
  6.15798801e-10 2.33842479e-09 1.77463488e-09 2.31063349e-10
  2.51353995e-08 5.24984722e-09 1.10130557e-07 1.75284693e-08
  1.85592464e-09 3.75729812e-08 2.58448445e-08 1.36112192e-07
  5.61655384e-07 1.63817526e-06 7.01444151e-07 1.20804016e-08
  4.10412582e-07 2.44279033e-08 1.32055185e-08 6.81733141e-08
  9.91835009e-07 6.44793330e-09 3.95840587e-08 8.25104891e-08
  8.84310580e-08 1.05503386e-06 1.23715580e-07 5.11195779e-08
  2.29701378e-08 1.93493630e-08 3.99379161e-08 1.11328568e-07
  2.94405087e-08 1.02859894e-06 2.77315282e-09 1.76443526e-09
  1.14213151e-07 3.36563971e-07 3.62121675e-08 1.56229703e-07
  3.02647685e-08 1.93130081e-08 7.83352618e-08 3.72106657e-09
  1.37457157e-08 3.21842952e-09 4.01118738e-09 5.88254778e-08
  1.71625688e-05 2.46156908e-08 1.90946690e-08 2.70466260e-10
  9.15545062e-09 5.94581913e-08 1.40009497e-05 3.65953776e-04
  9.12993699e-02 7.95830274e-05 5.06771292e-10 3.05090637e-08
  3.10992743e-09 8.13016143e-09 4.10747747e-09 4.78546847e-09
  8.55271631e-10 3.50548679e-09 1.97905758e-09 9.27323218e-10
  7.39824479e-10 5.78098958e-10 1.97315120e-09 5.62196023e-09
  4.70652495e-09 1.27731079e-08 5.31698907e-10 5.69808201e-08
  1.34427694e-07 8.07894818e-09 3.53207277e-08 6.06128694e-08
  6.06892647e-10 2.31130004e-09 1.14921628e-08 3.64421870e-09
  2.77337637e-08 1.41962531e-09 1.76038562e-08 2.96728846e-08]]

On printing yhat which contains the predicted data, we are unable to rule out which class does our image lies.

Hence, we will use default imagenet weights present readily for VGG16 model by using the decode_predictions method.

from keras.applications.vgg16 import decode_predictions

output = decode_predictions(yhat)
output = output[0][0]

print('The images is of\n: %s (%.2f%%)' % (output[1], output[2]*100))


Please enter your comment!
Please enter your name here