我要按顺序分析一组图像。我的训练数据的形状为(354,260,260,1)-有354个图像,每个图像的尺寸为(260,260,1)。我的意图是建立一个CNN模型,该模型可以计算每个图像中的对象数量,但是由于图像是按顺序排列的,因此我也试图在其中包括LSTM层,这就是我在输入中遇到问题的地方尺寸。不幸的是,我无法在此网站或google上找到解决方案。
我已经建立了这样的模型:
model = Sequential()
model.add(TimeDistributed(Conv2D(filters=16, kernel_size=3, activation='relu'),
input_shape=(5,260,260,1)))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2,2))))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(5))
model.add(Dense(16, activation='relu'))
model.add(Dense(1, activation='linear'))
我已将input_shape
指定为(5,260,260,1),因为我希望模型在考虑当前图像的同时考虑过去的5张图像(希望我对此有所了解是正确的,如果我错了,请纠正我)。
以上模型给出了以下摘要:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
time_distributed_61 (TimeDis (None, 5, 258, 258, 16) 160
_________________________________________________________________
time_distributed_62 (TimeDis (None, 5, 129, 129, 16) 0
_________________________________________________________________
time_distributed_63 (TimeDis (None, 5, 266256) 0
_________________________________________________________________
lstm_17 (LSTM) (None, 5, 5) 5325240
_________________________________________________________________
dense_33 (Dense) (None, 5, 16) 96
_________________________________________________________________
dense_34 (Dense) (None, 5, 1) 17
=================================================================
Total params: 5,325,513
Trainable params: 5,325,513
Non-trainable params: 0
但是,当我编译模型时,我遇到了这个错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-79-8e37c915d926> in <module>()
1 model.compile(loss='mean_squared_error', optimizer='adam')
----> 2 history = model.fit(xtrain_rs, ytrain, validation_data=(xtest_rs, ytest),epochs=50,batch_size=32,verbose=1)
2 frames
/usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
129 ': expected ' + names[i] + ' to have ' +
130 str(len(shape)) + ' dimensions, but got array '
--> 131 'with shape ' + str(data_shape))
132 if not check_batch_axis:
133 data_shape = data_shape[1:]
ValueError: Error when checking input: expected time_distributed_61_input to have 5 dimensions, but got array with shape (354, 260, 260, 1)
我目前停留在这里。.不确定如何将训练数据重塑为5个维度,或者是否在其他地方犯了错误?非常感谢您的帮助!
编辑:已解决,如果其他人也遇到相同的问题:
必须先使用TimeseriesGenerator进行批处理,然后Keras
才能推断出批处理大小:
from keras.preprocessing.sequence import TimeseriesGenerator
train_sequences = TimeseriesGenerator(xtrain_rs, ytrain, length=5, batch_size=64)
test_sequences = TimeseriesGenerator(xtest_rs, ytest, length=5, batch_size=64)
# fit model using fit_generator instead of fit
fit_generator(train_sequences,validation_data = test_sequences,epochs=10)
# sanity check
batch_x, batch_y = test_sequences[0]
batch_x.shape
从上方输出的形状为(64、5、260、260、1)。我上面的模型现在正在运行。