【问题标题】:Keras: autoencoder whit convolutional layersKeras:带有卷积层的自动编码器
【发布时间】:2020-06-24 23:24:06
【问题描述】:

我试图在 MNIST 数据库上执行此处提出的 CAE,但遇到了大小为 2 的瓶颈。https://www.researchgate.net/figure/The-structure-of-proposed-Convolutional-AutoEncoders-CAE-for-MNIST-In-the-middle-there_fig1_320658590 当我做模型总结时,卷积层出现错误,形状不匹配。

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)` 

【问题讨论】:

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


    【解决方案1】:

    似乎 keras 有填充问题(不确定,但在快速搜索后) 那么如何添加以下 2 行

    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)
    

    【讨论】:

      猜你喜欢
      • 2017-01-04
      • 2017-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-11
      • 1970-01-01
      • 2020-08-20
      • 1970-01-01
      相关资源
      最近更新 更多