Image classification using OpenCV with OpenVINO

Let's first create an image classification inference code using OpenCV. Since we are only concerned with inference, we will use a pre-trained model:

  1. First, let's download the Caffe model files, deploy.prototxt and bvlc_reference_caffenet.caffemodel, which can be obtained from Berkley Visions' repository (https://github.com/BVLC/caffe/tree/master/models/bvlc_reference_caffenet). Make sure that you download both files in your current working directory. We will also need a text file with the class labels mentioned. You can get it from https://github.com/torch/tutorials/blob/master/7_imagenet_classification/synset_words.txt.
  2. Let's also use a sample image of a giraffe for image classification:

Next, let's start writing some code for image classification using OpenCV with OpenVINO.

  1. Let's start off by importing some modules:
import numpy as np
import cv2
  1. Next, let's specify the model files:
image = cv2.imread("animal-barbaric-brown-1319515.jpg")
labels_file = "synset_words.txt"
prototxt = "deploy.prototxt"
caffemodel = "bvlc_reference_caffenet.caffemodel"
  1. Now, let's read the labels from the labels text file:
rows = open(labels_file).read().strip().split("
")
classes = [r[r.find(" ") + 1:].split(",")[0] for r in rows]
  1. Let's specify the backend that we will use for inference:
net = cv2.dnn.readNetFromCaffe(prototxt,caffemodel)
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_INFERENCE_ENGINE)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
  1. Let's carry out some basic image processing on the input image:
blob = cv2.dnn.blobFromImage(image,1,(224,224),(104,117,123))
  1. Finally, let's pass this image to the model and get the output:
net.setInput(blob)
predictions = net.forward()
  1. Let's obtain the top 10 predictions for the image of a giraffe we passed to the model:
indices = np.argsort(predictions[0])[::-1][:5]
  1. Finally, let's display the top 10 predictions:
for index in indices:
print("label: {}, prob.: {:.5}".format(classes[index], predictions[0][index]))

And, surprisingly, here is the result we obtain:

label: cheetah, prob.: 0.98357
label: leopard, prob.: 0.016108
label: snow leopard, prob.: 7.2455e-05
label: jaguar, prob.: 4.5286e-05
label: prairie chicken, prob.: 3.8205e-05

Notice that our model thought that the giraffe image we passed as input was actually a cheetah image. Why do you think that's the case? That's because giraffe was not present in the list of classes we had. So, the model came up with the closest match, which was because of the similar colored spots present on cheetahs and giraffes. So, the next time you carry out image classification, make sure the class is actually present in the label list.

We can also carry out a comparison between various backends to see the speedup obtained using OpenVINO's Inference Engine as the backend. Here's how it can be done. We need to change just one line in the preceding code:

net.setPreferableBackend(cv2.dnn.DNN_BACKEND_INFERENCE_ENGINE)

We can choose between the following backends:

  • cv2.dnn.DNN_BACKEND_DEFAULT: This is if you have OpenVINO installed and will use it as the default backend.
  • cv2.dnn.DNN_BACKEND_HALIDE: This requires OpenCV to be built with Halide. You can find detailed documentation for this at https://docs.opencv.org/4.1.0/de/d37/tutorial_dnn_halide.html.
  • cv2.dnn.DNN_BACKEND_OPENCV: This is the best choice for carrying out a comparison between both the backends. 

So, all you need to do is run the same code but replace the preceding line of code with the following:

net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)

And that's it! You can now carry out a comparison to see the speedup you obtain by using OpenVINO's Inference Engine as the backend.

You won't be able to see much difference in speed. To get a noticeable difference, use a for loop to carry out the inference 100 times, add up the total time taken during each step, and then divide it by 100 to obtain an average.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.149.238