def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 stride=1,
                 padding=0,
                 dilation=1,
                 groups=1,
                 bias=True,
                 padding_mode='zeros',
                 momentum=0.01,
                 a_bits=8,
                 w_bits=8,
                 bn_fuse=0,
                 q_type=1,
                 q_level=0,
                 first_layer=0):
        super(QuantConvBNReLU, self).__init__()
        self.bn_fuse = bn_fuse

        if self.bn_fuse == 1:
            self.quant_bn_fuse_conv = QuantBNFuseConv2d(in_channels, out_channels,
                                                        kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=False, padding_mode=padding_mode, a_bits=a_bits, w_bits=w_bits, q_type=q_type, q_level=q_level, first_layer=first_layer)
        else:
            self.quant_conv = QuantConv2d(in_channels, out_channels,
                                          kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias, padding_mode=padding_mode, a_bits=a_bits, w_bits=w_bits, q_type=q_type, q_level=q_level, first_layer=first_layer)
            self.bn = nn.BatchNorm2d(out_channels, momentum=momentum) # 考虑量化带来的抖动影响,对momentum进行调整(0.1 ——> 0.01),削弱batch统计参数占比,一定程度抑制抖动。经实验量化训练效果更好,acc提升1%左右
        self.relu = QuantReLU(inplace=True, a_bits=a_bits, q_type=q_type)
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 stride=1,
                 padding=0,
                 dilation=1,
                 groups=1,
                 bias=True,
                 padding_mode='zeros',
                 momentum=0.1,
                 channel_shuffle=0,
                 shuffle_groups=1,
                 a_bits=8,
                 w_bits=8,
                 first_layer=0):
        super(QuantConvBNReLU, self).__init__()
        self.channel_shuffle_flag = channel_shuffle
        self.shuffle_groups = shuffle_groups

        self.quant_conv = QuantConv2d(in_channels,
                                      out_channels,
                                      kernel_size,
                                      stride=stride,
                                      padding=padding,
                                      dilation=dilation,
                                      groups=groups,
                                      bias=bias,
                                      padding_mode=padding_mode,
                                      a_bits=a_bits,
                                      w_bits=w_bits,
                                      first_layer=first_layer)
        self.bn = nn.BatchNorm2d(out_channels, momentum=momentum)
        self.relu = nn.ReLU(inplace=True)
Example #3
0
    def __init__(self, input_channels, output_channels,
            kernel_size=-1, stride=-1, padding=-1, groups=1, channel_shuffle=0, shuffle_groups=1, abits=8, wbits=8, first_layer=0):
        super(QuantConvBNReLU, self).__init__()
        self.channel_shuffle_flag = channel_shuffle
        self.shuffle_groups = shuffle_groups 

        self.quant_conv = QuantConv2d(input_channels, output_channels,
                kernel_size=kernel_size, stride=stride, padding=padding, groups=groups, a_bits=abits, w_bits=wbits, first_layer=first_layer)
        self.bn = nn.BatchNorm2d(output_channels)
        self.relu = nn.ReLU(inplace=True)
    def __init__(self,
                 input_channels,
                 output_channels,
                 kernel_size=-1,
                 stride=-1,
                 padding=-1,
                 groups=1,
                 channel_shuffle=0,
                 shuffle_groups=1,
                 abits=8,
                 wbits=8,
                 bn_fuse=0,
                 q_type=1,
                 q_level=0,
                 first_layer=0):
        super(QuantConvBNReLU, self).__init__()
        self.channel_shuffle_flag = channel_shuffle
        self.shuffle_groups = shuffle_groups
        self.bn_fuse = bn_fuse

        if self.bn_fuse == 1:
            self.quant_bn_fuse_conv = QuantBNFuseConv2d(
                input_channels,
                output_channels,
                kernel_size=kernel_size,
                stride=stride,
                padding=padding,
                groups=groups,
                a_bits=abits,
                w_bits=wbits,
                q_type=q_type,
                q_level=q_level,
                first_layer=first_layer)
        else:
            self.quant_conv = QuantConv2d(input_channels,
                                          output_channels,
                                          kernel_size=kernel_size,
                                          stride=stride,
                                          padding=padding,
                                          groups=groups,
                                          a_bits=abits,
                                          w_bits=wbits,
                                          q_type=q_type,
                                          q_level=q_level,
                                          first_layer=first_layer)
            self.bn = nn.BatchNorm2d(
                output_channels, momentum=0.01
            )  # 考虑量化带来的抖动影响,对momentum进行调整(0.1 ——> 0.01),削弱batch统计参数占比,一定程度抑制抖动。经实验量化训练效果更好,acc提升1%左右
        self.relu = QuantReLU(inplace=True, a_bits=abits, q_type=q_type)