-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtflite_run.py
executable file
·73 lines (52 loc) · 1.73 KB
/
tflite_run.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import time
import argparse
import numpy as np
from PIL import Image
import tensorflow as tf
def predict(model_path ,image_path):
interpreter = tf.lite.Interpreter(
model_path=model_path)
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# check the type of the input tensor
floating_model = input_details[0]['dtype'] == np.float32
# NxHxWxC, H:1, W:2
height = input_details[0]['shape'][1]
width = input_details[0]['shape'][2]
img = Image.open(image_path).resize((width, height))
# add N dim
input_data = np.expand_dims(img, axis=0)
if floating_model:
input_data = (np.float32(input_data))
interpreter.set_tensor(input_details[0]['index'], input_data)
start_time = time.time()
interpreter.invoke()
stop_time = time.time()
output_data = interpreter.get_tensor(output_details[0]['index'])
results = np.squeeze(output_data)
top_k = results.argsort()[-5:][::-1]
labels = ['close', 'open']
for i in top_k:
if floating_model:
print('{:08.6f}: {}'.format(float(results[i]), labels[i]))
else:
print('{:08.6f}: {}'.format(float(results[i] / 255.0), labels[i]))
print('Predicted label is: {}'.format(labels[top_k[0]]))
print('time: {:.3f}ms'.format((stop_time - start_time) * 1000))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'-i',
'--image',
default='./test.jpg',
help='image to be classified')
parser.add_argument(
'-m',
'--model_file',
default='./model.tflite',
help='.tflite model to be executed')
args = parser.parse_args()
predict(args.model_file, args.image)