Beispiel #1
0
    def output_shape(self):
        stage1_shape = ShapeSpec(channels=48, stride=4)
        stage2_shape = ShapeSpec(channels=64, stride=8)
        stage3_shape = ShapeSpec(channels=80, stride=16)
        stage4_shape = ShapeSpec(channels=96, stride=32)
        stage5_shape = ShapeSpec(channels=112, stride=64)

        return {
            'conv_stage1_out': stage1_shape,
            'conv_stage2_out': stage2_shape,
            'conv_stage3_out': stage3_shape,
            'conv_stage4_out': stage4_shape,
            'conv_stage5_out': stage5_shape
        }
Beispiel #2
0
    def output_shape(self):
        out_shape = {
            self.names[i]: ShapeSpec(channels=768, stride=2**(i + 2))
            for i in range(len(self.names))
        }

        return out_shape
Beispiel #3
0
 def output_shape(self):
     return {
         name: ShapeSpec(
             channels=self._out_feature_channels[name],
             stride=self._out_feature_strides[name],
         )
         for name in self._out_features
     }
Beispiel #4
0
    def __init__(self, config: Config, *args, **kwargs):
        super().__init__()
        self.config = config
        pretrained = config.get("pretrained", False)
        pretrained_path = config.get("pretrained_path", None)

        self.resnet = build_resnet_backbone(config, ShapeSpec(channels=3))

        if pretrained:
            state_dict = torch.hub.load_state_dict_from_url(pretrained_path,
                                                            progress=False)
            new_state_dict = OrderedDict()
            replace_layer = {"backbone.": ""}

            for key, value in state_dict["model"].items():
                new_key = re.sub(r"(backbone\.)",
                                 lambda x: replace_layer[x.groups()[0]], key)
                new_state_dict[new_key] = value
            self.resnet.load_state_dict(new_state_dict, strict=False)

        self.out_dim = 2048
Beispiel #5
0
    def output_shape(self):
        """
		Returns:
			dict[str->ShapeSpec]
		"""
        return {"2D-Feature": ShapeSpec(channels=1024, stride=32)}
Beispiel #6
0
 def output_shape(self):
     return {"conv1": ShapeSpec(channels=64, stride=16)}
Beispiel #7
0
 def output_shape(self):
     return {
         name: ShapeSpec(channels=c, stride=s)
         for name, c, s in zip(self._out_features, self._out_channels,
                               self._out_strides)
     }
Beispiel #8
0
def build_backbone(config: omegaconf.DictConfig):
    """
    Difference between this and the build_backbone provided
    by detectron2 is as follows:
    - Different stem, include caffe_maxpool
    - Number of blocks is different, unconfigurable in detectron
    - Freeze-at operates differently in detectron
    """
    input_shape = ShapeSpec(channels=len(config.MODEL.PIXEL_MEAN))
    norm = config.MODEL.RESNETS.NORM
    stem = BasicStem(
        in_channels=input_shape.channels,
        out_channels=config.MODEL.RESNETS.STEM_OUT_CHANNELS,
        norm=norm,
        caffe_maxpool=config.MODEL.MAX_POOL,
    )
    freeze_at = config.MODEL.BACKBONE.FREEZE_AT

    if freeze_at >= 1:
        for p in stem.parameters():
            p.requires_grad = False

    out_features = config.MODEL.RESNETS.OUT_FEATURES
    depth = config.MODEL.RESNETS.DEPTH
    num_groups = config.MODEL.RESNETS.NUM_GROUPS
    width_per_group = config.MODEL.RESNETS.WIDTH_PER_GROUP
    bottleneck_channels = num_groups * width_per_group
    in_channels = config.MODEL.RESNETS.STEM_OUT_CHANNELS
    out_channels = config.MODEL.RESNETS.RES2_OUT_CHANNELS
    stride_in_1x1 = config.MODEL.RESNETS.STRIDE_IN_1X1
    res5_dilation = config.MODEL.RESNETS.RES5_DILATION
    assert res5_dilation in {1, 2}, f"res5_dilation cannot be {res5_dilation}."

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

    stages = []
    out_stage_idx = [{
        "res2": 2,
        "res3": 3,
        "res4": 4,
        "res5": 5
    }[f] for f in out_features]
    max_stage_idx = max(out_stage_idx)
    for idx, stage_idx in enumerate(range(2, max_stage_idx + 1)):
        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,
            "bottleneck_channels":
            bottleneck_channels,
            "out_channels":
            out_channels,
            "num_groups":
            num_groups,
            "norm":
            norm,
            "stride_in_1x1":
            stride_in_1x1,
            "dilation":
            dilation,
        }

        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=-1)
Beispiel #9
0
 def output_shape(self):
     return {self.name: ShapeSpec(stride=8, channels=96, height=16, width=16)}