簡體   English   中英

Keras:帶有卷積層的自動編碼器

[英]Keras: autoencoder whit convolutional layers

我正在嘗試在MNIST數據庫上執行此處提出的 CAE,但遇到大小為 2 的瓶頸。 -In-the-middle-there_fig1_320658590當我做 model 總結時,我在卷積層中遇到錯誤,形狀不匹配。

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

似乎 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM