How to build an Image Classifier using OpenVINO & Alexnet models

Surabhi Agarwal
Analytics Vidhya
Published in
4 min readApr 13, 2020

This tutorial helps in getting a better understanding of Image Classification using an alexnet model and OpenVINO libraries. By the end of this tutorial you can write your very own Image classifer with just a couple of lines of code.

All you need is the latest version of Intel OpenVINO toolkit installed on your system along with an alexnet model.

Once you have OpenVINO installed, download the alexnet model by navigating to the model_downloader directory as shown below

Now, run the downloader.py file using the following command, also specify the directory you want to save the model in. I have saved the model in the Downloads directory.

This should create a folder called ‘alexnet’ in your Downloads directory.

Now, navigate to the model_optimizer and convert the alexnet caffe model into xml and bin files by doing the steps as shown below

In order to check that you have the files downloaded, go over to the alexnet folder in the Downloads directory, you should see the alexnet.xml and alexnet.bin files.

Once you have the model downloaded, open the favorite editor of your choice and create a python file, let’s call it inference_script.py.

Let’s hop right into it!

Step 1 : Load the libraries

Load all the libraries we’ll be using in the tutorial.

Step 2 : Load the models

Give the paths to the alexnet model xml and bin files.

Step 3 : Load the network

Using a function, we will load the network by passing the xml and bin files of the model as parameters.

The input and output placeholder key allows us to highlight a specific area for the inference.

The function then returns the network and the input and output placeholder keys.

Step 4 : Define the image parameters like batch size, channels and dimensions

Step 5 : Now call the loadNetwork function and provide the image source path.

Pass the model xml and bin files as function arguments and provide the image source path.

You can use the image of your choice, but I have used the following cat image

Step 6 : Define the load source function to load the image

Using Opencv libraries, we will load the image and resize it according to the need for inference.

Step 7 : Adding batches to the image using add_batches function

Now, we will add the image parameters like the batches to our image and then return the new image.

Step 8 : Classify the image

Finally, classify the image and determine the device used for performing the inference and then return this result.

Step 9 : Display the results!

Output:

You should see something like this in your output terminal.

The highlighted numbers on the last two lines denote the result of the inference.

This can be viewed in this Github gist: look for the number 282 and you will see tiger, cat as the result. This proves that our inference script works for our given input!

The following Github gist contains the entire source code:

Congratulations! You have built your very first image classifier from scratch!

Credits : Tejas Pandey, Intel Movidius Advanced Architectures Team

--

--

Surabhi Agarwal
Analytics Vidhya

Computer Science student at University College Dublin, Intern at Intel Movidius. https://surabhiagarwal.pb.online/