예제 #1
0
 def __init__(self, in_ch=3, num_classes=1000):
     '''
         The AlexNet.
         args:
             in_ch: int, the number of channels of inputs
             num_classes: int, the number of classes that need to predict
         reference:
             "One weird trick for parallelizing convolutional neural networks"<https://arxiv.org/abs/1404.5997>
     '''
     super(AlexNet, self).__init__()
     #the part to extract feature
     self.features = M.Sequential(
         M.Conv2d(in_ch, 64, kernel_size=11, stride=4, padding=11 // 4),
         M.ReLU(),
         M.MaxPool2d(kernel_size=3, stride=2),
         M.Conv2d(64, 192, kernel_size=5, padding=2),
         M.ReLU(),
         M.MaxPool2d(kernel_size=3, stride=2),
         M.Conv2d(192, 384, kernel_size=3, stride=1, padding=1),
         M.ReLU(),
         M.Conv2d(384, 256, kernel_size=3, stride=1, padding=1),
         M.ReLU(),
         M.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
         M.ReLU(),
         M.MaxPool2d(kernel_size=3, stride=2),
     )
     #global avg pooling
     self.avgpool = M.AdaptiveAvgPool2d((6, 6))
     #classify part
     self.classifier = M.Sequential(M.Dropout(),
                                    M.Linear(256 * 6 * 6, 4096), M.ReLU(),
                                    M.Dropout(), M.Linear(4096, 4096),
                                    M.ReLU(), M.Linear(4096, num_classes))
예제 #2
0
    def __init__(self, cfg):
        super().__init__()

        self.cfg = cfg
        self.output_stride = 16
        self.sub_output_stride = self.output_stride // 4
        self.num_classes = cfg.num_classes

        self.aspp = ASPP(in_channels=2048,
                         out_channels=256,
                         dr=16 // self.output_stride)
        self.dropout = M.Dropout(0.5)

        self.upstage1 = M.Sequential(
            M.Conv2d(256, 48, 1, 1, padding=1 // 2, bias=False),
            M.BatchNorm2d(48),
            M.ReLU(),
        )

        self.upstage2 = M.Sequential(
            M.Conv2d(256 + 48, 256, 3, 1, padding=1, bias=False),
            M.BatchNorm2d(256),
            M.ReLU(),
            M.Dropout(0.5),
            M.Conv2d(256, 256, 3, 1, padding=1, bias=False),
            M.BatchNorm2d(256),
            M.ReLU(),
            M.Dropout(0.1),
        )
        self.conv_out = M.Conv2d(256, self.num_classes, 1, 1, padding=0)

        for m in self.modules():
            if isinstance(m, M.Conv2d):
                M.init.msra_normal_(m.weight,
                                    mode="fan_out",
                                    nonlinearity="relu")
            elif isinstance(m, M.BatchNorm2d):
                M.init.ones_(m.weight)
                M.init.zeros_(m.bias)

        self.backbone = getattr(resnet, cfg.backbone)(
            replace_stride_with_dilation=[False, False, True],
            pretrained=cfg.backbone_pretrained,
        )
        del self.backbone.fc
예제 #3
0
    def __init__(self, class_num=21, pretrained=None):
        super().__init__()

        self.output_stride = 16
        self.sub_output_stride = self.output_stride // 4
        self.class_num = class_num

        self.aspp = ASPP(in_channels=2048,
                         out_channels=256,
                         dr=16 // self.output_stride)
        self.dropout = M.Dropout(0.5)

        self.upstage1 = M.Sequential(
            M.Conv2d(256, 48, 1, 1, padding=1 // 2, bias=True),
            M.BatchNorm2d(48),
            M.ReLU(),
        )

        self.upstage2 = M.Sequential(
            M.Conv2d(256 + 48, 256, 3, 1, padding=1, bias=True),
            M.BatchNorm2d(256),
            M.ReLU(),
            M.Dropout(0.5),
            M.Conv2d(256, 256, 3, 1, padding=1, bias=True),
            M.BatchNorm2d(256),
            M.ReLU(),
            M.Dropout(0.1),
        )
        self.convout = M.Conv2d(256, self.class_num, 1, 1, padding=0)

        for m in self.modules():
            if isinstance(m, M.Conv2d):
                M.init.msra_normal_(m.weight,
                                    mode="fan_out",
                                    nonlinearity="relu")
            elif isinstance(m, M.BatchNorm2d):
                M.init.ones_(m.weight)
                M.init.zeros_(m.bias)

        self.backbone = ModifiedResNet(
            Bottleneck, [3, 4, 23, 3],
            replace_stride_with_dilation=[False, False, True])
        if pretrained is not None:
            model_dict = mge.load(pretrained)
            self.backbone.load_state_dict(model_dict)
예제 #4
0
    def __init__(self,
                 cfg,
                 num_classes=1000,
                 in_channels=3,
                 init_weights=True,
                 batch_norm=False):
        '''
            VGGNet from paper
            "Very Deep Convolutional Networks For Large-Scale Image Recognition"<https://arxiv.org/pdf/1409.1556.pdf>
        '''
        super(VGG, self).__init__()
        self.features = self._make_layers(in_channels, cfg, batch_norm)
        self.avgpool = M.AdaptiveAvgPool2d((7, 7))
        self.classifier = M.Sequential(M.Linear(512 * 7 * 7, 4096), M.ReLU(),
                                       M.Dropout(), M.Linear(4096, 4096),
                                       M.ReLU(), M.Dropout(),
                                       M.Linear(4096, num_classes))

        if init_weights:
            self._init_weights()
예제 #5
0
    def __init__(self, in_channels, out_channels, mid_channels, dropout_rate):
        super(MobileNetV3Classifier, self).__init__()
        self.use_dropout = (dropout_rate != 0.0)

        self.conv1 = conv1x1(in_channels=in_channels,
                             out_channels=mid_channels)
        self.activ = HSwish()
        if self.use_dropout:
            self.dropout = M.Dropout(dropout_rate)
        self.conv2 = conv1x1(in_channels=mid_channels,
                             out_channels=out_channels,
                             bias=True)
예제 #6
0
    def __init__(self,
                 channels,
                 residuals,
                 init_block_kernel_size,
                 init_block_channels,
                 maxpool_pad,
                 in_channels=3,
                 in_size=(224, 224),
                 num_classes=1000):
        super(SqueezeNet, self).__init__()
        self.in_size = in_size
        self.num_classes = num_classes

        self.feature = []
        init_block = SqueezeInitBlock(in_channels=in_channels,
                                      out_channels=init_block_channels,
                                      kernel_size=init_block_kernel_size)
        self.feature.append(init_block)
        in_channels = init_block_channels

        for i, channels_per_stage in enumerate(channels):
            stage = []

            pool = M.MaxPool2d(kernel_size=3, stride=2, padding=maxpool_pad[i])
            stage.append(pool)

            for j, out_channels in enumerate(channels_per_stage):
                expand_channels = out_channels // 2
                squeeze_channels = out_channels // 8
                unit = FireUnit(in_channels=in_channels,
                                squeeze_channels=squeeze_channels,
                                expand1x1_channels=expand_channels,
                                expand3x3_channels=expand_channels,
                                residual=((residuals is not None)
                                          and (residuals[i][j] == 1)))
                stage.append(unit)
                in_channels = out_channels
            self.feature += stage
        self.feature.append(M.Dropout(drop_prob=0.5))
        self.feature = M.Sequential(*self.feature)

        self.output = []
        final_conv = M.Conv2d(in_channels=in_channels,
                              out_channels=num_classes,
                              kernel_size=1)
        self.output.append(final_conv)
        final_activ = M.ReLU()
        self.output.append(final_activ)
        final_pool = M.AvgPool2d(kernel_size=13, stride=1)
        self.output.append(final_pool)
        self.output = M.Sequential(*self.output)
예제 #7
0
파일: head.py 프로젝트: KingsYR123/YR
    def __init__(self, feature_dim, channel, size=7):
        """initialzation

        Args:
            feature_dim (int): dimension number of output embedding
            channel (int): channel number of input feature map
            size (int, optional): size of input feature map. defaults to 7
        """
        super().__init__()
        self.size = size
        self.bn1 = M.BatchNorm2d(channel)
        self.dropout = M.Dropout(drop_prob=0.1)
        self.fc = M.Linear(channel, feature_dim)
        self.bn2 = M.BatchNorm1d(feature_dim, affine=False)
예제 #8
0
    def __init__(self, in_ch, out_ch):
        super(Unet, self).__init__()

        self.conv1 = DoubleConv(in_ch, 32)
        self.conv2 = DoubleConv(32, 64)
        self.conv3 = DoubleConv(64, 128)
        self.conv4 = DoubleConv(128, 256)
        self.conv5 = DoubleConv(256, 512)

        self.pool = M.MaxPool2d(2)
        self.dropout = M.Dropout(0.3)

        self.conv6 = DoubleConv(768, 256)
        self.conv7 = DoubleConv(384, 128)
        self.conv8 = DoubleConv(192, 64)
        self.conv9 = DoubleConv(96, 32)
        self.conv10 = M.Conv2d(32, out_ch, 1)
예제 #9
0
    def __init__(self,
                 num_classes=1000,
                 aux_logits=True,
                 transform_input=False,
                 inception_blocks=None):
        super(Inception3, self).__init__()
        if inception_blocks is None:
            inception_blocks = [
                BasicConv2d, InceptionA, InceptionB, InceptionC, InceptionD,
                InceptionE, InceptionAux
            ]

        assert len(inception_blocks) == 7
        conv_block = inception_blocks[0]
        inception_a = inception_blocks[1]
        inception_b = inception_blocks[2]
        inception_c = inception_blocks[3]
        inception_d = inception_blocks[4]
        inception_e = inception_blocks[5]
        inception_aux = inception_blocks[6]

        self.aux_logits = aux_logits
        self.transform_input = transform_input
        self.Conv2d_1a_3x3 = conv_block(3, 32, kernel_size=3, stride=2)
        self.Conv2d_2a_3x3 = conv_block(32, 32, kernel_size=3)
        self.Conv2d_2b_3x3 = conv_block(32, 64, kernel_size=3, padding=1)
        self.maxpool1 = M.MaxPool2d(kernel_size=3, stride=2)
        self.Conv2d_3b_1x1 = conv_block(64, 80, kernel_size=1)
        self.Conv2d_4a_3x3 = conv_block(80, 192, kernel_size=3)
        self.maxpool2 = M.MaxPool2d(kernel_size=3, stride=2)
        self.Mixed_5b = inception_a(192, pool_features=32)
        self.Mixed_5c = inception_a(256, pool_features=64)
        self.Mixed_5d = inception_a(288, pool_features=64)
        self.Mixed_6a = inception_b(288)
        self.Mixed_6b = inception_c(768, channels_7x7=128)
        self.Mixed_6c = inception_c(768, channels_7x7=160)
        self.Mixed_6d = inception_c(768, channels_7x7=160)
        self.Mixed_6e = inception_c(768, channels_7x7=192)
        if aux_logits:
            self.AuxLogits = inception_aux(768, num_classes)
        self.Mixed_7a = inception_d(768)
        self.Mixed_7b = inception_e(1280)
        self.Mixed_7c = inception_e(2048)
        self.avgpool = M.AvgPool2d(8)
        self.dropout = M.Dropout()
        self.fc = M.Linear(2048, num_classes)
예제 #10
0
def test_M_dropout_static_diff_result():
    m = M.Dropout(0.5)

    @jit.trace(symbolic=True)
    def graph_a(x):
        return m(x)

    @jit.trace(symbolic=True)
    def graph_b(x):
        return m(x)

    x = np.ones(10, dtype="float32")
    a = graph_a(x)
    a = a.numpy().copy()
    b = graph_b(x)
    c = graph_a(x)
    assert np.any(a != b.numpy())
    assert np.any(a != c.numpy())
예제 #11
0
def test_M_dropout_static_same_result():
    m = M.Dropout(0.5)

    @jit.trace(symbolic=True)
    def graph_a(x):
        return m(x)

    @jit.trace(symbolic=True)
    def graph_b(x):
        return m(x)

    x = np.ones(10, dtype="float32")
    R.manual_seed(0)
    a = graph_a(x)
    a = a.numpy().copy()
    R.manual_seed(0)
    b = graph_b(x)
    R.manual_seed(0)  # useless
    c = graph_a(x)
    assert np.all(a == b.numpy())
    assert np.any(a != c.numpy())
예제 #12
0
    def __init__(self,
                 num_classes=1000,
                 aux_logits=True,
                 transform_input=False,
                 init_weights=True):
        super(GoogLeNet, self).__init__()
        self.aux_logits = aux_logits
        self.transform_input = transform_input

        self.conv1 = BasicConv2d(3, 64, kernel_size=7, stride=2, padding=3)
        self.maxpool1 = M.MaxPool2d(3, stride=2, padding=1)  #向上取整
        self.conv2 = BasicConv2d(64, 64, kernel_size=1)
        self.conv3 = BasicConv2d(64, 192, kernel_size=3, padding=1)
        self.maxpool2 = M.MaxPool2d(3, stride=2, padding=1)

        self.inception3a = Inception(192, 64, 96, 128, 16, 32, 32)
        self.inception3b = Inception(256, 128, 128, 192, 32, 96, 64)
        self.maxpool3 = M.MaxPool2d(3, stride=2, padding=1)

        self.inception4a = Inception(480, 192, 96, 208, 16, 48, 64)
        self.inception4b = Inception(512, 160, 112, 224, 24, 64, 64)
        self.inception4c = Inception(512, 128, 128, 256, 24, 64, 64)
        self.inception4d = Inception(512, 112, 144, 288, 32, 64, 64)
        self.inception4e = Inception(528, 256, 160, 320, 32, 128, 128)
        self.maxpool4 = M.MaxPool2d(2, stride=2, padding=0)

        self.inception5a = Inception(832, 256, 160, 320, 32, 128, 128)
        self.inception5b = Inception(832, 384, 192, 384, 48, 128, 128)

        if aux_logits:
            self.aux1 = InceptionAux(512, num_classes)
            self.aux2 = InceptionAux(528, num_classes)

        # TODO
        self.avgpool = M.AvgPool2d(7)
        self.dropout = M.Dropout(0.2)
        self.fc = M.Linear(1024, num_classes)
예제 #13
0
    def __init__(self, input_size=224, num_classes=1000, model_size="1.5x"):
        super(ShuffleNetV2, self).__init__()

        self.stage_repeats = [4, 8, 4]
        self.model_size = model_size
        if model_size == "0.5x":
            self.stage_out_channels = [-1, 24, 48, 96, 192, 1024]
        elif model_size == "1.0x":
            self.stage_out_channels = [-1, 24, 116, 232, 464, 1024]
        elif model_size == "1.5x":
            self.stage_out_channels = [-1, 24, 176, 352, 704, 1024]
        elif model_size == "2.0x":
            self.stage_out_channels = [-1, 24, 244, 488, 976, 2048]
        else:
            raise NotImplementedError

        # building first layer
        input_channel = self.stage_out_channels[1]
        self.first_conv = M.Sequential(
            M.Conv2d(3, input_channel, 3, 2, 1, bias=False),
            M.BatchNorm2d(input_channel),
            FReLU(input_channel),
        )

        self.maxpool = M.MaxPool2d(kernel_size=3, stride=2, padding=1)

        self.features = []
        for idxstage in range(len(self.stage_repeats)):
            numrepeat = self.stage_repeats[idxstage]
            output_channel = self.stage_out_channels[idxstage + 2]

            for i in range(numrepeat):
                if i == 0:
                    self.features.append(
                        ShuffleV2Block(
                            input_channel,
                            output_channel,
                            mid_channels=output_channel // 2,
                            ksize=3,
                            stride=2,
                        ))
                else:
                    self.features.append(
                        ShuffleV2Block(
                            input_channel // 2,
                            output_channel,
                            mid_channels=output_channel // 2,
                            ksize=3,
                            stride=1,
                        ))

                input_channel = output_channel

        self.features = M.Sequential(*self.features)

        self.conv_last = M.Sequential(
            M.Conv2d(input_channel,
                     self.stage_out_channels[-1],
                     1,
                     1,
                     0,
                     bias=False),
            M.BatchNorm2d(self.stage_out_channels[-1]),
            FReLU(self.stage_out_channels[-1]),
        )
        self.globalpool = M.AvgPool2d(7)
        if self.model_size == "2.0x":
            self.dropout = M.Dropout(0.2)
        self.classifier = M.Sequential(
            M.Linear(self.stage_out_channels[-1], num_classes, bias=False))
        self._initialize_weights()
예제 #14
0
    def __init__(self,
                 channels,
                 init_block_channels,
                 final_block_channels,
                 kernel_sizes,
                 strides_per_stage,
                 expansion_factors,
                 dropout_rate=0.2,
                 tf_mode=False,
                 bn_eps=1e-5,
                 in_channels=3,
                 in_size=(224, 224),
                 num_classes=1000):
        super(EfficientNet, self).__init__()
        self.in_size = in_size
        self.num_classes = num_classes
        activation = Swish()

        self.features = []
        init_block = EffiInitBlock(in_channels=in_channels,
                                   out_channels=init_block_channels,
                                   bn_eps=bn_eps,
                                   activation=activation,
                                   tf_mode=tf_mode)
        self.features.append(init_block)
        in_channels = init_block_channels
        for i, channels_per_stage in enumerate(channels):
            kernel_sizes_per_stage = kernel_sizes[i]
            expansion_factors_per_stage = expansion_factors[i]
            stage = []
            for j, out_channels in enumerate(channels_per_stage):
                kernel_size = kernel_sizes_per_stage[j]
                expansion_factor = expansion_factors_per_stage[j]
                stride = strides_per_stage[i] if (j == 0) else 1
                if i == 0:
                    unit = EffiDwsConvUnit(in_channels=in_channels,
                                           out_channels=out_channels,
                                           stride=stride,
                                           bn_eps=bn_eps,
                                           activation=activation,
                                           tf_mode=tf_mode)
                    stage.append(unit)
                else:
                    unit = EffiInvResUnit(in_channels=in_channels,
                                          out_channels=out_channels,
                                          kernel_size=kernel_size,
                                          stride=stride,
                                          exp_factor=expansion_factor,
                                          se_factor=4,
                                          bn_eps=bn_eps,
                                          activation=activation,
                                          tf_mode=tf_mode)
                    stage.append(unit)
                in_channels = out_channels
            self.features += stage
        final_block = conv1x1_block(in_channels=in_channels,
                                    out_channels=final_block_channels,
                                    bn_eps=bn_eps,
                                    activation=activation)
        self.features.append(final_block)
        in_channels = final_block_channels
        final_pool = GlobalAvgPool2D()
        self.features.append(final_pool)
        self.features = M.Sequential(*self.features)

        self.output = []
        if dropout_rate > 0.0:
            dropout = M.Dropout(dropout_rate)
            self.output.append(dropout)
        fc = M.Linear(in_features=in_channels, out_features=num_classes)
        self.output.append(fc)
        self.output = M.Sequential(*self.output)
    def __init__(
        self,
        block,
        layers,
        num_classes=1000,
        zero_init_residual=False,
        groups=1,
        width_per_group=64,
        replace_stride_with_dilation=None,
        norm=M.BatchNorm2d,
    ):
        super(ResNet, self).__init__()
        self.in_channels = 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
        self.conv1 = M.Conv2d(3, self.in_channels, kernel_size=7, stride=2, padding=3, bias=True)
        self.bn1 = norm(self.in_channels)
        self.maxpool = M.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(block, 64, layers[0], norm=norm)
        self.layer2 = self._make_layer(
            block, 128, layers[1], stride=2, dilate=replace_stride_with_dilation[0], norm=norm,
        )
        self.layer3 = self._make_layer(
            block, 256, layers[2], stride=2, dilate=replace_stride_with_dilation[1], norm=norm,
        )
        self.layer4 = self._make_layer(
            Bottleneck, 512, layers[3], stride=2, dilate=replace_stride_with_dilation[2], norm=norm,
        )
        self.fc = M.Linear(512 * block.expansion, num_classes)
        self.dropout = M.Dropout(0.2)

        for m in self.modules():
            if isinstance(m, M.Conv2d):
                M.init.msra_normal_(m.weight, mode="fan_out", nonlinearity="relu")
                if m.bias is not None:
                    fan_in, _ = M.init.calculate_fan_in_and_fan_out(m.weight)
                    bound = 1 / math.sqrt(fan_in)
                    M.init.uniform_(m.bias, -bound, bound)
            elif isinstance(m, M.BatchNorm2d):
                M.init.ones_(m.weight)
                M.init.zeros_(m.bias)
            elif isinstance(m, M.Linear):
                M.init.msra_uniform_(m.weight, a=math.sqrt(5))
                if m.bias is not None:
                    fan_in, _ = M.init.calculate_fan_in_and_fan_out(m.weight)
                    bound = 1 / math.sqrt(fan_in)
                    M.init.uniform_(m.bias, -bound, bound)

        # 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 m in self.modules():
                if isinstance(m, Bottleneck):
                    M.init.zeros_(m.bn3.weight)
                elif isinstance(m, BasicBlock):
                    M.init.zeros_(m.bn2.weight)
예제 #16
0
 def __init__(self):
     super().__init__()
     self.data = np.random.random((1, 2, 3, 4)).astype(np.float32)
     self.drop_out = M.Dropout()
예제 #17
0
    def __init__(
        self,
        num_classes=1000,
        width_mult=1.0,
        inverted_residual_setting=None,
        round_nearest=8,
    ):
        """
        MobileNet V2 main class

        Args:
            num_classes (int): Number of classes
            width_mult (float): Width multiplier - adjusts number of channels
            in each layer by this amount
            inverted_residual_setting: Network structure
            round_nearest (int): Round the number of channels in each layer
            to be a multiple of this number
            Set to 1 to turn off rounding
        """
        super(MobileNetV2, self).__init__()
        block = InvertedResidual
        input_channel = 32
        last_channel = 1280

        if inverted_residual_setting is None:
            inverted_residual_setting = [
                # t, c, n, s
                [1, 16, 1, 1],
                [6, 24, 2, 2],
                [6, 32, 3, 2],
                [6, 64, 4, 2],
                [6, 96, 3, 1],
                [6, 160, 3, 2],
                [6, 320, 1, 1],
            ]

        # only check the first element, assuming user knows t,c,n,s are required
        if (len(inverted_residual_setting) == 0
                or len(inverted_residual_setting[0]) != 4):
            raise ValueError("inverted_residual_setting should be non-empty "
                             "or a 4-element list, got {}".format(
                                 inverted_residual_setting))

        # building first layer
        input_channel = _make_divisible(input_channel * width_mult,
                                        round_nearest)
        self.last_channel = _make_divisible(
            last_channel * max(1.0, width_mult), round_nearest)
        features = [
            M.ConvBnRelu2d(3,
                           input_channel,
                           kernel_size=3,
                           padding=1,
                           stride=2,
                           bias=False)
        ]
        # building inverted residual blocks
        for t, c, n, s in inverted_residual_setting:
            output_channel = _make_divisible(c * width_mult, round_nearest)
            for i in range(n):
                stride = s if i == 0 else 1
                features.append(
                    block(input_channel,
                          output_channel,
                          stride,
                          expand_ratio=t))
                input_channel = output_channel
        # building last several layers
        features.append(
            M.ConvBnRelu2d(input_channel,
                           self.last_channel,
                           kernel_size=1,
                           bias=False))
        # make it M.Sequential
        self.features = M.Sequential(*features)

        # building classifier
        self.classifier = M.Sequential(
            M.Dropout(0.2),
            M.Linear(self.last_channel, num_classes),
        )
        self.classifier.disable_quantize()

        self.quant = M.QuantStub()
        self.dequant = M.DequantStub()

        # weight initialization
        for m in self.modules():
            if isinstance(m, M.Conv2d):
                M.init.msra_normal_(m.weight, mode="fan_out")
                if m.bias is not None:
                    M.init.zeros_(m.bias)
            elif isinstance(m, M.BatchNorm2d):
                M.init.ones_(m.weight)
                M.init.zeros_(m.bias)
            elif isinstance(m, M.Linear):
                M.init.normal_(m.weight, 0, 0.01)
                M.init.zeros_(m.bias)
예제 #18
0
    def __init__(self, num_classes=1001, stem_filters=96, penultimate_filters=4032, filters_multiplier=2):
        super(NASNetALarge, self).__init__()
        self.num_classes = num_classes
        self.stem_filters = stem_filters
        self.penultimate_filters = penultimate_filters
        self.filters_multiplier = filters_multiplier

        filters = self.penultimate_filters // 24
        # 24 is default value for the architecture

        self.conv0 = []
        self.conv0.append(M.Conv2d(in_channels=3, out_channels=self.stem_filters, kernel_size=3, padding=0, stride=2,
                                                bias=False))
        self.conv0.append(M.BatchNorm2d(self.stem_filters, eps=0.001, momentum=0.1, affine=True))
        self.conv0 = M.Sequential(*self.conv0)

        self.cell_stem_0 = CellStem0(self.stem_filters, num_filters=filters // (filters_multiplier ** 2))
        self.cell_stem_1 = CellStem1(self.stem_filters, num_filters=filters // filters_multiplier)

        self.cell_0 = FirstCell(in_channels_left=filters, out_channels_left=filters//2,
                                in_channels_right=2*filters, out_channels_right=filters)
        self.cell_1 = NormalCell(in_channels_left=2*filters, out_channels_left=filters,
                                 in_channels_right=6*filters, out_channels_right=filters)
        self.cell_2 = NormalCell(in_channels_left=6*filters, out_channels_left=filters,
                                 in_channels_right=6*filters, out_channels_right=filters)
        self.cell_3 = NormalCell(in_channels_left=6*filters, out_channels_left=filters,
                                 in_channels_right=6*filters, out_channels_right=filters)
        self.cell_4 = NormalCell(in_channels_left=6*filters, out_channels_left=filters,
                                 in_channels_right=6*filters, out_channels_right=filters)
        self.cell_5 = NormalCell(in_channels_left=6*filters, out_channels_left=filters,
                                 in_channels_right=6*filters, out_channels_right=filters)

        self.reduction_cell_0 = ReductionCell0(in_channels_left=6*filters, out_channels_left=2*filters,
                                               in_channels_right=6*filters, out_channels_right=2*filters)

        self.cell_6 = FirstCell(in_channels_left=6*filters, out_channels_left=filters,
                                in_channels_right=8*filters, out_channels_right=2*filters)
        self.cell_7 = NormalCell(in_channels_left=8*filters, out_channels_left=2*filters,
                                 in_channels_right=12*filters, out_channels_right=2*filters)
        self.cell_8 = NormalCell(in_channels_left=12*filters, out_channels_left=2*filters,
                                 in_channels_right=12*filters, out_channels_right=2*filters)
        self.cell_9 = NormalCell(in_channels_left=12*filters, out_channels_left=2*filters,
                                 in_channels_right=12*filters, out_channels_right=2*filters)
        self.cell_10 = NormalCell(in_channels_left=12*filters, out_channels_left=2*filters,
                                  in_channels_right=12*filters, out_channels_right=2*filters)
        self.cell_11 = NormalCell(in_channels_left=12*filters, out_channels_left=2*filters,
                                  in_channels_right=12*filters, out_channels_right=2*filters)

        self.reduction_cell_1 = ReductionCell1(in_channels_left=12*filters, out_channels_left=4*filters,
                                               in_channels_right=12*filters, out_channels_right=4*filters)

        self.cell_12 = FirstCell(in_channels_left=12*filters, out_channels_left=2*filters,
                                 in_channels_right=16*filters, out_channels_right=4*filters)
        self.cell_13 = NormalCell(in_channels_left=16*filters, out_channels_left=4*filters,
                                  in_channels_right=24*filters, out_channels_right=4*filters)
        self.cell_14 = NormalCell(in_channels_left=24*filters, out_channels_left=4*filters,
                                  in_channels_right=24*filters, out_channels_right=4*filters)
        self.cell_15 = NormalCell(in_channels_left=24*filters, out_channels_left=4*filters,
                                  in_channels_right=24*filters, out_channels_right=4*filters)
        self.cell_16 = NormalCell(in_channels_left=24*filters, out_channels_left=4*filters,
                                  in_channels_right=24*filters, out_channels_right=4*filters)
        self.cell_17 = NormalCell(in_channels_left=24*filters, out_channels_left=4*filters,
                                  in_channels_right=24*filters, out_channels_right=4*filters)

        self.relu = M.ReLU()
        self.avg_pool = M.AvgPool2d(11, stride=1, padding=0)
        self.dropout = M.Dropout()
        self.last_linear = M.Linear(24*filters, self.num_classes)