如何使输出图像的大小与原始图像的大小相同以计算 CNN 中的损失?

时间:2021-02-23 23:50:21

标签: python tensorflow machine-learning keras deep-learning

我为自动编码器定义了 CNN 模型如下:

filters = (32, 16)
X = Input(shape = (32, 32, 3))

# encode
for f in filters:
    X = Conv2D(filters = f, kernel_size = (3, 3), activation = 'relu')(X)
    X = MaxPooling2D(pool_size = (2, 2), strides = (2, 2), padding = 'same')(X)
    X = BatchNormalization(axis = -1)(X)

# decode
for f in filters[::-1]:
    X = Conv2D(filters = f, kernel_size = (3, 3), activation = 'relu')(X)
    X = UpSampling2D(size = (2, 2))(X)
    X = BatchNormalization(axis = -1)(X)

模型摘要是

Model: "functional_13"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_7 (InputLayer)         [(None, 32, 32, 3)]       0         
_________________________________________________________________
conv2d_24 (Conv2D)           (None, 30, 30, 32)        896       
_________________________________________________________________
max_pooling2d_12 (MaxPooling (None, 15, 15, 32)        0         
_________________________________________________________________
batch_normalization_24 (Batc (None, 15, 15, 32)        128       
_________________________________________________________________
conv2d_25 (Conv2D)           (None, 13, 13, 16)        4624      
_________________________________________________________________
max_pooling2d_13 (MaxPooling (None, 7, 7, 16)          0         
_________________________________________________________________
batch_normalization_25 (Batc (None, 7, 7, 16)          64        
_________________________________________________________________
conv2d_26 (Conv2D)           (None, 5, 5, 16)          2320      
_________________________________________________________________
up_sampling2d_12 (UpSampling (None, 10, 10, 16)        0         
_________________________________________________________________
batch_normalization_26 (Batc (None, 10, 10, 16)        64        
_________________________________________________________________
conv2d_27 (Conv2D)           (None, 8, 8, 32)          4640      
_________________________________________________________________
up_sampling2d_13 (UpSampling (None, 16, 16, 32)        0         
_________________________________________________________________
batch_normalization_27 (Batc (None, 16, 16, 32)        128       
=================================================================
Total params: 12,864
Trainable params: 12,672
Non-trainable params: 192
_________________________________________________________________

因为输出图像与输入图像的尺寸不同,我得到了错误

InvalidArgumentError:  Incompatible shapes: [128,32,32,3] vs. [128,16,16,32]
     [[node mean_squared_error/SquaredDifference (defined at <ipython-input-7-a9683921f595>:83) ]] [Op:__inference_train_function_21329]

Function call stack:
train_function

因此无法计算损失函数。能否请您详细说明如何解决此问题?

1 个答案:

答案 0 :(得分:2)

我建议你在卷积中使用 padding='same'。还要注意不要用其他变量覆盖您的输入层。你也错过了一个最终输出层,其通道数等于输入图像的通道

filters = (32, 16)
inp = Input(shape = (32, 32, 3))

# encode
X = inp
for f in filters:
    X = Conv2D(filters = f, kernel_size = (3, 3), padding = 'same', activation = 'relu')(X)
    X = MaxPooling2D(pool_size = (2, 2), strides = (2, 2), padding = 'same')(X)
    X = BatchNormalization(axis = -1)(X)

# decode
for f in filters[::-1]:
    X = Conv2D(filters = f, kernel_size = (3, 3), padding = 'same', activation = 'relu')(X)
    X = UpSampling2D(size = (2, 2))(X)
    X = BatchNormalization(axis = -1)(X)
out =  Conv2D(filters = 3, kernel_size = (3, 3), padding = 'same')(X)
    
model = Model(inp, out)

现在的模型摘要是

Layer (type)                 Output Shape              Param #   
=================================================================
input_9 (InputLayer)         [(None, 32, 32, 3)]       0         
_________________________________________________________________
conv2d_22 (Conv2D)           (None, 32, 32, 32)        896       
_________________________________________________________________
max_pooling2d_10 (MaxPooling (None, 16, 16, 32)        0         
_________________________________________________________________
batch_normalization_20 (Batc (None, 16, 16, 32)        128       
_________________________________________________________________
conv2d_23 (Conv2D)           (None, 16, 16, 16)        4624      
_________________________________________________________________
max_pooling2d_11 (MaxPooling (None, 8, 8, 16)          0         
_________________________________________________________________
batch_normalization_21 (Batc (None, 8, 8, 16)          64        
_________________________________________________________________
conv2d_24 (Conv2D)           (None, 8, 8, 16)          2320      
_________________________________________________________________
up_sampling2d_10 (UpSampling (None, 16, 16, 16)        0         
_________________________________________________________________
batch_normalization_22 (Batc (None, 16, 16, 16)        64        
_________________________________________________________________
conv2d_25 (Conv2D)           (None, 16, 16, 32)        4640      
_________________________________________________________________
up_sampling2d_11 (UpSampling (None, 32, 32, 32)        0         
_________________________________________________________________
batch_normalization_23 (Batc (None, 32, 32, 32)        128       
_________________________________________________________________
conv2d_26 (Conv2D)           (None, 32, 32, 3)         867       
=================================================================
Total params: 13,731
Trainable params: 13,539
Non-trainable params: 192
_________________________________________________________________