Keras CNN-LSTM:制作y_train时出错

时间:2019-10-29 15:14:17

标签: keras conv-neural-network lstm recurrent-neural-network train-test-split

这是我第一次在这里问一个问题(这意味着我真的需要帮助),对于我的英语不好,我感到抱歉。我想在Keras中为视频分类创建一个cnn-lstm层,但是在制作y_train时遇到问题。在此之后,我将描述我的问题。 我有视频数据集(1个视频有10帧),并将视频转换为图像。 首先,我将数据集拆分为xtrain,xtest,ytrain和ytest(20%测试,80%训练),然后做到了。

X_train, X_test = img_data[:trainco], img_data[trainco:]
y_train, y_test = y[:trainco], y[trainco:]

X_train shape:(2280,64,64,1)->我有2280张图像,64x64高度x宽度,1个通道

y_train shape:(2280,26)-> 26个类

然后,在进入cnn-lstm进程之前,我必须重塑它们的形状。 *注意:我对x_test和y_test做相同的事情

time_steps = 10 (because I have 10 frames per video)

X_train = X_train.reshape(int(X_train.shape[0] / time_steps), time_steps, X_train.shape[1], X_train.shape[2], X_train.shape[3])
y_train = y_train.reshape(int(y_train.shape[0] / time_steps), time_steps, y_train.shape[1])

X_train形状:(228,10,64,64,1),y_train形状:(228,10,26)

然后这是我的模型:

model = Sequential()
model.add(TimeDistributed(Conv2D(32, (3, 3), strides=(2, 2), activation='relu', padding='same'), input_shape=X_train.shape[1:]))
model.add(TimeDistributed(MaxPooling2D((2, 2), strides=(2, 2))))
model.add(TimeDistributed(Conv2D(32, (3, 3), padding='same', activation='relu')))
model.add(TimeDistributed(MaxPooling2D((2, 2), strides=(2, 2))))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(256, return_sequences=False, input_shape=(64, 64)))
model.add(Dense(128))
model.add(Dense(64))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=["accuracy"])
checkpoint = ModelCheckpoint(fname, monitor='acc', verbose=1, save_best_only=True, mode='max', save_weights_only=True)
hist = model.fit(X_train, y_train, batch_size=num_batch, nb_epoch=num_epoch, verbose=1, validation_data=(X_test, y_test), callbacks=[checkpoint])

但是我收到一条错误消息

ValueError: Error when checking target: expected dense_3 to have 2 dimensions, but got array with shape (228, 10, 26)

就像它说的预期有2个维度。我将代码更改为

y_train = y_train.reshape(int(y_train.shape[0] / time_steps), y_train.shape[1])

然后我又收到一个错误消息

ValueError: cannot reshape array of size 59280 into shape (228,26)

然后我再次将代码更改为

y_train = y_train.reshape(y_train.shape[0], y_train.shape[1])

我仍然遇到错误

ValueError: Input arrays should have the same number of samples as target arrays. Found 228 input samples and 2280 target samples.

我该怎么办?我知道这个问题,但不知道如何解决。请帮助我。

1 个答案:

答案 0 :(得分:0)

我重新创建了您情况的简化版本,以重现此问题。基本上,LSTM层似乎只在整个时间步序列中给出一个结果,从而将输出中的维数从3减小到2。如果您在下面运行我的程序,则添加了model.summary(),它提供了体系结构的详细信息。

from keras import Sequential
from keras.layers import TimeDistributed, Dense, Conv2D, MaxPooling2D, Flatten, LSTM
import numpy as np

X_train = np.random.random((228, 10, 64, 64, 1))
y_train = np.random.randint(2, size=(228, 10, 26))
num_classes = 26

# Create the model
model = Sequential()
model.add(TimeDistributed(Conv2D(32, (3, 3), strides=(2, 2), activation='relu', padding='same'), input_shape=X_train.shape[1:]))
model.add(TimeDistributed(MaxPooling2D((2, 2), strides=(2, 2))))
model.add(TimeDistributed(Conv2D(32, (3, 3), padding='same', activation='relu')))
model.add(TimeDistributed(MaxPooling2D((2, 2), strides=(2, 2))))
model.add(TimeDistributed(Flatten(),name='Flatten'))
model.add(LSTM(256, return_sequences=False, input_shape=(64, 64)))
model.add(Dense(128))
model.add(Dense(64))
model.add(Dense(num_classes, activation='softmax', name='FinalDense'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=["accuracy"])

#
model.summary()
# hist = model.fit(X_train, y_train, epochs=1)

我认为您需要决定是否要减小y_train(目标)数据的维数以与模型保持一致,或更改模型。我希望这会有所帮助。