【问题标题】:TensorFlow: Classify multiple images from a folderTensorFlow:对文件夹中的多个图像进行分类
【发布时间】:2019-02-05 22:01:42
【问题描述】:

我之前编写的代码在单个图像上运行良好。但现在我希望程序运行多个图像。我只需要将文件夹名称作为参数。

我修改了我的代码,它打开了一个目录并保存了图像,但我得到了一个错误

ValueError: 无法将大小为 98304 的数组重新整形为形状 (1,128,128,3)

x_batch=images.reshape(1,128,128,3):

images = []

for filename in os.listdir(folder):
    image = cv2.imread(os.path.join(folder,filename))
    image = cv2.resize(image, (128, 128))
    images = np.append(images, image)
    images = np.array(images, dtype=np.uint8)
    images = images.astype('float32')
    images = np.multiply(images, 1.0/255.0)
    x_batch=images.reshape(1,128,128,3)        <------ ERROR HERE

    sess = tf.Session()
    saver = tf.train.import_meta_graph('/home/kubuntu/SimpleCode/.meta')
    saver.restore(sess, tf.train.latest_checkpoint('./'))
    graph = tf.get_default_graph()
    y_pred = graph.get_tensor_by_name("y_pred:0")    
    x= graph.get_tensor_by_name("x:0")
    y_true = graph.get_tensor_by_name("y_true:0")
    y_test_images = np.zeros((1, 6))
    feed_dict_testing= {x:x_batch, y_true:y_test_images}
    result=sess.run(y_pred, feed_dict=feed_dict_testing)
    print("Up  :"+str(result[0][0]))
    print("Down :"+str(result[0][1]))
    print("Left  :"+str(result[0][2]))
    print("Right  :"+str(result[0][3]))
    print("Forward  :"+str(result[0][4]))
    print("Backward  :"+str(result[0][5]))

这是从文件夹中读取图像的正确方法吗?如何对给定文件夹中的所有图像进行分类并给出每个图像的预测?

【问题讨论】:

  • 你为什么要做重塑?它的目的是什么?
  • @gorjan 网络的输入形状为 [128 128]。
  • 您想一次读取所有图像,然后对它们执行推理,或者您希望在从文件夹中读取图像时对每个图像执行推理?
  • @gorjan 从文件夹中读取图像以对每个图像执行推理。
  • 我发布了一个完整的答案。我想你现在应该可以走了。

标签: python tensorflow


【解决方案1】:

根据您的回答,您应该执行以下操作:

for filename in os.listdir(folder):
    image = cv2.imread(os.path.join(folder,filename))
    image = cv2.resize(image, (128, 128))
    image = np.array(image, dtype=np.uint8)
    image = image.astype('float32')
    image = np.multiply(image, 1.0/255.0)
    x_batch=image.reshape(1,128,128,3)

当您读取第二张图片时代码失败,因为images 数组附加了两张图片,而您试图将其重塑为只有一张图片。

此外,在 for 循环中迭代创建 tf.Session 并一直加载图表是一种非常糟糕的做法。我将通过以下方式更改整个代码:

with tf.Session() as sess:
    saver = tf.train.import_meta_graph('/home/kubuntu/SimpleCode/.meta')
    saver.restore(sess, tf.train.latest_checkpoint('./'))
    graph = tf.get_default_graph()
    y_pred = graph.get_tensor_by_name("y_pred:0")    
    x = graph.get_tensor_by_name("x:0")
    y_true = graph.get_tensor_by_name("y_true:0")
    y_test_images = np.zeros((1, 6))

    for filename in os.listdir(folder):
        image = cv2.imread(os.path.join(folder,filename))
        image = cv2.resize(image, (128, 128))
        image = np.array(image, dtype=np.uint8)
        image = image.astype('float32')
        image = np.multiply(image, 1.0/255.0)
        x_batch=image.reshape(1,128,128,3)
        feed_dict_testing= {x:x_batch, y_true:y_test_images}
        result = sess.run(y_pred, feed_dict=feed_dict_testing)
        print("Up  :"+str(result[0][0]))
        print("Down :"+str(result[0][1]))
        print("Left  :"+str(result[0][2]))
        print("Right  :"+str(result[0][3]))
        print("Forward  :"+str(result[0][4]))
        print("Backward  :"+str(result[0][5]))

【讨论】:

  • 谢谢!现在工作得很好。
  • 太棒了!以后对我的第二点要格外小心。即使它有效,您的代码也会非常缓慢。
猜你喜欢
  • 2020-02-09
  • 2016-10-16
  • 2017-12-15
  • 2018-07-23
  • 2019-10-06
  • 1970-01-01
  • 2016-04-19
  • 2018-03-07
  • 2017-07-18
相关资源
最近更新 更多