Ejemplo n.º 1
0
def build_fpn_backbone(cfg: CfgNode, input_shape: ShapeSpec) -> FPN:
    """
    Build the Resnet 50 backbone and the FPN:
    Label FPN:
      * Input:  C2, C3, C4
      * Output: P2, P3, P4

    Args:
        cfg (CfgNode):              A detectron2 CfgNode
        input_shape (ShapeSpec):    The input shape of the backbone.

    Returns:
        backbone (nn.Module):   Backbone module, must be a subclass of :class:`Backbone`.
    """
    # Build the feature extractor (Resnet 50)
    bottom_up: ResNet = build_resnet_backbone(cfg, input_shape)

    # Label FPN
    label_in_features: List[str] = cfg.MODEL.FPN.IN_FEATURES
    label_out_channels: List[str] = cfg.MODEL.FPN.OUT_CHANNELS
    label_fpn: FPN = FPN(bottom_up=bottom_up,
                         in_features=label_in_features,
                         out_channels=label_out_channels,
                         norm=cfg.MODEL.FPN.NORM,
                         top_block=None,
                         fuse_type=cfg.MODEL.FPN.FUSE_TYPE)

    return label_fpn
Ejemplo n.º 2
0
def build_fcos_mnv2_fpn_backbone(cfg, input_shape: ShapeSpec):
    """
    Args:
        cfg: a detectron2 CfgNode
    Returns:
        backbone (Backbone): backbone module, must be a subclass of :class:`Backbone`.
    """
    bottom_up = build_mnv2_backbone(cfg, input_shape)
    in_features = cfg.MODEL.FPN.IN_FEATURES
    out_channels = cfg.MODEL.FPN.OUT_CHANNELS
    top_levels = cfg.MODEL.FCOS.TOP_LEVELS
    in_channels_top = out_channels
    if top_levels == 2:
        top_block = LastLevelP6P7(in_channels_top, out_channels, "p5")
    if top_levels == 1:
        top_block = LastLevelP6(in_channels_top, out_channels, "p5")
    elif top_levels == 0:
        top_block = None
    backbone = FPN(
        bottom_up=bottom_up,
        in_features=in_features,
        out_channels=out_channels,
        norm=cfg.MODEL.FPN.NORM,
        top_block=top_block,
        fuse_type=cfg.MODEL.FPN.FUSE_TYPE,
    )
    return backbone
def build_fpn_backbones(cfg: CfgNode,
                        input_shape: ShapeSpec) -> Tuple[FPN, FPN]:
    """
    Build the Resnet 50 backbone and the two FPN networks:
    * Panel FPN:
        * Input:  C3, C4, C5
        * Output: P3, P4, P5, P6, P7
    * Label FPN:
        * Input:  C2, C3, C4
        * Output: P2, P3, P4

    Args:
        cfg (CfgNode):              A detectron2 CfgNode.
        input_shape (ShapeSpec):    The input shape of the backbone.

    Returns:
        backbone (Backbone):    Backbone module, must be a subclass of :class:`Backbone`.
    """
    # Build the feature extractor (Resnet 50)
    bottom_up: ResNet = build_resnet_backbone(cfg, input_shape)

    # Panel FPN
    panel_in_features: List[str] = cfg.MODEL.PANEL_FPN.IN_FEATURES
    panel_out_channels: List[str] = cfg.MODEL.PANEL_FPN.OUT_CHANNELS
    in_channels_p6p7: int = bottom_up.output_shape()['res5'].channels
    panel_fpn: FPN = FPN(bottom_up=bottom_up,
                         in_features=panel_in_features,
                         out_channels=panel_out_channels,
                         norm=cfg.MODEL.FPN.NORM,
                         top_block=LastLevelP6P7(in_channels_p6p7,
                                                 panel_out_channels),
                         fuse_type=cfg.MODEL.FPN.FUSE_TYPE)


    # Label FPN
    label_in_features: List[str] = cfg.MODEL.LABEL_FPN.IN_FEATURES
    label_out_channels: List[str] = cfg.MODEL.LABEL_FPN.OUT_CHANNELS
    label_fpn: FPN = FPN(bottom_up=bottom_up,
                         in_features=label_in_features,
                         out_channels=label_out_channels,
                         norm=cfg.MODEL.FPN.NORM,
                         top_block=None,
                         fuse_type=cfg.MODEL.FPN.FUSE_TYPE)

    return panel_fpn, label_fpn
