I am trying to create an image classification program on a Raspberry Pi 3 using TensorFlow and a modified version of label_image.py.
I'm using a MobileNet model which I obtained from here. At the beginning, classifications take around 3 seconds, but this increases over time (up to over 7 seconds after 10 minutes), and I can't figure out why this is happening.
Here is the code within my loop:
while True:
startTime = datetime.now()
t = read_tensor_from_image_file(file_name,
input_height=input_height,
input_width=input_width,
input_mean=input_mean,
input_std=input_std)
input_name = "import/" + input_layer
output_name = "import/" + output_layer
input_operation = graph.get_operation_by_name(input_name);
output_operation = graph.get_operation_by_name(output_name);
results = sess.run(output_operation.outputs[0],
{input_operation.outputs[0]: t})
results = np.squeeze(results)
top_k = results.argsort()[-5:][::-1]
labels = load_labels(label_file)
for i in top_k:
print(labels[i], results[i])
print(datetime.now() - startTime)
The TensorFlow session is started before the loop along with the loading of the graph.
I'm using Python 3.4.2 and TensorFlow 1.3.0.
I found another question on StackOverflow with the same issue. I tried the solution posted there but I get errors stating "AttributeError: 'Tensor' object has no attribute 'endswith'".
I found a solution here which works for me. By adding a with tf.Graph().as_default(): around the body of the read_tensor_from_image_file() function, my classifications now take around 1.20s even after 30 mins.
so my read_tensor_from_image_file() function looks like this:
def read_tensor_from_image_file(file_name, input_height=192, input_width=192,
input_mean=0, input_std=255):
with tf.Graph().as_default():
input_name = "file_reader"
output_name = "normalized"
file_reader = tf.read_file(file_name, input_name)
if file_name.endswith(".png"):
image_reader = tf.image.decode_png(file_reader, channels = 3,
name='png_reader')
elif file_name.endswith(".gif"):
image_reader = tf.squeeze(tf.image.decode_gif(file_reader,
name='gif_reader'))
elif file_name.endswith(".bmp"):
image_reader = tf.image.decode_bmp(file_reader, name='bmp_reader')
else:
image_reader = tf.image.decode_jpeg(file_reader, channels = 3,
name='jpeg_reader')
float_caster = tf.cast(image_reader, tf.float32)
dims_expander = tf.expand_dims(float_caster, 0);
resized = tf.image.resize_bilinear(dims_expander, [input_height,
input_width])
normalized = tf.divide(tf.subtract(resized, [input_mean]),[input_std])
result = sess.run(normalized)
return result
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With