Exemple #1
0
    def __init__(self, C, num_classes, layers, genotype):
        super(NetworkCIFAR, self).__init__()
        self._layers = layers

        stem_multiplier = 3
        C_curr = stem_multiplier * C
        self.stem = nn.Sequential(
            nn.Conv2d(3, C_curr, 3, padding=1, bias=False),
            nn.BatchNorm2d(C_curr)
        )

        C_prev_prev, C_prev, C_curr = C_curr, C_curr, C
        self.cells = nn.ModuleList()
        reduction_prev = False
        for i in range(layers):
            if i in [layers // 3, 2 * layers // 3]:
                C_curr *= 2
                reduction = True
            else:
                reduction = False
            cell = Cell(genotype, C_prev_prev, C_prev, C_curr, reduction, reduction_prev)
            reduction_prev = reduction
            self.cells.append(cell)
            C_prev_prev, C_prev = C_prev, cell.multiplier * C_curr

        self.global_pooling = nn.AdaptiveAvgPool2d(1)
        self.classifier = nn.Linear(C_prev, num_classes)
Exemple #2
0
 def __init__(self, num_classes=1000, aux_logits=True, init_weights=True, blocks=None):
     super(GoogLeNet, self).__init__()
     if (blocks is None):
         blocks = [BasicConv2d, Inception, InceptionAux]
     assert (len(blocks) == 3)
     conv_block = blocks[0]
     inception_block = blocks[1]
     inception_aux_block = blocks[2]
     self.aux_logits = aux_logits
     self.conv1 = conv_block(3, 64, kernel_size=7, stride=2, padding=3)
     self.maxpool1 = nn.Pool(3, stride=2, ceil_mode=True, op='maximum')
     self.conv2 = conv_block(64, 64, kernel_size=1)
     self.conv3 = conv_block(64, 192, kernel_size=3, padding=1)
     self.maxpool2 = nn.Pool(3, stride=2, ceil_mode=True, op='maximum')
     self.inception3a = inception_block(192, 64, 96, 128, 16, 32, 32)
     self.inception3b = inception_block(256, 128, 128, 192, 32, 96, 64)
     self.maxpool3 = nn.Pool(3, stride=2, ceil_mode=True, op='maximum')
     self.inception4a = inception_block(480, 192, 96, 208, 16, 48, 64)
     self.inception4b = inception_block(512, 160, 112, 224, 24, 64, 64)
     self.inception4c = inception_block(512, 128, 128, 256, 24, 64, 64)
     self.inception4d = inception_block(512, 112, 144, 288, 32, 64, 64)
     self.inception4e = inception_block(528, 256, 160, 320, 32, 128, 128)
     self.maxpool4 = nn.Pool(2, stride=2, ceil_mode=True, op='maximum')
     self.inception5a = inception_block(832, 256, 160, 320, 32, 128, 128)
     self.inception5b = inception_block(832, 384, 192, 384, 48, 128, 128)
     if aux_logits:
         self.aux1 = inception_aux_block(512, num_classes)
         self.aux2 = inception_aux_block(528, num_classes)
     else:
         self.aux1 = None
         self.aux2 = None
     self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
     self.dropout = nn.Dropout(0.2)
     self.fc = nn.Linear(1024, num_classes)
Exemple #3
0
    def __init__(self,
                 block,
                 layers,
                 num_classes=1000,
                 zero_init_residual=False,
                 groups=1,
                 width_per_group=64,
                 replace_stride_with_dilation=None,
                 norm_layer=None):
        super(ResNet, self).__init__()
        if (norm_layer is None):
            norm_layer = nn.BatchNorm
        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.Conv(3,
                             self.inplanes,
                             kernel_size=7,
                             stride=2,
                             padding=3,
                             bias=False)
        jt.init.relu_invariant_gauss_(self.conv1.weight, mode="fan_out")
        self.bn1 = norm_layer(self.inplanes)
        self.relu = nn.GELU()
        self.maxpool = nn.Pool(kernel_size=3,
                               stride=2,
                               padding=1,
                               op='maximum')
        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.AdaptiveAvgPool2d((1, 1))

        # self.conv2 = conv1x1((512 * block.expansion), 1024)
        # self.at = Attention(1024, num_heads=1, kdim=1024,
        #                     vdim=1024, self_attention=True)
        # self.conv3 = conv1x1(1024, (512 * block.expansion))

        self.fc = nn.Linear((512 * block.expansion), num_classes)
Exemple #4
0
 def __init__(self, block, layers, baseWidth=26, scale=4, num_classes=1000):
     self.inplanes = 64
     super(Res2Net, self).__init__()
     self.baseWidth = baseWidth
     self.scale = scale
     self.conv1 = nn.Sequential(
         nn.Conv(3, 32, 3, stride=2, padding=1, bias=False),
         nn.BatchNorm(32), nn.ReLU(),
         nn.Conv(32, 32, 3, stride=1, padding=1, bias=False),
         nn.BatchNorm(32), nn.ReLU(),
         nn.Conv(32, 64, 3, stride=1, padding=1, bias=False))
     self.bn1 = nn.BatchNorm(64)
     self.relu = nn.ReLU()
     self.maxpool = nn.Pool(3, stride=2, padding=1, op='maximum')
     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.AdaptiveAvgPool2d(1)
     self.fc = nn.Linear((512 * block.expansion), num_classes)
     for m in self.modules():
         if isinstance(m, nn.Conv):
             nn.init.kaiming_normal_(m.weight, mode='fan_out')
         elif isinstance(m, nn.BatchNorm):
             init.constant_(m.weight, value=1)
             init.constant_(m.bias, value=0)
Exemple #5
0
 def __init__(self, version='1_0', num_classes=1000):
     super(SqueezeNet, self).__init__()
     self.num_classes = num_classes
     if (version == '1_0'):
         self.features = nn.Sequential(
             nn.Conv(3, 96, kernel_size=7, stride=2), nn.Relu(),
             nn.Pool(kernel_size=3, stride=2, ceil_mode=True, op='maximum'),
             Fire(96, 16, 64, 64), Fire(128, 16, 64, 64),
             Fire(128, 32, 128, 128),
             nn.Pool(kernel_size=3, stride=2, ceil_mode=True, op='maximum'),
             Fire(256, 32, 128, 128), Fire(256, 48, 192, 192),
             Fire(384, 48, 192, 192), Fire(384, 64, 256, 256),
             nn.Pool(kernel_size=3, stride=2, ceil_mode=True, op='maximum'),
             Fire(512, 64, 256, 256))
     elif (version == '1_1'):
         self.features = nn.Sequential(
             nn.Conv(3, 64, kernel_size=3, stride=2), nn.Relu(),
             nn.Pool(kernel_size=3, stride=2, ceil_mode=True, op='maximum'),
             Fire(64, 16, 64, 64), Fire(128, 16, 64, 64),
             nn.Pool(kernel_size=3, stride=2, ceil_mode=True, op='maximum'),
             Fire(128, 32, 128, 128), Fire(256, 32, 128, 128),
             nn.Pool(kernel_size=3, stride=2, ceil_mode=True, op='maximum'),
             Fire(256, 48, 192, 192), Fire(384, 48, 192, 192),
             Fire(384, 64, 256, 256), Fire(512, 64, 256, 256))
     else:
         raise ValueError(
             'Unsupported SqueezeNet version {version}:1_0 or 1_1 expected'.
             format(version=version))
     final_conv = nn.Conv(512, self.num_classes, kernel_size=1)
     self.classifier = nn.Sequential(nn.Dropout(p=0.5),
                                     final_conv, nn.Relu(),
                                     nn.AdaptiveAvgPool2d((1, 1)))
Exemple #6
0
    def __init__(self, block, layers, baseWidth=26, scale=4, num_classes=1000):
        self.inplanes = 64
        super(Res2Net, self).__init__()
        self.baseWidth = baseWidth
        self.scale = scale
        self.conv1 = nn.Conv2d(3,
                               64,
                               kernel_size=7,
                               stride=2,
                               padding=3,
                               bias=False)
        self.bn1 = nn.BatchNorm2d(64)
        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)
        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.AdaptiveAvgPool2d(1)
        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)
 def _forward(self, x):
     x = self.Conv2d_1a_3x3(x)
     x = self.Conv2d_2a_3x3(x)
     x = self.Conv2d_2b_3x3(x)
     x = nn.pool(x, 3, "maximum", stride=2)
     x = self.Conv2d_3b_1x1(x)
     x = self.Conv2d_4a_3x3(x)
     x = nn.pool(x, 3, "maximum", stride=2)
     x = self.Mixed_5b(x)
     x = self.Mixed_5c(x)
     x = self.Mixed_5d(x)
     x = self.Mixed_6a(x)
     x = self.Mixed_6b(x)
     x = self.Mixed_6c(x)
     x = self.Mixed_6d(x)
     x = self.Mixed_6e(x)
     aux_defined = self.aux_logits
     if aux_defined:
         aux = self.AuxLogits(x)
     else:
         aux = None
     x = self.Mixed_7a(x)
     x = self.Mixed_7b(x)
     x = self.Mixed_7c(x)
     x = nn.AdaptiveAvgPool2d(1)(x)
     x = nn.Dropout()(x)
     x = jt.reshape(x, (x.shape[0], (-1)))
     x = self.fc(x)
     return (x, aux)
