EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials Keras Tutorial Keras Applications
Secondary Sidebar
Keras Tutorial
  • Basic
    • What is Keras?
    • Keras Install
    • Keras Applications
    • Keras Sequential
    • Keras Model Predict
    • Keras Save Model
    • Keras conv2D
    • Keras ImageDataGenerator
    • Keras input
    • Keras Datasets
    • Keras Early Stopping
    • Keras input
    • Keras Model Save
    • Keras LSTM Example
    • Keras Flatten
    • Keras Optimizers
    • Keras Layers
    • Keras Dense
    • Keras fit
    • Keras Model
    • Keras Metrics
    • Keras Batch Normalization
    • Keras CNN
    • Keras predict
    • Keras Dropout
    • Keras Embedding
    • Keras LSTM
    • Keras GPU
    • Keras Tuner
    • Keras VGG16
    • Keras Generator
    • Keras Pre-trained Models
    • Keras Custom Loss Function
    • keras.utils.to_categorical
    • Keras Neural Network
    • Keras Preprocessing
    • Keras Regularization
    • Keras Softmax
    • Keras Regression
    • Keras MaxPooling2D
    • Keras U-Net
    • Keras Initializers
    • Keras Transformer
    • Keras Data Augmentation
    • Keras ResNet50
    • Keras Verbose
    • Keras Plot Model
    • Keras OCR
    • Keras Utils Sequence
    • Keras Binary Classification
    • Keras Padding
    • UpSampling2d
    • Keras EfficientNet
    • Keras pad_sequences

Keras Applications

Keras Applications

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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)[0])

The execution of above output gives the following resultant –

11

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 –

• VGG19
• Xception
• VGG16
• InceptionV3
• ResNet50
• MobileNet
• InceptionResNetV2
• NasNet
• DenseNet
• MobileNetV2

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 –

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 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().

Conclusion

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.

Recommended Articles

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 –

  1. What is Keras?
  2. TensorFlow Keras Model
  3. TensorFlow vs Keras
  4. PyTorch vs Keras
Popular Course in this category
Keras Training (2 Courses, 8 Projects)
  2 Online Courses |  8 Hands-on Project |  24+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more