paddlenlp/transformers/layoutxlm/visual_backbone.py
def build_resnet_backbone(cfg, input_shape=None):
    """
    Create a ResNet instance from config.

    Returns:
        ResNet: a :class:`ResNet` instance.
    """
    # need registration of new blocks/stems?
    if input_shape is None:
        ch = 3
    else:
        ch = input_shape.channels
    norm = cfg.MODEL.RESNETS.NORM
    stem = BasicStem(
        in_channels=ch,
        out_channels=cfg.MODEL.RESNETS.STEM_OUT_CHANNELS,
        norm=norm, )

    # fmt: off
    freeze_at = cfg.MODEL.BACKBONE.FREEZE_AT  # default as 2
    out_features = cfg.MODEL.RESNETS.OUT_FEATURES
    depth = cfg.MODEL.RESNETS.DEPTH
    num_groups = cfg.MODEL.RESNETS.NUM_GROUPS
    width_per_group = cfg.MODEL.RESNETS.WIDTH_PER_GROUP
    bottleneck_channels = num_groups * width_per_group
    in_channels = cfg.MODEL.RESNETS.STEM_OUT_CHANNELS
    out_channels = cfg.MODEL.RESNETS.RES2_OUT_CHANNELS
    stride_in_1x1 = cfg.MODEL.RESNETS.STRIDE_IN_1X1
    res5_dilation = cfg.MODEL.RESNETS.RES5_DILATION
    deform_on_per_stage = cfg.MODEL.RESNETS.DEFORM_ON_PER_STAGE
    deform_modulated = cfg.MODEL.RESNETS.DEFORM_MODULATED
    deform_num_groups = cfg.MODEL.RESNETS.DEFORM_NUM_GROUPS
    # fmt: on
    assert res5_dilation in {1, 2}, "res5_dilation cannot be {}.".format(
        res5_dilation)

    num_blocks_per_stage = {
        18: [2, 2, 2, 2],
        34: [3, 4, 6, 3],
        50: [3, 4, 6, 3],
        101: [3, 4, 23, 3],
        152: [3, 8, 36, 3],
    }[depth]

    if depth in [18, 34]:
        assert out_channels == 64, "Must set MODEL.RESNETS.RES2_OUT_CHANNELS = 64 for R18/R34"
        assert not any(
            deform_on_per_stage
        ), "MODEL.RESNETS.DEFORM_ON_PER_STAGE unsupported for R18/R34"
        assert res5_dilation == 1, "Must set MODEL.RESNETS.RES5_DILATION = 1 for R18/R34"
        assert num_groups == 1, "Must set MODEL.RESNETS.NUM_GROUPS = 1 for R18/R34"

    stages = []

    for idx, stage_idx in enumerate(range(2, 6)):
        # res5_dilation is used this way as a convention in R-FCN & Deformable Conv paper
        dilation = res5_dilation if stage_idx == 5 else 1
        first_stride = 1 if idx == 0 or (stage_idx == 5 and
                                         dilation == 2) else 2
        stage_kargs = {
            "num_blocks": num_blocks_per_stage[idx],
            "stride_per_block":
            [first_stride] + [1] * (num_blocks_per_stage[idx] - 1),
            "in_channels": in_channels,
            "out_channels": out_channels,
            "norm": norm,
        }
        # Use BasicBlock for R18 and R34.
        if depth in [18, 34]:
            stage_kargs["block_class"] = BasicBlock
        else:
            stage_kargs["bottleneck_channels"] = bottleneck_channels
            stage_kargs["stride_in_1x1"] = stride_in_1x1
            stage_kargs["dilation"] = dilation
            stage_kargs["num_groups"] = num_groups
            if deform_on_per_stage[idx]:
                stage_kargs["block_class"] = DeformBottleneckBlock
                stage_kargs["deform_modulated"] = deform_modulated
                stage_kargs["deform_num_groups"] = deform_num_groups
            else:
                stage_kargs["block_class"] = BottleneckBlock
        blocks = ResNet.make_stage(**stage_kargs)
        in_channels = out_channels
        out_channels *= 2
        bottleneck_channels *= 2
        stages.append(blocks)
    return ResNet(stem, stages, out_features=out_features, freeze_at=freeze_at)

    Create a ResNet instance from config.

    Returns:
        ResNet: a :class:`ResNet` instance.
    
