Reputation: 49
I have trained a tensorflow model using the retrain image-retraining example: https://www.tensorflow.org/versions/master/how_tos/image_retraining/index.html
Now I want to use it to predict on many images, I've modified this python script to run on many images:
import numpy as np
import tensorflow as tf
import glob
import os
modelFullPath = 'output_graph.pb'
def create_graph():
"""Creates a graph from saved GraphDef file and returns a saver."""
# Creates graph from saved graph_def.pb.
with tf.gfile.FastGFile(modelFullPath, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
if __name__ == '__main__':
imagePath = 'MYFOLDERWITHIMAGES/*.jpg'
testimages=glob.glob(imagePath)
## init numpy array to hold all predictions
all_predictions = np.zeros(shape=(len(testimages),121)) ## 121 categories
# Creates graph from saved GraphDef.
create_graph()
with tf.Session() as sess:
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
for i in range(len(testimages)):
image_data1 = tf.gfile.FastGFile(testimages[i], 'rb').read()
predictions = sess.run(softmax_tensor,
{'DecodeJpeg/contents:0': image_data1})
all_predictions[i,:] = np.squeeze(predictions)
if i % 100 == 0:
print(str(i) +' of a total of '+ str(len(testimages)))
but even running on my gpu it is rather slow (aprox. 25 sec per 500 images). How can I speed it up?
Upvotes: 0
Views: 2451
Reputation: 5206
The standard ways of speeding up tensorflow are probably a good idea here. For example, using input queues can help you keep your GPU busy, as described in the reading data section of the tensorflow documentation. Also to improve GPU utilization you want to use a larger batch size than predicting one image at a time.
Upvotes: 0