目前,我正在研究图像分类问题,并根据在线教程Image Classification using Keras创建了以下代码。
代码工作正常,但是当addind LSTM层时,input_shape存在问题,我无法找出解决方案:
ValueError:输入0与lstm_1层不兼容:预期ndim = 3,发现ndim = 4
代码:
img_width, img_height = 224, 135
train_dir = './train'
test_dir = './test'
train_samples = 46822
test_samples = 8994
epochs = 25
batch_size = 16
input_shape = (img_width, img_height, 3)
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape = input_shape, activation = 'relu'))
model.add(LSTM(3, return_sequences=True, input_shape = input_shape))
model.add(AveragePooling2D(pool_size = (2, 2)))
model.add(Flatten())
model.add(Dense(units = 128, activation = 'softmax'))
model.compile(loss ='categorical_crossentropy',
optimizer ='adam',
metrics =['accuracy'])
train_datagen = ImageDataGenerator(
rescale = 1. / 255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1. / 255)
train_generator = train_datagen.flow_from_directory(train_dir, target_size =(img_width, img_height), batch_size = batch_size, class_mode ='categorical')
validation_generator = test_datagen.flow_from_directory(test_dir, target_size =(img_width, img_height), batch_size = batch_size, class_mode ='categorical')
model.fit_generator(train_generator,
steps_per_epoch = train_samples // batch_size,
epochs = epochs, validation_data = validation_generator,
validation_steps = test_samples // batch_size)
其他信息:
input_shape的大小=(224,135,3)
train和test文件夹中每个都有3个子文件夹,其中包含一组基于“人体运动”序列的图像。
提到的错误确实提供了一些Google结果,但就我而言却没有提供解决方案->我尝试将LSTM层的input_shape更改为各种选项,例如(224,3)或任何变体,等
我可能正在监督一件愚蠢的事情,希望这里的某人有一个主意?
答案 0 :(得分:0)
我认为这里存在一个基本问题,LSTM
层通常需要“序列”维。
我怀疑您最好去除LSTM层并坚持使用更适合卷积神经网络的层。