Keras Applications – Learn when to use Keras?

Free Keras course with real-time projects Start Now!!

With rich user experience, reusability of code and extensibility, Keras provides the ease and the flexibility to write codes. But not just that, Keras provides a few other features which extend the range of keras applications.

The major applications of Keras are the deep learning models that are available with their pretrained weights. The user can directly use these models to make predictions or extract its features to use in their work without creating and training their own models.

In this article, we will talk about these pretrained models and how to use these models.

Before we start with Keras applications, let us see features of Keras that make it so useful.

 

Keras Pretrained models

There are 10 pretrained models available in Keras. These models are used for image classification and their weights are trained on ImageNet dataset.

The models are available in the “applications” module of Keras, hence to load these models we import it from keras.applications._model_name_

The available models are:

  • Xception
  • InceptionResNetV2
  • VGG16
  • MobileNet
  • VGG19
  • MobileNetV2
  • ResNet, ResNetV2
  • DenseNet
  • InceptionV3
  • NASNet

For eg: to load and instantiate ResNet50 model

from keras.applications.resnet50 import ResNet50
model=ResNet50(weights='imagenet')

All the models have different sizes of weights and when we instantiate a model, weights are downloaded automatically.
It may take some time to instantiate a model depending upon the size of weights.

Keras Resnet

What is ImageNet?

ImageNet is a large dataset of annotated objects. The intention of the ImageNet project was to develop computer vision algorithms.
“imagenet” is a collection of object image data.”imagenet” includes around 1000 categories of images with its annotations.
Since 2010, the ImageNet project annually organizes a contest named ImageNet Large Scale Visual Recognition Challenge to build models on imagenet dataset for object or image classification.

Some popular classes in imagenet dataset are:
1. Animal

  • Fish
  • Bird
  • mammals

2. Plant

  • Tree
  • Flower
  • Vegetable

3. Material

  • Fabric

4. Instrument

  • Utensil
  • Tool
  • Appliance

5. Scene

  • Room

These categories are further classified into sub-categories.

Implementation of Keras Pretrained model

1. Import the model and required libraries

from keras.preprocessing import image
from keras.applications.resnet50 import ResNet50
from keras.applications.resnet50 import preprocess_input
from keras.applications.resnet50 import decode_predictions
import numpy as np

2. Instantiate the model

model=ResNet50(weights='imagenet')

You can analyze the model using

model.summary()

model summary (1)

4. Give image input for prediction

path='./….../...png' #path of image
img=image.load_img(path,target_size=(224,224))
x=image.img_to_array(img)
x=np.expand_dims(x,axis=0)
x=preprocess_input(x)

5. Make prediction

res=model.predict(x)
print(decode_predictions(res,top=5)[0])

Here top 5 predictions are there from the models. The model’s output is

model predict (1)

You can see, the model predicts the sample image of the bird as goldfinch, jacamar, robin, brambling or water_ouzel which are the types of birds.

Similarly, you can use the available models to classify other images without training a model.

There is also a way to use these models on your own images and make predictions according to the classes you specify. This is known as transfer learning, where we extract the features from pre-trained models and use them according to our problem.

Summary

This article illustrates the major application of Keras, i.e the pre-trained models that are available in Keras. In this article, we discuss ImageNet dataset and its classes. This article shows how easy it is to classify images using available models and also gives a glimpse of transfer learning.
The available models are some of the best neural net structures with state of art accuracy for image or object classification.

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

follow dataflair on YouTube

2 Responses

  1. Vishal Rochlani says:

    Very good content, never saw such a great tutorial. My all doubts are clear now.

  2. Yash Sharma says:

    Nice tutorial want some more tutorials like that.

Leave a Reply

Your email address will not be published. Required fields are marked *