我正在适应this tutorial here这样我就可以在自己的图像集中训练 ConvNet。
所以我创建了这个函数来尝试获取批处理,尽管它还没有创建批处理:
def training_batch(batch_size):
images = trainpaths
for i in range(len(images)):
# converting the path to an image
image = mpimg.imread(images[i])
images[i] = image
# Create batches
X, Y = images, trainlabels
return X, Y
这个函数在这里被调用:
def optimize(num_iterations):
global total_iterations
for i in range(total_iterations,
total_iterations + num_iterations):
# Get a batch of training examples.
# x_batch now holds a batch of images and
# y_true_batch are the true labels for those images.
x_batch, y_true_batch = training_batch(train_batch_size)
# Put the batch into a dict with the proper names
# for placeholder variables in the TensorFlow graph.
feed_dict_train = {x: x_batch,
y_true: y_true_batch}
# Run the optimizer using this batch of training data.
# TensorFlow assigns the variables in feed_dict_train
# to the placeholder variables and then runs the optimizer.
session.run(optimizer, feed_dict=feed_dict_train)
(...)
事情是,如果我运行这个代码,我会得到
Traceback (most recent call last):
File "scr.py", line 405, in <module>
optimize(1)
File "scr.py", line 379, in optimize session.run(optimizer, feed_dict=feed_dict_train)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 905, in run run_metadata_ptr)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1116, in _run str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (2034, 218, 178, 3) for Tensor u'x:0', which has shape '(?, 116412)'
有人可以阐明如何解决这个问题吗?
最佳答案
添加以下行:
x_batch = x_batch.reshape((-1, 218 * 178 * 3))
应该修复该错误。但是,由于您正在构建卷积神经网络,因此无论如何您都需要图像的空间信息。因此,我建议您将 x
占位符更改为形状 (None, 218, 178, 3)
,而不是 (None, 116412)
反而。在这种情况下,x_batch
转换不是必需的。
关于python - ValueError - 将图像数组输入字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49416470/