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

标签 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

因此无法计算损失函数。您能详细说明一下如何解决这个问题吗?

最佳答案

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

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
_________________________________________________________________

关于python - 如何使输出图像的大小与原始图像的大小相同以计算CNN中的损失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66342708/

相关文章:

python - 如何解决tf_serving_entrypoint.sh : line 3: 6 Illegal instruction (core dumped) when using tensorflow/serving image

machine-learning - 带有附加类别的 stanford NER 分类

java - 在 Weka 中创建兼容的训练和测试实例

python - Prometheus python 客户端报错 地址已在使用中

python - 从 python 调用开发人员命令提示符

python - 在 Ubuntu 12.04 中的 Python 2.7 中导入 Tensorflow 时出错。 'GLIBC_2.17 not found'

tensorflow - 在 keras.sequential 模型中使用 keras.layers.Add()

python - 如何将spark rdd转换为numpy数组?

python - 使用 Google AppEngine 在 Python 中进行单元测试和模拟电子邮件发件人

android - 无法在 hexagon-sim 上执行 hexagon_graph_execution