Ejemplo n.º 1
0
 def __init__(self):
     super().__init__()
     self.conv1 = nn.Conv2d(3, 6, 5)
     self.pool = nn.MaxPool2d(2, 2)
     self.conv2 = nn.Conv2d(6, 16, 5)
     self.fc1 = nn.Linear(16 * 5 * 5, 120)
     self.fc2 = nn.Linear(120, 84)
     self.fc3 = nn.Linear(84, 10)
Ejemplo n.º 2
0
 def __init__(self):
     super(ModuleDict, self).__init__()
     self.choices = nn.ModuleDict(
         {"conv": nn.Conv2d(10, 10, 3), "pool": nn.MaxPool2d(3)}
     )
     self.activations = nn.ModuleDict(
         {"relu": nn.ReLU(), "prelu": nn.PReLU()}
     )
Ejemplo n.º 3
0
 def __init__(self):
     super(LeNet, self).__init__()
     self.conv = nn.Sequential(
         nn.Conv2d(1, 6,
                   kernel_size=5),  # in_channels, out_channels, kernel_size
         nn.ReLU(),
         nn.MaxPool2d(kernel_size=2, stride=2),  # kernel_size, stride
         nn.Conv2d(6, 16, 5),
         nn.ReLU(),
         nn.MaxPool2d(kernel_size=2, stride=2),
     )
     self.fc = nn.Sequential(
         nn.Linear(16 * 4 * 4, 120),
         nn.ReLU(),
         nn.Linear(120, 84),
         nn.ReLU(),
         nn.Linear(84, 10),
     )
Ejemplo n.º 4
0
    def __init__(
            self,
            in_channels: int,
            conv_block: Optional[Callable[..., nn.Module]] = None) -> None:
        super(InceptionB, self).__init__()
        if conv_block is None:
            conv_block = BasicConv2d
        self.branch3x3 = conv_block(in_channels, 384, kernel_size=3, stride=2)

        self.branch3x3dbl_1 = conv_block(in_channels, 64, kernel_size=1)
        self.branch3x3dbl_2 = conv_block(64, 96, kernel_size=3, padding=1)
        self.branch3x3dbl_3 = conv_block(96, 96, kernel_size=3, stride=2)
        self.max_pool = nn.MaxPool2d(kernel_size=3, stride=2)
Ejemplo n.º 5
0
    def __init__(
        self,
        stages_repeats: List[int],
        stages_out_channels: List[int],
        num_classes: int = 1000,
        inverted_residual: Callable[..., nn.Module] = InvertedResidual,
    ) -> None:
        super().__init__()

        if len(stages_repeats) != 3:
            raise ValueError(
                "expected stages_repeats as list of 3 positive ints")
        if len(stages_out_channels) != 5:
            raise ValueError(
                "expected stages_out_channels as list of 5 positive ints")
        self._stage_out_channels = stages_out_channels

        input_channels = 3
        output_channels = self._stage_out_channels[0]
        self.conv1 = nn.Sequential(
            nn.Conv2d(input_channels, output_channels, 3, 2, 1, bias=False),
            nn.BatchNorm2d(output_channels),
            nn.ReLU(inplace=True),
        )
        input_channels = output_channels

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

        # Static annotations for mypy
        self.stage2: nn.Sequential
        self.stage3: nn.Sequential
        self.stage4: nn.Sequential
        stage_names = ["stage{}".format(i) for i in [2, 3, 4]]
        for name, repeats, output_channels in zip(
                stage_names, stages_repeats, self._stage_out_channels[1:]):
            seq = [inverted_residual(input_channels, output_channels, 2)]
            for i in range(repeats - 1):
                seq.append(
                    inverted_residual(output_channels, output_channels, 1))
            setattr(self, name, nn.Sequential(*seq))
            input_channels = output_channels

        output_channels = self._stage_out_channels[-1]
        self.conv5 = nn.Sequential(
            nn.Conv2d(input_channels, output_channels, 1, 1, 0, bias=False),
            nn.BatchNorm2d(output_channels),
            nn.ReLU(inplace=True),
        )

        self.fc = nn.Linear(output_channels, num_classes)
Ejemplo n.º 6
0
    def __init__(self, input_channels):

        super().__init__()
        self.Branch_2 = nn.MaxPool2d(3, stride=2)

        self.Branch_0 = nn.Sequential(
            BasicConv2d(input_channels, 384, kernel_size=3, stride=2)
        )

        self.Branch_1 = nn.Sequential(
            BasicConv2d(input_channels, 256, kernel_size=1),
            BasicConv2d(256, 256, kernel_size=3, padding=1),
            BasicConv2d(256, 384, kernel_size=3, stride=2),
        )
