当前位置: 首页 > 知识库问答 >
问题:

检查输入时出错:预期conv2d\u 1\u输入具有形状(50,50,1),但获得具有形状(50,50,3)的数组

东门奕
2023-03-14
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架构。我使用的图像是灰度图。

如果我将通道值指定为粗体分类器中指定的1。添加(卷积2d(32,kernel\u size=3,input\u shape=(50,50,1),激活='relu'))

Im获取错误为

检查输入时出错:预期conv2d\u 1\u输入具有形状(50,50,1),但获得具有形状(50,50,3)的数组

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

共有1个答案

魏烨熠
2023-03-14

“flow\u from\u directory”(来自目录)采用指定加载图像的通道数的参数“color\u mode”(颜色模式)。如果要使用灰度图像,需要指定它(默认为“rgb”):

train_datagen.flow_from_directory('/...',
                                  color_mode='grayscale', #<<<<<<<<<<<<<<<<<<<<<
                                  target_size = (50, 50),
                                  batch_size = 32,
                                  class_mode = 'binary')
 类似资料: