-
Notifications
You must be signed in to change notification settings - Fork 1
/
all.py
executable file
·335 lines (277 loc) · 9.87 KB
/
all.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/python3
# ****************************************************************************
# Copyright(c) 2017 Intel Corporation.
# License: MIT See LICENSE file in root directory.
# ****************************************************************************
# How to classify images using DNNs on Intel Neural Compute Stick (NCS)
# Forked by Tim Spann and added Sense Hat Code and JSON
# 2017-December-28
from sense_hat import SenseHat
import json
import sys, socket
import os
import psutil
import subprocess
import time
import datetime
from time import sleep
from time import gmtime, strftime
import mvnc.mvncapi as mvnc
import skimage
from skimage import io, transform
import numpy
import json
import traceback
import math
import random, string
import base64
import json
import mxnet as mx
import numpy as np
import time
import cv2, os, urllib
from collections import namedtuple
import os
import sys
import numpy
import ntpath
import argparse
import skimage.io
import skimage.transform
start = time.time()
starttime= strftime("%Y-%m-%d %H:%M:%S",gmtime())
# User modifiable input parameters
NCAPPZOO_PATH = os.path.expanduser( '/opt/movidius/ncappzoo' )
GRAPH_PATH = NCAPPZOO_PATH + '/caffe/GoogLeNet/graph'
IMAGE_PATH = sys.argv[1]
LABELS_FILE_PATH = NCAPPZOO_PATH + '/data/ilsvrc12/synset_words.txt'
IMAGE_MEAN = [ 104.00698793, 116.66876762, 122.67891434]
IMAGE_STDDEV = 1
IMAGE_DIM = ( 224, 224 )
NUM_PREDICTIONS = 5
Batch = namedtuple('Batch', ['data'])
# Load the symbols for the networks
with open('/opt/demo/incubator-mxnet/synset.txt', 'r') as f:
synsets = [l.rstrip() for l in f]
# Load the network parameters
sym, arg_params, aux_params = mx.model.load_checkpoint('/opt/demo/incubator-mxnet/Inception-BN', 0)
# Load the network into an MXNet module and bind the corresponding parameters
mod = mx.mod.Module(symbol=sym, context=mx.cpu())
mod.bind(for_training=False, data_shapes=[('data', (1,3,224,224))])
mod.set_params(arg_params, aux_params)
'''
Function to predict objects by giving the model a pointer to an image file and running a forward pass through the model.
inputs:
filename = jpeg file of image to classify objects in
mod = the module object representing the loaded model
synsets = the list of symbols representing the model
N = Optional parameter denoting how many predictions to return (default is top 5)
outputs:
python list of top N predicted objects and corresponding probabilities
'''
def predict(filename, mod, synsets, N=5):
tic = time.time()
img = cv2.cvtColor(cv2.imread(filename), cv2.COLOR_BGR2RGB)
if img is None:
return None
img = cv2.resize(img, (224, 224))
img = np.swapaxes(img, 0, 2)
img = np.swapaxes(img, 1, 2)
img = img[np.newaxis, :]
toc = time.time()
mod.forward(Batch([mx.nd.array(img)]))
prob = mod.get_outputs()[0].asnumpy()
prob = np.squeeze(prob)
topN = []
a = np.argsort(prob)[::-1]
for i in a[0:N]:
topN.append((prob[i], synsets[i]))
return topN
# Code to download an image from the internet and run a prediction on it
def predict_from_url(url, N=5):
filename = url.split("/")[-1]
urllib.urlretrieve(url, filename)
img = cv2.imread(filename)
if img is None:
print( "Failed to download" )
else:
return predict(filename, mod, synsets, N)
# Code to predict on a local file
def predict_from_local_file(filename, N=5):
return predict(filename, mod, synsets, N)
packet_size=3000
# Create unique image name
uniqueid = 'mxnet_uuid_{0}_{1}.json'.format('json',strftime("%Y%m%d%H%M%S",gmtime()))
filename = IMAGE_PATH
topn = []
# Run inception prediction on image
try:
topn = predict_from_local_file(filename, N=5)
except:
print("Error")
errorcondition = "true"
# ---- Step 1: Open the enumerated device and get a handle to it -------------
# Look for enumerated NCS device(s); quit program if none found.
devices = mvnc.enumerate_devices()
if len( devices ) == 0:
print( 'No Movidius devices found' )
quit()
# Get a handle to the first enumerated device and open it
device = mvnc.Device( devices[0] )
device.open()
# ---- Step 2: Load a graph file onto the NCS device -------------------------
# 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 )
fifo_in, fifo_out = graph.allocate_with_fifos( device, blob )
# ---- Step 3: Offload image onto the NCS to run inference -------------------
# Read & resize image [Image size is defined during training]
img = print_img = skimage.io.imread( IMAGE_PATH )
img = skimage.transform.resize( img, IMAGE_DIM, preserve_range=True )
# Convert RGB to BGR [skimage reads image in RGB, but Caffe uses BGR]
img = img[:, :, ::-1]
# Mean subtraction & scaling [A common technique used to center the data]
#img = img.astype( numpy.float32 )
img = ( img - IMAGE_MEAN ) * IMAGE_STDDEV
# Load the image as a half-precision floating point array
#graph.LoadTensor( img.astype( numpy.float16 ), 'user object' )
# ---- Step 4: Read & print inference results from the NCS -------------------
# Get the results from NCS
#output, userobj = graph.GetResult()
# labels = numpy.loadtxt( LABELS_FILE_PATH, str, delimiter = '\t' )
#order = output.argsort()[::-1][:6]
# Load the labels file
labels = [line.rstrip('\n') for line in
open(LABELS_FILE_PATH) if line != 'classes\n']
# Load the image as an array
graph.queue_inference_with_fifo_elem(fifo_in, fifo_out, img.astype(numpy.float32), None)
# Get the results from NCS
output, userobj = fifo_out.read_elem()
# Sort the indices of top predictions
order = output.argsort()[::-1][:NUM_PREDICTIONS]
# Get execution time
inference_time = graph.get_option(mvnc.GraphOption.RO_TIME_TAKEN)
#### Initialization
external_IP_and_port = ('198.41.0.4', 53) # a.root-servers.net
socket_family = socket.AF_INET
host = os.uname()[1]
def getCPUtemperature():
res = os.popen('vcgencmd measure_temp').readline()
return(res.replace("temp=","").replace("'C\n",""))
def IP_address():
try:
s = socket.socket(socket_family, socket.SOCK_DGRAM)
s.connect(external_IP_and_port)
answer = s.getsockname()
s.close()
return answer[0] if answer else None
except socket.error:
return None
cpuTemp=int(float(getCPUtemperature()))
ipaddress = IP_address()
host = os.uname()[1]
rasp = ('armv' in os.uname()[4])
cpu = psutil.cpu_percent(interval=1)
if rasp:
f = open('/sys/class/thermal/thermal_zone0/temp', 'r')
l = f.readline()
ctemp = 1.0 * float(l)/1000
usage = psutil.disk_usage("/")
mem = psutil.virtual_memory()
diskrootfree = "{:.1f} MB".format(float(usage.free) / 1024 / 1024)
mempercent = mem.percent
external_IP_and_port = ('198.41.0.4', 53) # a.root-servers.net
socket_family = socket.AF_INET
# IP Address
ipaddress = IP_address()
# Sense Hat Sensors
sense = SenseHat()
sense.clear()
temp = sense.get_temperature()
temp = round(temp, 2)
humidity = sense.get_humidity()
humidity = round(humidity, 1)
pressure = sense.get_pressure()
pressure = round(pressure, 1)
orientation = sense.get_orientation()
pitch = orientation['pitch']
roll = orientation['roll']
yaw = orientation['yaw']
acceleration = sense.get_accelerometer_raw()
x = acceleration['x']
y = acceleration['y']
z = acceleration['z']
x=round(x, 0)
y=round(y, 0)
z=round(z, 0)
pitch=round(pitch,0)
roll=round(roll,0)
yaw=round(yaw,0)
try:
# Top 5 MXNET Analysis
top1 = str(topn[0][1])
top1pct = str(round(topn[0][0],3) * 100)
top2 = str(topn[1][1])
top2pct = str(round(topn[1][0],3) * 100)
top3 = str(topn[2][1])
top3pct = str(round(topn[2][0],3) * 100)
top4 = str(topn[3][1])
top4pct = str(round(topn[3][0],3) * 100)
top5 = str(topn[4][1])
top5pct = str(round(topn[4][0],3) * 100)
end = time.time()
currenttime= strftime("%Y-%m-%d %H:%M:%S",gmtime())
# old runtime str(round(end - start))
# json row
row = { 'uuid': uniqueid,
'top1pct': '{:3.1f}%'.format( float(top1pct) ),
'top1': top1,
'top2pct': '{:3.1f}%'.format( float(top2pct) ),
'top2': top2,
'top3pct': '{:3.1f}%'.format( float(top3pct) ),
'top3': top3,
'top4pct': '{:3.1f}%'.format( float(top4pct) ),
'top4': top4,
'top5pct': '{:3.1f}%'.format( float(top5pct) ),
'top5': top5,
'imagefilename': filename,
'runtime': str(round(end - start)),
'cafferuntime': str(numpy.sum(inference_time)) + "ms",
'cputemp2': round(ctemp,2),
'temp': temp,
'tempf': round(((temp * 1.8) + 12),2),
'humidity': humidity,
'pressure': pressure,
'pitch': pitch,
'roll': roll,
'yaw': yaw, 'x': x, 'y': y, 'z': z,
'memory': mempercent,
'diskfree': diskrootfree,
'label1': labels[order[0]],
'label2': labels[order[1]],
'label3': labels[order[2]],
'label4': labels[order[3]],
'label5': labels[order[4]],
'value1': '{:3.1f}%'.format( float(100.0 * output[order[0]])),
'value2': '{:3.1f}%'.format( float(100.0 * output[order[1]])),
'value3': '{:3.1f}%'.format( float(100.0 * output[order[2]])),
'value4': '{:3.1f}%'.format( float(100.0 * output[order[3]])),
'value5': '{:3.1f}%'.format( float(100.0 * output[order[4]])),
'currenttime': currenttime,
'host': host,
'cputemp': round(cpuTemp,2),
'ipaddress': ipaddress,
'starttime': starttime }
json_string = json.dumps(row)
print (json_string)
except:
print("{\"message\": \"Failed to run\"}")
# ---- Step 5: Unload the graph and close the device -------------------------
fifo_in.destroy()
fifo_out.destroy()
graph.destroy()
device.close()
device.destroy()
# ==== End of file ===========================================================