Ejemplo n.º 1
0
    def __init__(self, block, layers, num_classes=1000):
        self.inplanes = 64
        super(ResNet, self).__init__()
        self.conv1 = custom.Con2d_Class(3,
                                        64,
                                        kernel_size=7,
                                        stride=2,
                                        padding=3,
                                        bias=False)
        self.bn1 = custom.BN_Class(64)
        self.relu = nn.ReLU(inplace=False)
        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)
        self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
        self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
        self.avgpool = nn.AvgPool2d(7, stride=1)
        self.classifier = custom.Linear_Class(512 * block.expansion,
                                              num_classes)

        for m in self.modules():
            if isinstance(m, custom.Con2d_Class):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, custom.BN_Class):
                m.weight.data.fill_(1)
                m.bias.data.zero_()
Ejemplo n.º 2
0
    def __init__(self,
                 depth=22,
                 block_name='BasicBlock',
                 dropRate=0,
                 num_classes=10,
                 growthRate=12,
                 compressionRate=2):
        super(DenseNet, self).__init__()

        # Model type specifies number of layers for CIFAR-10 model
        if block_name.lower() == 'basicblock':
            assert (
                depth - 4
            ) % 3 == 0, 'When use basicblock, depth should be 3n+4, e.g. 40, 100, 190, 250'
            n = (depth - 4) // 3
            block = BasicBlock
        elif block_name.lower() == 'bottleneck':
            assert (
                depth - 4
            ) % 6 == 0, 'When use bottleneck, depth should be 6n+4, e.g. 40, 100, 190, 250'
            n = (depth - 4) // 6
            block = Bottleneck
        else:
            raise ValueError('block_name shoule be Basicblock or Bottleneck')

        self.growthRate = growthRate
        self.dropRate = dropRate

        # self.inplanes is a global variable used across multiple
        # helper functions
        self.inplanes = growthRate * 2
        self.conv1 = custom.Con2d_Class(3,
                                        self.inplanes,
                                        kernel_size=3,
                                        padding=1,
                                        bias=False)
        self.dense1 = self._make_denseblock(block, n)
        self.trans1 = self._make_transition(compressionRate)
        self.dense2 = self._make_denseblock(block, n)
        self.trans2 = self._make_transition(compressionRate)
        self.dense3 = self._make_denseblock(block, n)
        self.bn = nn.BatchNorm2d(self.inplanes)
        self.relu = nn.ReLU(inplace=True)
        self.avgpool = nn.AvgPool2d(8)
        self.classifier = custom.Linear_Class(self.inplanes, num_classes)

        # Weight initialization
        for m in self.modules():
            if isinstance(m, custom.Linear_Class) or isinstance(
                    m, custom.Con2d_Class):
                init.kaiming_normal_(m.weight)
                if m.bias is not None:
                    m.bias.data.zero_()
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                if m.bias is not None:
                    m.bias.data.zero_()
Ejemplo n.º 3
0
    def __init__(self,
                 cardinality,
                 depth,
                 num_classes,
                 widen_factor=4,
                 dropRate=0):
        """ Constructor
        Args:
            cardinality: number of convolution groups.
            depth: number of layers.
            num_classes: number of classes
            widen_factor: factor to adjust the channel dimensionality
        """
        super(CifarResNeXt, self).__init__()
        self.cardinality = cardinality
        self.depth = depth
        self.block_depth = (self.depth - 2) // 9
        self.widen_factor = widen_factor
        self.num_classes = num_classes
        self.output_size = 64
        self.stages = [
            64, 64 * self.widen_factor, 128 * self.widen_factor,
            256 * self.widen_factor
        ]

        self.conv_1_3x3 = custom.Con2d_Class(3, 64, 3, 1, 1, bias=False)
        self.bn_1 = nn.BatchNorm2d(64)
        self.stage_1 = self.block('stage_1', self.stages[0], self.stages[1], 1)
        self.stage_2 = self.block('stage_2', self.stages[1], self.stages[2], 2)
        self.stage_3 = self.block('stage_3', self.stages[2], self.stages[3], 2)
        self.classifier = custom.Linear_Class(1024, num_classes)
        init.kaiming_normal(self.classifier.weight)

        for m in self.modules():
            if isinstance(m, custom.Linear_Class) or isinstance(
                    m, custom.Con2d_Class):
                init.kaiming_normal_(m.weight)
                if m.bias is not None:
                    m.bias.data.zero_()
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                if m.bias is not None:
                    m.bias.data.zero_()
