def __init__(self, stacked_convs=2, feat_in=256, feat_out=256, num_classes=15, anchor_strides=[8, 16, 32, 64, 128], anchor_scales=[4], anchor_ratios=[1.0], target_means=0.0, target_stds=1.0, align_conv_type='AlignConv', align_conv_size=3, use_sigmoid_cls=True, anchor_assign=RBoxAssigner().__dict__, reg_loss_weight=[1.0, 1.0, 1.0, 1.0, 1.1], cls_loss_weight=[1.1, 1.05], reg_loss_type='l1'):
    super(S2ANetHead, self).__init__()
    self.stacked_convs = stacked_convs
    self.feat_in = feat_in
    self.feat_out = feat_out
    self.anchor_list = None
    self.anchor_scales = anchor_scales
    self.anchor_ratios = anchor_ratios
    self.anchor_strides = anchor_strides
    self.anchor_strides = paddle.to_tensor(anchor_strides)
    self.anchor_base_sizes = list(anchor_strides)
    self.means = (paddle.ones(shape=[5]) * target_means)
    self.stds = (paddle.ones(shape=[5]) * target_stds)
    assert (align_conv_type in ['AlignConv', 'Conv', 'DCN'])
    self.align_conv_type = align_conv_type
    self.align_conv_size = align_conv_size
    self.use_sigmoid_cls = use_sigmoid_cls
    self.cls_out_channels = (num_classes if self.use_sigmoid_cls else 1)
    self.sampling = False
    self.anchor_assign = anchor_assign
    self.reg_loss_weight = reg_loss_weight
    self.cls_loss_weight = cls_loss_weight
    self.alpha = 1.0
    self.beta = 1.0
    self.reg_loss_type = reg_loss_type
    self.s2anet_head_out = None
    self.anchor_generators = []
    for anchor_base in self.anchor_base_sizes:
        self.anchor_generators.append(S2ANetAnchorGenerator(anchor_base, anchor_scales, anchor_ratios))
    self.anchor_generators = nn.LayerList(self.anchor_generators)
    self.fam_cls_convs = nn.Sequential()
    self.fam_reg_convs = nn.Sequential()
    for i in range(self.stacked_convs):
        chan_in = (self.feat_in if (i == 0) else self.feat_out)
        self.fam_cls_convs.add_sublayer('fam_cls_conv_{}'.format(i), nn.Conv2D(in_channels=chan_in, out_channels=self.feat_out, kernel_size=3, padding=1, weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)), bias_attr=ParamAttr(initializer=Constant(0))))
        self.fam_cls_convs.add_sublayer('fam_cls_conv_{}_act'.format(i), nn.ReLU())
        self.fam_reg_convs.add_sublayer('fam_reg_conv_{}'.format(i), nn.Conv2D(in_channels=chan_in, out_channels=self.feat_out, kernel_size=3, padding=1, weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)), bias_attr=ParamAttr(initializer=Constant(0))))
        self.fam_reg_convs.add_sublayer('fam_reg_conv_{}_act'.format(i), nn.ReLU())
    self.fam_reg = nn.Conv2D(self.feat_out, 5, 1, weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)), bias_attr=ParamAttr(initializer=Constant(0)))
    prior_prob = 0.01
    bias_init = float((- np.log(((1 - prior_prob) / prior_prob))))
    self.fam_cls = nn.Conv2D(self.feat_out, self.cls_out_channels, 1, weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)), bias_attr=ParamAttr(initializer=Constant(bias_init)))
    if (self.align_conv_type == 'AlignConv'):
        self.align_conv = AlignConv(self.feat_out, self.feat_out, self.align_conv_size)
    elif (self.align_conv_type == 'Conv'):
        self.align_conv = nn.Conv2D(self.feat_out, self.feat_out, self.align_conv_size, padding=((self.align_conv_size - 1) // 2), bias_attr=ParamAttr(initializer=Constant(0)))
    elif (self.align_conv_type == 'DCN'):
        self.align_conv_offset = nn.Conv2D(self.feat_out, (2 * (self.align_conv_size ** 2)), 1, weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)), bias_attr=ParamAttr(initializer=Constant(0)))
        self.align_conv = paddle.vision.ops.DeformConv2D(self.feat_out, self.feat_out, self.align_conv_size, padding=((self.align_conv_size - 1) // 2), weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)), bias_attr=False)
    self.or_conv = nn.Conv2D(self.feat_out, self.feat_out, kernel_size=3, padding=1, weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)), bias_attr=ParamAttr(initializer=Constant(0)))
    self.odm_cls_convs = nn.Sequential()
    self.odm_reg_convs = nn.Sequential()
    for i in range(self.stacked_convs):
        ch_in = self.feat_out
        self.odm_cls_convs.add_sublayer('odm_cls_conv_{}'.format(i), nn.Conv2D(in_channels=ch_in, out_channels=self.feat_out, kernel_size=3, stride=1, padding=1, weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)), bias_attr=ParamAttr(initializer=Constant(0))))
        self.odm_cls_convs.add_sublayer('odm_cls_conv_{}_act'.format(i), nn.ReLU())
        self.odm_reg_convs.add_sublayer('odm_reg_conv_{}'.format(i), nn.Conv2D(in_channels=self.feat_out, out_channels=self.feat_out, kernel_size=3, stride=1, padding=1, weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)), bias_attr=ParamAttr(initializer=Constant(0))))
        self.odm_reg_convs.add_sublayer('odm_reg_conv_{}_act'.format(i), nn.ReLU())
    self.odm_cls = nn.Conv2D(self.feat_out, self.cls_out_channels, 3, padding=1, weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)), bias_attr=ParamAttr(initializer=Constant(bias_init)))
    self.odm_reg = nn.Conv2D(self.feat_out, 5, 3, padding=1, weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)), bias_attr=ParamAttr(initializer=Constant(0)))
    self.featmap_sizes = []
    self.base_anchors_list = []
    self.refine_anchor_list = []
