【问题标题】:How to fix LSTM Layer Error - Image Classification如何修复 LSTM 层错误 - 图像分类
【发布时间】:2019-07-09 10:21:03
【问题描述】:

目前我正在处理图像分类问题,并根据在线教程创建了以下代码 - Image Classification using Keras

代码运行良好,但添加 LSTM 层时,input_shape 出现问题,我无法找出解决方案:

ValueError: Input 0 is in compatible with layer lstm_1: expected ndim=3, found 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) 或任何变体,等等

我可能正在监督一件愚蠢的事情,希望这里有人能有一个想法?

【问题讨论】:

    标签: python tensorflow keras lstm keras-layer


    【解决方案1】:

    在 LSTM 层之前添加TimeDistributed(Flatten()) 层。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-03
      • 2019-11-02
      • 2012-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多