gpt4 book ai didi

python - Keras 中的二维卷积错误

转载 作者:太空宇宙 更新时间:2023-11-04 02:55:48 24 4
gpt4 key购买 nike

我知道这类问题在这里被问过很多次,但我无法从这些问题中找出答案。我有一张 100x100 的灰度图像。尝试在第一层执行 2D 卷积时出现以下错误。

    import theano
from keras.layers import Activation, Flatten, Dense
from keras.layers import Convolution2D,MaxPooling2D
from keras.models import Sequential

nb_epoch = 40
batch_size = 32
nb_classes = 2
model = Sequential()
model.add(Convolution2D(32,3,3,border_mode = 'valid',subsample = (1,1),init = 'glorot_uniform',input_shape = (1,100,100)))
model.add(Activation('relu'))

train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range = 300,
horizontal_flip=True,
vertical_flip = True)

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=16,
class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
test_data_dir,
target_size=(img_width, img_height),
batch_size=16,
class_mode='binary')

model.fit_generator(
train_generator,
samples_per_epoch=nb_train_samples,
nb_epoch=nb_epoch,
validation_data=validation_generator,
nb_val_samples=nb_validation_samples)

我收到这样的错误:检查模型输入时出错:预期 convolution2d_input_1 的形状为 (None, 1, 100, 100) 但得到的数组的形状为 (32, 3, 100, 100)。我不确定我哪里出错了。

最佳答案

尝试:

 train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=16,
color_mode='grayscale',
class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
test_data_dir,
target_size=(img_width, img_height),
batch_size=16,
color_mode='grayscale
class_mode='binary')

关于python - Keras 中的二维卷积错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42437662/

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