Ejemplo n.º 7
0
 def _make_layers(self, cfg):
     layers = []
     in_channels = 3
     for x in cfg:
         if x == 'M':
             layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
         else:
             layers += [
                 nn.Conv2d(in_channels, x, kernel_size=3, padding=1),
                 nn.BatchNorm2d(x),
                 nn.ReLU(inplace=True)
             ]
             in_channels = x
     layers += [nn.AvgPool2d(kernel_size=1, stride=1)]
     return nn.Sequential(*layers)
Ejemplo n.º 8
0
def make_layers(cfg: List[Union[str, int]],
                batch_norm: bool = False) -> nn.Sequential:
    layers: List[nn.Module] = []
    in_channels = 3
    for v in cfg:
        if v == "M":
            layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
        else:
            v = cast(int, v)
            conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1)
            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)
Ejemplo n.º 9
0
    def __init__(
        self,
        block: Type[Union[BasicBlock, Bottleneck]],
        layers: List[int],
        num_classes: int = 1000,
        pretrained=None,
        groups: int = 1,
        width_per_group: int = 64,
        replace_stride_with_dilation: Optional[List[bool]] = None,
        norm_layer: Optional[Callable[..., nn.Module]] = None,
    ) -> None:
        super(ResNet, self).__init__()
        if norm_layer is None:
            norm_layer = nn.BatchNorm2d
        self._norm_layer = norm_layer
        self.pretrained = pretrained

        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
        self.conv1 = nn.Conv2d(
            3, self.inplanes, kernel_size=7, stride=2, padding=3, bias=False
        )
        self.bn1 = norm_layer(self.inplanes)
        self.relu = nn.ReLU()
        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]
        )
Ejemplo n.º 10
0
    def _make_level(self, block, inplanes, planes, blocks, stride=1):
        downsample = None
        if stride != 1 or inplanes != planes:
            downsample = nn.Sequential(
                nn.MaxPool2d(stride, stride=stride),
                nn.Conv2d(inplanes,
                          planes,
                          kernel_size=1,
                          stride=1,
                          bias=False),
                BatchNorm(planes),
            )

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

        return nn.Sequential(*layers)
Ejemplo n.º 11
0
    def __init__(
            self,
            in_channels: int,
            conv_block: Optional[Callable[..., nn.Module]] = None) -> None:
        super(InceptionD, self).__init__()
        if conv_block is None:
            conv_block = BasicConv2d
        self.branch3x3_1 = conv_block(in_channels, 192, kernel_size=1)
        self.branch3x3_2 = conv_block(192, 320, kernel_size=3, stride=2)

        self.branch7x7x3_1 = conv_block(in_channels, 192, kernel_size=1)
        self.branch7x7x3_2 = conv_block(192,
                                        192,
                                        kernel_size=(1, 7),
                                        padding=(0, 3))
        self.branch7x7x3_3 = conv_block(192,
                                        192,
                                        kernel_size=(7, 1),
                                        padding=(3, 0))
        self.branch7x7x3_4 = conv_block(192, 192, kernel_size=3, stride=2)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2)
Ejemplo n.º 12
0
def make_layers(cfg, in_channels=3, batch_norm=False, dilation=False):
    if dilation:
        d_rate = 2
    else:
        d_rate = 1
    layers = []
    for v in cfg:
        if v == "M":
            layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
        else:
            conv2d = nn.Conv2d(in_channels,
                               v,
                               kernel_size=3,
                               padding=d_rate,
                               dilation=d_rate)
            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)
Ejemplo n.º 13
0
    def __init__(self, in_planes, n1x1, n3x3red, n3x3, n5x5red, n5x5,
                 pool_planes):
        super(Inception, self).__init__()
        # 1x1 conv branch
        self.b1 = nn.Sequential(
            nn.Conv2d(in_planes, n1x1, kernel_size=1),
            nn.BatchNorm2d(n1x1),
            nn.ReLU(True),
        )

        # 1x1 conv -> 3x3 conv branch
        self.b2 = nn.Sequential(
            nn.Conv2d(in_planes, n3x3red, kernel_size=1),
            nn.BatchNorm2d(n3x3red),
            nn.ReLU(True),
            nn.Conv2d(n3x3red, n3x3, kernel_size=3, padding=1),
            nn.BatchNorm2d(n3x3),
            nn.ReLU(True),
        )

        # 1x1 conv -> 5x5 conv branch
        self.b3 = nn.Sequential(
            nn.Conv2d(in_planes, n5x5red, kernel_size=1),
            nn.BatchNorm2d(n5x5red),
            nn.ReLU(True),
            nn.Conv2d(n5x5red, n5x5, kernel_size=3, padding=1),
            nn.BatchNorm2d(n5x5),
            nn.ReLU(True),
            nn.Conv2d(n5x5, n5x5, kernel_size=3, padding=1),
            nn.BatchNorm2d(n5x5),
            nn.ReLU(True),
        )

        # 3x3 pool -> 1x1 conv branch
        self.b4 = nn.Sequential(
            nn.MaxPool2d(3, stride=1, padding=1),
            nn.Conv2d(in_planes, pool_planes, kernel_size=1),
            nn.BatchNorm2d(pool_planes),
            nn.ReLU(True),
        )
