【发布时间】: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