Exemple #8
0
    def __init__(self,
                 num_features,
                 eps=1e-5,
                 momentum=0.1,
                 affine=True,
                 is_train=True,
                 sync=True):
        super(RepresentativeBatchNorm2d,
              self).__init__(num_features, eps, momentum, affine, is_train,
                             sync)
        self.sync = sync
        self.num_features = num_features
        self.is_train = is_train
        self.eps = eps
        self.momentum = momentum
        self.affine = affine
        self.weight = init.constant(
            (1, num_features, 1, 1), "float32", 1.0) if affine else 1.0
        self.bias = init.constant(
            (1, num_features, 1, 1), "float32", 0.0) if affine else 0.0
        self.running_mean = init.constant((num_features, ), "float32",
                                          0.0).stop_grad()
        self.running_var = init.constant((num_features, ), "float32",
                                         1.0).stop_grad()

        ### weights for centering calibration ###        $
        self.center_weight = init.constant((1, num_features, 1, 1), "float32",
                                           0.0)
        ### weights for scaling calibration ###            $
        self.scale_weight = init.constant((1, num_features, 1, 1), "float32",
                                          1.0)
        self.scale_bias = init.constant((1, num_features, 1, 1), "float32",
                                        0.0)
        ### calculate statistics ###$
        self.stas = nn.AdaptiveAvgPool2d((1, 1))
