Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 张量流在许多图像上的预测_Python_Tensorflow - Fatal编程技术网

Python 张量流在许多图像上的预测

Python 张量流在许多图像上的预测,python,tensorflow,Python,Tensorflow,我已经使用Retain image Retaining示例训练了tensorflow模型: 现在我想用它来预测许多图像,我已经修改了这个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 f

我已经使用Retain image Retaining示例训练了tensorflow模型:

现在我想用它来预测许多图像,我已经修改了这个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秒)。
如何加速?

加速tensorflow的标准方法可能是一个好主意。例如,使用输入队列可以帮助您保持GPU繁忙,如中所述。此外,为了提高GPU利用率,您希望使用比一次预测一个图像更大的批处理大小。

加速tensorflow的标准方法可能是一个好主意。例如,使用输入队列可以帮助您保持GPU繁忙,如中所述。此外,为了提高GPU利用率,您希望使用比一次预测一个图像更大的批处理大小。

嗨,Alexandre,您能提供一个关于如何“使用更大的批处理进行预测”的示例吗?嗨,Alexandre,您能提供一个关于如何“使用更大的批处理进行预测”的示例吗