Updated June 14, 2023
Introduction to Keras Applications
Keras applications include models that are from the deep learning domain and are only available in scenarios where we have pre-trained weights. The models of Keras applications have their usage in a wide spectrum, including fine-tuning, prediction, and extraction of features. In this article, we will dive deep into the topic of Keras applications. We will try to understand it by using the subtopics of what keras applications are, how to use models from Keras.applications, keras applications model, and a conclusion about the same.
What are Keras Applications?
Keras applications contain deep learning models used in fine-tuning, extraction of features, and prediction. When the model is instantiated, all the necessary weights are automatically downloaded and saved in the folder of /.keras/models. When the Keras applications are instantiated, then as per the format of image data, the Keras models are built considering all the configurations mentioned inside the Keras file located at .keras/Keras.JSON.
Let’s take a sample consideration that is the configuration of image_data_format is set to the value of channels_last, then concerning the data format of TensorFlow convention followed by 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 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 lists 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)[0])
The execution of the above output gives the following resultant –
Keras Applications Model
The architecture of models of Keras applications is completely compatible with CNTK, Theano, and Tensorflow backends. The below list includes the models available in Keras applications –
- VGG19
- Xception
- VGG16
- InceptionV3
- ResNet50
- MobileNet
- InceptionResNetV2
- NasNet
- DenseNet
- MobileNetV2
There are various application models available in Keras.
Some of them are listed below –
- ResNet50 – This model has a size of 95 MB and has a top accuracy of 0.749, having a total of 25636712 parameters 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 a top accuracy of 0.790, having a total of 22910480 parameters 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 a top accuracy of 0.713, having a total of 143667240 parameters 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 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 a top accuracy of 0.750, having a total of 8062504 parameters 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 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 the 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 a 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 has a total ram of 1.7 T with 32 as the batch size and Tesla A100 GPU.
We can load any Keras application models by importing the Keras and the required model from Keras.applications. The next step is to instantiate the model’s architecture by considering the weights of the image. If you need any architecture of the model to instantiate, we can set the value of the model’s weights to none.
Let us consider a sample code snippet for importing the VGG16 model of the Keras application, which looks like the below –
Import Keras
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 Keras applications by importing the required library, 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 the Numpy format containing the channels along with width and height with the help of a 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 predicting by using the function modelname. predict(). We can also convert the results of predictions to labels with the help of a function named decode_predictions().
Conclusion
Instead of making an effort to train the model that we have created, we can make use of available models that are predefined in Keras applications.
Recommended Articles
This is a guide to Keras Applications. Here we discuss the introduction, what are keras applications, and how to use models from Keras.applications. You may also have a look at the following articles to learn more –