Esempio n. 1
0
    def _make_layer(self,
                    block,
                    planes,
                    blocks,
                    mask_init,
                    mask_scale,
                    threshold_fn,
                    stride=1):
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                nl.ElementWiseConv2d(self.inplanes,
                                     planes * block.expansion,
                                     kernel_size=1,
                                     stride=stride,
                                     bias=False,
                                     mask_init=mask_init,
                                     mask_scale=mask_scale,
                                     threshold_fn=threshold_fn),
                nn.BatchNorm2d(planes * block.expansion),
            )

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

        return nn.Sequential(*layers)
Esempio n. 2
0
    def __init__(self,
                 block,
                 layers,
                 mask_init,
                 mask_scale,
                 threshold_fn,
                 num_classes=1000):
        self.inplanes = 64
        super(ResNet, self).__init__()
        self.conv1 = nl.ElementWiseConv2d(3,
                                          64,
                                          kernel_size=7,
                                          stride=2,
                                          padding=3,
                                          bias=False,
                                          mask_init=mask_init,
                                          mask_scale=mask_scale,
                                          threshold_fn=threshold_fn)
        self.bn1 = nn.BatchNorm2d(64)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(block, 64, layers[0], mask_init,
                                       mask_scale, threshold_fn)
        self.layer2 = self._make_layer(block,
                                       128,
                                       layers[1],
                                       mask_init,
                                       mask_scale,
                                       threshold_fn,
                                       stride=2)
        self.layer3 = self._make_layer(block,
                                       256,
                                       layers[2],
                                       mask_init,
                                       mask_scale,
                                       threshold_fn,
                                       stride=2)
        self.layer4 = self._make_layer(block,
                                       512,
                                       layers[3],
                                       mask_init,
                                       mask_scale,
                                       threshold_fn,
                                       stride=2)
        self.avgpool = nn.AvgPool2d(7, stride=1)
        self.fc = nn.Linear(512 * block.expansion, num_classes)

        for m in self.modules():
            if isinstance(m, nl.ElementWiseConv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()
Esempio n. 3
0
 def __init__(self,
              inplanes,
              planes,
              mask_init,
              mask_scale,
              threshold_fn,
              stride=1,
              downsample=None):
     super(Bottleneck, self).__init__()
     self.conv1 = nl.ElementWiseConv2d(inplanes,
                                       planes,
                                       kernel_size=1,
                                       bias=False,
                                       mask_init=mask_init,
                                       mask_scale=mask_scale,
                                       threshold_fn=threshold_fn)
     self.bn1 = nn.BatchNorm2d(planes)
     self.conv2 = nl.ElementWiseConv2d(planes,
                                       planes,
                                       kernel_size=3,
                                       stride=stride,
                                       padding=1,
                                       bias=False,
                                       mask_init=mask_init,
                                       mask_scale=mask_scale,
                                       threshold_fn=threshold_fn)
     self.bn2 = nn.BatchNorm2d(planes)
     self.conv3 = nl.ElementWiseConv2d(planes,
                                       planes * 4,
                                       kernel_size=1,
                                       bias=False,
                                       mask_init=mask_init,
                                       mask_scale=mask_scale,
                                       threshold_fn=threshold_fn)
     self.bn3 = nn.BatchNorm2d(planes * 4)
     self.relu = nn.ReLU(inplace=True)
     self.downsample = downsample
     self.stride = stride
Esempio n. 4
0
def conv3x3(in_planes,
            out_planes,
            mask_init,
            mask_scale,
            threshold_fn,
            stride=1):
    "3x3 convolution with padding"
    return nl.ElementWiseConv2d(in_planes,
                                out_planes,
                                kernel_size=3,
                                stride=stride,
                                padding=1,
                                bias=False,
                                mask_init=mask_init,
                                mask_scale=mask_scale,
                                threshold_fn=threshold_fn)
Esempio n. 5
0
def make_layers(cfg, mask_init, mask_scale, threshold_fn, batch_norm=False):
    layers = []
    in_channels = 3
    for v in cfg:
        if v == 'M':
            layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
        else:
            conv2d = nl.ElementWiseConv2d(
                in_channels, v, kernel_size=3, padding=1,
                mask_init=mask_init, mask_scale=mask_scale,
                threshold_fn=threshold_fn)
            if batch_norm:
                layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)]
            else:
                layers += [conv2d, nn.ReLU(inplace=True)]
            in_channels = v
    return nn.Sequential(*layers)