Exemple #1
0
 def init_func(m):
     classname = m.__class__.__name__
     if (classname.find('Conv') != (-1)):
         init.gauss_(0.0, mean=gain)
     elif (classname.find('BatchNorm') != (-1)):
         init.gauss_(1.0, mean=0.02)
         m.bias.data.fill_(0)
Exemple #2
0
def weights_init_normal(m):
    classname = m.__class__.__name__
    if (classname.find('Linear') != (- 1)):
        init.gauss_(m.weight, mean=0.0, std=0.02)
    elif (classname.find('BatchNorm') != (- 1)):
        init.gauss_(m.weight, mean=1.0, std=0.02)
        init.constant_(m.bias, value=0.0)
Exemple #3
0
def make_conv3x3(
    in_channels,
    out_channels,
    dilation=1,
    stride=1,
    use_gn=False,
    use_relu=False,
    kaiming_init=True
):
    conv = Conv2d(
        in_channels,
        out_channels,
        kernel_size=3,
        stride=stride,
        padding=dilation,
        dilation=dilation,
        bias=False if use_gn else True
    )
    if kaiming_init:
        init.kaiming_normal_(
            conv.weight, mode="fan_out", nonlinearity="relu"
        )
    else:
        init.gauss_(conv.weight, std=0.01)
    if not use_gn:
        init.constant_(conv.bias, 0)
    module = [conv,]
    if use_gn:
        module.append(group_norm(out_channels))
    if use_relu:
        module.append(nn.ReLU())
    if len(module) > 1:
        return nn.Sequential(*module)
    return conv
Exemple #4
0
    def __init__(self, cfg, in_channels, num_anchors):
        """
        Arguments:
            cfg              : config
            in_channels (int): number of channels of the input feature
            num_anchors (int): number of anchors to be predicted
        """
        super(RPNHead, self).__init__()
        self.conv = nn.Conv(in_channels,
                            in_channels,
                            kernel_size=3,
                            stride=1,
                            padding=1)
        self.cls_logits = nn.Conv(in_channels,
                                  num_anchors,
                                  kernel_size=1,
                                  stride=1)
        self.bbox_pred = nn.Conv(in_channels,
                                 num_anchors * 4,
                                 kernel_size=1,
                                 stride=1)

        for l in [self.conv, self.cls_logits, self.bbox_pred]:
            init.gauss_(l.weight, std=0.01)
            init.constant_(l.bias, 0)
Exemple #5
0
    def __init__(self, cfg, in_channels):
        """
        Arguments:
            in_channels (int): number of channels of the input feature
            num_anchors (int): number of anchors to be predicted
        """
        super(RetinaNetHead, self).__init__()
        # TODO: Implement the sigmoid version first.
        num_classes = cfg.MODEL.RETINANET.NUM_CLASSES - 1
        num_anchors = len(cfg.MODEL.RETINANET.ASPECT_RATIOS) \
                        * cfg.MODEL.RETINANET.SCALES_PER_OCTAVE

        cls_tower = []
        bbox_tower = []
        for i in range(cfg.MODEL.RETINANET.NUM_CONVS):
            cls_tower.append(
                nn.Conv(
                    in_channels,
                    in_channels,
                    kernel_size=3,
                    stride=1,
                    padding=1
                )
            )
            cls_tower.append(nn.ReLU())
            bbox_tower.append(
                nn.Conv(
                    in_channels,
                    in_channels,
                    kernel_size=3,
                    stride=1,
                    padding=1
                )
            )
            bbox_tower.append(nn.ReLU())

        self.cls_tower = nn.Sequential(*cls_tower)
        self.bbox_tower =  nn.Sequential(*bbox_tower)
        self.cls_logits = nn.Conv(
            in_channels, num_anchors * num_classes, kernel_size=3, stride=1,
            padding=1
        )
        self.bbox_pred = nn.Conv(
            in_channels,  num_anchors * 4, kernel_size=3, stride=1,
            padding=1
        )

        # Initialization
        for modules in [self.cls_tower, self.bbox_tower, self.cls_logits,
                  self.bbox_pred]:
            for l in modules.modules():
                if isinstance(l, nn.Conv):
                    init.gauss_(l.weight, std=0.01)
                    init.constant_(l.bias, 0)


        # retinanet_bias_init
        prior_prob = cfg.MODEL.RETINANET.PRIOR_PROB
        bias_value = -math.log((1 - prior_prob) / prior_prob)
        init.constant_(self.cls_logits.bias, bias_value)
    def __init__(self, cfg):
        super(MaskIoUPredictor, self).__init__()
        num_classes = cfg.MODEL.ROI_BOX_HEAD.NUM_CLASSES

        self.maskiou = nn.Linear(1024, num_classes)

        init.gauss_(self.maskiou.weight, mean=0, std=0.01)
        init.constant_(self.maskiou.bias, 0)