Exemple #9
0
    def __init__(self, in_channels, out_channels, pool_size):
        super(PyramidPool, self).__init__()

        self.conv = nn.Sequential(
            nn.AdaptiveAvgPool2d(pool_size),
            nn.Conv(in_channels, out_channels, 1, bias=False),
            nn.BatchNorm(out_channels), nn.ReLU())
Exemple #10
0
 def execute(self, x):
     x = nn.AdaptiveAvgPool2d(4)(x)
     x = self.conv(x)
     x = jt.reshape(x, (x.shape[0], (- 1)))
     x = nn.relu(self.fc1(x))
     x = nn.Dropout(0.7)(x)
     x = self.fc2(x)
     return x
Exemple #11
0
 def __init__(self, channel, reduction=4):
     super(SEModule, self).__init__()
     self.avg_pool = nn.AdaptiveAvgPool2d(1)
     self.fc1 = nn.Conv(channel, channel // reduction, kernel_size=1,
                          padding=0)
     self.relu = nn.ReLU()
     self.fc2 = nn.Conv(channel // reduction, channel , kernel_size=1,
                          padding=0)
     self.hsigmoid = Hsigmoid()
Exemple #12
0
 def __init__(self, channel, reduction=16):
     super(SELayer, self).__init__()
     self.avg_pool = nn.AdaptiveAvgPool2d(1)
     self.fc = nn.Sequential(
         nn.Linear(channel, channel // reduction, bias=False),
         nn.Relu(),
         nn.Linear(channel // reduction, channel, bias=False),
         nn.Sigmoid()
     )
    def __init__(self, C):
        super(SEModule, self).__init__()
        mid = max(C // self.reduction, 8)
        conv1 = Conv2d(C, mid, 1, 1, 0)
        conv2 = Conv2d(mid, C, 1, 1, 0)

        self.op = nn.Sequential(
            nn.AdaptiveAvgPool2d(1), conv1, nn.ReLU(), conv2, nn.Sigmoid()
        )
    def execute(self, x):
        x = nn.pool(x, kernel_size=5, op="mean", stride=3)
        x = self.conv0(x)
        x = self.conv1(x)

        x = nn.AdaptiveAvgPool2d(1)(x)
        x = jt.reshape(x, (x.shape[0], (-1)))
        x = self.fc(x)
        return x
Exemple #15
0
 def __init__(self,
              c1,
              c2,
              k=1,
              s=1,
              p=None,
              g=1):  # ch_in, ch_out, kernel, stride, padding, groups
     super(Classify, self).__init__()
     self.aap = nn.AdaptiveAvgPool2d(1)  # to x(b,c1,1,1)
     self.conv = nn.Conv(c1, c2, k, s, autopad(k, p),
                         groups=g)  # to x(b,c2,1,1)
     self.flat = Flatten()
Exemple #16
0
 def __init__(self, features, num_classes=1000, init_weights=True):
     super(VGG, self).__init__()
     self.features = features
     self.avgpool = nn.AdaptiveAvgPool2d((7, 7))
     self.classifier = nn.Sequential(
         nn.Linear(512 * 7 * 7, 4096),
         nn.ReLU(),
         nn.Dropout(),
         nn.Linear(4096, 4096),
         nn.ReLU(),
         nn.Dropout(),
         nn.Linear(4096, num_classes),
     )
Exemple #17
0
    def direct_mask_loss(self, pos_idx, idx_t, loc_data, mask_data, priors,
                         masks):
        """ Crops the gt masks using the predicted bboxes, scales them down, and outputs the BCE loss. """
        loss_m = 0
        for idx in range(mask_data.shape[0]):
            with jt.no_grad():
                cur_pos_idx = pos_idx[idx]
                cur_pos_idx_squeezed = cur_pos_idx[:, 1]

                # Shape: [num_priors, 4], decoded predicted bboxes
                pos_bboxes = decode(loc_data[idx], priors.data,
                                    cfg.use_yolo_regressors)
                pos_bboxes = pos_bboxes[cur_pos_idx].view(-1, 4).clamp(0, 1)
                pos_lookup = idx_t[idx, cur_pos_idx_squeezed]

                cur_masks = masks[idx]
                pos_masks = cur_masks[pos_lookup]

                # Convert bboxes to absolute coordinates
                num_pos, img_height, img_width = pos_masks.shape

                # Take care of all the bad behavior that can be caused by out of bounds coordinates
                x1, x2 = sanitize_coordinates(pos_bboxes[:, 0],
                                              pos_bboxes[:, 2], img_width)
                y1, y2 = sanitize_coordinates(pos_bboxes[:, 1],
                                              pos_bboxes[:, 3], img_height)

                # Crop each gt mask with the predicted bbox and rescale to the predicted mask size
                # Note that each bounding box crop is a different size so I don't think we can vectorize this
                scaled_masks = []
                for jdx in range(num_pos):
                    tmp_mask = pos_masks[jdx, y1[jdx]:y2[jdx], x1[jdx]:x2[jdx]]

                    # Restore any dimensions we've left out because our bbox was 1px wide
                    while tmp_mask.ndim < 2:
                        tmp_mask = tmp_mask.unsqueeze(0)

                    new_mask = nn.AdaptiveAvgPool2d(cfg.mask_size)(
                        tmp_mask.unsqueeze(0))
                    scaled_masks.append(new_mask.view(1, -1))

                mask_t = (jt.contrib.concat(scaled_masks, 0) >
                          0.5).float()  # Threshold downsampled mask

            pos_mask_data = mask_data[idx, cur_pos_idx_squeezed, :]
            loss_m += nn.bce_loss(jt.clamp(pos_mask_data, 0, 1),
                                  mask_t,
                                  size_average=False) * cfg.mask_alpha

        return loss_m
Exemple #18
0
    def __init__(self, config, in_channels):
        super(FastRCNNPredictor, self).__init__()
        assert in_channels is not None

        num_inputs = in_channels

        num_classes = config.MODEL.ROI_BOX_HEAD.NUM_CLASSES
        self.avgpool = nn.AdaptiveAvgPool2d(1)
        self.cls_score = nn.Linear(num_inputs, num_classes)
        num_bbox_reg_classes = 2 if config.MODEL.CLS_AGNOSTIC_BBOX_REG else num_classes
        self.bbox_pred = nn.Linear(num_inputs, num_bbox_reg_classes * 4)

        init.gauss_(self.cls_score.weight, mean=0, std=0.01)
        init.constant_(self.cls_score.bias, 0)

        init.gauss_(self.bbox_pred.weight, mean=0, std=0.001)
        init.constant_(self.bbox_pred.bias, 0)
Exemple #19
0
 def __init__(self, num_classes=1000):
     super(AlexNet, self).__init__()
     self.features = nn.Sequential(
         nn.Conv(3, 64, kernel_size=11, stride=4, padding=2), nn.Relu(),
         nn.Pool(kernel_size=3, stride=2, op='maximum'),
         nn.Conv(64, 192, kernel_size=5, padding=2), nn.Relu(),
         nn.Pool(kernel_size=3, stride=2, op='maximum'),
         nn.Conv(192, 384, kernel_size=3, padding=1), nn.Relu(),
         nn.Conv(384, 256, kernel_size=3, padding=1), nn.Relu(),
         nn.Conv(256, 256, kernel_size=3, padding=1), nn.Relu(),
         nn.Pool(kernel_size=3, stride=2, op='maximum'))
     self.avgpool = nn.AdaptiveAvgPool2d((6, 6))
     self.classifier = nn.Sequential(nn.Dropout(),
                                     nn.Linear(((256 * 6) * 6), 4096),
                                     nn.Relu(), nn.Dropout(),
                                     nn.Linear(4096, 4096), nn.Relu(),
                                     nn.Linear(4096, num_classes))
Exemple #20
0
    def __init__(self, n_downsample, input_dim, dim, style_dim, norm, activ,
                 pad_type):
        super(StyleEncoder, self).__init__()
        self.model = []
        self.model += [
            ConvBlock(input_dim,
                      dim,
                      7,
                      1,
                      3,
                      norm=norm,
                      activation=activ,
                      pad_type=pad_type)
        ]
        for i in range(2):
            self.model += [
                ConvBlock(dim,
                          2 * dim,
                          4,
                          2,
                          1,
                          norm=norm,
                          activation=activ,
                          pad_type=pad_type)
            ]
            dim *= 2
        for i in range(n_downsample - 2):
            self.model += [
                ConvBlock(dim,
                          dim,
                          4,
                          2,
                          1,
                          norm=norm,
                          activation=activ,
                          pad_type=pad_type)
            ]
        self.model += [nn.AdaptiveAvgPool2d(1)]  # global average pooling
        self.model += [nn.Conv(dim, style_dim, 1, 1, 0)]

        self.model = nn.Sequential(*self.model)

        self.output_dim = dim
Exemple #21
0
 def __init__(self, pool_size):
     super(PyramidPool, self).__init__()
     self.pool = nn.AdaptiveAvgPool2d(pool_size)
Exemple #22
0
 def _forward_impl(self, x):
     x = self.features(x)
     x = nn.AdaptiveAvgPool2d(1)(x)
     x = jt.reshape(x, (x.shape[0], -1))
     x = self.classifier(x)
     return x