Exemplo n.º 1
0
 def get_active_subnet(self, in_channel, preserve_weight=True):
     sub_layer = ConvLayer(
         in_channel, self.active_out_channel, self.kernel_size, self.stride, self.dilation,
         use_bn=self.use_bn, act_func=self.act_func
     )
     sub_layer = sub_layer.to(get_net_device(self))
     
     if not preserve_weight:
         return sub_layer
     
     sub_layer.conv.weight.data.copy_(self.conv.conv.weight.data[:self.active_out_channel, :in_channel, :, :])
     if self.use_bn:
         copy_bn(sub_layer.bn, self.bn.bn)
     
     return sub_layer
Exemplo n.º 2
0
    def get_active_subnet(self, in_channel, preserve_weight=True):
        middle_channel = make_divisible(
            round(in_channel * self.active_expand_ratio), 8)

        # build the new layer
        sub_layer = MBInvertedConvLayer(
            in_channel,
            self.active_out_channel,
            self.active_kernel_size,
            self.stride,
            self.active_expand_ratio,
            act_func=self.act_func,
            mid_channels=middle_channel,
            use_se=self.use_se,
        )
        sub_layer = sub_layer.to(get_net_device(self))

        if not preserve_weight:
            return sub_layer

        # copy weight from current layer
        if sub_layer.inverted_bottleneck is not None:
            sub_layer.inverted_bottleneck.conv.weight.data.copy_(
                self.inverted_bottleneck.conv.conv.weight.
                data[:middle_channel, :in_channel, :, :])
            copy_bn(sub_layer.inverted_bottleneck.bn,
                    self.inverted_bottleneck.bn.bn)

        sub_layer.depth_conv.conv.weight.data.copy_(
            self.depth_conv.conv.get_active_filter(
                middle_channel, self.active_kernel_size).data)
        copy_bn(sub_layer.depth_conv.bn, self.depth_conv.bn.bn)

        if self.use_se:
            se_mid = make_divisible(middle_channel // SEModule.REDUCTION,
                                    divisor=8)
            sub_layer.depth_conv.se.fc.reduce.weight.data.copy_(
                self.depth_conv.se.fc.reduce.weight.
                data[:se_mid, :middle_channel, :, :])
            sub_layer.depth_conv.se.fc.reduce.bias.data.copy_(
                self.depth_conv.se.fc.reduce.bias.data[:se_mid])

            sub_layer.depth_conv.se.fc.expand.weight.data.copy_(
                self.depth_conv.se.fc.expand.weight.
                data[:middle_channel, :se_mid, :, :])
            sub_layer.depth_conv.se.fc.expand.bias.data.copy_(
                self.depth_conv.se.fc.expand.bias.data[:middle_channel])

        sub_layer.point_linear.conv.weight.data.copy_(
            self.point_linear.conv.conv.weight.
            data[:self.active_out_channel, :middle_channel, :, :])
        copy_bn(sub_layer.point_linear.bn, self.point_linear.bn.bn)

        return sub_layer
Exemplo n.º 3
0
    def get_active_subnet(self, in_channel, preserve_weight=True):
        middle_channel = make_divisible(
            round(in_channel * self.active_expand_ratio), 8)

        # build the new layer
        sub_layer = MBInvertedQConvLayer(
            in_channel,
            self.active_out_channel,
            self.active_kernel_size,
            self.stride,
            self.active_expand_ratio,
            act_func=self.act_func,
            mid_channels=middle_channel,
            pw_w_bit=self.point_linear.conv.w_bit,
            pw_a_bit=self.point_linear.conv.a_bit,
            dw_w_bit=self.depth_conv.conv.w_bit,
            dw_a_bit=self.depth_conv.conv.a_bit,
        )
        sub_layer = sub_layer.to(get_net_device(self))

        if not preserve_weight:
            return sub_layer

        # copy weight from current layer
        if sub_layer.inverted_bottleneck is not None:
            sub_layer.inverted_bottleneck.conv.weight.data.copy_(
                self.inverted_bottleneck.conv.conv.weight.
                data[:middle_channel, :in_channel, :, :])
            copy_bn(sub_layer.inverted_bottleneck.bn,
                    self.inverted_bottleneck.bn.bn)

        sub_layer.depth_conv.conv.weight.data.copy_(
            self.depth_conv.conv.get_active_filter(
                middle_channel, self.active_kernel_size).data)
        copy_bn(sub_layer.depth_conv.bn, self.depth_conv.bn.bn)

        sub_layer.point_linear.conv.weight.data.copy_(
            self.point_linear.conv.conv.weight.
            data[:self.active_out_channel, :middle_channel, :, :])
        copy_bn(sub_layer.point_linear.bn, self.point_linear.bn.bn)

        return sub_layer