提升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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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