Пример #1
0
    def __init__(self, in_planes, planes, bits, stride=1):
        super(QuantBottleneck, self).__init__()
        first_bit = bits.pop(0)
        self.conv1 = DSConv2d(in_planes,
                              planes,
                              1,
                              32,
                              bit=first_bit,
                              bias=False)
        self.bn1 = nn.BatchNorm2d(planes)
        self.activation1 = BFPActivation(bits[0], 32)
        self.conv2 = DSConv2d(
            planes,
            planes,
            kernel_size=3,
            block_size=32,
            bit=bits.pop(0),
            stride=stride,
            padding=1,
            bias=False,
        )
        self.bn2 = nn.BatchNorm2d(planes)
        self.activation2 = BFPActivation(bits[0], 32)
        self.conv3 = DSConv2d(
            planes,
            self.expansion * planes,
            kernel_size=1,
            block_size=32,
            bit=bits.pop(0),
            bias=False,
        )
        self.bn3 = nn.BatchNorm2d(self.expansion * planes)
        self.activation3 = (BFPActivation(bits[0], 32)
                            if len(bits) > 1 else nn.Identity())

        self.shortcut = nn.Sequential()
        if stride != 1 or in_planes != self.expansion * planes:
            self.shortcut = nn.Sequential(
                DSConv2d(
                    in_planes,
                    self.expansion * planes,
                    1,
                    block_size=32,
                    bit=first_bit,
                    stride=stride,
                    bias=False,
                ),
                nn.BatchNorm2d(self.expansion * planes),
            )
Пример #2
0
    def __init__(self, in_planes, planes, bits, stride=1):
        super(QuantBasicBlock, self).__init__()
        first_bit = bits.pop(0)
        self.conv1 = DSConv2d(
            in_planes,
            planes,
            3,
            block_size=32,
            bit=first_bit,
            stride=stride,
            padding=1,
            bias=False,
        )
        self.bn1 = nn.BatchNorm2d(planes)
        self.activation1 = BFPActivation(bits[0], 32)
        self.conv2 = DSConv2d(
            planes,
            planes,
            kernel_size=3,
            block_size=32,
            bit=bits.pop(0),
            stride=1,
            padding=1,
            bias=False,
        )
        self.bn2 = nn.BatchNorm2d(planes)
        # This is just because the last layer doesn't need to quantize the activation
        self.activation2 = (BFPActivation(bits[0], 32)
                            if len(bits) > 0 else nn.Identity())

        self.shortcut = nn.Sequential()
        if stride != 1 or in_planes != self.expansion * planes:
            self.shortcut = nn.Sequential(
                DSConv2d(
                    in_planes,
                    self.expansion * planes,
                    1,
                    block_size=32,
                    bit=first_bit,
                    stride=stride,
                    bias=False,
                ),
                nn.BatchNorm2d(self.expansion * planes),
            )
Пример #3
0
def conv1x1(in_planes, out_planes, block_size, bit, stride=1):
    """1x1 convolution"""
    return DSConv2d(
        in_planes,
        out_planes,
        kernel_size=1,
        block_size=block_size,
        bit=bit,
        stride=stride,
        bias=False,
    )
Пример #4
0
    def __init__(self, bits):
        super(CNNX, self).__init__(bits, self.number_bits)
        self.block_size = 32

        bit = self.bits.pop(0)
        self.conv1 = DSConv2d(3, 64, (3, 3), block_size=32, bit=bit, padding=1)
        self.bn1 = torch.nn.BatchNorm2d(64)

        self.features1, outch = self.__make_layers__(64, 3)
        self.max_pool1 = torch.nn.MaxPool2d(2, stride=2)

        self.features2, outch = self.__make_layers__(outch, 3)
        self.max_pool2 = torch.nn.MaxPool2d(2, stride=2)

        self.features3, outch = self.__make_layers__(outch, 3)
        self.avg_pool = torch.nn.AvgPool2d(8)

        self.linear = torch.nn.Linear(outch, 10)
Пример #5
0
def conv3x3(in_planes,
            out_planes,
            block_size,
            bit,
            stride=1,
            groups=1,
            dilation=1):
    """3x3 convolution with padding"""
    return DSConv2d(
        in_planes,
        out_planes,
        kernel_size=3,
        block_size=block_size,
        bit=bit,
        stride=stride,
        padding=dilation,
        groups=groups,
        bias=False,
        dilation=dilation,
    )
