Image Classification is a popular topic of pattern recognition. The process of classifying images based on contextual information presented on the image is called image classification. Automatic image organizations, product discovery through visual search, face recognition are few of the applications of image classification. The state of art technology to perform image classification is convolutional neural network (CNN). We can either design and train a CNN on our own or we can use pretrained CNN to classify images. The Google Net is one of the most popular pretrained CNNs. In this article, I explained how to use GoogleNet for image classification in MATLAB.

Video Lesson

Software and Add-ons Requirements

The code explained in this article has been implemented in MATLAB 2019B. Along with that the ‘MATLAB Support Package for USB’ has been used access the webcam. This example demonstrates how to use GoogleNet to classify images. However, GoogleNet is not a built-in feature of MATLAB. That is why we have to install the ‘Deep Learning Toolbox Model for GoogleNet Network’ add-ons. Another essential add-on for this example is ‘Deep Learning Toolbox’.

Software:

  1. MATLAB 2019B or higher

Add-Ons and Support Package:

  • Deep Learning Toolbox
  • MATLAB Support Package for USB and
  • Deep Learning Toolbox Model for GoogleNet Network

The Code

clear all 

the_camera = webcam;
the_network = googlenet;

required_input_size = the_network.Layers(1).InputSize(1:2)

figure
single_Image = snapshot(the_camera);
image(single_Image)
single_Image = imresize(single_Image, required_input_size);

[predicted_Item,probability] = classify(the_network, single_Image);
title({char(predicted_Item), num2str(max(probability),2)});

How it Works

At the very beginning of the code, add the ‘clear all’. After running the code, the system accesses the webcam and keeps it occupied. In order to free the webcam every time, we run the code, it is mandatory to use the ‘clear all’ at the beginning. Otherwise MATLAB won’t be able to access the webcam.

Then the ‘webcam’ object has been stored in a variable named ‘the_camera’. We can access the webcam anytime using this object. After that the ‘GoogleNet’ has been loaded in the ‘the_network’ variable. In order to input an image to the googlenet, we need to resize the image. The size of the image and the size of the input layer of the network must have to be the same. To find out the size of the input layer of googlenet, we used ‘the_network.Layers(1).InputSize(1:2)’. This function returns the size of the input layer of the googlenet.

The next task is to take a snapshot from the webcam. We can do it using ‘snapshot()’ function. This function requires the ‘webcam’ object as the argument and it returns a snapshot. In our code, the snapshot has been stored in a variable named ‘single_Image’. As discussed earlier, it is mandatory to make sure that the image size and the input layer size of the network are same. To make it sure that both sizes are same, the ‘resize()’ function has been used. Once the image is resized, we can pass it to the googlenet.

To classify an image, we use ‘classify()’ function. This function requires two arguments. The first argument is the network we use to classify the image and the second argument is the image itself. The function transfers the image in the network. The network classifies the image and returns the class name with a confidence score. The class name and the confidence score have been stored in two variables in our code. Finally they were inserted on the image using ‘title()’ function.

It is unnecessary to design and train a convolutional neural network when a better performing pretrained CNN is already available. This article shows how to use GoogleNet to classify images easily. The GoogleNet is a well-trained CNN and can classify images with acceptable accuracy.