Ejemplo n.º 4
0
def build_mnv1_fpn_wo_top_block_backbone(cfg, input_shape: ShapeSpec):
    bottom_up = build_mnv1_backbone(cfg, input_shape)
    in_features = cfg.MODEL.FPN.IN_FEATURES
    out_channels = cfg.MODEL.FPN.OUT_CHANNELS
    backbone = FPN(bottom_up=bottom_up,
                   in_features=in_features,
                   out_channels=out_channels,
                   norm=cfg.MODEL.FPN.NORM,
                   fuse_type=cfg.MODEL.FPN.FUSE_TYPE)

    return backbone
Ejemplo n.º 5
0
def build_efficientnet_fpn_backbone(cfg, input_shape: ShapeSpec):
    bottom_up = build_efficientnet_backbone(cfg, input_shape)
    in_features = cfg.MODEL.FPN.IN_FEATURES
    out_channels = cfg.MODEL.FPN.OUT_CHANNELS
    backbone = FPN(
        bottom_up=bottom_up,
        in_features=in_features,
        out_channels=out_channels,
        top_block=LastLevelMaxPool(),
        fuse_type=cfg.MODEL.FPN.FUSE_TYPE,
    )
    return backbone
Ejemplo n.º 6
0
def build_mobilenetv2_flgc_fpn_backbone(cfg, input_shape: ShapeSpec):
    out_features = cfg.MODEL.MOBILENET.OUT_FEATURES
    bottom_up = MobileNetV2(cfg, groups_in_1x1=8)
    bottom_up._out_features = out_features
    in_features = cfg.MODEL.FPN.IN_FEATURES
    out_channels = cfg.MODEL.FPN.OUT_CHANNELS
    backbone = FPN(
        bottom_up=bottom_up,
        in_features=in_features,
        out_channels=out_channels,
        norm=cfg.MODEL.FPN.NORM,
        top_block=LastLevelMaxPool(),
        fuse_type=cfg.MODEL.FPN.FUSE_TYPE,
    )
    return backbone
Ejemplo n.º 7
0
def build_mnetv2_fpn_backbone(cfg, input_shape: ShapeSpec):
    bottom_up = build_mnetv2_backbone(cfg, input_shape)
    # ['res5', ...]
    in_features = cfg.MODEL.FPN.IN_FEATURES
    # scalar
    out_features = cfg.MODEL.FPN.OUT_CHANNELS
    backbone = FPN(
        bottom_up=bottom_up,
        in_features=in_features,
        out_channels=out_channels,
        norm=cfg.MODEL.FPN.NORM,
        top_block=LastLevelMaxPool(),
        fuse_type=cfg.MODEL.FPN.FUSE_TYPE,
    )
    return backbone
Ejemplo n.º 8
0
def fPNBackBone(model_name="resnet34", pretrained=False, freeze_at=2):
    # ["res2", "res3", "res4", "res5"]
    in_features = ["res2", "res3", "res4", "res5"]
    out_channels = 256
    norm = ""  # "GN"
    fuse_type = "sum"  # "sum" or "avg"
    backbone = FPN(
        bottom_up=ResnetBone(model_name, pretrained, freeze_at),
        in_features=in_features,
        out_channels=out_channels,
        norm=norm,
        top_block=LastLevelMaxPool(),
        fuse_type=fuse_type,
    )

    return backbone
def build_efficientnet_fpn_backbone(cfg, input_shape: ShapeSpec):
    """
    Args:
        cfg: a detectron2 CfgNode

    Returns:
        backbone (Backbone): backbone module, must be a subclass of :class:`Backbone`.
    """
    bottom_up = build_efficientnet_backbone(cfg, input_shape)
    in_features = cfg.MODEL.FPN.IN_FEATURES
    out_channels = cfg.MODEL.FPN.OUT_CHANNELS
    backbone = FPN(bottom_up=bottom_up,
                   in_features=in_features,
                   out_channels=out_channels,
                   norm=cfg.MODEL.FPN.NORM,
                   top_block=LastLevelMaxPool(),
                   fuse_type=cfg.MODEL.FPN.FUSE_TYPE)
    return backbone
