DeepLearning: Conv/Deconv/Relu/Loss/BatchNorm 的关系(非理论探究)

本文探讨了卷积层(ConvLayers)、激活层(ActivationLayers)及反卷积层(DeconvLayers)在神经网络中的正确使用顺序。强调了在上采样操作中DeconvLayer的重要性,并提供了Caffe框架下具体的参数设置,同时指出了连接损失层(loss layer)时的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Contact Me:
王雪豪 xuehaowang@buaa.edu.cn

It is generally known that the order is important when you use the Conv Layers and Activation Layers.

We usually use them as follow:

conv -> relu
conv -> batchnorm -> scale -> relu

This order shown above can work well. However, how to add into a Deconv Layer?

We use Deconv Layers to upsample the input data and the paras can be set as this(caffe framwork):

layer {
  name: "deconv"
  type: "Deconvolution"
  bottom: "conv"
  top: "deconv"
  param {lr_mult: 0 decay_mult: 0}
  param {lr_mult: 0 decay_mult: 0}
  convolution_param {
    num_output: 1
    pad: 4
    kernel_size: 16
    stride: 8 #扩大的倍数
    weight_filler {
      type:"bilinear" #差值填充
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}

This is a part of caffe network designed to upsample the input 'conv'. The lr is set as 0 to avoid changing the paras during training phase.

So if we want to combine the Conv layer and Deconv layer, we could follow this order:

Conv -> Deconv -> Relu
Conv -> Deconv -> batchnorm -> scale -> Relu

Essentially the Deconv layer can be treat as a post-operate of the Conv layer when you just want to upsample input data and Do Not Need a Activation Layer to active the Conv layer additionally.

Meanwhile if your network has a Loss layer to deal with the data from Conv and Deconv, you must directly connect the loss layer with Deconv layer instead of Conv layer, unless your data from Deconv and Conv layer are both single channel.

The follow order can work well and if you conect the loss layer with conv layer which is output by the deconv layer, you will get an unstable loss value.

Conv(维度下降) -> Deconv(上采样) -> loss
INSTEAD OF
Deconv(上采样) -> Conv(维度下降) -> loss

 

# -*- coding: utf-8 -*- import torch import torch.nn as nn import torch.nn.functional as F from layers import unetConv2 from init_weights import init_weights class UNet_3Plus_DeepSup(nn.Module): def __init__(self, in_channels=1, n_classes=1, feature_scale=4, is_deconv=True, is_batchnorm=True): super(UNet_3Plus_DeepSup, self).__init__() self.is_deconv = is_deconv self.in_channels = in_channels self.is_batchnorm = is_batchnorm self.feature_scale = feature_scale filters = [64, 128, 256, 512, 1024] ## -------------Encoder-------------- self.conv1 = unetConv2(self.in_channels, filters[0], self.is_batchnorm) # channel:1 -> 64 self.maxpool1 = nn.MaxPool2d(kernel_size=2) # 64 self.conv2 = unetConv2(filters[0], filters[1], self.is_batchnorm) # channel:64 -> 128 self.maxpool2 = nn.MaxPool2d(kernel_size=2) # 32 self.conv3 = unetConv2(filters[1], filters[2], self.is_batchnorm) # channel:128 -> 256 self.maxpool3 = nn.MaxPool2d(kernel_size=2) # 16 self.conv4 = unetConv2(filters[2], filters[3], self.is_batchnorm) # channel:256 -> 512 self.maxpool4 = nn.MaxPool2d(kernel_size=2) # 8 self.conv5 = unetConv2(filters[3], filters[4], self.is_batchnorm) # channel:512 -> 1024 ## -------------Decoder-------------- self.CatChannels = filters[0] self.CatBlocks = 5 self.UpChannels = self.CatChannels * self.CatBlocks '''stage 4d''' # h1->320*320, hd4->40*40, Pooling 8 times self.h1_PT_hd4 = nn.MaxPool2d(8, 8, ceil_mode=True) self.h1_PT_hd4_conv = nn.Conv2d(filters[0], self.CatChannels, 3, padding=1) # channel:64 -> 64 self.h1_PT_hd4_bn = nn.BatchNorm2d(self.CatChannels) self.h1_PT_hd4_relu = nn.ReLU(inplace=True) # h2->160*160, hd4->40*40, Pooling 4 times self.h2_PT_hd4 = nn.MaxPool2d(4, 4, ceil_mode=True) self.h2_PT_hd4_conv = nn.Conv2d(filters
03-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值