Keras 卷积自动编码器不起作用

Keras convolutional autoencoder doesn't work

我正在测试来自 Keras 作者的卷积自动编码器: https://blog.keras.io/building-autoencoders-in-keras.html

但是我有这个问题:

Exception: Error when checking model target: expected convolution2d_7 to have shape (None, 8, 32, 1) but got array with shape (60000, 1, 28, 28)

准确地说,我已经在最后一个转换层中设置了字段'border_mode='same''。 所以我真的不知道它来自哪里.. 这是摘要:

Layer (type)                     Output Shape          Param #         Connected to                     
        ====================================================================================================
input_1 (InputLayer)             (None, 1, 28, 28)     0                                            
 ____________________________________________________________________________________________________
convolution2d_1 (Convolution2D)  (None, 1, 28, 16)     4048        input_1[0][0]                    
____________________________________________________________________________________________________
maxpooling2d_1 (MaxPooling2D)    (None, 1, 14, 16)     0           convolution2d_1[0][0]            
        ______________________________________________________________________________    ______________________
convolution2d_2 (Convolution2D)  (None, 1, 14, 8)      1160        maxpooling2d_1[0][0]             
____________________________________________________________________________________________________
maxpooling2d_2 (MaxPooling2D)    (None, 1, 7, 8)       0           convolution2d_2[0][0]            
____________________________________________________________________________________________________
convolution2d_3 (Convolution2D)  (None, 1, 7, 8)       584         maxpooling2d_2[0][0]             
____________________________________________________________________________________________________
maxpooling2d_3 (MaxPooling2D)    (None, 1, 4, 8)       0           convolution2d_3[0][0]            
____________________________________________________________________________________________________
convolution2d_4 (Convolution2D)  (None, 1, 4, 8)       584         maxpooling2d_3[0][0]             
____________________________________________________________________________________________________
upsampling2d_1 (UpSampling2D)    (None, 2, 8, 8)       0           convolution2d_4[0][0]            
 ____________________________________________________________________________________________________
convolution2d_5 (Convolution2D)  (None, 2, 8, 8)       584         upsampling2d_1[0][0]             
____________________________________________________________________________________________________
upsampling2d_2 (UpSampling2D)    (None, 4, 16, 8)      0           convolution2d_5[0][0]            
____________________________________________________________________________________________________
convolution2d_6 (Convolution2D)  (None, 4, 16, 16)     1168        upsampling2d_2[0][0]             
____________________________________________________________________________________________________
upsampling2d_3 (UpSampling2D)    (None, 8, 32, 16)     0           convolution2d_6[0][0]            
______________________________________________________________________________ ______________________

convolution2d_7 (Convolution2D)  (None, 8, 32, 1)      145             

upsampling2d_3[0][0]             
====================================================================================================
Total params: 8273
 ____________________________________________________________________________________________________

终于找到答案了。 我认为教程的​​创建者使用 32x32 MNIST 图像而不是 28x28 对其进行了测试。

因为,当添加到最后一个卷积层时 border_mode='same',你得到的输出形状为 (32,32,1) 因此,为了获得良好的输出 (28,28,1),您需要将 border_mode='valid' 添加到最后一个转换层之前。

总结: 将维度排序更正为 28x28x1 而不是 1x28x28。 然后在最后一个conv层添加border mode same 最后添加边界模式有效到最后一个conv层。

希望这会有所帮助。