Ejemplo n.º 14
0
    def __init__(self):
        super(GoogLeNet, self).__init__()
        self.pre_layers = nn.Sequential(
            nn.Conv2d(3, 192, kernel_size=3, padding=1),
            nn.BatchNorm2d(192),
            nn.ReLU(True),
        )

        self.a3 = Inception(192, 64, 96, 128, 16, 32, 32)
        self.b3 = Inception(256, 128, 128, 192, 32, 96, 64)

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

        self.a4 = Inception(480, 192, 96, 208, 16, 48, 64)
        self.b4 = Inception(512, 160, 112, 224, 24, 64, 64)
        self.c4 = Inception(512, 128, 128, 256, 24, 64, 64)
        self.d4 = Inception(512, 112, 144, 288, 32, 64, 64)
        self.e4 = Inception(528, 256, 160, 320, 32, 128, 128)

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

        self.avgpool = nn.AvgPool2d(8, stride=1)
        self.linear = nn.Linear(1024, 10)
Ejemplo n.º 15
0
 def __init__(
     self,
     levels,
     block,
     in_channels,
     out_channels,
     stride=1,
     level_root=False,
     root_dim=0,
     root_kernel_size=1,
     dilation=1,
     root_residual=False,
 ):
     super(Tree, self).__init__()
     if root_dim == 0:
         root_dim = 2 * out_channels
     if level_root:
         root_dim += in_channels
     if levels == 1:
         self.tree1 = block(in_channels,
                            out_channels,
                            stride,
                            dilation=dilation)
         self.tree2 = block(out_channels,
                            out_channels,
                            1,
                            dilation=dilation)
     else:
         self.tree1 = Tree(
             levels - 1,
             block,
             in_channels,
             out_channels,
             stride,
             root_dim=0,
             root_kernel_size=root_kernel_size,
             dilation=dilation,
             root_residual=root_residual,
         )
         self.tree2 = Tree(
             levels - 1,
             block,
             out_channels,
             out_channels,
             root_dim=root_dim + out_channels,
             root_kernel_size=root_kernel_size,
             dilation=dilation,
             root_residual=root_residual,
         )
     if levels == 1:
         self.root = Root(root_dim, out_channels, root_kernel_size,
                          root_residual)
     self.level_root = level_root
     self.root_dim = root_dim
     self.downsample = None
     self.project = None
     self.levels = levels
     if stride > 1:
         self.downsample = nn.MaxPool2d(stride, stride=stride)
     if in_channels != out_channels:
         self.project = nn.Sequential(
             nn.Conv2d(in_channels,
                       out_channels,
                       kernel_size=1,
                       stride=1,
                       bias=False),
             BatchNorm(out_channels),
         )
Ejemplo n.º 16
0
    def __init__(
        self,
        num_classes: int = 1000,
        aux_logits: bool = True,
        transform_input: bool = False,
        inception_blocks: Optional[List[Callable[..., nn.Module]]] = None,
        init_weights: Optional[bool] = None,
    ) -> None:
        super(Inception3, self).__init__()
        if inception_blocks is None:
            inception_blocks = [
                BasicConv2d,
                InceptionA,
                InceptionB,
                InceptionC,
                InceptionD,
                InceptionE,
                InceptionAux,
            ]
        if init_weights is None:
            warnings.warn(
                "The default weight initialization of inception_v3 will be changed in future releases of "
                "torchvision. If you wish to keep the old behavior (which leads to long initialization times"
                " due to scipy/scipy#11299), please set init_weights=True.",
                FutureWarning,
            )
            init_weights = True
        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 = nn.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 = nn.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)
        self.AuxLogits: Optional[nn.Module] = None
        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 = nn.AdaptiveAvgPool2d((1, 1))
        self.dropout = nn.Dropout()
        self.fc = nn.Linear(2048, num_classes)

        if init_weights:
            for m in self.modules():
                if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
                    stddev = float(m.stddev) if hasattr(
                        m, "stddev") else 0.1  # type: ignore
                    flow.nn.init.trunc_normal_(m.weight,
                                               mean=0.0,
                                               std=stddev,
                                               a=-2,
                                               b=2)
                elif isinstance(m, nn.BatchNorm2d):
                    nn.init.constant_(m.weight, 1)
                    nn.init.constant_(m.bias, 0)
