2
2
"cells" : [
3
3
{
4
4
"cell_type" : " markdown" ,
5
+ "id" : " 291dc37b" ,
6
+ "metadata" : {},
5
7
"source" : [
6
8
" # Hello World\n " ,
7
9
" \n " ,
8
10
" A very basic introduction to OpenVINO that shows how to do inference on a given IR model.\n " ,
9
11
" \n " ,
10
12
" 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 " ,
11
13
" \n "
12
- ],
13
- "metadata" : {}
14
+ ]
14
15
},
15
16
{
16
17
"cell_type" : " markdown" ,
18
+ "id" : " e4c8cbe5" ,
19
+ "metadata" : {},
17
20
"source" : [
18
21
" ## Imports"
19
- ],
20
- "metadata" : {}
22
+ ]
21
23
},
22
24
{
23
25
"cell_type" : " code" ,
24
26
"execution_count" : null ,
27
+ "id" : " 41ee9436" ,
28
+ "metadata" : {},
29
+ "outputs" : [],
25
30
"source" : [
26
31
" import json\n " ,
27
32
" \n " ,
28
33
" import cv2\n " ,
29
34
" import matplotlib.pyplot as plt\n " ,
30
35
" import numpy as np\n " ,
31
36
" from openvino.inference_engine import IECore"
32
- ],
33
- "outputs" : [],
34
- "metadata" : {}
37
+ ]
35
38
},
36
39
{
37
40
"cell_type" : " markdown" ,
41
+ "id" : " 55e49ae7" ,
42
+ "metadata" : {},
38
43
"source" : [
39
44
" ## Load the network"
40
- ],
41
- "metadata" : {}
45
+ ]
42
46
},
43
47
{
44
48
"cell_type" : " code" ,
45
49
"execution_count" : null ,
50
+ "id" : " e3c4d6fc" ,
51
+ "metadata" : {},
52
+ "outputs" : [],
46
53
"source" : [
47
54
" 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 " ,
51
56
" exec_net = ie.load_network(net, \" CPU\" )\n " ,
52
57
" \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
+ ]
58
61
},
59
62
{
60
63
"cell_type" : " markdown" ,
64
+ "id" : " a19fc080" ,
65
+ "metadata" : {},
61
66
"source" : [
62
67
" ## Load an Image"
63
- ],
64
- "metadata" : {}
68
+ ]
65
69
},
66
70
{
67
71
"cell_type" : " code" ,
68
72
"execution_count" : null ,
73
+ "id" : " eca45b68" ,
74
+ "metadata" : {},
75
+ "outputs" : [],
69
76
"source" : [
70
77
" # The MobileNet network expects images in RGB format\n " ,
71
78
" 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
+ ]
80
85
},
81
86
{
82
87
"cell_type" : " markdown" ,
88
+ "id" : " 6be327b6" ,
89
+ "metadata" : {},
83
90
"source" : [
84
91
" ## Do Inference"
85
- ],
86
- "metadata" : {}
92
+ ]
87
93
},
88
94
{
89
95
"cell_type" : " code" ,
90
96
"execution_count" : null ,
97
+ "id" : " 1ed78a71" ,
98
+ "metadata" : {},
99
+ "outputs" : [],
91
100
"source" : [
92
101
" result = exec_net.infer(inputs={input_key: input_image})[output_key]\n " ,
93
102
" result_index = np.argmax(result)"
94
- ],
95
- "outputs" : [],
96
- "metadata" : {}
103
+ ]
97
104
},
98
105
{
99
106
"cell_type" : " code" ,
100
107
"execution_count" : null ,
108
+ "id" : " bf29578c" ,
109
+ "metadata" : {},
110
+ "outputs" : [],
101
111
"source" : [
102
112
" # Convert the inference result to a class name.\n " ,
103
113
" imagenet_classes = json.loads(open(\" utils/imagenet_class_index.json\" ).read())\n " ,
104
114
" # The model description states that for this model, class 0 is background,\n " ,
105
115
" # 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 " ,
109
117
" imagenet_classes[result_index]"
110
- ],
111
- "outputs" : [],
112
- "metadata" : {}
118
+ ]
113
119
}
114
120
],
115
121
"metadata" : {
133
139
},
134
140
"nbformat" : 4 ,
135
141
"nbformat_minor" : 5
136
- }
142
+ }
0 commit comments