Keras:卷积层的自动编码器

时间:2020-06-24 23:24:06

标签: python tensorflow keras conv-neural-network autoencoder

我试图在MNIST数据库上执行此处建议的CAE,但遇到了大小2的瓶颈。{{3}} 当我进行模型汇总时,在卷积层中出现错误,形状不匹配。

Model: "model_11"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_20 (InputLayer)        (None, 28, 28, 1)         0         
_________________________________________________________________
conv2d_58 (Conv2D)           (None, 14, 14, 32)        6304      
_________________________________________________________________
conv2d_59 (Conv2D)           (None, 7, 7, 64)          100416    
_________________________________________________________________
conv2d_60 (Conv2D)           (None, 4, 4, 128)         73856     
_________________________________________________________________
flatten_15 (Flatten)         (None, 2048)              0         
_________________________________________________________________
dense_38 (Dense)             (None, 1152)              2360448   
_________________________________________________________________
dense_39 (Dense)             (None, 2)                 2306      
_________________________________________________________________
dense_40 (Dense)             (None, 1152)              3456      
_________________________________________________________________
reshape_16 (Reshape)         (None, 3, 3, 128)         0         
_________________________________________________________________
conv2d_transpose_31 (Conv2DT (None, 6, 6, 64)          401472    
_________________________________________________________________
conv2d_transpose_32 (Conv2DT (None, 12, 12, 32)        401440    
_________________________________________________________________
conv2d_transpose_33 (Conv2DT (None, 24, 24, 1)         25089     
=================================================================
Total params: 3,374,787
Trainable params: 3,374,787
Non-trainable params: 0
_________________________________________________________________

这是完整的代码

(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_images = x_train.reshape(x_train.shape[0], 28, 28)
input_img = Input(shape=(28, 28, 1))
encoded = Convolution2D(32, 14, 14, activation = "relu", border_mode="same",subsample = (2,2))(input_img)
encoded = Convolution2D(64, 7, 7, activation = "relu", border_mode="same",subsample = (2,2))(encoded)
encoded = Convolution2D(128, 3, 3, activation = "relu", border_mode="same",subsample = (2,2))(encoded)
encoded = Flatten()(encoded)
encoded = Dense(1152)(encoded)

encoded = Dense(2)(encoded)

decoded = Dense(1152)(encoded)
decoded = Reshape((3,3,128))(decoded)
decoded = Deconvolution2D(64, 7, 7, activation = "relu",border_mode="same", subsample = (2,2))(decoded)
decoded = Deconvolution2D(32, 14, 14, activation = "relu",border_mode="same",subsample = (2,2))(decoded)
decoded = Deconvolution2D(1, 28, 28, activation = "relu",border_mode="same",subsample = (2,2))(decoded)
autoencoder = Model(input=input_img, output=decoded)` 

1 个答案:

答案 0 :(得分:1)

似乎喀拉拉邦的填充有问题(不确定,但经过快速搜索) 那么添加下面两行怎么办

decoded = Flatten()(decoded)
decoded = Dense(3136)(decoded)
decoded = Reshape((7,7,64))(decoded)

最终代码如下

encoded = Convolution2D(32, 14, 14, activation = 
"relu", border_mode="same",subsample = (2,2))(input_img)
encoded = Convolution2D(64, 7, 7, activation = "relu", border_mode="same",subsample = (2,2))(encoded)
encoded = Convolution2D(128, 3, 3, activation = "relu", border_mode="valid",subsample = (2,2))(encoded)
encoded = Flatten()(encoded)
encoded = Dense(1152)(encoded)

encoded = Dense(2)(encoded)

decoded = Dense(1152)(encoded)
decoded = Reshape((3,3,128))(decoded)
decoded = Flatten()(decoded)
decoded = Dense(3136)(decoded)
decoded = Reshape((7,7,64))(decoded)
# decoded = Deconvolution2D(64, 7, 7, activation = "relu",border_mode="same", subsample = (2,2))(decoded)
decoded = Deconvolution2D(32, 14, 14, activation = "relu",border_mode="same",subsample = (2,2))(decoded)
decoded = Deconvolution2D(1, 28, 28, activation = "relu",border_mode="same",subsample = (2,2))(decoded)
autoencoder = Model(input=input_img, output=decoded)