Keras 自动编码器输入图像大小



考虑这个自动编码器:

import numpy as np
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D, Flatten, Reshape
from keras.models import Model
class ConvAutoencoder:
def __init__(self, image_size, latent_dim):
inp = Input(shape=(image_size[0], image_size[1], 1))
x = Conv2D(16, (3, 3), activation='relu', padding='same')(inp)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
# at this point the representation is (4, 4, 8) i.e. 128-dimensional
d = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
d = UpSampling2D((2, 2))(d)
d = Conv2D(8, (3, 3), activation='relu', padding='same')(d)
d = UpSampling2D((2, 2))(d)
d = Conv2D(16, (3, 3), activation='relu')(d)
d = UpSampling2D((2, 2))(d)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(d)
self.model = Model(inp, decoded)
self.encoder = Model(inp, encoded)
self.model.compile(loss='mse', optimizer='Adam')
print(self.model.summary())

我实例化它

ConvAutoencoder(image_size=(32,32), latent_dim=10)

哪些打印

Model: "model_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 32, 32, 1)         0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 32, 32, 16)        160       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 16, 16, 16)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 16, 16, 8)         1160      
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 8, 8, 8)           0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 8, 8, 8)           584       
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 4, 4, 8)           0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 4, 4, 8)           584       
_________________________________________________________________
up_sampling2d_1 (UpSampling2 (None, 8, 8, 8)           0         
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 8, 8, 8)           584       
_________________________________________________________________
up_sampling2d_2 (UpSampling2 (None, 16, 16, 8)         0         
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 14, 14, 16)        1168      
_________________________________________________________________
up_sampling2d_3 (UpSampling2 (None, 28, 28, 16)        0         
_________________________________________________________________
conv2d_7 (Conv2D)            (None, 28, 28, 1)         145       
=================================================================
Total params: 4,385
Trainable params: 4,385
Non-trainable params: 0
_________________________________________________________________
None

如您所见,输入图像大小(32,32)但输出图像大小(28,28)
* 问题 1:如何更改自动编码器的架构,使输出图像大小变得(32,32)
* 问题 2:如您所见,该类需要一个名为latent_dim的参数。目前,此参数未使用。有没有一种简单的方法可以将自动编码器的潜在尺寸"强制"到一定数量?例如,在中间添加一个完全连接的层或类似的东西?

问题 1

好吧,您在上次上采样中忘记了padding='same'

它应该看起来像这样

# at this point the representation is (4, 4, 8) i.e. 128-dimensional
d = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
d = UpSampling2D((2, 2))(d)
d = Conv2D(8, (3, 3), activation='relu', padding='same')(d)
d = UpSampling2D((2, 2))(d)
d = Conv2D(16, (3, 3), activation='relu', padding='same')(d)
d = UpSampling2D((2, 2))(d)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(d)

问题2

你是说内核吗?那怎么办

x = Conv2D(latent_dim*4, (3, 3), activation='relu', padding='same')(inp)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(latent_dim*2, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(latent_dim, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
# at this point the representation is (4, 4, 8) i.e. 128-dimensional
d = Conv2D(latent_dim, (3, 3), activation='relu', padding='same')(encoded)
d = UpSampling2D((2, 2))(d)
d = Conv2D(latent_dim*2, (3, 3), activation='relu', padding='same')(d)
d = UpSampling2D((2, 2))(d)
d = Conv2D(latent_dim*4, (3, 3), activation='relu', padding='same')(d)
d = UpSampling2D((2, 2))(d)

但是,如果您的意思是希望中间层具有特定的内核大小,那么您可以像这样大步替换MaxPooling2DConv2D

encoded = Conv2D(latent_dim, (3, 3), activation='relu', padding='same', strides=2)(x)

实际上,您可以删除所有Maxpooling2D并将strides=2添加到所有Conv2D

相关内容

  • 没有找到相关文章

最新更新