Ejemplo n.º 4
0
    def __init__(self, depth, num_classes=1000, block_name='BasicBlock'):
        super(ResNet, self).__init__()
        # Model type specifies number of layers for CIFAR-10 model
        if block_name.lower() == 'basicblock':
            assert (
                depth - 2
            ) % 6 == 0, 'When use basicblock, depth should be 6n+2, e.g. 20, 32, 44, 56, 110, 1202'
            n = (depth - 2) // 6
            block = BasicBlock
        elif block_name.lower() == 'bottleneck':
            assert (
                depth - 2
            ) % 9 == 0, 'When use bottleneck, depth should be 9n+2, e.g. 20, 29, 47, 56, 110, 1199'
            n = (depth - 2) // 9
            block = Bottleneck
        else:
            raise ValueError('block_name shoule be Basicblock or Bottleneck')

        self.inplanes = 16
        self.conv1 = custom.Con2d_Class(3,
                                        16,
                                        kernel_size=3,
                                        padding=1,
                                        bias=False)
        self.bn1 = custom.BN_Class(16)
        self.relu = nn.ReLU(inplace=False)
        self.layer1 = self._make_layer(block, 16, n)
        self.layer2 = self._make_layer(block, 32, n, stride=2)
        self.layer3 = self._make_layer(block, 64, n, stride=2)
        self.avgpool = nn.AvgPool2d(8)
        self.classifier = custom.Linear_Class(64 * block.expansion,
                                              num_classes)

        for m in self.modules():
            if isinstance(m, custom.Linear_Class) or isinstance(
                    m, custom.Con2d_Class):
                init.kaiming_normal_(m.weight)
                if m.bias is not None:
                    m.bias.data.zero_()
            elif isinstance(m, custom.BN_Class):
                m.weight.data.fill_(1)
                if m.bias is not None:
                    m.bias.data.zero_()
Ejemplo n.º 5
0
    def __init__(self, batch_norm=False, bias=True, num_classes=1000):
        super(CnX, self).__init__()

        self.layer1 = Layer(3, 32, batch_norm=batch_norm)
        self.layer2 = Layer(32, 32, batch_norm=batch_norm)
        self.layer3 = Layer(32, 32, batch_norm=batch_norm)

        self.avgpool = nn.AvgPool2d(8)
        self.classifier = custom.Linear_Class(512, num_classes, bias=bias)

        for m in self.modules():
            if isinstance(m, custom.Linear_Class) or isinstance(
                    m, custom.Con2d_Class):
                init.kaiming_normal_(m.weight)
                if m.bias is not None:
                    m.bias.data.zero_()
            elif isinstance(m, custom.BN_Class):
                m.weight.data.fill_(1)
                if m.bias is not None:
                    m.bias.data.zero_()
Ejemplo n.º 6
0
    def __init__(self, depth, num_classes, widen_factor=1, dropRate=0.0):
        super(WideResNet, self).__init__()
        nChannels = [
            16, 16 * widen_factor, 32 * widen_factor, 64 * widen_factor
        ]
        assert (depth - 4) % 6 == 0, 'depth should be 6n+4'
        n = (depth - 4) // 6
        block = BasicBlock
        # 1st conv before any network block
        self.conv1 = custom.Con2d_Class(3,
                                        nChannels[0],
                                        kernel_size=3,
                                        stride=1,
                                        padding=1,
                                        bias=False)
        # 1st block
        self.block1 = NetworkBlock(n, nChannels[0], nChannels[1], block, 1,
                                   dropRate)
        # 2nd block
        self.block2 = NetworkBlock(n, nChannels[1], nChannels[2], block, 2,
                                   dropRate)
        # 3rd block
        self.block3 = NetworkBlock(n, nChannels[2], nChannels[3], block, 2,
                                   dropRate)
        # global average pooling and classifier
        self.bn1 = custom.BN_Class(nChannels[3])
        self.relu = nn.ReLU(inplace=True)
        self.classifier = custom.Linear_Class(nChannels[3], num_classes)
        self.nChannels = nChannels[3]

        for m in self.modules():
            if isinstance(m, custom.Con2d_Class):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, custom.BN_Class):
                m.weight.data.fill_(1)
                m.bias.data.zero_()
            elif isinstance(m, custom.Linear_Class):
                if m.bias is not None:
                    m.bias.data.zero_()
Ejemplo n.º 7
0
 def __init__(self, features, num_classes=1000):
     super(VGG, self).__init__()
     self.features = features
     self.classifier = custom.Linear_Class(512, num_classes)
     self._initialize_weights()