特遣部队.如何在Keras的自动编码器中适应尺寸



我使用卷积自编码器的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卷积。

这是怎么发生的?

您可以将最后的过滤器大小增加到(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

相关内容

  • 没有找到相关文章

最新更新