Skip to content

Commit deaa959

Browse files
authored
Change read_network to use only model.xml (openvinotoolkit#198)
1 parent af3b01c commit deaa959

File tree

4 files changed

+638
-594
lines changed

4 files changed

+638
-594
lines changed

notebooks/001-hello-world/001-hello-world.ipynb

+45-39
Original file line numberDiff line numberDiff line change
@@ -2,114 +2,120 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5+
"id": "291dc37b",
6+
"metadata": {},
57
"source": [
68
"# Hello World\n",
79
"\n",
810
"A very basic introduction to OpenVINO that shows how to do inference on a given IR model.\n",
911
"\n",
1012
"We use a [MobileNetV3 model](https://docs.openvinotoolkit.org/latest/omz_models_model_mobilenet_v3_small_1_0_224_tf.html) from [Open Model Zoo](https://github.com/openvinotoolkit/open_model_zoo/). See the [TensorFlow to OpenVINO Notebook](101-tensorflow-to-openvino) for information on how this OpenVINO IR model was created.\n",
1113
"\n"
12-
],
13-
"metadata": {}
14+
]
1415
},
1516
{
1617
"cell_type": "markdown",
18+
"id": "e4c8cbe5",
19+
"metadata": {},
1720
"source": [
1821
"## Imports"
19-
],
20-
"metadata": {}
22+
]
2123
},
2224
{
2325
"cell_type": "code",
2426
"execution_count": null,
27+
"id": "41ee9436",
28+
"metadata": {},
29+
"outputs": [],
2530
"source": [
2631
"import json\n",
2732
"\n",
2833
"import cv2\n",
2934
"import matplotlib.pyplot as plt\n",
3035
"import numpy as np\n",
3136
"from openvino.inference_engine import IECore"
32-
],
33-
"outputs": [],
34-
"metadata": {}
37+
]
3538
},
3639
{
3740
"cell_type": "markdown",
41+
"id": "55e49ae7",
42+
"metadata": {},
3843
"source": [
3944
"## Load the network"
40-
],
41-
"metadata": {}
45+
]
4246
},
4347
{
4448
"cell_type": "code",
4549
"execution_count": null,
50+
"id": "e3c4d6fc",
51+
"metadata": {},
52+
"outputs": [],
4653
"source": [
4754
"ie = IECore()\n",
48-
"net = ie.read_network(\n",
49-
" model=\"model/v3-small_224_1.0_float.xml\", weights=\"model/v3-small_224_1.0_float.bin\"\n",
50-
")\n",
55+
"net = ie.read_network(model=\"model/v3-small_224_1.0_float.xml\")\n",
5156
"exec_net = ie.load_network(net, \"CPU\")\n",
5257
"\n",
53-
"input_key = list(exec_net.input_info)[0]\n",
54-
"output_key = list(exec_net.outputs.keys())[0]"
55-
],
56-
"outputs": [],
57-
"metadata": {}
58+
"input_key = next(iter(exec_net.input_info))\n",
59+
"output_key = next(iter(exec_net.outputs.keys()))"
60+
]
5861
},
5962
{
6063
"cell_type": "markdown",
64+
"id": "a19fc080",
65+
"metadata": {},
6166
"source": [
6267
"## Load an Image"
63-
],
64-
"metadata": {}
68+
]
6569
},
6670
{
6771
"cell_type": "code",
6872
"execution_count": null,
73+
"id": "eca45b68",
74+
"metadata": {},
75+
"outputs": [],
6976
"source": [
7077
"# The MobileNet network expects images in RGB format\n",
7178
"image = cv2.cvtColor(cv2.imread(\"data/coco.jpg\"), cv2.COLOR_BGR2RGB)\n",
72-
"input_image = cv2.resize(image, (224, 224)) # resize to MobileNet image shape\n",
73-
"input_image = np.expand_dims(\n",
74-
" input_image.transpose(2, 0, 1), 0\n",
75-
") # reshape to network input shape\n",
76-
"plt.imshow(image)"
77-
],
78-
"outputs": [],
79-
"metadata": {}
79+
"# resize to MobileNet image shape\n",
80+
"input_image = cv2.resize(image, (224, 224))\n",
81+
"# reshape to network input shape\n",
82+
"input_image = np.expand_dims(input_image.transpose(2, 0, 1), 0)\n",
83+
"plt.imshow(image);"
84+
]
8085
},
8186
{
8287
"cell_type": "markdown",
88+
"id": "6be327b6",
89+
"metadata": {},
8390
"source": [
8491
"## Do Inference"
85-
],
86-
"metadata": {}
92+
]
8793
},
8894
{
8995
"cell_type": "code",
9096
"execution_count": null,
97+
"id": "1ed78a71",
98+
"metadata": {},
99+
"outputs": [],
91100
"source": [
92101
"result = exec_net.infer(inputs={input_key: input_image})[output_key]\n",
93102
"result_index = np.argmax(result)"
94-
],
95-
"outputs": [],
96-
"metadata": {}
103+
]
97104
},
98105
{
99106
"cell_type": "code",
100107
"execution_count": null,
108+
"id": "bf29578c",
109+
"metadata": {},
110+
"outputs": [],
101111
"source": [
102112
"# Convert the inference result to a class name.\n",
103113
"imagenet_classes = json.loads(open(\"utils/imagenet_class_index.json\").read())\n",
104114
"# The model description states that for this model, class 0 is background,\n",
105115
"# so we add 1 to the network output to get the class name\n",
106-
"imagenet_classes = {\n",
107-
" int(key) + 1: value for key, value in imagenet_classes.items()\n",
108-
"}\n",
116+
"imagenet_classes = {int(key) + 1: value for key, value in imagenet_classes.items()}\n",
109117
"imagenet_classes[result_index]"
110-
],
111-
"outputs": [],
112-
"metadata": {}
118+
]
113119
}
114120
],
115121
"metadata": {
@@ -133,4 +139,4 @@
133139
},
134140
"nbformat": 4,
135141
"nbformat_minor": 5
136-
}
142+
}

0 commit comments

Comments
 (0)