我正在使用卷积自动编码器用于Mnist图像数据(维度为28*28);下面是我的代码:
input_img = Input(shape=(28, 28, 1))
x = Convolution2D(16, (5, 5), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Convolution2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Convolution2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
x = Convolution2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(16, (5, 5), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Convolution2D(1, (3, 3), activation='sigmoid', padding='same')(x)
我得到一个错误消息(每层都有padding ='same'):
ValueError:检查目标时出错:预期conv2d_148具有形状(32,32,1),但得到的数组具有形状(28,28,1)
以下是我的模型总结:
Layer (type) Output Shape Param #
=================================================================
input_20 (InputLayer) (None, 28, 28, 1) 0
_________________________________________________________________
conv2d_142 (Conv2D) (None, 28, 28, 16) 416
_________________________________________________________________
max_pooling2d_64 (MaxPooling (None, 14, 14, 16) 0
_________________________________________________________________
conv2d_143 (Conv2D) (None, 14, 14, 8) 1160
_________________________________________________________________
max_pooling2d_65 (MaxPooling (None, 7, 7, 8) 0
_________________________________________________________________
conv2d_144 (Conv2D) (None, 7, 7, 8) 584
_________________________________________________________________
max_pooling2d_66 (MaxPooling (None, 4, 4, 8) 0
_________________________________________________________________
conv2d_145 (Conv2D) (None, 4, 4, 8) 584
_________________________________________________________________
up_sampling2d_64 (UpSampling (None, 8, 8, 8) 0
_________________________________________________________________
conv2d_146 (Conv2D) (None, 8, 8, 8) 584
_________________________________________________________________
up_sampling2d_65 (UpSampling (None, 16, 16, 8) 0
_________________________________________________________________
conv2d_147 (Conv2D) (None, 16, 16, 16) 3216
_________________________________________________________________
up_sampling2d_66 (UpSampling (None, 32, 32, 16) 0
_________________________________________________________________
conv2d_148 (Conv2D) (None, 32, 32, 1) 145
=================================================================
Total params: 6,689
Trainable params: 6,689
Non-trainable params: 0
我知道如果我把第一层改成
x = Convolution2D(16, (3, 3), activation='relu', padding='same')(input_img)
它可以工作,但我想使用5*5卷积。
它是如何发生的?
1条答案
按热度按时间9o685dep1#
您可以将最后一个过滤器的大小增加到
(5, 5)
来实现此功能:或者,使用
tf.keras.Conv2DTranspose