【问题标题】:Tensorflow value error: Cannot feed value of shape (96, 50, 50) for Tensor u'InputData/X:0', which has shape '(?, 50, 50, 1)'Tensorflow 值错误:无法为具有形状 '(?, 50, 50, 1)' 的张量 u'InputData/X:0' 提供形状 (96, 50, 50) 的值
【发布时间】:2020-07-11 20:59:21
【问题描述】:

我是 TensorFlow 和 python 的新手。我正在尝试使用 CNN 运行用于肺癌检测的代码。这是脚本:我正在尝试训练一个 CNN 模型。当我在训练时使用model.fit 时出现错误

    from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.estimator import regression
from tflearn.data_preprocessing import ImagePreprocessing
from tflearn.data_augmentation import ImageAugmentation

img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()

img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation(max_angle=25.)
img_aug.add_random_blur(sigma_max=3.)

network = input_data(shape=[None, 50, 50, 1],
                     data_preprocessing=img_prep,
                     data_augmentation=img_aug)
network = conv_2d(network, 50, 3, activation='relu')
network = max_pool_2d(network, 2)
network = conv_2d(network, 64, 3, activation='relu')
network = conv_2d(network, 64, 3, activation='relu')
network = max_pool_2d(network, 2)
network = fully_connected(network, 512, activation='relu')
network = dropout(network, 0.5)
network = fully_connected(network, 2, activation='softmax')
network = regression(network, optimizer='adam',
                     loss='categorical_crossentropy',
                     learning_rate=0.001)

model = tflearn.DNN(network, tensorboard_verbose=0, checkpoint_path='nodule-classifier.tfl.ckpt')


model.fit(X_train_images, Y_train_labels, n_epoch=100, shuffle=True, validation_set=(X_val_images, Y_val_labels),
          show_metric=True, batch_size=96, snapshot_epoch=True, 
          run_id='noduleclassifier')

model.save("nodule-classifier.tfl")
print("Network trained and saved as nodule-classifier.tfl!")

我正在尝试训练一个 CNN 模型。当我在训练时使用model.fit 时出现错误 -->

ValueErrorTraceback (most recent call last)

<ipython-input-60-e6a88471dbf1> in <module>()
      5 model.fit(X_train_images, Y_train_labels, n_epoch=100, shuffle=True, validation_set=(X_val_images, Y_val_labels),
      6           show_metric=True, batch_size=96, snapshot_epoch=True,
----> 7           run_id='noduleclassifier')
      8 
      9 # Save model when training is complete to a file

-----------------------------------------------------------------------------

/tensorflow-1.15.2/python2.7/tensorflow_core/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1154                 'Cannot feed value of shape %r for Tensor %r, '
   1155                 'which has shape %r' %
-> 1156                 (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
   1157           if not self.graph.is_feedable(subfeed_t):
   1158             raise ValueError('Tensor %s may not be fed.' % subfeed_t)

ValueError: Cannot feed value of shape (96, 50, 50) for Tensor u'InputData/X:0', which has shape '(?, 50, 50, 1)'

error_image

链接到原始代码 --> link

谁能帮我解决这个问题?

【问题讨论】:

  • 您好!欢迎来到 SO!请转至how to ask
  • 只看标题,numpy.squeeze 可以帮助您摆脱形状 1 的额外维度。
  • 你能解释一下吗
  • 哪部分不明白?
  • 如何使用'numpy.squeeze'

标签: python deep-learning google-colaboratory conv-neural-network


【解决方案1】:

模型需要一个维度为 4 的张量。 您必须向训练数据添加第四个维度。 使用

X_train_images = np.expand_dims(X_train_images, axis=-1)

扩大尺寸和 np.squeeze 缩小尺寸

【讨论】:

  • 仅展开维度。不要减少它。你没做好。如果你做对了,数据将有形状 (?, 50,50,1),因为你有 96 个数据,(96,50,50,1)
  • 是的,我明白了。现在,当我使用 X_train_images.shape 时,我得到的形状为 (90, 50, 50, 1) 但是当我开始训练时,它仍然显示相同的错误。
  • 使用input_data 向网络提供数据的方式是否存在问题?
猜你喜欢
  • 1970-01-01
  • 2017-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多