Пример #6
0
    def __init__(self, block, num_blocks, bits, num_classes=10):
        super(QUANTIZED_ResNet, self).__init__()
        self.in_planes = 64

        _bits_ = bits.copy()

        self.conv1 = DSConv2d(
            3,
            64,
            kernel_size=3,
            block_size=32,
            bit=_bits_.pop(0),
            stride=1,
            padding=1,
            bias=False,
        )
        self.activation = BFPActivation(_bits_[0], 32)
        self.bn1 = nn.BatchNorm2d(64)
        self.layer1 = self._make_layer(block,
                                       64,
                                       num_blocks[0],
                                       stride=1,
                                       bits=_bits_)
        self.layer2 = self._make_layer(block,
                                       128,
                                       num_blocks[1],
                                       stride=2,
                                       bits=_bits_)
        self.layer3 = self._make_layer(block,
                                       256,
                                       num_blocks[2],
                                       stride=2,
                                       bits=_bits_)
        self.layer4 = self._make_layer(block,
                                       512,
                                       num_blocks[3],
                                       stride=2,
                                       bits=_bits_)
        self.linear = nn.Linear(512 * block.expansion, num_classes)
Пример #7
0
 def __init__(self, in_planes, out_planes, kernel, block_size, bit, **kwargs):
     super(BaseConv, self).__init__()
     self.activation = BFPActivation(bit, 7, block_size)
     self.conv = DSConv2d(in_planes, out_planes, kernel, block_size, bit, **kwargs)
     self.bn = torch.nn.BatchNorm2d(out_planes)
Пример #8
0
    def __init__(
        self,
        block,
        layers,
        bits,
        number_bits,
        num_classes=1000,
        zero_init_residual=False,
        groups=1,
        width_per_group=64,
        replace_stride_with_dilation=None,
        norm_layer=None,
    ):
        super(ResNet, self).__init__(bits, number_bits)
        if norm_layer is None:
            norm_layer = nn.BatchNorm2d
        self._norm_layer = norm_layer

        self.inplanes = 64
        self.dilation = 1
        if replace_stride_with_dilation is None:
            # each element in the tuple indicates if we should replace
            # the 2x2 stride with a dilated convolution instead
            replace_stride_with_dilation = [False, False, False]
        if len(replace_stride_with_dilation) != 3:
            raise ValueError("replace_stride_with_dilation should be None "
                             "or a 3-element tuple, got {}".format(
                                 replace_stride_with_dilation))
        self.groups = groups
        self.base_width = width_per_group
        bit = self.bits.pop(0)
        self.conv1 = DSConv2d(
            3,
            self.inplanes,
            kernel_size=7,
            block_size=32,
            bit=bit,
            stride=2,
            padding=3,
            bias=False,
        )
        self.bn1 = norm_layer(self.inplanes)
        self.relu = nn.ReLU(inplace=True)
        bit = self.bits[0]
        self.activation1 = BFPActivation(bit, 32)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(block, 64, layers[0])
        self.layer2 = self._make_layer(block,
                                       128,
                                       layers[1],
                                       stride=2,
                                       dilate=replace_stride_with_dilation[0])
        self.layer3 = self._make_layer(block,
                                       256,
                                       layers[2],
                                       stride=2,
                                       dilate=replace_stride_with_dilation[1])
        self.layer4 = self._make_layer(
            block,
            512,
            layers[3],
            stride=2,
            dilate=replace_stride_with_dilation[2],
            final=True,
        )
        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.fc = nn.Linear(512 * block.expansion, num_classes)

        for mod in self.modules():
            if isinstance(mod, DSConv2d):
                nn.init.kaiming_normal_(mod.weight,
                                        mode="fan_out",
                                        nonlinearity="relu")
            elif isinstance(mod, (nn.BatchNorm2d, nn.GroupNorm)):
                nn.init.constant_(mod.weight, 1)
                nn.init.constant_(mod.bias, 0)

        # Zero-initialize the last BN in each residual branch,
        # so that the residual branch starts with zeros,
        # and each residual block behaves like an identity.
        # This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677
        if zero_init_residual:
            for mod in self.modules():
                if isinstance(mod, Bottleneck):
                    nn.init.constant_(mod.bn3.weight, 0)
                elif isinstance(mod, BasicBlock):
                    nn.init.constant_(mod.bn2.weight, 0)