69
69
"""Absolute path to image file.""" )
70
70
tf .app .flags .DEFINE_integer ('num_top_predictions' , 5 ,
71
71
"""Display this many predictions.""" )
72
+ # MODIFICATION BY SAM ABRAHAMS
73
+ tf .app .flags .DEFINE_integer ('warmup_runs' , 10 ,
74
+ "Number of times to run Session before starting test" )
75
+ tf .app .flags .DEFINE_integer ('num_runs' , 25 ,
76
+ "Number of sample runs to collect benchmark statistics" )
77
+ # END OF MODIFICATION
72
78
73
79
# pylint: disable=line-too-long
74
80
DATA_URL = 'http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz'
@@ -159,21 +165,14 @@ def run_inference_on_image(image):
159
165
Returns:
160
166
Nothing
161
167
"""
162
-
163
- # MODIFICATION BY SAM ABRAHAMS
164
- start_time = time .time ()
165
- # END OF MODIFICATION
166
-
167
168
if not tf .gfile .Exists (image ):
168
169
tf .logging .fatal ('File does not exist %s' , image )
169
170
image_data = tf .gfile .FastGFile (image , 'rb' ).read ()
170
171
171
172
# Creates graph from saved GraphDef.
173
+ start_time = time .time ()
172
174
create_graph ()
173
-
174
- # MODIFICATION BY SAM ABRAHAMS
175
- mid_time = time .time ()
176
- # END OF MODIFICATION
175
+ graph_time = time .time () - start_time
177
176
178
177
with tf .Session () as sess :
179
178
# Some useful tensors:
@@ -185,27 +184,21 @@ def run_inference_on_image(image):
185
184
# encoding of the image.
186
185
# Runs the softmax tensor by feeding the image_data as input to the graph.
187
186
softmax_tensor = sess .graph .get_tensor_by_name ('softmax:0' )
188
- predictions = sess .run (softmax_tensor ,
189
- {'DecodeJpeg/contents:0' : image_data })
190
- predictions = np .squeeze (predictions )
191
-
192
- # Creates node ID --> English string lookup.
193
- node_lookup = NodeLookup ()
194
-
195
- top_k = predictions .argsort ()[- FLAGS .num_top_predictions :][::- 1 ]
196
-
197
- for node_id in top_k :
198
- human_string = node_lookup .id_to_string (node_id )
199
- score = predictions [node_id ]
200
- print ('%s (score = %.5f)' % (human_string , score ))
201
-
202
- # MODIFICATION BY SAM ABRAHAMS
203
- end_time = time .time ()
204
- graph_time = mid_time - start_time
205
- eval_time = end_time - mid_time
206
- print ('Build graph time: %f' % graph_time )
207
- print ('Eval time: %f' % eval_time )
208
- # END OF MODIFICATION
187
+ # MODIFICATION BY SAM ABRAHAMS
188
+ for i in range (FLAGS .warmup_runs ):
189
+ predictions = sess .run (softmax_tensor ,
190
+ {'DecodeJpeg/contents:0' : image_data })
191
+ runs = []
192
+ for i in range (FLAGS .num_runs ):
193
+ start_time = time .time ()
194
+ predictions = sess .run (softmax_tensor ,
195
+ {'DecodeJpeg/contents:0' : image_data })
196
+ runs .append (time .time () - start_time )
197
+ for i , run in enumerate (runs ):
198
+ print ('Run %03d:\t %0.4f seconds' % (i , run ))
199
+ print ('Average run: %0.4f' % float (sum (runs ) / len (runs )))
200
+ print ('Build graph time: %0.4f' % graph_time )
201
+ # END OF MODIFICATION
209
202
210
203
def maybe_download_and_extract ():
211
204
"""Download and extract model tar file."""
0 commit comments