Exemple #7
0
def weights_init_normal(m):
    classname = m.__class__.__name__
    if (classname.find('Conv') != (-1)):
        init.gauss_(m.weight, mean=0.0, std=0.02)
        if (hasattr(m, 'bias') and (m.bias is not None)):
            init.constant_(m.bias, value=0.0)
    elif (classname.find('BatchNorm') != (-1)):
        init.gauss_(m.weight, mean=1.0, std=0.02)
        init.constant_(m.bias, value=0.0)
    def __init__(self, cfg):
        super(MaskIoUGAPPredictor, self).__init__()
        num_classes = cfg.MODEL.ROI_BOX_HEAD.NUM_CLASSES
        in_channel = cfg.MODEL.ROI_MASK_HEAD.CONV_LAYERS[0]

        self.maskiou = nn.Linear(in_channel, num_classes)

        init.gauss_(self.maskiou.weight, mean=0, std=0.01)
        init.constant_(self.maskiou.bias, 0)
Exemple #9
0
def init_weights(m, mode='MSRAFill'):
    if isinstance(m, (nn.Conv, nn.ConvTranspose)):
        if mode == 'GaussianFill':
            init.gauss_(m.weight, std=0.001)
        elif mode == 'MSRAFill':
            MSRAFill(m.weight)
        else:
            raise ValueError
        if m.bias is not None:
            init.constant_(m.bias, 0)
    if isinstance(m, nn.Linear):
        XavierFill(m.weight)
        init.constant_(m.bias, 0)
 def _initialize_weights(self):
     for m in self.modules():
         if isinstance(m, Conv2d):
             n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
             init.gauss_(m.weight, 0, (2. / n)**0.5)
             if m.bias is not None:
                 init.constant_(m.bias, 0.0)
         # elif isinstance(m, BatchNorm2d):
         #     m.weight.data.fill_(1)
         #     m.bias.data.zero_()
         elif isinstance(m, nn.Linear):
             n = m.weight.shape[1]
             init.gauss_(m.weight, 0, 0.01)
             init.constant_(m.bias, 0.0)
Exemple #11
0
    def __init__(self, cfg, in_channels):
        super(FPNPredictor, self).__init__()
        num_classes = cfg.MODEL.ROI_BOX_HEAD.NUM_CLASSES
        representation_size = in_channels

        self.cls_score = nn.Linear(representation_size, num_classes)
        num_bbox_reg_classes = 2 if cfg.MODEL.CLS_AGNOSTIC_BBOX_REG else num_classes
        self.bbox_pred = nn.Linear(representation_size,
                                   num_bbox_reg_classes * 4)

        init.gauss_(self.cls_score.weight, std=0.01)
        init.gauss_(self.bbox_pred.weight, std=0.001)
        for l in [self.cls_score, self.bbox_pred]:
            init.constant_(l.bias, 0)
Exemple #12
0
    def __init__(self, cfg, in_channels):
        """
        Arguments:
            cfg              : config
            in_channels (int): number of channels of the input feature
        """
        super(RPNHeadFeatureSingleConv, self).__init__()
        self.conv = nn.Conv(
            in_channels, in_channels, kernel_size=3, stride=1, padding=1
        )

        for l in [self.conv]:
            init.gauss_(l.weight, std=0.01)
            init.constant_(l.bias, 0)

        self.out_channels = in_channels
