Introduction to Keras Applications
Keras applications includes the models that are from the deep learning domain and are only available in scenarios where we have pretrained weights. The models of Keras applications have their usage in wide spectrum including fine-tuning, prediction and extraction of features. In this article, we will dive deep into the topic of Keras applications and will try to understand it by using the subtopics What is Keras applications, How to use models from Keras. applications, Keras applications model, and Conclusion about the same.
What is Keras applications?
Keras applications contains the models of deep learning that are used in fine-tuning, extraction of features and prediction. When the model is instantiated then all the necessary weights for the model are automatically downloaded and are saved in the folder of /.keras/models. When the Keras applications is instantiated then as per the format of image data the models of Keras are build considering all the configurations mentioned inside the Keras file located at .keras/Keras.JSON.
Lets take a sample consideration that is the configuration of image_data_format is set to the value of channels_last then with respect to the data format of TensorFlow convention followed height width and depth the loading of the model automatically takes place from this repository.
How to use models from Keras.applications?
Let us understand how we can make the use of keras.applications resnet model with the help of an example –
We will try to get the tuples in the list format which will include probability, description and class information by decoding them. We will even display one of the list in the output from the result in the batch.
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as numpyLibrary
sampleEducbaModel = ResNet50(weights='imagenet')
pathOfImage = 'bunny.jpg'
sampleEducbaImage = image.load_img(pathOfImage, target_size=(224, 224))
referenceVar = image.img_to_array(sampleEducbaImage)
referenceVar = numpyLibrary.expand_dims(referenceVar , axis=0)
referenceVar = preprocess_input(referenceVar)
madePredictions = sampleEducbaModel.predict(referenceVar)
print('The resultant sample list of predictions that are made include :', decode_predictions(madePredictions, top=3))
The execution of above output gives the following resultant –
Keras Applications Model
The architecture of models of Keras applications are completely compatible with CNTK, Theano, and Tensorflow backends. The below list includes the models available in Keras applications –
There are various application models available in keras. Some of them are as listed below –
- ResNet50 – This model has a size of 95 MB and has the top accuracy of 0.749 having a total of 25636712 parameters and time per step of CPU inference is 58.20 ms while for GPU is 4.55.
- Xception – This model has a size of 88 MB and has the top accuracy of 0.790 having a total of 22910480 parameters and time per step of CPU inference is 109.42 ms while for GPU is 8.06.
- VGG19 – This model has a size of 549 MB and has the top accuracy of 0.713 having a total of 143667240 parameters and time per step of CPU inference is 84.75 ms while for GPU is 4.38.
- MobileNet – This model has a size of 16 MB and has the top accuracy of 0.704 having a total of 4253864 parameters and time per step of CPU inference is 22.60 ms while for GPU is 3.44.
- DenseNet121 – This model has a size of 33 MB and has the top accuracy of 0.750 having a total of 8062504 parameters and time per step of CPU inference is 77.14 ms while for GPU is 5.38.
- NASNetMobile – This model has a size of 23 MB and has the top accuracy of 0.744 having a total of 5326716 parameters and time per step of CPU inference is 27.04 ms while for GPU is 6.70.
- EfficientNetB0 to B7 – EfficientNetB0 model has a size of 29 MB having a total of 5330571 parameters and time per step of CPU inference is 46.00 ms while for GPU is 4.91.
- InceptionResNetV2 – This model has a size of 215 MB and has the top accuracy of 0.803 having a total of 55873736 parameters and time per step of CPU inference is 130.19 ms while for GPU is 10.02.
The top accuracy stands for the image validation performance of the model for the dataset while time per step inference stands for the 10 repetitions and 30 batches average. For CPU, 92 core IBPB with AMD EPYC CPU processor having a total ram of 1.7 T with 32 as the size of batch and Tesla A100 GPU.
We can simply load any of the required model of Keras applications by importing the Keras and the model that is required from Keras. applications. The next step is to instantiate the architecture of the model by considering the weights of the image. If you need any architecture of model to instantiate, we can simply set the value of weights of model to none.
Let us consider a sample code snippet for importing the VGG16 model of keras application which looks like below –
From Keras. applications import vgg16
Import numpy as sampleNumpy
sampleEducbaVgg16Model = vgg16.VGG16 (weights = “imagenet”)
Similarly, we can create the sample model and instantiate the same of keras applications by simply importing the required library and then passing the parameters and instantiating the reference.
The next step is to load the image which can be done by using the method of load_img and passing the name of the file and target size. We will then have to convert the PIL format of the image to Numpy format containing the channels along with width and height with the help of function named image_to_array().
We will next need to convert the image to 4D tensor having batch size along with channels, width, and height. We can do this by using NumPy.expand_dims and passing the image of NumPy and the axis as zero.
The further steps include the optional normalization of the image by preprocessing them and then predict by using the function modelname. predict(). We can also convert the results of predictions to the labels with the help of function named decode_predictions().
Instead of taking the efforts for training the model that we have created, we can make the use of available models that are predefined in Keras applications.
This is a guide to Keras Applications. Here we discuss the Introduction, What is Keras applications, How to use models from Keras. applications, examples with code implementation. You may also have a look at the following articles to learn more –