-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_tf_model.py
38 lines (30 loc) · 1.15 KB
/
time_tf_model.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
import tensorflow as tf
import os
import time
from PIL import Image
import numpy as np
model_path = "./model/ssd_mobilenet_v2_coco_2018_03_29/model.ckpt"
image = Image.open('./writeup-images/test-image.jpg')
image_np_expanded = np.expand_dims(image, axis=0)
detection_graph = tf.Graph()
with tf.compat.v1.Session(graph=detection_graph) as sess:
# Load the graph with the trained states
loader = tf.compat.v1.train.import_meta_graph(model_path+'.meta')
loader.restore(sess, model_path)
# Get the tensors by their variable name
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
scores = detection_graph.get_tensor_by_name('detection_scores:0')
start = time.time()
for ind in range(100):
if (ind%10==0):
print(ind)
# Make predictions
_boxes, _scores = sess.run([boxes, scores], feed_dict={image_tensor: image_np_expanded})
end = time.time()
elapsed = end-start
print('=== Output of the inference')
print(_boxes)
print(_scores)
print('=== Average time for single inference')
print(f' {elapsed/100 * 1000} ms')