-
Notifications
You must be signed in to change notification settings - Fork 0
/
ov.py
executable file
·82 lines (59 loc) · 2.45 KB
/
ov.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
# Copyright 2004-2021 Primate Labs Inc. All rights reserved.
import tensorflow as tf
from tensorflow.keras.applications.imagenet_utils import preprocess_input
import numpy as np
import matplotlib.pyplot as plt
from openvino.inference_engine import IECore, IENetwork
import PIL
from PIL import Image
import os
# Normalize image values from 0~255 to -1~1 for float32 and float16
def normalize(img):
img = np.expand_dims(img / 255, 0)
img = np.moveaxis(img, -1, 1)
return img
def image_classification():
h5_model = tf.keras.models.load_model('./image_classification/mobilenet_v1_f32.h5')
os.system("mkdir temp")
tf.saved_model.save(h5_model, "./temp")
os.system("python ./model_optimizer/mo_tf.py \
--input_shape='[1,224,224,3]' \
--saved_model_dir ./temp \
--output_dir ./image_classification")
os.system("rm -rf ./temp")
src = Image.open("ic-001.jpeg")
src = src.resize((224, 224))
src = normalize(np.array(src))
ie = IECore()
network = ie.read_network(model = "image_classification/saved_model.xml", weights = "image_classification/saved_model.bin")
exec_net = ie.load_network(network=network, device_name='CPU')
input_blob = next(iter(network.input_info.keys()))
output_blob = next(iter(network.outputs))
result = exec_net.infer(inputs={input_blob: src})
score = result['StatefulPartitionedCall/mobilenet_1.00_224/predictions/Softmax'][0]
klass = score.tolist().index(max(score))
print(klass)
def image_segmentation():
h5_model = tf.keras.models.load_model('image_segmentation/deeplabv3_mobilenetv2_f32.h5',
custom_objects={ 'tf': tf, 'relu6': tf.nn.relu6 })
os.system("mkdir temp")
tf.saved_model.save(h5_model, "./temp")
os.system("python ./model_optimizer/mo_tf.py \
--saved_model_dir ./temp \
--output_dir ./image_segmentation")
os.system("rm -rf ./temp")
src = Image.open("is-001.jpeg")
src = src.resize((384, 384))
src = normalize(np.array(src))
ie = IECore()
network = ie.read_network(model = "image_segmentation/saved_model.xml", weights = "image_segmentation/saved_model.bin")
exec_net = ie.load_network(network=network, device_name='CPU')
input_blob = next(iter(network.input_info.keys()))
output_blob = next(iter(network.outputs))
result = exec_net.infer(inputs={input_blob: src})
def main():
# image_classification()
image_segmentation()
if __name__ == '__main__':
main()