Hello Image Classification

This tutorial is also available as a Jupyter notebook that can be cloned directly from GitHub. See the installation guide for instructions to run this tutorial locally on Windows, Linux or macOS. To run without installing anything, click the launch binder button.

Binder Github

A very basic introduction to OpenVINO that shows how to perform inference with an image classification model.

We use a pre-trained MobileNetV3 model from the Open Model Zoo. See the TensorFlow to OpenVINO tutorial to learn more about how this OpenVINO IR model was created.


import cv2
import matplotlib.pyplot as plt
import numpy as np
from openvino.inference_engine import IECore

Load the Model

ie = IECore()
net = ie.read_network(
    model="model/v3-small_224_1.0_float.xml", weights="model/v3-small_224_1.0_float.bin"
exec_net = ie.load_network(network=net, device_name="CPU")

input_key = next(iter(exec_net.input_info))
output_key = next(iter(exec_net.outputs.keys()))

Load an Image

# The MobileNet network expects images in RGB format
image = cv2.cvtColor(cv2.imread(filename="data/coco.jpg"), code=cv2.COLOR_BGR2RGB)

# resize to MobileNet image shape
input_image = cv2.resize(src=image, dsize=(224, 224))

# reshape to network input shape
input_image = np.expand_dims(input_image.transpose(2, 0, 1), 0)

Do Inference

result = exec_net.infer(inputs={input_key: input_image})[output_key]
result_index = np.argmax(result)
# Convert the inference result to a class name.
imagenet_classes = open("utils/imagenet_2012.txt").read().splitlines()

# The model description states that for this model, class 0 is background,
# so we add background at the beginning of imagenet_classes
imagenet_classes = ['background'] + imagenet_classes

'n02099267 flat-coated retriever'