-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_inference.py
executable file
·117 lines (90 loc) · 3.66 KB
/
run_inference.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import numpy as np
import cv2
import matplotlib.pyplot as plt
import os
import sys
import argparse
import mvnc.mvncapi as mvnc
AGE_GRAPH = './graph/AgeNet.graph'
GENDER_GRAPH = './graph/GenderNet.graph'
INPUT_DIMENSION = (227, 227)
AGE_LIST = ['0-9', '10-19', '20-29', '30-39', '40-49', '50-59', '60-69', '70-79', '80-120']
GENDER_LIST = ['Male', 'Female']
def human_distance(enc1, enc2):
return np.sqrt(np.sum(np.square(normalize(enc1) - normalize(enc2))))
def open_ncs_device():
# Look for enumerated NCS device(s); quit program if none found.
devices = mvnc.enumerate_devices()
if len( devices ) == 0:
print( "No devices found" )
quit()
# Get a handle to the first enumerated device and open it
device = mvnc.Device( devices[0] )
device.open()
return device
def load_graph(device, graph_path):
# Read the graph file into a buffer
with open( graph_path, mode='rb' ) as f:
blob = f.read()
# Load the graph buffer into the NCS
graph = mvnc.Graph(graph_path)
# Set up fifos
fifo_in, fifo_out = graph.allocate_with_fifos(device, blob)
return graph, fifo_in, fifo_out
def pre_process_image(image_path, mean_file):
ilsvrc_mean = np.load(mean_file).mean(1).mean(1) #loading the mean file
img = cv2.imread(image_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img,INPUT_DIMENSION)
img = img.astype(np.float32)
img[:,:,0] = (img[:,:,0] - ilsvrc_mean[0])
img[:,:,1] = (img[:,:,1] - ilsvrc_mean[1])
img[:,:,2] = (img[:,:,2] - ilsvrc_mean[2])
return img
def infer_image(graph, img, fifo_in, fifo_out):
# The first inference takes an additional ~20ms due to memory
# initializations, so we make a 'dummy forward pass'.
graph.queue_inference_with_fifo_elem(fifo_in, fifo_out, img.astype(np.float32), None)
output, userobj = fifo_out.read_elem()
# Load the image as an array
graph.queue_inference_with_fifo_elem(fifo_in, fifo_out, img.astype(np.float32), None)
# Get the results from NCS
output, userobj = fifo_out.read_elem()
# Get execution time
inference_time = graph.get_option(mvnc.GraphOption.RO_TIME_TAKEN)
# Print the results
print("Execution time: " + str(np.sum( inference_time )) + "ms")
output = np.expand_dims(output,axis=0)
return output
def clean_up(device, graph, fifo_in, fifo_out):
fifo_in.destroy()
fifo_out.destroy()
graph.destroy()
device.close()
device.destroy()
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--image_path", help="path to image",
required=True)
parser.add_argument("--mean_file", required=True)
args = parser.parse_args()
img_path = args.image_path
mean_file = args.mean_file
img = pre_process_image(img_path, mean_file)
device = open_ncs_device()
age_graph, age_fifo_in, age_fifo_out = load_graph(device, AGE_GRAPH)
gender_graph, gender_fifo_in, gender_fifo_out = load_graph(device, GENDER_GRAPH)
age_out = infer_image(age_graph, img, age_fifo_in, age_fifo_out)[0]
gender_out = infer_image(gender_graph, img, gender_fifo_in, gender_fifo_out)[0]
age_pred = AGE_LIST[age_out.argsort()[-1]]
age_prob = age_out[age_out.argsort()[-1]]
print("Age predicted: %s, %.2f%%" % (age_pred, age_prob*100))
gender_pred = GENDER_LIST[gender_out.argsort()[-1]]
gender_prob = gender_out[gender_out.argsort()[-1]]
print("Gender predicted: %s, %.2f%%" % (gender_pred, gender_prob*100))
gender_fifo_in.destroy()
gender_fifo_out.destroy()
gender_graph.destroy()
clean_up(device, age_graph, age_fifo_in, age_fifo_out)
if __name__ == "__main__":
sys.exit(main())