我有一组图像,我想按顺序分析。我的训练数据有形状(354,260,260,1)-有354个图像,每个维度(260260,1)。我的目的是建立一个CNN模型,计算每个图像中对象的数量,但是由于图像是按顺序排列的,所以我也尝试在上面包含LSTM层,这就是我遇到输入维度问题的地方。不幸的是,我还没有找到一个解决方案在这个网站上或谷歌。你知道吗
我将我的模型设置为:
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
指定为(5260260,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)。我上面的模型正在运行。你知道吗
你好像在把你的整个数据集传给模型, 您必须从数据集中获取一批5幅图像,在这种情况下,您应该设置您的输入\u shape=(260260,1),当您适合您的模型时,按如下方式传递参数
model.fit(x_train, y_train,epochs=20,batch_size=5)
,但要做到这一点,您必须准备好数据,以便能够按批弹出5幅图像,下面是关于如何进行批处理的link和关于时间分布如何工作的another相关问题 更多 >
编程相关推荐