Keras中卷积自动编码器的输出大小

时间:2016-09-13 14:50:58

标签: python neural-network deep-learning keras autoencoder

我正在做Keras库的作者编写的卷积自动编码器教程: https://blog.keras.io/building-autoencoders-in-keras.html

但是,当我启动完全相同的代码并使用summary()分析网络架构时,似乎输出大小与输入大小不兼容(在自动编码器的情况下是必需的)。这是summary()的输出:

**____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
input_1 (InputLayer)             (None, 1, 28, 28)     0                                            
____________________________________________________________________________________________________
convolution2d_1 (Convolution2D)  (None, 16, 28, 28)    160         input_1[0][0]                    
____________________________________________________________________________________________________
maxpooling2d_1 (MaxPooling2D)    (None, 16, 14, 14)    0           convolution2d_1[0][0]            
____________________________________________________________________________________________________
convolution2d_2 (Convolution2D)  (None, 8, 14, 14)     1160        maxpooling2d_1[0][0]             
____________________________________________________________________________________________________
maxpooling2d_2 (MaxPooling2D)    (None, 8, 7, 7)       0           convolution2d_2[0][0]            
____________________________________________________________________________________________________
convolution2d_3 (Convolution2D)  (None, 8, 7, 7)       584         maxpooling2d_2[0][0]             
____________________________________________________________________________________________________
maxpooling2d_3 (MaxPooling2D)    (None, 8, 3, 3)       0           convolution2d_3[0][0]            
____________________________________________________________________________________________________
convolution2d_4 (Convolution2D)  (None, 8, 3, 3)       584         maxpooling2d_3[0][0]             
____________________________________________________________________________________________________
upsampling2d_1 (UpSampling2D)    (None, 8, 6, 6)       0           convolution2d_4[0][0]            
____________________________________________________________________________________________________
convolution2d_5 (Convolution2D)  (None, 8, 6, 6)       584         upsampling2d_1[0][0]             
____________________________________________________________________________________________________
upsampling2d_2 (UpSampling2D)    (None, 8, 12, 12)     0           convolution2d_5[0][0]            
____________________________________________________________________________________________________
convolution2d_6 (Convolution2D)  (None, 16, 10, 10)    1168        upsampling2d_2[0][0]             
____________________________________________________________________________________________________
upsampling2d_3 (UpSampling2D)    (None, 16, 20, 20)    0           convolution2d_6[0][0]            
____________________________________________________________________________________________________
convolution2d_7 (Convolution2D)  (None, 1, 20, 20)     145         upsampling2d_3[0][0]             
====================================================================================================
Total params: 4385
____________________________________________________________________________________________________**

1 个答案:

答案 0 :(得分:4)

请注意,您在前一个卷积层中缺少border_mode选项。

from keras.layers import Input, Dense, Convolution2D, MaxPooling2D, UpSampling2D
from keras.models import Model

input_img = Input(shape=(1, 28, 28))

x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(input_img)
x = MaxPooling2D((2, 2), border_mode='same')(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
x = MaxPooling2D((2, 2), border_mode='same')(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
encoded = MaxPooling2D((2, 2), border_mode='same')(x)

# at this point the representation is (8, 4, 4) i.e. 128-dimensional

x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Convolution2D(1, 3, 3, activation='sigmoid', border_mode='same')(x)

autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

这应该可以正常工作