ValueError:检查输入时出错:期望lstm_1_input具有形状(无,296,2048)但是具有形状的数组(296,2048,1)

时间:2017-09-08 13:37:21

标签: machine-learning computer-vision deep-learning keras lstm

我正面临标题中的错误。我有数千个视频,每个视频有37帧。我已经使用CNN模型为每个帧提取了特征并保存了它们。 我有一个堆叠的LSTM模型:

batch_size = 8
features_length = 2048
seq_length = 37*batch_size
in_shape = (seq_length, features_length)
lstm_model = Sequential()
lstm_model.add(LSTM(2048, return_sequences=True, input_shape = in_shape, dropout=0.5))
lstm_model.add(Flatten())
lstm_model.add(Dense(512, activation='relu'))
lstm_model.add(Dropout(0.5))
lstm_model.add(Dense(number_of_classes, activation='softmax'))
optimizer = Adam(lr=1e-6)
lstm_model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics = metrics)
lstm_model.fit_generator(generator = generator, steps_per_epoch = train_steps_per_epoch, epochs = nb_epoch, verbose = 1, callbacks=[checkpointer, tb, early_stopper, csv_logger], validation_data=val_generator, validation_steps = val_steps_per_epoch)

我有一台发电机;数据包括所有培训视频。

def generator(data):

    while 1:
        X, y = [], []
        for _ in range(batch_size):
            sequence = None
            sample = random.choice(data)
            folder_content, folder_name, class_name, video_features_loc = get_video_features(sample)
            for f in folder_content:
                image_feature_location = video_features_loc + f
                feat = get_extracted_feature(image_feature_location)

                X.append(feat)
                y.append(get_one_class_rep(class_name))         
        yield np.array(X), np.array(y)

生成器数据中X的形状为=(296,2048,1)

生成器数据中y的形状为=(296,27)

此代码抛出错误。我知道有几个类似的问题。我尝试了那里的建议,但没有运气。例如,一个建议是重塑数组;

X = np.reshape(X, (X.shape[2], X.shape[0], X.shape[1]))

我如何将输入提供给LSTM?

提前致谢

1 个答案:

答案 0 :(得分:2)

错误消息告诉您所需的一切。

X的形状应为(number of samples, 296, 2048) - 看起来你只有一个样本,形状为X.

但是如果你有37个框架,你肯定应该为接受的东西改变你的模型:(Batch size, 37, 2048) - 这里,批量大小似乎是8。

seq_length=37