ValueError - 将图像数组提供到字典中

时间:2018-03-21 21:03:22

标签: 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)'

有人可以解释如何解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

添加以下行:

x_batch = x_batch.reshape((-1, 218 * 178 * 3))

应该修复错误。但是,由于您正在构建卷积神经网络,因此您无论如何都需要图像的空间信息。因此,我建议您更改x占位符以改变(None, 218, 178, 3),而不是(None, 116412)。在这种情况下,x_batch转换不是必需的。

答案 1 :(得分:0)

您需要将输入重新整形为(?, 116412)

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)
    x_batch = tf.reshape(x_batch,[-1, 218 * 178 * 3])
    # 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)

    (...)