【问题标题】:tensorflow predict on many images张量流预测许多图像
【发布时间】:2017-04-13 08:17:46
【问题描述】:

我已经使用 retrain image-retraining 示例训练了一个 tensorflow 模型:https://www.tensorflow.org/versions/master/how_tos/image_retraining/index.html

现在我想用它来预测很多图像,我已经修改了这个 python script 以在很多图像上运行:

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 秒)。 如何加快速度?

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    加速张量流的标准方法在这里可能是一个好主意。例如,使用输入队列可以帮助您保持 GPU 忙碌,如 the reading data section of the tensorflow documentation 中所述。此外,为了提高 GPU 利用率,您希望使用比一次预测一张图像更大的批量大小。

    【讨论】:

    • 嗨,Alexandre,您能否提供一个有关如何“使用更大的批次进行预测”的示例?
    猜你喜欢
    • 1970-01-01
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    相关资源
    最近更新 更多