Exemple #13
0
    def __init__(self, cfg, in_channels):
        super(FPNXconv1fcFeatureExtractor, self).__init__()

        resolution = cfg.MODEL.ROI_BOX_HEAD.POOLER_RESOLUTION
        scales = cfg.MODEL.ROI_BOX_HEAD.POOLER_SCALES
        sampling_ratio = cfg.MODEL.ROI_BOX_HEAD.POOLER_SAMPLING_RATIO
        pooler = Pooler(
            output_size=(resolution, resolution),
            scales=scales,
            sampling_ratio=sampling_ratio,
        )
        self.pooler = pooler

        use_gn = cfg.MODEL.ROI_BOX_HEAD.USE_GN
        conv_head_dim = cfg.MODEL.ROI_BOX_HEAD.CONV_HEAD_DIM
        num_stacked_convs = cfg.MODEL.ROI_BOX_HEAD.NUM_STACKED_CONVS
        dilation = cfg.MODEL.ROI_BOX_HEAD.DILATION

        xconvs = []
        for ix in range(num_stacked_convs):
            xconvs.append(
                nn.Conv(in_channels,
                        conv_head_dim,
                        kernel_size=3,
                        stride=1,
                        padding=dilation,
                        dilation=dilation,
                        bias=False if use_gn else True))
            in_channels = conv_head_dim
            if use_gn:
                xconvs.append(group_norm(in_channels))
            xconvs.append(nn.ReLU())

        self.xconvs = nn.Sequential(*xconvs)
        for modules in [
                self.xconvs,
        ]:
            for l in modules.modules():
                if isinstance(l, nn.Conv):
                    init.gauss_(l.weight, std=0.01)
                    if not use_gn:
                        init.constant_(l.bias, 0)

        input_size = conv_head_dim * resolution**2
        representation_size = cfg.MODEL.ROI_BOX_HEAD.MLP_HEAD_DIM
        self.fc6 = make_fc(input_size, representation_size, use_gn=False)
        self.out_channels = representation_size
Exemple #14
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 #15
0
    def __init__(self, in_channels, n_class, roi_size, spatial_scale,
                 sampling_ratio):
        # n_class includes the background
        super(RoIHead, self).__init__()

        self.classifier = nn.Sequential(
            nn.Linear(in_channels * roi_size * roi_size, 4096), nn.ReLU(),
            nn.Linear(4096, 4096), nn.ReLU())
        self.cls_loc = nn.Linear(4096, n_class * 4)
        self.score = nn.Linear(4096, n_class)

        self.n_class = n_class
        self.roi_size = roi_size
        self.spatial_scale = spatial_scale
        self.roi = ROIAlign((self.roi_size, self.roi_size),
                            self.spatial_scale,
                            sampling_ratio=sampling_ratio)

        init.gauss_(self.cls_loc.weight, 0, 0.001)
        init.constant_(self.cls_loc.bias, 0)
        init.gauss_(self.score.weight, 0, 0.01)
        init.constant_(self.score.bias, 0)
Exemple #16
0
def weights_init_normal(m):
    classname = m.__class__.__name__
    if classname.find("Conv") != -1:
        init.gauss_(m.weight, 0.0, 0.02)
        init.gauss_(m.bias, 0.0, 0.02)
    elif classname.find("BatchNorm") != -1:
        init.gauss_(m.weight, 1.0, 0.02)
        init.constant_(m.bias, 0.0)
Exemple #17
0
def initialize_weights(net):
    for m in net.modules():
        if isinstance(m, nn.Conv):
            init.gauss_(m.weight, 0, 0.02)
            init.constant_(m.bias, 0)
        elif isinstance(m, nn.ConvTranspose):
            init.gauss_(m.weight, 0, 0.02)
            init.constant_(m.bias, 0)
        elif isinstance(m, nn.Linear):
            init.gauss_(m.weight, 0, 0.02)
            init.constant_(m.bias, 0)