def __init__(self, stacked_convs=2, feat_in=256, feat_out=256, num_classes=15, anchor_strides=[8, 16, 32, 64, 128], anchor_scales=[4], anchor_ratios=[1.0], target_means=0.0, target_stds=1.0, align_conv_type='AlignConv', align_conv_size=3, use_sigmoid_cls=True, anchor_assign=RBoxAssigner().__dict__, reg_loss_weight=[1.0, 1.0, 1.0, 1.0, 1.1], cls_loss_weight=[1.1, 1.05], reg_loss_type='l1'):
    super(S2ANetHead, self).__init__()
    self.stacked_convs = stacked_convs
    self.feat_in = feat_in
    self.feat_out = feat_out
    self.anchor_list = None
    self.anchor_scales = anchor_scales
    self.anchor_ratios = anchor_ratios
    self.anchor_strides = anchor_strides
    self.anchor_strides = paddle.to_tensor(anchor_strides)
    self.anchor_base_sizes = list(anchor_strides)
    self.means = (paddle.ones(shape=[5]) * target_means)
    self.stds = (paddle.ones(shape=[5]) * target_stds)
    assert (align_conv_type in ['AlignConv', 'Conv', 'DCN'])
    self.align_conv_type = align_conv_type
    self.align_conv_size = align_conv_size
    self.use_sigmoid_cls = use_sigmoid_cls
    self.cls_out_channels = (num_classes if self.use_sigmoid_cls else 1)
    self.sampling = False
    self.anchor_assign = anchor_assign
    self.reg_loss_weight = reg_loss_weight
    self.cls_loss_weight = cls_loss_weight
    self.alpha = 1.0
    self.beta = 1.0
    self.reg_loss_type = reg_loss_type
    self.s2anet_head_out = None
    self.anchor_generators = []
    for anchor_base in self.anchor_base_sizes:
        self.anchor_generators.append(S2ANetAnchorGenerator(anchor_base, anchor_scales, anchor_ratios))
    self.anchor_generators = nn.LayerList(self.anchor_generators)
    self.fam_cls_convs = nn.Sequential()
    self.fam_reg_convs = nn.Sequential()
    for i in range(self.stacked_convs):
        chan_in = (self.feat_in if (i == 0) else self.feat_out)
        self.fam_cls_convs.add_sublayer('fam_cls_conv_{}'.format(i), nn.Conv2D(in_channels=chan_in, out_channels=self.feat_out, kernel_size=3, padding=1, weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)), bias_attr=ParamAttr(initializer=Constant(0))))
        self.fam_cls_convs.add_sublayer('fam_cls_conv_{}_act'.format(i), nn.ReLU())
        self.fam_reg_convs.add_sublayer('fam_reg_conv_{}'.format(i), nn.Conv2D(in_channels=chan_in, out_channels=self.feat_out, kernel_size=3, padding=1, weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)), bias_attr=ParamAttr(initializer=Constant(0))))
        self.fam_reg_convs.add_sublayer('fam_reg_conv_{}_act'.format(i), nn.ReLU())
    self.fam_reg = nn.Conv2D(self.feat_out, 5, 1, weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)), bias_attr=ParamAttr(initializer=Constant(0)))
    prior_prob = 0.01
    bias_init = float((- np.log(((1 - prior_prob) / prior_prob))))
    self.fam_cls = nn.Conv2D(self.feat_out, self.cls_out_channels, 1, weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)), bias_attr=ParamAttr(initializer=Constant(bias_init)))
    if (self.align_conv_type == 'AlignConv'):
        self.align_conv = AlignConv(self.feat_out, self.feat_out, self.align_conv_size)
    elif (self.align_conv_type == 'Conv'):
        self.align_conv = nn.Conv2D(self.feat_out, self.feat_out, self.align_conv_size, padding=((self.align_conv_size - 1) // 2), bias_attr=ParamAttr(initializer=Constant(0)))
    elif (self.align_conv_type == 'DCN'):
        self.align_conv_offset = nn.Conv2D(self.feat_out, (2 * (self.align_conv_size ** 2)), 1, weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)), bias_attr=ParamAttr(initializer=Constant(0)))
        self.align_conv = paddle.vision.ops.DeformConv2D(self.feat_out, self.feat_out, self.align_conv_size, padding=((self.align_conv_size - 1) // 2), weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)), bias_attr=False)
    self.or_conv = nn.Conv2D(self.feat_out, self.feat_out, kernel_size=3, padding=1, weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)), bias_attr=ParamAttr(initializer=Constant(0)))
    self.odm_cls_convs = nn.Sequential()
    self.odm_reg_convs = nn.Sequential()
    for i in range(self.stacked_convs):
        ch_in = self.feat_out
        self.odm_cls_convs.add_sublayer('odm_cls_conv_{}'.format(i), nn.Conv2D(in_channels=ch_in, out_channels=self.feat_out, kernel_size=3, stride=1, padding=1, weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)), bias_attr=ParamAttr(initializer=Constant(0))))
        self.odm_cls_convs.add_sublayer('odm_cls_conv_{}_act'.format(i), nn.ReLU())
        self.odm_reg_convs.add_sublayer('odm_reg_conv_{}'.format(i), nn.Conv2D(in_channels=self.feat_out, out_channels=self.feat_out, kernel_size=3, stride=1, padding=1, weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)), bias_attr=ParamAttr(initializer=Constant(0))))
        self.odm_reg_convs.add_sublayer('odm_reg_conv_{}_act'.format(i), nn.ReLU())
    self.odm_cls = nn.Conv2D(self.feat_out, self.cls_out_channels, 3, padding=1, weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)), bias_attr=ParamAttr(initializer=Constant(bias_init)))
    self.odm_reg = nn.Conv2D(self.feat_out, 5, 3, padding=1, weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)), bias_attr=ParamAttr(initializer=Constant(0)))
    self.featmap_sizes = []
    self.base_anchors_list = []
    self.refine_anchor_list = []