Create a ResNet instance from config.
Create a ResNet instance from config .
def build_resnet_backbone(cfg, input_shape=None):
    if input_shape is None:
        ch = 3
    else:
        ch = input_shape.channels
    norm = cfg.MODEL.RESNETS.NORM
    stem = BasicStem(
        in_channels=ch,
        out_channels=cfg.MODEL.RESNETS.STEM_OUT_CHANNELS,
        norm=norm, )
    freeze_at = cfg.MODEL.BACKBONE.FREEZE_AT  
    out_features = cfg.MODEL.RESNETS.OUT_FEATURES
    depth = cfg.MODEL.RESNETS.DEPTH
    num_groups = cfg.MODEL.RESNETS.NUM_GROUPS
    width_per_group = cfg.MODEL.RESNETS.WIDTH_PER_GROUP
    bottleneck_channels = num_groups * width_per_group
    in_channels = cfg.MODEL.RESNETS.STEM_OUT_CHANNELS
    out_channels = cfg.MODEL.RESNETS.RES2_OUT_CHANNELS
    stride_in_1x1 = cfg.MODEL.RESNETS.STRIDE_IN_1X1
    res5_dilation = cfg.MODEL.RESNETS.RES5_DILATION
    deform_on_per_stage = cfg.MODEL.RESNETS.DEFORM_ON_PER_STAGE
    deform_modulated = cfg.MODEL.RESNETS.DEFORM_MODULATED
    deform_num_groups = cfg.MODEL.RESNETS.DEFORM_NUM_GROUPS
    assert res5_dilation in {1, 2}, "res5_dilation cannot be {}.".format(
        res5_dilation)
    num_blocks_per_stage = {
        18: [2, 2, 2, 2],
        34: [3, 4, 6, 3],
        50: [3, 4, 6, 3],
        101: [3, 4, 23, 3],
        152: [3, 8, 36, 3],
    }[depth]
    if depth in [18, 34]:
        assert out_channels == 64, "Must set MODEL.RESNETS.RES2_OUT_CHANNELS = 64 for R18/R34"
        assert not any(
            deform_on_per_stage
        ), "MODEL.RESNETS.DEFORM_ON_PER_STAGE unsupported for R18/R34"
        assert res5_dilation == 1, "Must set MODEL.RESNETS.RES5_DILATION = 1 for R18/R34"
        assert num_groups == 1, "Must set MODEL.RESNETS.NUM_GROUPS = 1 for R18/R34"
    stages = []
    for idx, stage_idx in enumerate(range(2, 6)):
        dilation = res5_dilation if stage_idx == 5 else 1
        first_stride = 1 if idx == 0 or (stage_idx == 5 and
                                         dilation == 2) else 2
        stage_kargs = {
            "num_blocks": num_blocks_per_stage[idx],
            "stride_per_block":
            [first_stride] + [1] * (num_blocks_per_stage[idx] - 1),
            "in_channels": in_channels,
            "out_channels": out_channels,
            "norm": norm,
        }
        if depth in [18, 34]:
            stage_kargs["block_class"] = BasicBlock
        else:
            stage_kargs["bottleneck_channels"] = bottleneck_channels
            stage_kargs["stride_in_1x1"] = stride_in_1x1
            stage_kargs["dilation"] = dilation
            stage_kargs["num_groups"] = num_groups
            if deform_on_per_stage[idx]:
                stage_kargs["block_class"] = DeformBottleneckBlock
                stage_kargs["deform_modulated"] = deform_modulated
                stage_kargs["deform_num_groups"] = deform_num_groups
            else:
                stage_kargs["block_class"] = BottleneckBlock
        blocks = ResNet.make_stage(**stage_kargs)
        in_channels = out_channels
        out_channels *= 2
        bottleneck_channels *= 2
        stages.append(blocks)
    return ResNet(stem, stages, out_features=out_features, freeze_at=freeze_at)
