如何在Keras的自动编码器中拟合尺寸

x0fgdtte  于 2023-04-30  发布在  其他
关注(0)|答案(1)|浏览(162)

我正在使用卷积自动编码器用于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卷积。
它是如何发生的?

9o685dep

9o685dep1#

您可以将最后一个过滤器的大小增加到(5, 5)来实现此功能:

from tensorflow.keras.layers import *
from tensorflow.keras import Model, Input
import numpy as np

input_img = Input(shape=(28, 28, 1))

x = Conv2D(16, (5, 5), activation='relu', padding='same')(input_img)
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)

x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (5, 5), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)

decoded = Conv2D(1, (5, 5), activation='sigmoid', padding='valid')(x)

auto = Model(input_img, decoded)

auto.build(input_shape=(1, 28, 28, 1))
auto(np.random.rand(1, 28, 28, 1)).shape
TensorShape([1, 28, 28, 1])

或者,使用tf.keras.Conv2DTranspose

相关问题