【问题标题】:ValueError - Feeding an array of images into a dictionaryValueError - 将图像数组输入字典
【发布时间】:2018-03-21 21:03:22
【问题描述】:

我正在调整 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)'

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

【问题讨论】:

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


    【解决方案1】:

    添加以下行:

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

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

    【讨论】:

      【解决方案2】:

      您需要将输入重塑为(?, 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)
      
          (...)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-09
        • 1970-01-01
        • 1970-01-01
        • 2021-10-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多