def __init__(self, num_classes=80, width_mult=1.0, depthwise=False, in_channels=[256, 512, 1024], feat_channels=256, fpn_strides=(8, 16, 32), l1_epoch=285, act='silu', assigner=SimOTAAssigner(use_vfl=False), nms='MultiClassNMS', loss_weight={ 'cls': 1.0, 'obj': 1.0, 'iou': 5.0, 'l1': 1.0 }): super(YOLOXHead, self).__init__() self._dtype = paddle.framework.get_default_dtype() self.num_classes = num_classes assert len(in_channels) > 0, "in_channels length should > 0" self.in_channels = in_channels feat_channels = int(feat_channels * width_mult) self.fpn_strides = fpn_strides self.l1_epoch = l1_epoch self.assigner = assigner self.nms = nms self.loss_weight = loss_weight self.iou_loss = IouLoss(loss_weight=1.0) # default loss_weight 2.5 ConvBlock = DWConv if depthwise else BaseConv self.stem_conv = nn.LayerList() self.conv_cls = nn.LayerList() self.conv_reg = nn.LayerList() # reg [x,y,w,h] + obj for in_c in self.in_channels: self.stem_conv.append(BaseConv(in_c, feat_channels, 1, 1, act=act)) self.conv_cls.append( nn.Sequential(*[ ConvBlock(feat_channels, feat_channels, 3, 1, act=act), ConvBlock(feat_channels, feat_channels, 3, 1, act=act), nn.Conv2D(feat_channels, self.num_classes, 1, bias_attr=ParamAttr(regularizer=L2Decay(0.0))) ])) self.conv_reg.append( nn.Sequential(*[ ConvBlock(feat_channels, feat_channels, 3, 1, act=act), ConvBlock(feat_channels, feat_channels, 3, 1, act=act), nn.Conv2D( feat_channels, 4 + 1, # reg [x,y,w,h] + obj 1, bias_attr=ParamAttr(regularizer=L2Decay(0.0))) ])) self._init_weights()
def __init__(self, num_channels, filter_size, num_filters, stride, padding, channels=None, num_groups=1, act='hard_swish'): super(ConvBNLayer, self).__init__() self._conv = Conv2D(in_channels=num_channels, out_channels=num_filters, kernel_size=filter_size, stride=stride, padding=padding, groups=num_groups, weight_attr=ParamAttr(initializer=KaimingNormal()), bias_attr=False) self._batch_norm = BatchNorm( num_filters, act=act, param_attr=ParamAttr(regularizer=L2Decay(0.0)), bias_attr=ParamAttr(regularizer=L2Decay(0.0)))
def __init__(self, in_dim=256, mlp_dim=1024, resolution=7, num_stages=1): super(TwoFCHead, self).__init__() self.in_dim = in_dim self.mlp_dim = mlp_dim self.num_stages = num_stages fan = in_dim * resolution * resolution self.fc6_list = [] self.fc6_relu_list = [] self.fc7_list = [] self.fc7_relu_list = [] for stage in range(num_stages): fc6_name = 'fc6_{}'.format(stage) fc7_name = 'fc7_{}'.format(stage) fc6 = self.add_sublayer( fc6_name, nn.Linear(in_dim * resolution * resolution, mlp_dim, weight_attr=ParamAttr(initializer=XavierUniform( fan_out=fan)), bias_attr=ParamAttr(learning_rate=2., regularizer=L2Decay(0.)))) fc6_relu = self.add_sublayer(fc6_name + 'act', ReLU()) fc7 = self.add_sublayer( fc7_name, nn.Linear(mlp_dim, mlp_dim, weight_attr=ParamAttr(initializer=XavierUniform()), bias_attr=ParamAttr(learning_rate=2., regularizer=L2Decay(0.)))) fc7_relu = self.add_sublayer(fc7_name + 'act', ReLU()) self.fc6_list.append(fc6) self.fc6_relu_list.append(fc6_relu) self.fc7_list.append(fc7) self.fc7_relu_list.append(fc7_relu)
def __init__(self, num_classes, in_channels, loss_cfg=dict(name='CrossEntropyLoss'), drop_ratio=0.4, std=0.01, data_format="NCHW", fclr5=True, **kwargs): super().__init__(num_classes, in_channels, loss_cfg, **kwargs) self.drop_ratio = drop_ratio self.std = std # NOTE: global pool performance self.avgpool2d = AdaptiveAvgPool2D((1, 1), data_format=data_format) if self.drop_ratio != 0: self.dropout = Dropout(p=self.drop_ratio) else: self.dropout = None self.fc = Linear( self.in_channels, self.num_classes, weight_attr=ParamAttr(learning_rate=5.0 if fclr5 else 1.0, regularizer=L2Decay(1e-4)), bias_attr=ParamAttr(learning_rate=10.0 if fclr5 else 1.0, regularizer=L2Decay(0.0)))
def __init__(self, in_channels, out_channels, kernel_size=3, padding=1, conv_decay=0): super(SepConvLayer, self).__init__() self.dw_conv = nn.Conv2D( in_channels=in_channels, out_channels=in_channels, kernel_size=kernel_size, stride=1, padding=padding, groups=in_channels, weight_attr=ParamAttr(regularizer=L2Decay(conv_decay)), bias_attr=False) self.bn = nn.BatchNorm2D( in_channels, weight_attr=ParamAttr(regularizer=L2Decay(0.)), bias_attr=ParamAttr(regularizer=L2Decay(0.))) self.pw_conv = nn.Conv2D( in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=1, padding=0, weight_attr=ParamAttr(regularizer=L2Decay(conv_decay)), bias_attr=False)
def __init__(self, num_channels, num_filters, filter_size, stride=1, dilation=1, groups=1, act=None, name=None): super(ConvBNLayer, self).__init__() bn_decay = 0.0 self._conv = Conv2D(in_channels=num_channels, out_channels=num_filters, kernel_size=filter_size, stride=stride, padding=(filter_size - 1) // 2, dilation=dilation, groups=groups, weight_attr=ParamAttr(name=name + "_weight"), bias_attr=False) self._batch_norm = BatchNorm( num_filters, act=act, param_attr=ParamAttr(name=name + "_scale", regularizer=L2Decay(bn_decay)), bias_attr=ParamAttr(name + "_offset", regularizer=L2Decay(bn_decay)), moving_mean_name=name + "_mean", moving_variance_name=name + "_variance")
def __init__(self, in_c, out_c, filter_size, stride, padding, num_groups=1, if_act=True, act=None, use_cudnn=True, name=""): super(ConvBNLayer, self).__init__() self.if_act = if_act self.act = act self.conv = Conv2D(in_channels=in_c, out_channels=out_c, kernel_size=filter_size, stride=stride, padding=padding, groups=num_groups, weight_attr=ParamAttr(name=name + "_weights"), bias_attr=False) self.bn = BatchNorm(num_channels=out_c, act=None, param_attr=ParamAttr(name=name + "_bn_scale", regularizer=L2Decay(0.0)), bias_attr=ParamAttr(name=name + "_bn_offset", regularizer=L2Decay(0.0)), moving_mean_name=name + "_bn_mean", moving_variance_name=name + "_bn_variance")
def __init__(self, in_c, out_c, filter_size, stride, padding, num_groups=1, if_act=True, act=None): super().__init__() self.conv = Conv2D( in_channels=in_c, out_channels=out_c, kernel_size=filter_size, stride=stride, padding=padding, groups=num_groups, bias_attr=False) self.bn = BatchNorm( num_channels=out_c, act=None, param_attr=ParamAttr(regularizer=L2Decay(0.0)), bias_attr=ParamAttr(regularizer=L2Decay(0.0))) self.if_act = if_act self.act = _create_act(act)
def __init__(self, sparse_feature_dim, num_field, layer_sizes_cin): super(CIN, self).__init__() self.sparse_feature_dim = sparse_feature_dim self.num_field = num_field self.layer_sizes_cin = layer_sizes_cin self.cnn_layers = [] last_s = self.num_field for i in range(len(layer_sizes_cin)): _conv = nn.Conv2D( in_channels=last_s * self.num_field, out_channels=layer_sizes_cin[i], kernel_size=(1, 1), weight_attr=paddle.ParamAttr( regularizer=L2Decay(coeff=0.0001), initializer=paddle.nn.initializer.Normal( std=1.0 / math.sqrt(last_s * self.num_field))), bias_attr=False) last_s = layer_sizes_cin[i] self.add_sublayer('cnn_%d' % i, _conv) self.cnn_layers.append(_conv) tmp_sum = sum(self.layer_sizes_cin) self.cin_linear = paddle.nn.Linear( in_features=tmp_sum, out_features=1, weight_attr=paddle.ParamAttr( regularizer=L2Decay(coeff=0.0001), initializer=paddle.nn.initializer.Normal(std=0.1 / math.sqrt(tmp_sum)))) self.add_sublayer('cnn_fc', self.cin_linear)
def __init__(self, num_classes, in_channels, drop_ratio=0.5, std=0.001, data_format="NCHW", **kwargs): super().__init__(num_classes, in_channels, drop_ratio=drop_ratio, std=std, data_format=data_format, **kwargs) self.fc = Linear(self.in_channels, self.num_classes, weight_attr=ParamAttr(learning_rate=5.0, regularizer=L2Decay(1e-4)), bias_attr=ParamAttr(learning_rate=10.0, regularizer=L2Decay(0.0))) assert (data_format in [ 'NCHW', 'NHWC' ]), f"data_format must be 'NCHW' or 'NHWC', but got {data_format}" self.data_format = data_format self.stdv = std
def __init__(self, in_channels, out_channels, kernel_size, stride=1, groups=1, act=None, name=None, data_format="NCHW"): super(ConvBNLayer, self).__init__() self._conv = Conv2D(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=(kernel_size - 1) // 2, groups=groups, weight_attr=ParamAttr(name=name + "_weights"), bias_attr=False, data_format=data_format) if name == "conv1": bn_name = "bn_" + name else: bn_name = "bn" + name[3:] self._act = act self._batch_norm = BatchNorm2D( out_channels, weight_attr=ParamAttr(name=bn_name + "_scale", regularizer=L2Decay(0.0)), bias_attr=ParamAttr(name=bn_name + "_offset", regularizer=L2Decay(0.0)), data_format=data_format)
def __init__(self, ch_in, ch_out=128, num_classes=80, conv_num=2): super(HMHead, self).__init__() head_conv = nn.Sequential() for i in range(conv_num): name = 'conv.{}'.format(i) head_conv.add_sublayer( name, nn.Conv2D(in_channels=ch_in if i == 0 else ch_out, out_channels=ch_out, kernel_size=3, padding=1, weight_attr=ParamAttr(initializer=Normal(0, 0.01)), bias_attr=ParamAttr(learning_rate=2., regularizer=L2Decay(0.)))) head_conv.add_sublayer(name + '.act', nn.ReLU()) self.feat = self.add_sublayer('hm_feat', head_conv) bias_init = float(-np.log((1 - 0.01) / 0.01)) self.head = self.add_sublayer( 'hm_head', nn.Conv2D(in_channels=ch_out, out_channels=num_classes, kernel_size=1, weight_attr=ParamAttr(initializer=Normal(0, 0.01)), bias_attr=ParamAttr(learning_rate=2., regularizer=L2Decay(0.), initializer=Constant(bias_init))))
def __init__(self, channel, lr_mult, conv_decay, reduction=4, name=""): super(SEModule, self).__init__() self.avg_pool = nn.AdaptiveAvgPool2D(1) mid_channels = int(channel // reduction) self.conv1 = nn.Conv2D( in_channels=channel, out_channels=mid_channels, kernel_size=1, stride=1, padding=0, weight_attr=ParamAttr( learning_rate=lr_mult, regularizer=L2Decay(conv_decay), name=name + "_1_weights"), bias_attr=ParamAttr( learning_rate=lr_mult, regularizer=L2Decay(conv_decay), name=name + "_1_offset")) self.conv2 = nn.Conv2D( in_channels=mid_channels, out_channels=channel, kernel_size=1, stride=1, padding=0, weight_attr=ParamAttr( learning_rate=lr_mult, regularizer=L2Decay(conv_decay), name=name + "_2_weights"), bias_attr=ParamAttr( learning_rate=lr_mult, regularizer=L2Decay(conv_decay), name=name + "_2_offset"))
def __init__(self, in_channels, out_channels, kernel_size, stride, padding, num_groups=1, act='relu', conv_lr=1., conv_decay=0., norm_decay=0., norm_type='bn', name=None): super(ConvBNLayer, self).__init__() self.act = act self._conv = nn.Conv2D(in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding, groups=num_groups, weight_attr=ParamAttr( learning_rate=conv_lr, initializer=KaimingNormal(), regularizer=L2Decay(conv_decay)), bias_attr=False) param_attr = ParamAttr(regularizer=L2Decay(norm_decay)) bias_attr = ParamAttr(regularizer=L2Decay(norm_decay)) if norm_type in ['sync_bn', 'bn']: self._batch_norm = nn.BatchNorm2D(out_channels, weight_attr=param_attr, bias_attr=bias_attr)
def __init__(self, ch_in, ch_out, filter_size=3, stride=1, groups=1, padding=0, act="leaky", name=None): super(ConvBNLayer, self).__init__() self.conv = nn.Conv2d(in_channels=ch_in, out_channels=ch_out, kernel_size=filter_size, stride=stride, padding=padding, groups=groups, weight_attr=ParamAttr(name=name + '.conv.weights'), bias_attr=False) bn_name = name + '.bn' self.batch_norm = nn.BatchNorm2d( ch_out, weight_attr=ParamAttr(name=bn_name + '.scale', regularizer=L2Decay(0.)), bias_attr=ParamAttr(name=bn_name + '.offset', regularizer=L2Decay(0.))) self.act = act
def __init__(self, in_channels, out_channels, kernel_size, stride, padding, groups=1, act=None): super(ConvBNLayer, self).__init__() self._conv = Conv2D(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=padding, groups=groups, weight_attr=ParamAttr(initializer=KaimingNormal()), bias_attr=False) self._batch_norm = BatchNorm2D( out_channels, weight_attr=ParamAttr(regularizer=L2Decay(0.0)), bias_attr=ParamAttr(regularizer=L2Decay(0.0))) if act == "hard_swish": act = 'hardswish' self.act = act
def __init__(self, in_channels, out_channels, kernel_size, stride=1, groups=1, act="relu", name=None): super(ConvBNLayer, self).__init__() self._conv = Conv2D(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=(kernel_size - 1) // 2, groups=groups, weight_attr=ParamAttr(initializer=KaimingNormal(), name=name + "_weights"), bias_attr=False) bn_name = name + "_bn" self._batch_norm = BatchNorm( num_channels=out_channels, act=act, param_attr=ParamAttr(name=bn_name + "_scale", regularizer=L2Decay(0.0)), bias_attr=ParamAttr(name=bn_name + "_offset", regularizer=L2Decay(0.0)), moving_mean_name=bn_name + "_mean", moving_variance_name=bn_name + "_variance")
def __init__(self, in_c, out_c, filter_size, stride, padding, num_groups=1, act=None, lr_mult=1., conv_decay=0., norm_type='bn', norm_decay=0., freeze_norm=False, name=""): super(ConvBNLayer, self).__init__() self.act = act self.conv = nn.Conv2D( in_channels=in_c, out_channels=out_c, kernel_size=filter_size, stride=stride, padding=padding, groups=num_groups, weight_attr=ParamAttr( learning_rate=lr_mult, regularizer=L2Decay(conv_decay), name=name + "_weights"), bias_attr=False) norm_lr = 0. if freeze_norm else lr_mult param_attr = ParamAttr( learning_rate=norm_lr, regularizer=L2Decay(norm_decay), name=name + "_bn_scale", trainable=False if freeze_norm else True) bias_attr = ParamAttr( learning_rate=norm_lr, regularizer=L2Decay(norm_decay), name=name + "_bn_offset", trainable=False if freeze_norm else True) global_stats = True if freeze_norm else False if norm_type == 'sync_bn': self.bn = nn.SyncBatchNorm( out_c, weight_attr=param_attr, bias_attr=bias_attr) else: self.bn = nn.BatchNorm( out_c, act=None, param_attr=param_attr, bias_attr=bias_attr, use_global_stats=global_stats, moving_mean_name=name + '_bn_mean', moving_variance_name=name + '_bn_variance') norm_params = self.bn.parameters() if freeze_norm: for param in norm_params: param.stop_gradient = True
def __init__( self, num_classes=1, num_identifiers=1, # defined by dataset.total_identities anchor_levels=3, anchor_scales=4, embedding_dim=512, emb_loss='JDEEmbeddingLoss', jde_loss='JDELoss'): super(JDEEmbeddingHead, self).__init__() self.num_classes = num_classes self.num_identifiers = num_identifiers self.anchor_levels = anchor_levels self.anchor_scales = anchor_scales self.embedding_dim = embedding_dim self.emb_loss = emb_loss self.jde_loss = jde_loss self.emb_scale = math.sqrt(2) * math.log( self.num_identifiers - 1) if self.num_identifiers > 1 else 1 self.identify_outputs = [] self.loss_params_cls = [] self.loss_params_reg = [] self.loss_params_ide = [] for i in range(self.anchor_levels): name = 'identify_output.{}'.format(i) identify_output = self.add_sublayer( name, nn.Conv2D(in_channels=64 * (2**self.anchor_levels) // (2**i), out_channels=self.embedding_dim, kernel_size=3, stride=1, padding=1, weight_attr=ParamAttr(name=name + '.conv.weights'), bias_attr=ParamAttr(name=name + '.conv.bias', regularizer=L2Decay(0.)))) self.identify_outputs.append(identify_output) loss_p_cls = self.add_sublayer('cls.{}'.format(i), LossParam(-4.15)) self.loss_params_cls.append(loss_p_cls) loss_p_reg = self.add_sublayer('reg.{}'.format(i), LossParam(-4.85)) self.loss_params_reg.append(loss_p_reg) loss_p_ide = self.add_sublayer('ide.{}'.format(i), LossParam(-2.3)) self.loss_params_ide.append(loss_p_ide) self.classifier = self.add_sublayer( 'classifier', nn.Linear(self.embedding_dim, self.num_identifiers, weight_attr=ParamAttr(learning_rate=1., initializer=Normal(mean=0.0, std=0.01)), bias_attr=ParamAttr(learning_rate=2., regularizer=L2Decay(0.))))
def __init__( self, ch_in, ch_out=128, num_classes=80, conv_num=2, dcn_head=False, lite_head=False, norm_type='bn', ): super(HMHead, self).__init__() head_conv = nn.Sequential() for i in range(conv_num): name = 'conv.{}'.format(i) if lite_head: lite_name = 'hm.' + name head_conv.add_sublayer( lite_name, LiteConv(in_channels=ch_in if i == 0 else ch_out, out_channels=ch_out, norm_type=norm_type)) else: if dcn_head: head_conv.add_sublayer( name, DeformableConvV2( in_channels=ch_in if i == 0 else ch_out, out_channels=ch_out, kernel_size=3, weight_attr=ParamAttr( initializer=Normal(0, 0.01)))) else: head_conv.add_sublayer( name, nn.Conv2D( in_channels=ch_in if i == 0 else ch_out, out_channels=ch_out, kernel_size=3, padding=1, weight_attr=ParamAttr(initializer=Normal(0, 0.01)), bias_attr=ParamAttr(learning_rate=2., regularizer=L2Decay(0.)))) head_conv.add_sublayer(name + '.act', nn.ReLU()) self.feat = head_conv bias_init = float(-np.log((1 - 0.01) / 0.01)) weight_attr = None if lite_head else ParamAttr( initializer=Normal(0, 0.01)) self.head = nn.Conv2D(in_channels=ch_out, out_channels=num_classes, kernel_size=1, weight_attr=weight_attr, bias_attr=ParamAttr( learning_rate=2., regularizer=L2Decay(0.), initializer=Constant(bias_init)))
def __init__(self, ch_in, ch_out, filter_size, stride=1, groups=1, norm_type=None, norm_groups=32, norm_decay=0., freeze_norm=False, act=None): super(ConvNormLayer, self).__init__() self.act = act norm_lr = 0. if freeze_norm else 1. if norm_type is not None: assert norm_type in ['bn', 'sync_bn', 'gn'], \ "norm_type should be one of ['bn', 'sync_bn', 'gn'], but got {}".format(norm_type) param_attr = ParamAttr( initializer=Constant(1.0), learning_rate=norm_lr, regularizer=L2Decay(norm_decay), ) bias_attr = ParamAttr(learning_rate=norm_lr, regularizer=L2Decay(norm_decay)) global_stats = True if freeze_norm else None if norm_type in ['bn', 'sync_bn']: self.norm = nn.BatchNorm2D( ch_out, weight_attr=param_attr, bias_attr=bias_attr, use_global_stats=global_stats, ) elif norm_type == 'gn': self.norm = nn.GroupNorm(num_groups=norm_groups, num_channels=ch_out, weight_attr=param_attr, bias_attr=bias_attr) norm_params = self.norm.parameters() if freeze_norm: for param in norm_params: param.stop_gradient = True conv_bias_attr = False else: conv_bias_attr = True self.norm = None self.conv = nn.Conv2D( in_channels=ch_in, out_channels=ch_out, kernel_size=filter_size, stride=stride, padding=(filter_size - 1) // 2, groups=groups, weight_attr=ParamAttr(initializer=Normal(mean=0., std=0.001)), bias_attr=conv_bias_attr)
def __init__(self, ch_in, ch_out, filter_size, stride=1, norm_type='bn', norm_groups=32, use_dcn=False, norm_decay=0., freeze_norm=False, act=None, name=None): super(ConvNormLayer, self).__init__() assert norm_type in ['bn', 'sync_bn', 'gn'] self.act = act self.conv = nn.Conv2D(in_channels=ch_in, out_channels=ch_out, kernel_size=filter_size, stride=stride, padding=(filter_size - 1) // 2, groups=1, weight_attr=ParamAttr(name=name + "_weights", initializer=Normal( mean=0., std=0.01)), bias_attr=False) norm_lr = 0. if freeze_norm else 1. norm_name = name + '_bn' param_attr = ParamAttr(name=norm_name + "_scale", learning_rate=norm_lr, regularizer=L2Decay(norm_decay)) bias_attr = ParamAttr(name=norm_name + "_offset", learning_rate=norm_lr, regularizer=L2Decay(norm_decay)) global_stats = True if freeze_norm else False if norm_type in ['bn', 'sync_bn']: self.norm = nn.BatchNorm(ch_out, param_attr=param_attr, bias_attr=bias_attr, use_global_stats=global_stats, moving_mean_name=norm_name + '_mean', moving_variance_name=norm_name + '_variance') elif norm_type == 'gn': self.norm = nn.GroupNorm(num_groups=norm_groups, num_channels=ch_out, weight_attr=param_attr, bias_attr=bias_attr) norm_params = self.norm.parameters() if freeze_norm: for param in norm_params: param.stop_gradient = True
def __init__(self, ch_in, ch_out, filter_size, stride, name_adapter, act=None, norm_type='bn', norm_decay=0., freeze_norm=True, lr=1.0, name=None): super(ConvNormLayer, self).__init__() assert norm_type in ['bn', 'sync_bn'] self.norm_type = norm_type self.act = act self.conv = Conv2D(in_channels=ch_in, out_channels=ch_out, kernel_size=filter_size, stride=stride, padding=(filter_size - 1) // 2, groups=1, weight_attr=ParamAttr(learning_rate=lr, name=name + "_weights"), bias_attr=False) bn_name = name_adapter.fix_conv_norm_name(name) norm_lr = 0. if freeze_norm else lr param_attr = ParamAttr(learning_rate=norm_lr, regularizer=L2Decay(norm_decay), name=bn_name + "_scale", trainable=False if freeze_norm else True) bias_attr = ParamAttr(learning_rate=norm_lr, regularizer=L2Decay(norm_decay), name=bn_name + "_offset", trainable=False if freeze_norm else True) global_stats = True if freeze_norm else False self.norm = BatchNorm(ch_out, act=act, param_attr=param_attr, bias_attr=bias_attr, use_global_stats=global_stats, moving_mean_name=bn_name + '_mean', moving_variance_name=bn_name + '_variance') norm_params = self.norm.parameters() if freeze_norm: for param in norm_params: param.stop_gradient = True
def __init__(self, in_chans, embed_dim=768, s=1): super().__init__() self.proj = nn.Sequential( nn.Conv2D( in_chans, embed_dim, 3, s, 1, bias_attr=paddle.ParamAttr(regularizer=L2Decay(0.0)), groups=embed_dim, weight_attr=paddle.ParamAttr(regularizer=L2Decay(0.0)), )) self.s = s
def __init__(self, ch_in, ch_out=64, conv_num=2, dcn_head=False, lite_head=False, norm_type='bn'): super(WHHead, self).__init__() head_conv = nn.Sequential() for i in range(conv_num): name = 'conv.{}'.format(i) if lite_head: lite_name = 'wh.' + name head_conv.add_sublayer( lite_name, LiteConv(in_channels=ch_in if i == 0 else ch_out, out_channels=ch_out, norm_type=norm_type)) head_conv.add_sublayer(lite_name + '.act', nn.ReLU6()) else: if dcn_head: head_conv.add_sublayer( name, DeformableConvV2( in_channels=ch_in if i == 0 else ch_out, out_channels=ch_out, kernel_size=3, weight_attr=ParamAttr( initializer=Normal(0, 0.01)))) else: head_conv.add_sublayer( name, nn.Conv2D( in_channels=ch_in if i == 0 else ch_out, out_channels=ch_out, kernel_size=3, padding=1, weight_attr=ParamAttr(initializer=Normal(0, 0.01)), bias_attr=ParamAttr(learning_rate=2., regularizer=L2Decay(0.)))) head_conv.add_sublayer(name + '.act', nn.ReLU()) self.feat = head_conv self.head = nn.Conv2D( in_channels=ch_out, out_channels=4, kernel_size=1, weight_attr=ParamAttr(initializer=Normal(0, 0.001)), bias_attr=ParamAttr(learning_rate=2., regularizer=L2Decay(0.)))
def __init__(self, ch_in, ch_out, filter_size, stride, norm_type='bn', norm_groups=32, use_dcn=False, norm_name=None, bias_on=False, lr_scale=1., name=None): super(ConvNormLayer, self).__init__() assert norm_type in ['bn', 'sync_bn', 'gn'] if bias_on: bias_attr = ParamAttr(name=name + "_bias", initializer=Constant(value=0.), learning_rate=lr_scale) else: bias_attr = False self.conv = nn.Conv2D(in_channels=ch_in, out_channels=ch_out, kernel_size=filter_size, stride=stride, padding=(filter_size - 1) // 2, groups=1, weight_attr=ParamAttr(name=name + "_weight", initializer=Normal( mean=0., std=0.01), learning_rate=1.), bias_attr=bias_attr) param_attr = ParamAttr(name=norm_name + "_scale", learning_rate=1., regularizer=L2Decay(0.)) bias_attr = ParamAttr(name=norm_name + "_offset", learning_rate=1., regularizer=L2Decay(0.)) if norm_type in ['bn', 'sync_bn']: self.norm = nn.BatchNorm2D(ch_out, weight_attr=param_attr, bias_attr=bias_attr) elif norm_type == 'gn': self.norm = nn.GroupNorm(num_groups=norm_groups, num_channels=ch_out, weight_attr=param_attr, bias_attr=bias_attr)
def __init__(self, anchors=[[10, 13], [16, 30], [33, 23], [30, 61], [62, 45], [59, 119], [116, 90], [156, 198], [373, 326]], anchor_masks=[[6, 7, 8], [3, 4, 5], [0, 1, 2]], num_classes=80, loss='YOLOv3Loss'): super(YOLOv3Head, self).__init__() self.num_classes = num_classes self.loss = loss self.parse_anchor(anchors, anchor_masks) self.num_outputs = len(self.anchors) self.yolo_outputs = [] for i in range(len(self.anchors)): num_filters = self.num_outputs * (self.num_classes + 5) name = 'yolo_output.{}'.format(i) yolo_output = self.add_sublayer( name, nn.Conv2D(in_channels=1024 // (2**i), out_channels=num_filters, kernel_size=1, stride=1, padding=0, weight_attr=ParamAttr(name=name + '.conv.weights'), bias_attr=ParamAttr(name=name + '.conv.bias', regularizer=L2Decay(0.)))) self.yolo_outputs.append(yolo_output)
def __init__(self, ch_in: int, ch_out: int, filter_size: int = 3, stride: int = 1, groups: int = 1, padding: int = 0, act: str = 'leakly', is_test: bool = False): super(ConvBNLayer, self).__init__() self.conv = nn.Conv2d( ch_in, ch_out, filter_size, padding=padding, stride=stride, groups=groups, weight_attr=paddle.ParamAttr(initializer=Normal(0., 0.02)), bias_attr=False) self.batch_norm = nn.BatchNorm(num_channels=ch_out, is_test=is_test, param_attr=paddle.ParamAttr( initializer=Normal(0., 0.02), regularizer=L2Decay(0.))) self.act = act
def __init__(self, in_channels, out_channel, min_level=0, max_level=4, spatial_scale=[0.25, 0.125, 0.0625, 0.03125]): super(FPN, self).__init__() self.lateral_convs = [] self.fpn_convs = [] fan = out_channel * 3 * 3 for i in range(min_level, max_level): if i == 3: lateral_name = 'fpn_inner_res5_sum' else: lateral_name = 'fpn_inner_res{}_sum_lateral'.format(i + 2) in_c = in_channels[i] lateral = self.add_sublayer( lateral_name, Conv2D( in_channels=in_c, out_channels=out_channel, kernel_size=1, weight_attr=ParamAttr( initializer=XavierUniform(fan_out=in_c)), bias_attr=ParamAttr( learning_rate=2., regularizer=L2Decay(0.)))) self.lateral_convs.append(lateral) fpn_name = 'fpn_res{}_sum'.format(i + 2) fpn_conv = self.add_sublayer( fpn_name, Conv2D( in_channels=out_channel, out_channels=out_channel, kernel_size=3, padding=1, weight_attr=ParamAttr( initializer=XavierUniform(fan_out=fan)), bias_attr=ParamAttr( learning_rate=2., regularizer=L2Decay(0.)))) self.fpn_convs.append(fpn_conv) self.min_level = min_level self.max_level = max_level self.spatial_scale = spatial_scale
def __init__(self, mask_roi_extractor=None, num_convs=0, feat_in=2048, feat_out=256, mask_num_stages=1, share_bbox_feat=False): super(MaskFeat, self).__init__() self.num_convs = num_convs self.feat_in = feat_in self.feat_out = feat_out self.mask_roi_extractor = mask_roi_extractor self.mask_num_stages = mask_num_stages self.share_bbox_feat = share_bbox_feat self.upsample_module = [] fan_conv = feat_out * 3 * 3 fan_deconv = feat_out * 2 * 2 for i in range(self.mask_num_stages): name = 'stage_{}'.format(i) mask_conv = Sequential() for j in range(self.num_convs): conv_name = 'mask_inter_feat_{}'.format(j + 1) mask_conv.add_sublayer( conv_name, Conv2D(in_channels=feat_in if j == 0 else feat_out, out_channels=feat_out, kernel_size=3, padding=1, weight_attr=ParamAttr(initializer=KaimingNormal( fan_in=fan_conv)), bias_attr=ParamAttr(learning_rate=2., regularizer=L2Decay(0.)))) mask_conv.add_sublayer(conv_name + 'act', ReLU()) mask_conv.add_sublayer( 'conv5_mask', Conv2DTranspose( in_channels=self.feat_in, out_channels=self.feat_out, kernel_size=2, stride=2, weight_attr=ParamAttr(initializer=KaimingNormal( fan_in=fan_deconv)), bias_attr=ParamAttr(learning_rate=2., regularizer=L2Decay(0.)))) mask_conv.add_sublayer('conv5_mask' + 'act', ReLU()) upsample = self.add_sublayer(name, mask_conv) self.upsample_module.append(upsample)