根据链接中的说明,我已经对新的类别重新训练了Tensorflow的Inception模型。
但我注意到,如果随后我想对一组图像进行分类,它会逐一处理这些图像然后进行分类。如果数据集很大,完成分类需要很长时间。例如,处理1000张图像需要45分钟。
对于图像分类,我使用了在线上可用的LabelImage.py脚本,如下所示:
import tensorflow as tfimport sysimage_path = sys.argv[1] #Pass the test file as argument# Read in the image_dataimage_data = tf.gfile.FastGFile(image_path, 'rb').read()# Loads label file (the retained labels from retraining) and strips off carriage returnlabel_lines = [line.rstrip() for line in tf.gfile.GFile("/tf_files/tf_files/retrained_labels.txt")]# Unpersists graph from filewith tf.gfile.FastGFile("/tf_files/tf_files/retrained_graph.pb", 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) _ = tf.import_graph_def(graph_def, name='')with tf.Session() as sess: # Feed the image_data as input to the graph and get first prediction i.e. the most likely result softmax_tensor = sess.graph.get_tensor_by_name('final_result:0') predictions = sess.run(softmax_tensor, \ {'DecodeJpeg/contents:0': image_data}) # Sort to show labels of first prediction in order of confidence top_k = predictions[0].argsort()[-len(predictions[0]):][::-1] for node_id in top_k: human_string = label_lines[node_id] score = predictions[0][node_id] print('%s (score = %.5f)' % (human_string, score))
如您所见,它是一张一张地处理图像。
有没有可能加快这个过程?由于我在重新训练库时没有为多GPU进行编译。还有其他加速分类过程的方法吗?
回答:
为了完善@aandroidtest给出的回答,如果您使用上述脚本,您会花费大部分时间在每次处理每张图像时重新加载模型等操作上。
相反,您应该只加载一次模型,然后在脚本中逐一处理图像。