首页 > 解决方案 > ValueError:检查输入时出错:预期 time_distributed_input 的形状为 (None, 224, 224, 3) 但得到的数组的形状为 (224, 224, 3, 1)

问题描述

我正在尝试使用 CNN-LSTM 对具有形状 (844、224、224、3) 的 rgb 图像数据进行分类。我开始使用代码将其重塑为 (844, 224, 224, 3, 1)

x_train = np.reshape(train_data, (train_data.shape[0], train_data.shape[1], train_data.shape[2], train_data.shape[3], 1))
x_test = np.reshape(test_data, (test_data.shape[0], test_data.shape[1], test_data.shape[2], test_data.shape[3], 1))

然后我使用了下面的模型:

model = Sequential()
model.add(TimeDistributed(Conv2D(64, (3, 3), activation='relu'), input_shape=(None, 224, 224, 3)))
model.add(TimeDistributed(Conv2D(64, (3, 3), activation='relu')))
model.add(TimeDistributed(GlobalAveragePooling2D()))
model.add(LSTM(1024, activation='relu', return_sequences=False))
model.add(Dense(1024, activation='relu'))
model.add(Dropout(.5))
model.add(Dense(3, activation='sigmoid'))
model.compile('adam', loss='categorical_crossentropy')

当我适合模型时

model.fit(x_train, y_tr, epochs=3, validation_data=(x_test, y_ts))

我得到了错误

ValueError: Error when checking input: expected time_distributed_input to have shape (None, 224, 224, 3) but got array with shape (224, 224, 3, 1)

请你帮我解决它。

标签: pythonimage-processingconv-neural-networklstm

解决方案


推荐阅读