Ejemplo n.º 10
0
def build_slmobilev2_fpn_backbone(cfg, input_shape: ShapeSpec):
    """
    Args:
        cfg: a detectron2 CfgNode

    Returns:
        backbone (Backbone): backbone module, must be a subclass of :class:`Backbone`.
    """
    bottom_up = build_slmobilev2_backbone(cfg)
    in_features = cfg.MODEL.FPN.IN_FEATURES
    out_channels = cfg.MODEL.FPN.OUT_CHANNELS
    in_channels_p6p7 = bottom_up.output_shape()["res5"].channels
    backbone = FPN(bottom_up=bottom_up,
                   in_features=in_features,
                   out_channels=out_channels,
                   norm=cfg.MODEL.FPN.NORM,
                   top_block=LastLevelP6P7(in_channels_p6p7, out_channels),
                   fuse_type=cfg.MODEL.FPN.FUSE_TYPE)
    return backbone
Ejemplo n.º 11
0
def build_highroadside_fpn_backbone(cfg, input_shape: ShapeSpec):
    """
    Implement FPN based on high roadside backbone.

    Returns:
    high_backbone + fpn
    """
    backbone = HighRoadsideBackbone()
    in_features = cfg.MODEL.FPN.IN_FEATURES
    out_channels = cfg.MODEL.FPN.OUT_CHANNELS

    fpn_backbone = FPN(bottom_up=backbone,
                       in_features=in_features,
                       out_channels=out_channels,
                       norm=cfg.MODEL.FPN.NORM,
                       top_block=None,
                       fuse_type=cfg.MODEL.FPN.FUSE_TYPE)

    return fpn_backbone
Ejemplo n.º 12
0
def build_resnet_fpn_p5_backbone(cfg, input_shape: ShapeSpec):
    """
    Build ResNet-FPN backbone with P6 and P7 from P5 feature.

    Returns:
        backbone (Backbone): backbone module, must be a subclass of :class:`Backbone`.
    """
    bottom_up = build_resnet_backbone(cfg, input_shape)
    in_features = cfg.MODEL.FPN.IN_FEATURES
    out_channels = cfg.MODEL.FPN.OUT_CHANNELS
    in_channels_p6p7 = out_channels
    backbone = FPN(
        bottom_up=bottom_up,
        in_features=in_features,
        out_channels=out_channels,
        norm=cfg.MODEL.FPN.NORM,
        top_block=LastLevelP6P7fromP5(in_channels_p6p7, out_channels),
        fuse_type=cfg.MODEL.FPN.FUSE_TYPE,
    )
    return backbone
Ejemplo n.º 13
0
def build_timm_fpn_backbone(cfg, input_shape: ShapeSpec):
    """
    Args:
        cfg: a detectron2 CfgNode
    Returns:
        backbone (Backbone): backbone module, must be a subclass of :class:`Backbone`.
    """
    bottom_up = build_timm_backbone(cfg, input_shape)
    in_features = cfg.MODEL.FPN.IN_FEATURES
    out_channels = cfg.MODEL.FPN.OUT_CHANNELS
    backbone = FPN(
        bottom_up=bottom_up,
        in_features=in_features,
        out_channels=out_channels,
        norm=cfg.MODEL.FPN.NORM,
        top_block=TimmLastLevelMaxPool("p{}".format(cfg.MODEL.TIMM.LAST_LAYER +
                                                    1)),
        fuse_type=cfg.MODEL.FPN.FUSE_TYPE,
    )
    return backbone
Ejemplo n.º 14
0
def build_vovnet_fpn_backbone(cfg, input_shape: ShapeSpec):
    """Create a VoVNet + FPN instance from config. The type of network (mobilenet-like or deeper model) is defined by the CONV_BODY config entry.

    Args:
        cfg: a detectron2 CfgNode
        input_shape (ShapeSpec): this argument is needed by Detectron2 althought not used here
    Returns:
        backbone (Backbone): backbone module, must be a subclass of :class:`Backbone`.
    """
    bottom_up = build_vovnet_backbone(cfg, input_shape)
    in_features = cfg.MODEL.FPN.IN_FEATURES
    out_channels = cfg.MODEL.FPN.OUT_CHANNELS
    backbone = FPN(
        bottom_up=bottom_up,
        in_features=in_features,
        out_channels=out_channels,
        norm=cfg.MODEL.FPN.NORM,
        top_block=LastLevelMaxPool(),
        fuse_type=cfg.MODEL.FPN.FUSE_TYPE,
    )
    return backbone
