gpt4 book ai didi

python - 检查输入 : expected conv2d_1_input to have shape (50, 50, 1) 时出错,但得到形状为 (50, 50, 3) 的数组

转载 作者:太空宇宙 更新时间:2023-11-03 20:18:30 25 4
gpt4 key购买 nike

enter code here



classifier = Sequential()


classifier.add(Convolution2D(32, kernel_size=3, input_shape = (50, 50 , 1), activation =
'relu'))


classifier.add(MaxPooling2D(pool_size = (2, 2)))


classifier.add(Convolution2D(32, kernel_size=3, activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))

classifier.add(Dropout(0.35))
classifier.add(Flatten())


classifier.add(Dense(output_dim = 128, activation = 'relu'))
classifier.add(Dropout(0.04))
classifier.add(Dense(1, activation = 'sigmoid'))


classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])



from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)

validation_datagen = ImageDataGenerator(rescale = 1./255)

training_set = train_datagen.flow_from_directory('/...',
target_size = (50, 50),
batch_size = 32,
class_mode = 'binary')


validation_set = validation_datagen.flow_from_directory('/…..',
target_size = (50, 50),
batch_size = 32,
class_mode = 'binary')


history=classifier.fit_generator(training_set,
samples_per_epoch = 5187,
nb_epoch = 25,
validation_data = validation_set,
nb_val_samples = 1287)

这是我制作的简单的 cnn 架构。我使用的图像是灰度图像。

如果我将 channel 值指定为粗体指定的 1classifier.add(Convolution2D(32, kernel_size=3, input_shape = (50, 50 , 1),activation = 'relu'))

我收到错误

检查输入时出错:预期 conv2d_1_input 的形状为 (50, 50, 1),但得到的数组形状为 (50, 50, 3)

但是如果我使用过滤器大小为 3,我不会收到任何错误,但这可能是对灰度图像使用 3 channel 的逻辑错误...请澄清这一点

最佳答案

flow_from_directory 采用 color_mode 参数来指定加载图像的 channel 数。如果要使用灰度图像,则需要指定它(默认为'rgb'):

train_datagen.flow_from_directory('/...',
color_mode='grayscale', #<<<<<<<<<<<<<<<<<<<<<
target_size = (50, 50),
batch_size = 32,
class_mode = 'binary')

关于python - 检查输入 : expected conv2d_1_input to have shape (50, 50, 1) 时出错,但得到形状为 (50, 50, 3) 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58300973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com