def build_resnet_backbone ( cfg , input_shape = None ) : if input_shape is None : ch = 3 else : ch = input_shape . channels norm = cfg . MODEL . RESNETS . NORM stem = BasicStem ( in_channels = ch , out_channels = cfg . MODEL . RESNETS . STEM_OUT_CHANNELS , norm = norm , ) freeze_at = cfg . MODEL . BACKBONE . FREEZE_AT out_features = cfg . MODEL . RESNETS . OUT_FEATURES depth = cfg . MODEL . RESNETS . DEPTH num_groups = cfg . MODEL . RESNETS . NUM_GROUPS width_per_group = cfg . MODEL . RESNETS . WIDTH_PER_GROUP bottleneck_channels = num_groups * width_per_group in_channels = cfg . MODEL . RESNETS . STEM_OUT_CHANNELS out_channels = cfg . MODEL . RESNETS . RES2_OUT_CHANNELS stride_in_1x1 = cfg . MODEL . RESNETS . STRIDE_IN_1X1 res5_dilation = cfg . MODEL . RESNETS . RES5_DILATION deform_on_per_stage = cfg . MODEL . RESNETS . DEFORM_ON_PER_STAGE deform_modulated = cfg . MODEL . RESNETS . DEFORM_MODULATED deform_num_groups = cfg . MODEL . RESNETS . DEFORM_NUM_GROUPS assert res5_dilation in { 1 , 2 } , "res5_dilation cannot be {}." . format ( res5_dilation ) num_blocks_per_stage = { 18 : [ 2 , 2 , 2 , 2 ] , 34 : [ 3 , 4 , 6 , 3 ] , 50 : [ 3 , 4 , 6 , 3 ] , 101 : [ 3 , 4 , 23 , 3 ] , 152 : [ 3 , 8 , 36 , 3 ] , } [ depth ] if depth in [ 18 , 34 ] : assert out_channels == 64 , "Must set MODEL.RESNETS.RES2_OUT_CHANNELS = 64 for R18/R34" assert not any ( deform_on_per_stage ) , "MODEL.RESNETS.DEFORM_ON_PER_STAGE unsupported for R18/R34" assert res5_dilation == 1 , "Must set MODEL.RESNETS.RES5_DILATION = 1 for R18/R34" assert num_groups == 1 , "Must set MODEL.RESNETS.NUM_GROUPS = 1 for R18/R34" stages = [ ] for idx , stage_idx in enumerate ( range ( 2 , 6 ) ) : dilation = res5_dilation if stage_idx == 5 else 1 first_stride = 1 if idx == 0 or ( stage_idx == 5 and dilation == 2 ) else 2 stage_kargs = { "num_blocks" : num_blocks_per_stage [ idx ] , "stride_per_block" : [ first_stride ] + [ 1 ] * ( num_blocks_per_stage [ idx ] - 1 ) , "in_channels" : in_channels , "out_channels" : out_channels , "norm" : norm , } if depth in [ 18 , 34 ] : stage_kargs [ "block_class" ] = BasicBlock else : stage_kargs [ "bottleneck_channels" ] = bottleneck_channels stage_kargs [ "stride_in_1x1" ] = stride_in_1x1 stage_kargs [ "dilation" ] = dilation stage_kargs [ "num_groups" ] = num_groups if deform_on_per_stage [ idx ] : stage_kargs [ "block_class" ] = DeformBottleneckBlock stage_kargs [ "deform_modulated" ] = deform_modulated stage_kargs [ "deform_num_groups" ] = deform_num_groups else : stage_kargs [ "block_class" ] = BottleneckBlock blocks = ResNet . make_stage ( ** stage_kargs ) in_channels = out_channels out_channels *= 2 bottleneck_channels *= 2 stages . append ( blocks ) return ResNet ( stem , stages , out_features = out_features , freeze_at = freeze_at )
Create a ResNet instance from config.
Create a ResNet instance from config .
"""
    Create a ResNet instance from config.

    Returns:
        ResNet: a :class:`ResNet` instance.
    """ # need registration of new blocks/stems? # fmt: off # default as 2 # fmt: on # res5_dilation is used this way as a convention in R-FCN & Deformable Conv paper # Use BasicBlock for R18 and R34.
a :class:`ResNet` instance.
a : class : ` ResNet ` instance .
ResNet
cfg
input_shape