示例#1
0
    def __init__(self, block, layers, num_classes):
        super(ResNet, self).__init__()
        self.inplanes = 16

        # self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1, bias=False)
        self.conv1 = SEConv2d(3,
                              16,
                              kernel_size=3,
                              stride=1,
                              padding=1,
                              bias=False)
        self.bn1 = nn.BatchNorm2d(16)
        self.relu = nn.ReLU(inplace=True)
        self.layer1 = self._make_layer(block, 16, layers[0])
        self.layer2 = self._make_layer(block, 32, layers[1], stride=2)
        self.layer3 = self._make_layer(block, 64, layers[2], stride=2)
        self.avgpool = nn.MaxPool2d(8, stride=1)
        # use conv as fc layer (addernet)
        # self.fc = nn.Conv2d(64 * block.expansion, num_classes, 1, bias=False)
        self.fc = SEConv2d(64 * block.expansion, num_classes, 1, bias=False)
        self.bn2 = nn.BatchNorm2d(num_classes)

        for m in self.modules():
            if isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()
    def __init__(self, block, layers, num_classes, threshold, sign_threshold, distribution, quantize=False, weight_bits=8, sparsity=0):
        super(ResNet, self).__init__()
        self.inplanes = 16
        self.quantize = quantize
        self.threshold = threshold
        self.sign_threshold = sign_threshold
        self.distribution = distribution
        self.weight_bits = weight_bits
        self.sparsity = sparsity
        # self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1, bias=False)
        self.conv1 = SEConv2d(3, 16, kernel_size=3, stride=1, padding=1, bias=False, threshold=threshold, sign_threshold=sign_threshold)
        self.bn1 = nn.BatchNorm2d(16)
        self.relu = nn.ReLU(inplace=True)
        self.layer1 = self._make_layer(block, 16, layers[0])
        self.layer2 = self._make_layer(block, 32, layers[1], stride=2)
        self.layer3 = self._make_layer(block, 64, layers[2], stride=2)
        self.avgpool = nn.AvgPool2d(8, stride=1)
        # use conv as fc layer (addernet)
        # self.fc = nn.Conv2d(64 * block.expansion, num_classes, 1, bias=False)
        self.fc = SEConv2d(64 * block.expansion, num_classes, 1, bias=False, threshold=threshold, sign_threshold=sign_threshold)
        self.bn2 = nn.BatchNorm2d(num_classes)

        for m in self.modules():
            if isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()
def conv1x1(in_planes,
            out_planes,
            threshold,
            sign_threshold,
            distribution,
            stride=1,
            quantize=False,
            weight_bits=8,
            sparsity=0):
    """1x1 convolution"""
    shift = SEConv2d(in_planes,
                     out_planes,
                     kernel_size=1,
                     stride=stride,
                     bias=False,
                     threshold=threshold,
                     sign_threshold=sign_threshold,
                     distribution=distribution)
    add = adder.Adder2D(out_planes,
                        out_planes,
                        kernel_size=1,
                        stride=1,
                        bias=False,
                        quantize=quantize,
                        weight_bits=weight_bits,
                        sparsity=sparsity)
    return nn.Sequential(shift, add)
示例#4
0
    def _make_layer(self, block, planes, blocks, stride=1):
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                SEConv2d(self.inplanes,
                         planes * block.expansion,
                         kernel_size=1,
                         stride=stride,
                         bias=False),  # shift
                Adder2D(planes * block.expansion,
                        planes * block.expansion,
                        kernel_size=1,
                        stride=1,
                        bias=False),  # add
                nn.BatchNorm2d(planes * block.expansion))

        layers = []
        layers.append(
            block(inplanes=self.inplanes,
                  planes=planes,
                  stride=stride,
                  downsample=downsample))
        self.inplanes = planes * block.expansion
        for _ in range(1, blocks):
            layers.append(block(inplanes=self.inplanes, planes=planes))

        return nn.Sequential(*layers)
示例#5
0
def conv_add(in_planes,
             out_planes,
             threshold,
             sign_threshold,
             distribution,
             kernel_size=(3, 3),
             stride=1,
             padding=0,
             quantize=False,
             weight_bits=8,
             sparsity=0):
    " 3x3 convolution with padding "
    shift = SEConv2d(in_planes,
                     out_planes,
                     kernel_size=kernel_size,
                     stride=stride,
                     padding=padding,
                     bias=False,
                     threshold=threshold,
                     sign_threshold=sign_threshold,
                     distribution=distribution)
    add = adder.Adder2D(out_planes,
                        out_planes,
                        kernel_size=(1, 1),
                        stride=1,
                        padding=padding,
                        bias=False,
                        quantize=quantize,
                        weight_bits=weight_bits,
                        sparsity=sparsity)
    return nn.Sequential(shift, add)
示例#6
0
def conv3x3(in_planes, out_planes, stride=1):
    " 3x3 convolution with padding "
    shift = SEConv2d(in_planes,
                     out_planes,
                     kernel_size=3,
                     stride=stride,
                     padding=1,
                     bias=False)
    add = Adder2D(out_planes,
                  out_planes,
                  kernel_size=3,
                  stride=1,
                  padding=1,
                  bias=False)
    return nn.Sequential(shift, add)
    def _make_layer(self, block, planes, blocks, stride=1):
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                SEConv2d(self.inplanes,
                         planes * block.expansion,
                         kernel_size=1,
                         stride=stride,
                         bias=False,
                         threshold=self.threshold,
                         sign_threshold=self.sign_threshold,
                         distribution=self.distribution),  # shift
                adder.Adder2D(planes * block.expansion,
                              planes * block.expansion,
                              kernel_size=1,
                              stride=1,
                              bias=False,
                              quantize=self.quantize,
                              weight_bits=self.weight_bits,
                              sparsity=self.sparsity),  # add
                nn.BatchNorm2d(planes * block.expansion))

        layers = []
        layers.append(
            block(inplanes=self.inplanes,
                  planes=planes,
                  threshold=self.threshold,
                  sign_threshold=self.sign_threshold,
                  distribution=self.distribution,
                  stride=stride,
                  downsample=downsample,
                  quantize=self.quantize,
                  weight_bits=self.weight_bits,
                  sparsity=self.sparsity))
        self.inplanes = planes * block.expansion
        for _ in range(1, blocks):
            layers.append(
                block(inplanes=self.inplanes,
                      planes=planes,
                      threshold=self.threshold,
                      sign_threshold=self.sign_threshold,
                      distribution=self.distribution,
                      quantize=self.quantize,
                      weight_bits=self.weight_bits,
                      sparsity=self.sparsity))

        return nn.Sequential(*layers)
def conv3x3(in_planes, out_planes, threshold, stride=1):
    " 3x3 convolution with padding "
    shift = SEConv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False, threshold=threshold)
    # add = adder.adder2d(out_planes, out_planes, kernel_size=3, stride=1, padding=1, bias=False)
    # return nn.Sequential(shift, add)
    return shift