python - ValueError - 将图像数组输入字典

标签 python dictionary tensorflow machine-learning conv-neural-network

我正在适应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/

相关文章:

python - 是否使用 numpy 的 dot 或 matmul 函数

python - 如何从 adodbapi 查询返回的行中获取列名?

python - 为 'group number' 和 '0' s 的 pandas 数据框列创建 '1' 列

python - 将代码转换为语法突出显示的 html

python - 通过Python将csv文件转换为numpy数组

java - Hashmap获取Value时的内部流程

python 3.4 : Pandas DataFrame not responding to ordered dictionary

python - model.fit() Keras 分类多输入-单输出给出错误 : AttributeError: 'NoneType' object has no attribute 'fit'

python - 如何在 Keras 中将张量的 2D 子集分配给另一个 2D 张量?

python-3.x - Keras "fit"输入不清楚