Ejemplo n.º 17
0
    def __init__(
        self,
        block,
        layers,
        zero_init_residual=False,
        groups=1,
        width_per_group=64,
        replace_stride_with_dilation=None,
        norm_layer=None,
        last_stride=1,
    ):
        super(ResNet, self).__init__()
        if norm_layer is None:
            norm_layer = nn.BatchNorm2d
        self._norm_layer = norm_layer
        self.feature_dim = 512 * block.expansion
        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
        self.conv1 = nn.Conv2d(3,
                               self.inplanes,
                               kernel_size=7,
                               stride=2,
                               padding=3,
                               bias=False)
        self.bn1 = norm_layer(self.inplanes)
        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])
        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=last_stride,
            dilate=replace_stride_with_dilation[2],
        )
        self._init_params()

        # 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):
                    nn.init.constant_(m.bn3.weight, 0)
                elif isinstance(m, BasicBlock):
                    nn.init.constant_(m.bn2.weight, 0)
Ejemplo n.º 18
0
 def __init__(self, in_channels, out_channels):
     super().__init__()
     self.maxpool_conv = nn.Sequential(
         nn.MaxPool2d(2), DoubleConv(in_channels, out_channels))
Ejemplo n.º 19
0
    def __init__(
        self,
        block: Type[Union[BasicBlock, Bottleneck]],
        layers: List[int],
        num_classes: int = 1000,
        zero_init_residual: bool = False,
        groups: int = 1,
        width_per_group: int = 64,
        replace_stride_with_dilation: Optional[List[bool]] = None,
        norm_layer: Optional[Callable[..., nn.Module]] = None,
    ) -> None:
        super(ResNet, self).__init__()
        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
        self.conv1 = nn.Conv2d(3,
                               self.inplanes,
                               kernel_size=7,
                               stride=2,
                               padding=3,
                               bias=False)
        self.bn1 = norm_layer(self.inplanes)
        self.relu = nn.ReLU()
        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])
        self.avgpool = nn.AvgPool2d((7, 7))
        self.fc = nn.Linear(512 * block.expansion, num_classes)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight,
                                        mode="fan_out",
                                        nonlinearity="relu")
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.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 m in self.modules():
                if isinstance(m, Bottleneck):
                    nn.init.constant_(m.bn3.weight,
                                      0)  # type: ignore[arg-type]
                elif isinstance(m, BasicBlock):
                    nn.init.constant_(m.bn2.weight,
                                      0)  # type: ignore[arg-type]
Ejemplo n.º 20
0
 def __init__(
     self,
     block: Type[Union[BasicBlock, Bottleneck]],
     layers: List[int],
     num_classes: int = 1000,
     zero_init_residual: bool = False,
     groups: int = 1,
     width_per_group: int = 64,
     replace_stride_with_dilation: Optional[List[bool]] = None,
     norm_layer: Optional[Callable[..., nn.Module]] = None,
 ) -> None:
     super(ResNet, self).__init__()
     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:
         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 = nn.Conv2d(3,
                            self.inplanes,
                            kernel_size=7,
                            stride=2,
                            padding=3,
                            bias=False)
     self.bn1 = norm_layer(self.inplanes)
     self.relu = nn.ReLU()
     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])
     self.avgpool = nn.AvgPool2d((7, 7))
     self.fc = nn.Linear(512 * block.expansion, num_classes)
     for m in self.modules():
         if isinstance(m, nn.Conv2d):
             nn.init.kaiming_normal_(m.weight,
                                     mode="fan_out",
                                     nonlinearity="relu")
         elif isinstance(m, nn.BatchNorm2d):
             nn.init.constant_(m.weight, 1)
             nn.init.constant_(m.bias, 0)
     if zero_init_residual:
         for m in self.modules():
             if isinstance(m, Bottleneck):
                 nn.init.constant_(m.bn3.weight, 0)
             elif isinstance(m, BasicBlock):
                 nn.init.constant_(m.bn2.weight, 0)