def build_mobilenetv2_quantization_fpn_backbone(cfg, input_shape: ShapeSpec):
    """
    Args:
        cfg: a detectron2 CfgNode
    Returns:
        backbone (Backbone): backbone module, must be a subclass of :class:`Backbone`.
    """
    _out_features = cfg.MODEL.MOBILENET.OUT_FEATURES
    bottom_up = mobilenet_v2(cfg)
    bottom_up._out_features = _out_features
    in_features = cfg.MODEL.FPN.IN_FEATURES
    out_channels = cfg.MODEL.FPN.OUT_CHANNELS
    backbone = FPN(
        bottom_up=bottom_up,
        in_features=in_features,
        out_channels=out_channels,
        norm=cfg.MODEL.FPN.NORM,
        top_block=LastLevelMaxPool(),
        fuse_type=cfg.MODEL.FPN.FUSE_TYPE,
    )
    return backbone
Ejemplo n.º 16
0
def build_fcos_efficientnet_fpn_backbone(cfg, input_shape: ShapeSpec):

    bottom_up = build_efficientnet_backbone(cfg, input_shape)
    in_features = cfg.MODEL.FPN.IN_FEATURES
    out_channels = cfg.MODEL.FPN.OUT_CHANNELS
    top_levels = cfg.MODEL.FCOS.TOP_LEVELS
    in_channels_top = out_channels
    if top_levels == 2:
        top_block = LastLevelP6P7(in_channels_top, out_channels, "p5")
    if top_levels == 1:
        top_block = LastLevelP6(in_channels_top, out_channels, "p5")
    elif top_levels == 0:
        top_block = None
    backbone = FPN(
        bottom_up=bottom_up,
        in_features=in_features,
        out_channels=out_channels,
        norm=cfg.MODEL.FPN.NORM,
        top_block=top_block,
        fuse_type=cfg.MODEL.FPN.FUSE_TYPE,
    )
    return backbone
Ejemplo n.º 17
0
def build_retinanet_resnet_fpn_backbone_use_p5(cfg, input_shape: ShapeSpec):
    """
    Args:
        cfg: a detectron2 CfgNode

    Returns:
        backbone (Backbone): backbone module, must be a subclass of :class:`Backbone`.
    """
    bottom_up = build_resnet_backbone(cfg, input_shape)
    in_features = cfg.MODEL.FPN.IN_FEATURES
    out_channels = cfg.MODEL.FPN.OUT_CHANNELS
    in_channels_p6p7 = out_channels
    backbone = FPN(
        bottom_up=bottom_up,
        in_features=in_features,
        out_channels=out_channels,
        norm=cfg.MODEL.FPN.NORM,
        top_block=LastLevelP6P7(in_channels_p6p7,
                                out_channels,
                                in_feature="p5"),
        fuse_type=cfg.MODEL.FPN.FUSE_TYPE,
    )
    return backbone
Ejemplo n.º 18
0
def build_mobilenet_fpn_backbone(cfg, input_shape: ShapeSpec):
    """Create a MobileNetV2 + FPN instance from config. 

    Args:
        cfg (cfgNode): Detectron2's cfg object
        input_shape (ShapeSpec): this argument is needed by Detectron2 althought not used here

    Returns:
        backbone (Backbone): backbone module, must be a subclass of :class:`Backbone`
    """

    bottom_up = build_mobilenet_backbone(cfg, input_shape)
    in_features = cfg.MODEL.FPN.IN_FEATURES
    out_channels = cfg.MODEL.FPN.OUT_CHANNELS
    backbone = FPN(
        bottom_up=bottom_up,
        in_features=in_features,
        out_channels=out_channels,
        norm=cfg.MODEL.FPN.NORM,
        top_block=LastLevelMaxPool(),
        fuse_type=cfg.MODEL.FPN.FUSE_TYPE,
    )
    return backbone