提升Tensorflow图像分类的速度

根据链接中的说明,我已经对新的类别重新训练了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给出的回答,如果您使用上述脚本,您会花费大部分时间在每次处理每张图像时重新加载模型等操作上。

相反,您应该只加载一次模型,然后在脚本中逐一处理图像。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注