def __init__(self, cfg, input_shape: List[ShapeSpec]): super().__init__(cfg, input_shape) head_params = cfg.MODEL.META_ARCH self.name = head_params.NAME self.in_features = head_params.IN_FEATURES self.num_points = head_params.NUM_POINTS assert self.num_points == 2 self.center_sampling_radius = head_params.CENTER_SAMPLING_RADIUS self.norm_reg_targets = head_params.NORM_REG_TARGETS self.centerness_on_loc = head_params.CENTERNESS_ON_LOC self.iou_loss_type = head_params.IOU_LOSS_TYPE self.pre_nms_thresh = head_params.PRE_NMS_THRESH self.pre_nms_top_n = head_params.PRE_NMS_TOP_N self.slender_centerness = head_params.SLENDER_CENTERNESS # init bbox pred self.loc_init_conv = nn.Conv2d(self.feat_channels, self.loc_feat_channels, 3, 1, 1) self.loc_init_out = nn.Conv2d(self.loc_feat_channels, self.num_points * 2, 1, 1, 0) # make feature adaptive layer self.cls_conv, self.loc_refine_conv = self.make_feature_adaptive_layers( ) self._prepare_offset() self.cls_out = nn.Conv2d(self.feat_channels, self.num_classes, 1, 1, 0) self.ctn_out = nn.Conv2d(self.feat_channels, 1, 1, 1, 0) self.loc_refine_out = nn.Conv2d(self.loc_feat_channels, self.num_points * 2, 1, 1, 0) self.scales_init = nn.ModuleList( [Scale(init_value=1.0) for _ in range(5)]) self.scales_refine = nn.ModuleList( [Scale(init_value=1.0) for _ in range(5)]) self._init_weights()
def __init__(self, cfg, input_shape: List[ShapeSpec]): """ Arguments: in_channels (int): number of channels of the input feature """ super(FCOSHead, self).__init__() # TODO: Implement the sigmoid version first. # fmt: off in_channels = input_shape[0].channels num_classes = cfg.MODEL.FCOS.NUM_CLASSES self.fpn_strides = cfg.MODEL.FCOS.FPN_STRIDES self.norm_reg_targets = cfg.MODEL.FCOS.NORM_REG_TARGETS self.centerness_on_reg = cfg.MODEL.FCOS.CENTERNESS_ON_REG self.use_dcn_in_tower = cfg.MODEL.FCOS.USE_DCN_IN_TOWER self.use_dcn_v2 = cfg.MODEL.FCOS.USE_DCN_V2 # fmt: on cls_tower = [] bbox_tower = [] for i in range(cfg.MODEL.FCOS.NUM_CONVS): use_dcn = False use_v2 = True if self.use_dcn_in_tower and i == cfg.MODEL.FCOS.NUM_CONVS - 1: conv_func = DFConv2d bias = False use_dcn = True if not self.use_dcn_v2: use_v2 = False else: conv_func = nn.Conv2d bias = True if use_dcn and not use_v2: cls_tower.append( conv_func(in_channels, in_channels, with_modulated_dcn=False, kernel_size=3, stride=1, padding=1, bias=bias)) else: cls_tower.append( conv_func(in_channels, in_channels, kernel_size=3, stride=1, padding=1, bias=bias)) cls_tower.append(nn.GroupNorm(32, in_channels)) cls_tower.append(nn.ReLU()) if use_dcn and not use_v2: bbox_tower.append( conv_func(in_channels, in_channels, with_modulated_dcn=False, kernel_size=3, stride=1, padding=1, bias=bias)) else: bbox_tower.append( conv_func(in_channels, in_channels, kernel_size=3, stride=1, padding=1, bias=bias)) bbox_tower.append(nn.GroupNorm(32, in_channels)) bbox_tower.append(nn.ReLU()) self.add_module('cls_tower', nn.Sequential(*cls_tower)) self.add_module('bbox_tower', nn.Sequential(*bbox_tower)) self.cls_logits = nn.Conv2d(in_channels, num_classes, kernel_size=3, stride=1, padding=1) self.bbox_pred = nn.Conv2d(in_channels, 4, kernel_size=3, stride=1, padding=1) self.centerness = nn.Conv2d(in_channels, 1, kernel_size=3, stride=1, padding=1) # initialization for modules in [ self.cls_tower, self.bbox_tower, self.cls_logits, self.bbox_pred, self.centerness ]: for l in modules.modules(): if isinstance(l, nn.Conv2d): torch.nn.init.normal_(l.weight, std=0.01) torch.nn.init.constant_(l.bias, 0) # initialize the bias for focal loss prior_prob = cfg.MODEL.FCOS.PRIOR_PROB bias_value = -math.log((1 - prior_prob) / prior_prob) torch.nn.init.constant_(self.cls_logits.bias, bias_value) self.scales = nn.ModuleList([Scale(init_value=1.0) for _ in range(5)])
def __init__(self, cfg, input_shape: List[ShapeSpec]): """ Arguments: in_channels (int): number of channels of the input feature """ super(FCOSRepPointsHead, self).__init__() # TODO: Implement the sigmoid version first. # fmt: off in_channels = input_shape[0].channels num_classes = cfg.MODEL.FCOS.NUM_CLASSES self.fpn_strides = cfg.MODEL.FCOS.FPN_STRIDES self.norm_reg_targets = cfg.MODEL.FCOS.NORM_REG_TARGETS self.centerness_on_reg = cfg.MODEL.FCOS.CENTERNESS_ON_REG self.use_dcn_in_tower = cfg.MODEL.FCOS.USE_DCN_IN_TOWER self.use_dcn_v2 = cfg.MODEL.FCOS.USE_DCN_V2 # fmt: on cls_tower = [] bbox_tower = [] for i in range(cfg.MODEL.FCOS.NUM_CONVS): use_dcn = False use_v2 = True if self.use_dcn_in_tower and i == cfg.MODEL.FCOS.NUM_CONVS - 1: conv_func = DFConv2d bias = False use_dcn = True if not self.use_dcn_v2: use_v2 = False else: conv_func = nn.Conv2d bias = True if use_dcn and not use_v2: cls_tower.append( conv_func( in_channels, in_channels, with_modulated_dcn=False, kernel_size=3, stride=1, padding=1, bias=bias ) ) else: cls_tower.append( conv_func(in_channels, in_channels, kernel_size=3, stride=1, padding=1, bias=bias) ) cls_tower.append(nn.GroupNorm(32, in_channels)) cls_tower.append(nn.ReLU()) if use_dcn and not use_v2: bbox_tower.append( conv_func( in_channels, in_channels, with_modulated_dcn=False, kernel_size=3, stride=1, padding=1, bias=bias ) ) else: bbox_tower.append( conv_func(in_channels, in_channels, kernel_size=3, stride=1, padding=1, bias=bias) ) bbox_tower.append(nn.GroupNorm(32, in_channels)) bbox_tower.append(nn.ReLU()) self.add_module('cls_tower', nn.Sequential(*cls_tower)) self.add_module('bbox_tower', nn.Sequential(*bbox_tower)) # rep part self.point_feat_channels = in_channels self.num_points = 9 self.dcn_kernel = int(np.sqrt(self.num_points)) self.dcn_pad = int((self.dcn_kernel - 1) / 2) self.cls_out_channels = num_classes self.gradient_mul = 0.1 dcn_base = np.arange(-self.dcn_pad, self.dcn_pad + 1).astype(np.float64) dcn_base_y = np.repeat(dcn_base, self.dcn_kernel) dcn_base_x = np.tile(dcn_base, self.dcn_kernel) dcn_base_offset = np.stack([dcn_base_y, dcn_base_x], axis=1).reshape((-1)) dcn_base_offset = torch.tensor(dcn_base_offset, dtype=torch.float32).view(1, -1, 1, 1) self.register_buffer("dcn_base_offset", dcn_base_offset) self.deform_cls_conv = DeformConv( self.point_feat_channels, self.point_feat_channels, self.dcn_kernel, 1, self.dcn_pad) self.deform_reg_conv = DeformConv( self.point_feat_channels, self.point_feat_channels, self.dcn_kernel, 1, self.dcn_pad) points_out_dim = 2 * self.num_points self.offsets_init = nn.Sequential( nn.Conv2d(self.point_feat_channels, self.point_feat_channels, 3, 1, 1), nn.ReLU(inplace=True), nn.Conv2d(self.point_feat_channels, points_out_dim, 1, 1, 0)) self.offsets_refine = nn.Sequential( nn.ReLU(), nn.Conv2d(self.point_feat_channels, points_out_dim, 1, 1, 0)) self.logits = nn.Sequential( nn.ReLU(), nn.Conv2d(self.point_feat_channels, self.cls_out_channels, 1, 1, 0)) # self.cls_logits = nn.Conv2d(in_channels, num_classes, kernel_size=3, stride=1, padding=1) # self.bbox_pred = nn.Conv2d(in_channels, 4, kernel_size=3, stride=1, padding=1) self.centerness = nn.Conv2d(in_channels, 1, kernel_size=3, stride=1, padding=1) self.ratio = nn.Conv2d(in_channels, 1, kernel_size=3, stride=1, padding=1) # initialization for modules in [self.cls_tower, self.bbox_tower, # self.cls_logits, self.bbox_pred, self.offsets_init, self.offsets_refine, self.deform_cls_conv, self.deform_reg_conv, self.centerness]: for l in modules.modules(): if isinstance(l, nn.Conv2d): torch.nn.init.normal_(l.weight, std=0.01) torch.nn.init.constant_(l.bias, 0) # initialize the bias for focal loss prior_prob = cfg.MODEL.FCOS.PRIOR_PROB bias_value = -math.log((1 - prior_prob) / prior_prob) # torch.nn.init.constant_(self.cls_logits.bias, bias_value) for module in self.logits.modules(): if hasattr(module, 'bias') and module.bias is not None: torch.nn.init.constant_(module.bias, bias_value) self.scales = nn.ModuleList([Scale(init_value=1.0) for _ in range(5)])