python - 大量图像上的Tensorflow预测很慢

标签 python opencv tensorflow computer-vision deep-learning

我正在按照本教程在Tensorflow中进行图像分类:http://cv-tricks.com/tensorflow-tutorial/training-convolutional-neural-network-for-image-classification/。对单个图像的训练和测试效果很好。但是,我对大量图像进行预测的代码非常慢,它消耗了100%的CPU,并且几乎消耗了最大内存!对于2700张图像,需要24多个小时!这是不实际的。有没有一种方法可以像进行批培训一样进行批测试?请注意,我还需要对图像进行标准化。这是我的代码:

import tensorflow as tf
import numpy as np
import os,glob,cv2
import sys,argparse


# First, pass the path of the image
os.chdir("/somepath")

i = 0
files = glob.glob('*.jpg')
files.extend(glob.glob('*.JPG'))
totalNumber = len(files)
print("total number of images is:", totalNumber)

image_size=128
num_channels=3
text_file = open("Results.txt", "w")

for file in files:
    images = []
    filename = file
    print(filename)
    text_file.write("\n")
    text_file.write(filename)
    # Reading the image using OpenCV
    image = cv2.imread(filename)
    # Resizing the image to our desired size and preprocessing will be done exactly as done during training
    image = cv2.resize(image, (image_size, image_size),0,0, cv2.INTER_LINEAR)
    images.append(image)
    images = np.array(images, dtype=np.uint8)
    images = images.astype('float32')
    images = np.multiply(images, 1.0/255.0) 
    #The input to the network is of shape [None image_size image_size num_channels]. Hence we reshape.
    x_batch = images.reshape(1, image_size,image_size,num_channels)

    ## Let us restore the saved model 
    sess = tf.Session()
    # Step-1: Recreate the network graph. At this step only graph is created.
    saver = tf.train.import_meta_graph('pathtomymeta/my_model-9909.meta')
    # Step-2: Now let's load the weights saved using the restore method.
    saver.restore(sess, tf.train.latest_checkpoint('pathtomycheckpoints/checkpoints/'))

    # Accessing the default graph which we have restored
    graph = tf.get_default_graph()

    # Now, let's get hold of the op that we can be processed to get the output.
    # In the original network y_pred is the tensor that is the prediction of the network
    y_pred = graph.get_tensor_by_name("y_pred:0")

    ## Let's feed the images to the input placeholders
    x = graph.get_tensor_by_name("x:0") 
    y_true = graph.get_tensor_by_name("y_true:0") 
    y_test_images = np.zeros((1, 3)) #np.zeros((1, 2)) 


    ### Creating the feed_dict that is required to be fed to calculate y_pred 
    feed_dict_testing = {x: x_batch, y_true: y_test_images}
    result = sess.run(y_pred, feed_dict=feed_dict_testing)
    # result is of this format [probabiliy_of_rose probability_of_sunflower]
    print(result)
    text_file.write("\n")
    text_file.write('%s' % result[i,0])
    text_file.write("\t")
    text_file.write('%s' % result[i,1])
    text_file.write("\t")
    text_file.write('%s' % result[i,2])

text_file.close()

最佳答案

我认为您应该在代码中考虑非常明显的“优化”。您正在执行for循环,并且在每次迭代中,您都在加载图像,还加载模型,构建图形,然后进行预测。

但是,加载模型和构建图实际上并不依赖于for循环或其中的任何变量(例如输入图像)。 for循环中的大多数时间都可能花费在加载模型上,而不进行实际的预测。您可以使用探查器进行查找。

因此,我建议您只需在for循环之前加载模型并构建一次图形,然后在for循环内使用以下两行代码:

feed_dict_testing = {x: x_batch, y_true: y_test_images}
result = sess.run(y_pred, feed_dict=feed_dict_testing)

它应该快得多。它可能仍然很慢,但是那是因为在CPU上评估大型神经网络本身很慢。

关于python - 大量图像上的Tensorflow预测很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48211346/

相关文章:

tensorflow - 用于 keras/tensorflow 中语义图像分割的多类加权损失

python - 如何在 TensorFlow 中导入模型

python - OpenAI API 错误 500 : "The server had an error while processing your request", 503: "Service Unavailable"或 504: "Gateway Timeout"

python - 用python模拟CLI Shell

python - 搜索数百万个 JSON 文件的最佳方法是什么?

c++ - 与 Windows 窗体应用程序一起使用时出现 opencv 错误

c++ - OpenCV:带有 CSV 文件的特征脸

file - OpenCV 3.4.3 - VideoWriter 时不调用 GStreamer

python - 安装tensorflow但用pip替换pip3,因为找不到pip3。错误?

python - session 未创建异常 : Message: session not created: This version of ChromeDriver only supports Chrome version 74 though Chrome v74 is installed