我正在尝试对时空数据使用自动编码器。我的数据形状是:batches , filters, timesteps, rows, columns
。我无法将自动编码器设置为正确的形状。
这是我的型号:
input_imag = Input(shape=(3, 81, 4, 4))
x = Conv3D(16, (5, 3, 3), data_format='channels_first', activation='relu', padding='same')(input_imag)
x = MaxPooling3D((3, 2, 2), data_format='channels_first', padding='same')(x)
x = Conv3D(8, (5, 3, 3), data_format='channels_first', activation='relu', padding='same')(x)
x = MaxPooling3D((3, 2, 2), data_format='channels_first', padding='same')(x)
x = Conv3D(4, (5, 3, 3), data_format='channels_first', activation='relu', padding='same')(x)
encoded = MaxPooling3D((3, 2, 2), data_format='channels_first', padding='same', name='encoder')(x)
x = Conv3D(4, (5, 3, 3), data_format='channels_first', activation='relu', padding='same')(encoded)
x = UpSampling3D((3, 2, 2), data_format='channels_first')(x)
x = Conv3D(8, (5, 3, 3), data_format='channels_first', activation='relu', padding='same')(x)
x = UpSampling3D((3, 2, 2), data_format='channels_first')(x)
x = Conv3D(16, (5, 3, 3), data_format='channels_first', activation='relu', padding='same')(x)
x = UpSampling3D((3, 2, 2), data_format='channels_first')(x)
decoded = Conv3D(3, (5, 3, 3), data_format='channels_first', activation='relu', padding='same')(x)
autoencoder = Model(input_imag, decoded)
autoencoder.compile(optimizer='adam', loss='mse')
autoencoder.summary()
这是总结:
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 3, 81, 4, 4)] 0
_________________________________________________________________
conv3d (Conv3D) (None, 16, 81, 4, 4) 2176
_________________________________________________________________
max_pooling3d (MaxPooling3D) (None, 16, 27, 2, 2) 0
_________________________________________________________________
conv3d_1 (Conv3D) (None, 8, 27, 2, 2) 5768
_________________________________________________________________
max_pooling3d_1 (MaxPooling3 (None, 8, 9, 1, 1) 0
_________________________________________________________________
conv3d_2 (Conv3D) (None, 4, 9, 1, 1) 1444
_________________________________________________________________
encoder (MaxPooling3D) (None, 4, 3, 1, 1) 0
_________________________________________________________________
conv3d_3 (Conv3D) (None, 4, 3, 1, 1) 724
_________________________________________________________________
up_sampling3d (UpSampling3D) (None, 4, 9, 2, 2) 0
_________________________________________________________________
conv3d_4 (Conv3D) (None, 8, 9, 2, 2) 1448
_________________________________________________________________
up_sampling3d_1 (UpSampling3 (None, 8, 27, 4, 4) 0
_________________________________________________________________
conv3d_5 (Conv3D) (None, 16, 27, 4, 4) 5776
_________________________________________________________________
up_sampling3d_2 (UpSampling3 (None, 16, 81, 8, 8) 0
_________________________________________________________________
conv3d_6 (Conv3D) (None, 3, 81, 8, 8) 2163
=================================================================
Total params: 19,499
Trainable params: 19,499
Non-trainable params: 0
我应该更改什么以使解码器输出形状为[?,3,81,4,4]
而不是[?,3,81,8,8]
?
看起来您希望MaxPooling3D和UpSampling3D操作是对称的(至少在输出形状方面(。让我们看看最后一个MaxPooling3D层的输入形状:
conv3d_2 (Conv3D) (None, 4, 9, 1, 1) 1444
_________________________________________________________________
encoder (MaxPooling3D) (None, 4, 3, 1, 1) 0
形状为(None, 4, 9, 1, 1)
。最后两个维度已经是1,因此它们不能除以2,如pool_size
中所指定的。因此,尽管MaxPooling3D层具有pool_size=(3, 2, 2)
,但它有效地利用pool_size=(3, 1, 1)
进行操作。至少我认为这就是幕后发生的事情。
当指定pool_size大于输入大小时,没有出现错误或警告,这让我有点惊讶。
要修复此问题,可以将第一个UpSampling3D层的形状设置为(3, 1, 1)
x = UpSampling3D((3, 1, 1), data_format='channels_first')(x)
因此,完整的解决方案:
input_imag = Input(shape=(3, 81, 4, 4))
x = Conv3D(16, (5, 3, 3), data_format='channels_first', activation='relu', padding='same')(input_imag)
x = MaxPooling3D((3, 2, 2), data_format='channels_first', padding='same')(x)
x = Conv3D(8, (5, 3, 3), data_format='channels_first', activation='relu', padding='same')(x)
x = MaxPooling3D((3, 2, 2), data_format='channels_first', padding='same')(x)
x = Conv3D(4, (5, 3, 3), data_format='channels_first', activation='relu', padding='same')(x)
encoded = MaxPooling3D((3, 2, 2), data_format='channels_first', padding='same', name='encoder')(x)
x = Conv3D(4, (5, 3, 3), data_format='channels_first', activation='relu', padding='same')(encoded)
x = UpSampling3D((3, 1, 1), data_format='channels_first')(x)
x = Conv3D(8, (5, 3, 3), data_format='channels_first', activation='relu', padding='same')(x)
x = UpSampling3D((3, 2, 2), data_format='channels_first')(x)
x = Conv3D(16, (5, 3, 3), data_format='channels_first', activation='relu', padding='same')(x)
x = UpSampling3D((3, 2, 2), data_format='channels_first')(x)
decoded = Conv3D(3, (5, 3, 3), data_format='channels_first', activation='relu', padding='same')(x)
autoencoder = Model(input_imag, decoded)
autoencoder.compile(optimizer='adam', loss='mse')
autoencoder.summary()
输出:
Model: "model_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_3 (InputLayer) [(None, 3, 81, 4, 4)] 0
conv3d_14 (Conv3D) (None, 16, 81, 4, 4) 2176
max_pooling3d_4 (MaxPooling (None, 16, 27, 2, 2) 0
3D)
conv3d_15 (Conv3D) (None, 8, 27, 2, 2) 5768
max_pooling3d_5 (MaxPooling (None, 8, 9, 1, 1) 0
3D)
conv3d_16 (Conv3D) (None, 4, 9, 1, 1) 1444
encoder (MaxPooling3D) (None, 4, 3, 1, 1) 0
conv3d_17 (Conv3D) (None, 4, 3, 1, 1) 724
up_sampling3d_6 (UpSampling (None, 4, 9, 1, 1) 0
3D)
conv3d_18 (Conv3D) (None, 8, 9, 1, 1) 1448
up_sampling3d_7 (UpSampling (None, 8, 27, 2, 2) 0
3D)
conv3d_19 (Conv3D) (None, 16, 27, 2, 2) 5776
up_sampling3d_8 (UpSampling (None, 16, 81, 4, 4) 0
3D)
conv3d_20 (Conv3D) (None, 3, 81, 4, 4) 2163
=================================================================
Total params: 19,499
Trainable params: 19,499
Non-trainable params: 0