【问题标题】:How can I solve the input shape issue in CNN?如何解决 CNN 中的输入形状问题?
【发布时间】:2020-11-09 11:02:53
【问题描述】:

我正在使用对手写字符进行分类并遵循我的 CNN 模型。 我已将图像转换为灰度并二值化。 我该如何解决这个问题?

model = models.Sequential()

model.add(layers.Conv2D(filters=10,kernel_size=(3,3),padding = 'same', input_shape=(100, 100, 3),activation = 'relu'))
model.add(layers.Conv2D(30,(5,5),activation='relu'))

model.add(layers.MaxPooling2D(pool_size=(2,2)))

model.add(layers.Dropout(0.5))

model.add(layers.Conv2D(40,(3,3),activation='relu'))
model.add(layers.Conv2D(60,(3,3),activation='relu'))

model.add(layers.MaxPooling2D(pool_size=(2,2)))

model.add(layers.Flatten())

model.add(layers.Dense(180,activation='relu'))

model.add(layers.Dropout(0.5))

model.add(layers.Dense(100,activation='relu'))
model.add(layers.Dense(12,activation='softmax'))

错误

ValueError: Input 0 of layer sequential_41 is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [None, 100, 100]

【问题讨论】:

标签: python machine-learning deep-learning conv-neural-network


【解决方案1】:

在您的模型中,您已将输入形状声明为(100,100,3)。该模型需要一个 3 通道图像,但您说您的输入数据是灰度的,这意味着它只有一个通道。这意味着您的输入数据的形状为(100,100,1),即为(100,100)。添加batch_size 维度后,它变为[None,100,100],这是您遇到的错误。

将输入形状更改为(100,100,1) 应该可以解决问题。

【讨论】:

  • 再次更改(100,100,1)后出现错误ValueError: Input 0 of layer sequential_43 is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [None, 100, 100]这里层序_41改为42
  • 那么说明输入的数据不是ndarray格式,可能还是图片格式。你的输入是什么样的?
  • x = []; y = []; for folder in names: files = os.listdir("/dataset/"+folder); for file in files: img = image.load_img("/dataset/"+folder+"/"+file,target_size=(100,100)); img = image.img_to_array(img); img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY); img = (img > 150) * 255; x.append(img); y.append(names.index(folder)) x = np.array(x); y = np.array(y)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-23
  • 2020-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多