Ejemplo n.º 21
0
    def __init__(
        self,
        growth_rate: int = 32,
        block_config: Tuple[int, int, int, int] = (6, 12, 24, 16),
        num_init_features: int = 64,
        bn_size: int = 4,
        drop_rate: float = 0,
        num_classes: int = 1000,
    ) -> None:

        super(DenseNet, self).__init__()

        # First convolution
        self.features = nn.Sequential(
            OrderedDict([
                (
                    "conv0",
                    nn.Conv2d(
                        3,
                        num_init_features,
                        kernel_size=7,
                        stride=2,
                        padding=3,
                        bias=False,
                    ),
                ),
                ("norm0", nn.BatchNorm2d(num_init_features)),
                ("relu0", nn.ReLU(inplace=True)),
                ("pool0", nn.MaxPool2d(kernel_size=3, stride=2, padding=1)),
            ]))

        # Each denseblock
        num_features = num_init_features
        for i, num_layers in enumerate(block_config):
            block = _DenseBlock(
                num_layers=num_layers,
                num_input_features=num_features,
                bn_size=bn_size,
                growth_rate=growth_rate,
                drop_rate=drop_rate,
            )
            self.features.add_module("denseblock%d" % (i + 1), block)
            num_features = num_features + num_layers * growth_rate
            if i != len(block_config) - 1:
                trans = _Transition(
                    num_input_features=num_features,
                    num_output_features=num_features // 2,
                )
                self.features.add_module("transition%d" % (i + 1), trans)
                num_features = num_features // 2

        # Final batch norm
        self.features.add_module("norm5", nn.BatchNorm2d(num_features))

        # Linear layer
        self.classifier = nn.Linear(num_features, num_classes)

        # Official init from torch repo.
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight)
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.Linear):
                nn.init.constant_(m.bias, 0)
Ejemplo n.º 22
0
 def quantize(
     self,
     quantization_bit=8,
     quantization_scheme="symmetric",
     quantization_formula="google",
     per_layer_quantization=True,
 ):
     self.q_features = nn.Sequential(
         q_conv(
             self.features[0],
             qi=True,
             qo=True,
             quantization_bit=quantization_bit,
             quantization_scheme=quantization_scheme,
             quantization_formula=quantization_formula,
             per_layer_quantization=per_layer_quantization,
         ),
         nn.ReLU(inplace=True),
         nn.MaxPool2d(kernel_size=3, stride=2),
         q_conv(
             self.features[3],
             qi=False,
             qo=True,
             quantization_bit=quantization_bit,
             quantization_scheme=quantization_scheme,
             quantization_formula=quantization_formula,
             per_layer_quantization=per_layer_quantization,
         ),
         nn.ReLU(inplace=True),
         nn.MaxPool2d(kernel_size=3, stride=2),
         q_conv(
             self.features[6],
             qi=False,
             qo=True,
             quantization_bit=quantization_bit,
             quantization_scheme=quantization_scheme,
             quantization_formula=quantization_formula,
             per_layer_quantization=per_layer_quantization,
         ),
         nn.ReLU(inplace=True),
         q_conv(
             self.features[8],
             qi=False,
             qo=True,
             quantization_bit=quantization_bit,
             quantization_scheme=quantization_scheme,
             quantization_formula=quantization_formula,
             per_layer_quantization=per_layer_quantization,
         ),
         nn.ReLU(inplace=True),
         q_conv(
             self.features[10],
             qi=False,
             qo=True,
             quantization_bit=quantization_bit,
             quantization_scheme=quantization_scheme,
             quantization_formula=quantization_formula,
             per_layer_quantization=per_layer_quantization,
         ),
         nn.ReLU(inplace=True),
         nn.MaxPool2d(kernel_size=3, stride=2),
     )
     self.q_avgpool = nn.AdaptiveAvgPool2d((6, 6))
     self.q_classifier = nn.Sequential(
         nn.Dropout(),
         q_linear(
             self.classifier[1],
             qi=False,
             qo=True,
             quantization_bit=quantization_bit,
             quantization_scheme=quantization_scheme,
             quantization_formula=quantization_formula,
             per_layer_quantization=per_layer_quantization,
         ),
         nn.ReLU(inplace=True),
         nn.Dropout(),
         q_linear(
             self.classifier[4],
             qi=False,
             qo=True,
             quantization_bit=quantization_bit,
             quantization_scheme=quantization_scheme,
             quantization_formula=quantization_formula,
             per_layer_quantization=per_layer_quantization,
         ),
         nn.ReLU(inplace=True),
         q_linear(
             self.classifier[6],
             qi=False,
             qo=True,
             quantization_bit=quantization_bit,
             quantization_scheme=quantization_scheme,
             quantization_formula=quantization_formula,
             per_layer_quantization=per_layer_quantization,
         ),
     )