Exemple #18
0
 def _normal_init(self):
     for var in [self.conv1, self.score, self.loc]:
         init.gauss_(var.weight, 0, 0.01)
         init.constant_(var.bias, 0.0)
Exemple #19
0
 def init_conv2d(self):
     """ Initialize convolution parameters. """
     for c in self.children():
         if isinstance(c, nn.Conv):
             init.gauss_(c.weight)
Exemple #20
0
def weights_init_normal(m):
    classname = m.__class__.__name__
    if (classname.find('Conv') != (-1)):
        init.gauss_(m.weight, mean=0.0, std=0.02)
Exemple #21
0
def MSRAFill(tensor):
    """Caffe2 MSRAFill Implementation"""
    size = reduce(operator.mul, tensor.shape, 1)
    fan_out = size / tensor.shape[1]
    scale = math.sqrt(2 / fan_out)
    return init.gauss_(tensor, 0, scale)
Exemple #22
0
    def __init__(self,
                 layers,
                 cardinality=1,
                 base_width=64,
                 usefpn=False,
                 num_classes=None):
        super(ResnetXtFPN, self).__init__()
        self.cardinality = cardinality
        self.base_width = base_width
        self.usefpn = usefpn
        self.num_classes = num_classes

        self.conv1 = nn.Conv(3,
                             64,
                             kernel_size=7,
                             stride=2,
                             padding=3,
                             bias=False)
        self.bn1 = _BN(64)
        self.relu = nn.ReLU()
        self.maxpool = nn.Pool(kernel_size=3,
                               stride=2,
                               padding=1,
                               op='maximum')

        self.layer1 = self._make_layer(inplanes=64,
                                       outplanes=256,
                                       stride=1,
                                       widen_factor=1,
                                       blocks=layers[0])
        self.layer2 = self._make_layer(inplanes=256,
                                       outplanes=512,
                                       stride=2,
                                       widen_factor=2,
                                       blocks=layers[1])
        self.layer3 = self._make_layer(inplanes=512,
                                       outplanes=1024,
                                       stride=2,
                                       widen_factor=3,
                                       blocks=layers[2])
        if len(layers) == 4:
            self.layer4 = self._make_layer(inplanes=1024,
                                           outplanes=2048,
                                           stride=2,
                                           widen_factor=4,
                                           blocks=layers[3])
        else:
            self.layer4 = None

        if usefpn:
            # c1, c2, c3, c4 all conv to same channel
            self.fpn_c4p4 = nn.Conv(2048,
                                    256,
                                    kernel_size=1,
                                    stride=1,
                                    padding=0)
            self.fpn_c3p3 = nn.Conv(1024,
                                    256,
                                    kernel_size=1,
                                    stride=1,
                                    padding=0)
            self.fpn_c2p2 = nn.Conv(512,
                                    256,
                                    kernel_size=1,
                                    stride=1,
                                    padding=0)
            self.fpn_c1p1 = nn.Conv(256,
                                    256,
                                    kernel_size=1,
                                    stride=1,
                                    padding=0)

            self.fpn_p4 = nn.Conv(256, 256, kernel_size=3, stride=1, padding=1)
            self.fpn_p3 = nn.Conv(256, 256, kernel_size=3, stride=1, padding=1)
            self.fpn_p2 = nn.Conv(256, 256, kernel_size=3, stride=1, padding=1)
            self.fpn_p1 = nn.Conv(256, 256, kernel_size=3, stride=1, padding=1)

        if (num_classes is not None) and (not usefpn):
            self.avgpool = nn.Pool(7, stride=1, op='mean')
            self.fc = nn.Linear(2048, num_classes)

        for m in self.modules():
            if isinstance(m, nn.Conv):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                init.gauss_(m.weight, 0, math.sqrt(2. / n))
            elif isinstance(m, _BN):
                init.constant_(m.weight, 1.0)
                init.constant_(m.bias, 0.0)