[英]tensorflow predict on many images
我使用重新培训图像再培训示例训练了张量流模型: https ://www.tensorflow.org/versions/master/how_tos/image_retraining/index.html
现在我想用它来预测许多图像,我已修改此python 脚本以在许多图像上运行:
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)))
但即使在我的gpu上运行也相当慢(每500张图像约25秒)。 我怎样才能加快速度?
加速张量流的标准方法可能是一个好主意。 例如,使用输入队列可以帮助您保持GPU忙碌,如tensorflow文档的读取数据部分所述 。 另外,为了提高GPU利用率,您希望使用比一次预测一个图像更大的批量大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.