Exemplo n.º 1
0
    def __init__(self, cfg=None, phase='train'):
        """
        :param cfg:  Network related settings.
        :param phase: train or test.
        """
        super(RetinaFace, self).__init__()
        self.cfg = cfg
        self.phase = phase
        backbone = None
        if cfg['name'] == 'mobilenet0.25':
            backbone = MobileNetV1()
            if cfg['pretrain']:
                checkpoint = torch.load(
                    "./weights/mobilenetV1X0.25_pretrain.tar",
                    map_location=torch.device('cpu'))
                from collections import OrderedDict
                new_state_dict = OrderedDict()
                for k, v in checkpoint['state_dict'].items():
                    name = k[7:]  # remove module.
                    new_state_dict[name] = v
                # load params
                backbone.load_state_dict(new_state_dict)
        elif cfg['name'] == 'Resnet50':
            import torchvision.models as models
            backbone = models.resnet50(pretrained=cfg['pretrain'])

        elif cfg['name'] == 'effnet':
            self.body = load_effnetm()

        elif cfg['name'] == 'Res2Net':
            from .res2net import res2net101_26w_4s
            model = res2net101_26w_4s(pretrained=True)
            self.body = _utils.IntermediateLayerGetter(model,
                                                       cfg['return_layers'])

        if cfg['name'] == 'mobilenet0.25' or cfg['name'] == 'Resnet50':
            self.body = _utils.IntermediateLayerGetter(backbone,
                                                       cfg['return_layers'])

        in_channels_stage2 = cfg['in_channel']
        in_channels_list = [
            in_channels_stage2 * 2,
            in_channels_stage2 * 4,
            in_channels_stage2 * 8,
        ]
        if cfg['name'] == 'effnet':
            in_channels_list = [64, 176, 512]

        out_channels = cfg['out_channel']
        self.fpn = FPN(in_channels_list, out_channels, cfg)
        self.ssh1 = SSH(out_channels, out_channels)
        self.ssh2 = SSH(out_channels, out_channels)
        self.ssh3 = SSH(out_channels, out_channels)

        self.ClassHead = self._make_class_head(fpn_num=3,
                                               inchannels=cfg['out_channel'])
        self.BboxHead = self._make_bbox_head(fpn_num=3,
                                             inchannels=cfg['out_channel'])
        self.LandmarkHead = self._make_landmark_head(
            fpn_num=3, inchannels=cfg['out_channel'])
Exemplo n.º 2
0
    def __init__(self, encoder):
        """
        :param cfg:  Network related settings.
        :param phase: train or test.
        """
        super(RetinaFace,self).__init__()
        backbone = None
        if encoder == "mobilenet":
            self.cfg = cfg_mnet
            backbone = MobileNetV1()
        elif encoder == "resnet":
            import torchvision.models as models
            self.cfg = cfg_re50
            backbone = models.resnet50(pretrained=False)
        else:
            raise NotImplementedError("UNKOWN BACKBONE ARCHITECTURE: {}".format(encoder))

        self.body = _utils.IntermediateLayerGetter(backbone, self.cfg['return_layers'])
        in_channels_stage2 = self.cfg['in_channel']
        in_channels_list = [
            in_channels_stage2 * 2,
            in_channels_stage2 * 4,
            in_channels_stage2 * 8,
        ]
        out_channels = self.cfg['out_channel']
        self.fpn = FPN(in_channels_list,out_channels)
        self.ssh1 = SSH(out_channels, out_channels)
        self.ssh2 = SSH(out_channels, out_channels)
        self.ssh3 = SSH(out_channels, out_channels)

        self.ClassHead = self._make_class_head(fpn_num=3, inchannels=self.cfg['out_channel'])
        self.BboxHead = self._make_bbox_head(fpn_num=3, inchannels=self.cfg['out_channel'])
        self.LandmarkHead = self._make_landmark_head(fpn_num=3, inchannels=self.cfg['out_channel'])
    def __init__(self, cfg):
        """
        :param cfg:  Network related settings.
        :param phase: train or test.
        """
        super().__init__()
        backbone = None
        if cfg['name'] == 'mobilenet0.25':
            backbone = MobileNetV1()
        elif cfg['name'] == 'Resnet50':
            import torchvision.models as models
            backbone = models.resnet50(pretrained=False)

        self.body = _utils.IntermediateLayerGetter(backbone,
                                                   cfg['return_layers'])
        in_channels_stage2 = cfg['in_channel']
        in_channels_list = [
            in_channels_stage2 * 2,
            in_channels_stage2 * 4,
            in_channels_stage2 * 8,
        ]
        out_channels = cfg['out_channel']
        self.fpn = FPN(in_channels_list, out_channels)
        self.ssh1 = SSH(out_channels, out_channels)
        self.ssh2 = SSH(out_channels, out_channels)
        self.ssh3 = SSH(out_channels, out_channels)

        self.ClassHead = self._make_class_head(fpn_num=3,
                                               inchannels=cfg['out_channel'])
        self.BboxHead = self._make_bbox_head(fpn_num=3,
                                             inchannels=cfg['out_channel'])
        self.LandmarkHead = self._make_landmark_head(
            fpn_num=3, inchannels=cfg['out_channel'])
    def __init__(self, cfg=None, phase='train'):
        """
        :param cfg:  Network related settings.
        :param phase: train or test.
        """
        super(TinyRetinaFace, self).__init__()
        self.phase = phase
        backbone = TinyMobileNetV1()

        self.body = _utils.IntermediateLayerGetter(backbone,
                                                   cfg['return_layers'])
        in_channels_stage2 = cfg['in_channel']
        in_channels_list = [
            in_channels_stage2 * 2,
            in_channels_stage2 * 4,
            in_channels_stage2 * 8,
        ]
        out_channels = cfg['out_channel']
        self.ssh1 = TinySSH(in_channels_list[0], out_channels)
        self.ssh2 = TinySSH(in_channels_list[1], out_channels)
        self.ssh3 = TinySSH(in_channels_list[2], out_channels)

        self.ClassHead = self._make_class_head(fpn_num=3,
                                               inchannels=cfg['out_channel'])
        self.BboxHead = self._make_bbox_head(fpn_num=3,
                                             inchannels=cfg['out_channel'])
        self.LandmarkHead = self._make_landmark_head(
            fpn_num=3, inchannels=cfg['out_channel'])
Exemplo n.º 5
0
 def __init__(self,backbone,return_layers,anchor_nums=3):
     super(RetinaFace,self).__init__()
     # if backbone_name == 'resnet50':
     #     self.backbone = resnet.resnet50(pretrained)
     # self.backbone = resnet.__dict__[backbone_name](pretrained=pretrained)
     # self.return_layers = {'layer1': 0, 'layer2': 1, 'layer3': 2, 'layer4': 3}
     assert backbone,'Backbone can not be none!'
     assert len(return_layers)>0,'There must be at least one return layers'
     self.body = _utils.IntermediateLayerGetter(backbone, return_layers)
     in_channels_stage2 = 256
     # in_channels_stage2 = 64
     in_channels_list = [
         #in_channels_stage2,
         in_channels_stage2 * 2,
         in_channels_stage2 * 4,
         in_channels_stage2 * 8,
     ]
     out_channels = 256
     self.fpn = FeaturePyramidNetwork(in_channels_list,out_channels)
     # self.ClassHead = ClassHead()
     # self.BboxHead = BboxHead()
     # self.LandmarkHead = LandmarkHead()
     self.ClassHead = self._make_class_head()
     self.BboxHead = self._make_bbox_head()
     self.LandmarkHead = self._make_landmark_head()
     self.anchors = Anchors()
     self.regressBoxes = RegressionTransform()
     self.losslayer = LossLayer()
Exemplo n.º 6
0
    def __init__(self, cfg=None):
        """
        :param cfg:  Network related settings.
        """
        super(RetinaFace, self).__init__()

        import torchvision.models as models
        backbone = models.resnet50(pretrained=cfg['pretrain'])

        self.body = _utils.IntermediateLayerGetter(backbone,
                                                   cfg['return_layers'])
        in_channels_stage2 = cfg['in_channel']
        in_channels_list = [
            in_channels_stage2 * 2,
            in_channels_stage2 * 4,
            in_channels_stage2 * 8,
        ]
        out_channels = cfg['out_channel']
        self.fpn = FPN(in_channels_list, out_channels)
        self.ssh1 = SSH(out_channels, out_channels)
        self.ssh2 = SSH(out_channels, out_channels)
        self.ssh3 = SSH(out_channels, out_channels)

        self.ClassHead = self._make_class_head(fpn_num=3,
                                               inchannels=cfg['out_channel'])
        self.BboxHead = self._make_bbox_head(fpn_num=3,
                                             inchannels=cfg['out_channel'])
        self.LandmarkHead = self._make_landmark_head(
            fpn_num=3, inchannels=cfg['out_channel'])
    def __init__(self, cfg=None, phase='train'):
        """
        :param cfg:  Network related settings.
        :param phase: train or test.
        """
        super(RetinaFace, self).__init__()
        self.phase = phase

        backbone = models.mobilenet_v2(pretrained=cfg['pretrain'], width_mult=cfg['width_mult']).features

        self.body = _utils.IntermediateLayerGetter(backbone, cfg['return_layers'])
        in_channels_list = [
            cfg['in_channel'][0],
            cfg['in_channel'][1],
            cfg['in_channel'][2],
        ]
        out_channels = cfg['out_channel']
        self.fpn = FPN(in_channels_list, out_channels)
        self.ssh1 = SSH(out_channels, out_channels)
        self.ssh2 = SSH(out_channels, out_channels)
        self.ssh3 = SSH(out_channels, out_channels)

        self.ClassHead = self._make_class_head(fpn_num=3, inchannels=cfg['out_channel'])
        self.BboxHead = self._make_bbox_head(fpn_num=3, inchannels=cfg['out_channel'])
        self.LandmarkHead = self._make_landmark_head(fpn_num=3, inchannels=cfg['out_channel'])
Exemplo n.º 8
0
def select_model(model_name: str) -> t.NoReturn:
    """
    API function for Algonauts; for a given model name, change the model of this module to the selected one;
    to get a list of all viable models, refer to viable_models
    """
    global model_, currently_selected_model, backbone_layer_keys, backbone_return_layers, intermediate_layer_getter
    global backbone
    model_ = torch.hub.load(torch_dir, model_name, pretrained=True)
    model_.to(device)
    model_.eval()
    currently_selected_model = model_name

    if hasattr(model_, "backbone"):
        backbone = model_.backbone
    elif hasattr(model_, 'features'):
        backbone = model_.features
    else:
        backbone = model_
        print("backbone could not be found; defaulting to usual model output")
    backbone.to(device)
    backbone.eval()

    # the names of the layers, that can be extracted from the backbone
    backbone_layer_keys = [name for name, module in backbone.named_children()]

    # preparing the model, that can return all layers specified in backbone_layer_keys
    backbone_return_layers = {
        layer_name: layer_name
        for layer_name in backbone_layer_keys
    }
    intermediate_layer_getter = _utils.IntermediateLayerGetter(
        backbone, backbone_return_layers)  # used to generate all features
    intermediate_layer_getter.to(device)
    intermediate_layer_getter.eval()
    def __init__(self):
        super(RetinaFace,self).__init__()

        backbone = MobileNetV1()

        return_layers = {'stage1': 1, 'stage2': 2, 'stage3': 3}
        in_channels = 32
        out_channels = 64

        self.body = _utils.IntermediateLayerGetter(backbone, return_layers)
        in_channels_stage2 = in_channels
        in_channels_list = [
            in_channels_stage2 * 2,
            in_channels_stage2 * 4,
            in_channels_stage2 * 8,
        ]
        out_channels = out_channels
        self.fpn = FPN(in_channels_list,out_channels)
        self.ssh1 = SSH(out_channels, out_channels)
        self.ssh2 = SSH(out_channels, out_channels)
        self.ssh3 = SSH(out_channels, out_channels)

        self.ClassHead = self._make_class_head(fpn_num=3, inchannels=out_channels)
        self.BboxHead = self._make_bbox_head(fpn_num=3, inchannels=out_channels)
        self.LandmarkHead = self._make_landmark_head(fpn_num=3, inchannels=out_channels)
    def __init__(self, cfg=None, phase='train'):
        """
        :param cfg:  Network related settings.
        :param phase: train or test.
        """
        super(Retina, self).__init__()
        self.phase = phase
        backbone = None
        if cfg['name'] == 'mobilenet0.25':
            
            backbone = MobileNetV1()
                
            self.body = _utils.IntermediateLayerGetter(backbone, cfg['return_layers'])     
            
        
        in_channels_stage2 = cfg['in_channel']
        in_channels_list = [
            in_channels_stage2 * 2,
            in_channels_stage2 * 4,
            in_channels_stage2 * 8,
        ]
        out_channels = cfg['out_channel']
        self.fpn = FPN(in_channels_list, out_channels)
        self.ssh1 = SSH(out_channels, out_channels)
        self.ssh2 = SSH(out_channels, out_channels)
        self.ssh3 = SSH(out_channels, out_channels)

        self.ClassHead = self._make_class_head(fpn_num=3, inchannels=cfg['out_channel'])
        self.BboxHead = self._make_bbox_head(fpn_num=3, inchannels=cfg['out_channel'])
        self.LandmarkHead = self._make_landmark_head(fpn_num=3, inchannels=cfg['out_channel'])
    def __init__(self, cfg=None, phase="train"):
        """
        :param cfg:  Network related settings.
        :param phase: train or test.
        """
        super(RetinaFace, self).__init__()
        self.phase = phase
        backbone = None
        if cfg["name"] == "mobilenet0.25":
            backbone = MobileNetV1()
        elif cfg["name"] == "Resnet50":
            import torchvision.models as models

            backbone = models.resnet50()

        self.body = _utils.IntermediateLayerGetter(backbone,
                                                   cfg["return_layers"])
        in_channels_stage2 = cfg["in_channel"]
        in_channels_list = [
            in_channels_stage2 * 2,
            in_channels_stage2 * 4,
            in_channels_stage2 * 8,
        ]
        out_channels = cfg["out_channel"]
        self.fpn = FPN(in_channels_list, out_channels)
        self.ssh1 = SSH(out_channels, out_channels)
        self.ssh2 = SSH(out_channels, out_channels)
        self.ssh3 = SSH(out_channels, out_channels)

        self.ClassHead = self._make_class_head(fpn_num=3,
                                               inchannels=cfg["out_channel"])
        self.BboxHead = self._make_bbox_head(fpn_num=3,
                                             inchannels=cfg["out_channel"])
        self.LandmarkHead = self._make_landmark_head(
            fpn_num=3, inchannels=cfg["out_channel"])
Exemplo n.º 12
0
    def __init__(self, cfg = None, pretrained = False, mode = 'train'):
        super(RetinaFace,self).__init__()
        backbone = None
        if cfg['name'] == 'mobilenet0.25':
            backbone = MobileNetV1()
            if pretrained:
                checkpoint = torch.load("./model_data/mobilenetV1X0.25_pretrain.tar", map_location=torch.device('cpu'))
                from collections import OrderedDict
                new_state_dict = OrderedDict()
                for k, v in checkpoint['state_dict'].items():
                    name = k[7:]
                    new_state_dict[name] = v
                backbone.load_state_dict(new_state_dict)
        elif cfg['name'] == 'Resnet50':
            backbone = models.resnet50(pretrained=pretrained)

        self.body = _utils.IntermediateLayerGetter(backbone, cfg['return_layers'])

        # 获得每个初步有效特征层的通道数
        in_channels_list = [cfg['in_channel'] * 2, cfg['in_channel'] * 4, cfg['in_channel'] * 8]
        self.fpn = FPN(in_channels_list, cfg['out_channel'])
        # 利用ssh模块提高模型感受野
        self.ssh1 = SSH(cfg['out_channel'], cfg['out_channel'])
        self.ssh2 = SSH(cfg['out_channel'], cfg['out_channel'])
        self.ssh3 = SSH(cfg['out_channel'], cfg['out_channel'])

        self.ClassHead = self._make_class_head(fpn_num=3, inchannels=cfg['out_channel'])
        self.BboxHead = self._make_bbox_head(fpn_num=3, inchannels=cfg['out_channel'])
        self.LandmarkHead = self._make_landmark_head(fpn_num=3, inchannels=cfg['out_channel'])

        self.mode = mode
Exemplo n.º 13
0
    def __init__(self, name: str, pretrained: bool, in_channels: int,
                 return_layers: Dict[str, int], out_channels: int) -> None:
        super().__init__()

        if name == "Resnet50":
            backbone = models.resnet50(pretrained=pretrained)
        else:
            raise NotImplementedError(
                f"Only Resnet50 backbone is supported but got {name}")

        self.body = _utils.IntermediateLayerGetter(backbone, return_layers)
        in_channels_stage2 = in_channels
        in_channels_list = [
            in_channels_stage2 * 2,
            in_channels_stage2 * 4,
            in_channels_stage2 * 8,
        ]
        self.fpn = FPN(in_channels_list, out_channels)
        self.ssh1 = SSH(out_channels, out_channels)
        self.ssh2 = SSH(out_channels, out_channels)
        self.ssh3 = SSH(out_channels, out_channels)

        self.ClassHead = self._make_class_head(fpn_num=3,
                                               in_channels=out_channels)
        self.BboxHead = self._make_bbox_head(fpn_num=3,
                                             in_channels=out_channels)
        self.LandmarkHead = self._make_landmark_head(fpn_num=3,
                                                     in_channels=out_channels)
Exemplo n.º 14
0
    def __init__(self, phase = 'train', net = 'mnet0.25', return_layers = {'stage1': 1, 'stage2': 2, 'stage3': 3}):
        super(RetinaFace,self).__init__()
        self.phase = phase
        backbone = None
        if net == 'mnet0.25':
            backbone = MobileNetV1()
            if True:
                checkpoint = torch.load("model_best.pth.tar", map_location=torch.device('cpu'))
                from collections import OrderedDict
                new_state_dict = OrderedDict()
                for k, v in checkpoint['state_dict'].items():
                    name = k[7:]  # remove module.
                    new_state_dict[name] = v
                # load params
                backbone.load_state_dict(new_state_dict)

        self.body = _utils.IntermediateLayerGetter(backbone, return_layers)
        in_channels_stage2 = 32
        in_channels_list = [
            in_channels_stage2 * 2,
            in_channels_stage2 * 4,
            in_channels_stage2 * 8,
        ]
        out_channels = 64
        self.fpn = FPN(in_channels_list,out_channels)
        self.ssh1 = SSH(out_channels, out_channels)
        self.ssh2 = SSH(out_channels, out_channels)
        self.ssh3 = SSH(out_channels, out_channels)

        self.ClassHead = self._make_class_head()
        self.BboxHead = self._make_bbox_head()
        self.LandmarkHead = self._make_landmark_head()
Exemplo n.º 15
0
    def __init__(self, cfg=None, phase='train'):
        """
        :param cfg:  Network related settings.
        :param phase: train or test.
        """
        super(RetinaFace, self).__init__()
        self.phase = phase
        backbone = None
        if cfg['name'] == 'mobilenet0.25':
            backbone = MobileNetV1()
            if cfg['pretrain']:
                checkpoint = torch.load(
                    "./weights/mobilenetV1X0.25_pretrain.tar",
                    map_location=torch.device('cpu'))
                from collections import OrderedDict
                new_state_dict = OrderedDict()
                for k, v in checkpoint['state_dict'].items():
                    name = k[7:]  # remove module.
                    new_state_dict[name] = v
                # load params
                backbone.load_state_dict(new_state_dict)
        elif cfg['name'] == 'Resnet50':
            import torchvision.models as models
            backbone = models.resnet50(pretrained=cfg['pretrain'])
        # 获取中间层输出
        self.body = _utils.IntermediateLayerGetter(backbone,
                                                   cfg['return_layers'])
        in_channels_stage2 = cfg['in_channel']
        in_channels_list = [
            in_channels_stage2 * 2,
            in_channels_stage2 * 4,
            in_channels_stage2 * 8,
        ]
        out_channels = cfg['out_channel']
        self.fpn = FPN(in_channels_list, out_channels)

        self.ssh1 = SSH(out_channels, out_channels)
        self.ssh2 = SSH(out_channels, out_channels)
        self.ssh3 = SSH(out_channels, out_channels)
        # 参考口罩分支,ssh单独新建
        self.ssh1_pose = SSH(out_channels, out_channels)
        self.ssh2_pose = SSH(out_channels, out_channels)
        self.ssh3_pose = SSH(out_channels, out_channels)
        # 对应ssh 的 框-关键点-2分类
        self.ClassHead = self._make_class_head(fpn_num=3,
                                               inchannels=cfg['out_channel'])
        self.BboxHead = self._make_bbox_head(fpn_num=3,
                                             inchannels=cfg['out_channel'])
        self.LandmarkHead = self._make_landmark_head(
            fpn_num=3, inchannels=cfg['out_channel'])
        # 对应ssh_pose 的 yaw-pitch-roll 的 3个66分类
        self.Pose_yaw_Head = self._make_pose_yaw_pitch_roll_head(
            fpn_num=3, inchannels=cfg['out_channel'])
        self.Pose_pitch_Head = self._make_pose_yaw_pitch_roll_head(
            fpn_num=3, inchannels=cfg['out_channel'])
        self.Pose_roll_Head = self._make_pose_yaw_pitch_roll_head(
            fpn_num=3, inchannels=cfg['out_channel'])
    def _make_body(self, cfg):
        backbone = None
        model_name = cfg['name']
        if model_name == 'mobilenet0.25' or model_name == 'Resnet50':
            if cfg['name'] == 'mobilenet0.25':
                backbone = MobileNetV1()
                if cfg['pretrain']:
                    checkpoint = torch.load(
                        "./weights/mobilenetV1X0.25_pretrain.tar",
                        map_location=torch.device('cpu'))
                    from collections import OrderedDict
                    new_state_dict = OrderedDict()
                    for k, v in checkpoint['state_dict'].items():
                        name = k[7:]  # remove module.
                        new_state_dict[name] = v
                    # load params
                    backbone.load_state_dict(new_state_dict)
            elif cfg['name'] == 'Resnet50':
                import torchvision.models as models
                backbone = models.resnet50(pretrained=cfg['pretrain'])

            self.body = _utils.IntermediateLayerGetter(backbone,
                                                       cfg['return_layers'])
            in_channels_stage2 = cfg['in_channel']
            in_channels_list = [
                in_channels_stage2 * 2,
                in_channels_stage2 * 4,
                in_channels_stage2 * 8,
            ]
            return in_channels_list
        elif model_name == 'mobilenetv3':
            backbone = MobileNetV3_Small()
            if cfg['pretrain']:
                checkpoint = torch.load("./weights/mbv3_small.pth.tar",
                                        map_location=torch.device('cpu'))
                from collections import OrderedDict
                new_state_dict = OrderedDict()
                for k, v in checkpoint['state_dict'].items():
                    name = k[7:]  # remove module.
                    new_state_dict[name] = v
                backbone.load_state_dict(new_state_dict)
                print("Loaded pretrained model!")
            self.body = backbone
        elif model_name == 'ghostnet':
            backbone = ghost_net(width_mult=cfg['width_mult'])
            self.body = backbone

        in_channels_stage2 = cfg['in_channel']
        in_channels_list = [
            in_channels_stage2,
            in_channels_stage2 * 2,
            in_channels_stage2 * 4,
        ]
        return in_channels_list
Exemplo n.º 17
0
    def __init__(self, cfg = None, phase = 'train'):
        """
        :param cfg:  Network related settings.
        :param phase: train or test.
        """
        super(RetinaFace,self).__init__()
        self.phase = phase
        backbone = None
        if cfg['name'] == 'mobilenet0.25':
            backbone = MobileNetV1()
            if cfg['pretrain']:
                checkpoint = torch.load("./weights/mobilenetV1X0.25_pretrain.tar", map_location=torch.device('cpu'))
                from collections import OrderedDict
                new_state_dict = OrderedDict()
                for k, v in checkpoint['state_dict'].items():
                    name = k[7:]  # remove module.
                    new_state_dict[name] = v
                # load params
                backbone.load_state_dict(new_state_dict)
        elif cfg['name'] == 'Resnet50':
            import torchvision.models as models
            backbone = models.resnet50(pretrained=cfg['pretrain'])
        elif cfg['name'] == 'detnas':
            print('You were using DetNas as backbone, this is exprimental, may not work.')
            self.backbone = ShuffleNetV2DetNAS(model_size='VOC_RetinaNet_300M')
            detnas_pretrained = 'pretrained/VOC_RetinaNet_300M.pkl'
            if os.path.exists(detnas_pretrained):
                checkpoint = torch.load("VOC_RetinaNet_300M.pkl", map_location=torch.device('cpu'))
            else:
                print('{} does not exist, pls download it so that DetNas can be used. see readme under pretrained/')
                exit(0)
            self.backbone.load_state_dict(checkpoint)
            self.backbone = self.backbone.features
            return_layers = {'6': 1, '9': 2, '16': 3}
            in_channels_stage2 = 80

        self.body = _utils.IntermediateLayerGetter(backbone, cfg['return_layers'])
        in_channels_stage2 = cfg['in_channel']
        in_channels_list = [
            in_channels_stage2 * 2,
            in_channels_stage2 * 4,
            in_channels_stage2 * 8,
        ]
        out_channels = cfg['out_channel']
        self.fpn = FPN(in_channels_list,out_channels)
        self.ssh1 = SSH(out_channels, out_channels)
        self.ssh2 = SSH(out_channels, out_channels)
        self.ssh3 = SSH(out_channels, out_channels)

        self.ClassHead = self._make_class_head(fpn_num=3, inchannels=cfg['out_channel'])
        self.BboxHead = self._make_bbox_head(fpn_num=3, inchannels=cfg['out_channel'])
        self.LandmarkHead = self._make_landmark_head(fpn_num=3, inchannels=cfg['out_channel'])
Exemplo n.º 18
0
    def __init__(
        self,
        cfg=None,
        phase='train',
        calculate_prior_boxes=False,
        backbone=None,
    ):
        """
        :param cfg:  Network related settings.
        :param phase: train or test.
        """
        super(RetinaFace, self).__init__()
        self.phase = phase
        # backbone = None
        # if cfg['name'] == 'mobilenet0.25':
        #     backbone = MobileNetV1()
        #     if cfg['pretrain']:
        #         checkpoint = torch.load("./weights/mobilenetV1X0.25_pretrain.tar", map_location=torch.device('cpu'))
        #         from collections import OrderedDict
        #         new_state_dict = OrderedDict()
        #         for k, v in checkpoint['state_dict'].items():
        #             name = k[7:]  # remove module.
        #             new_state_dict[name] = v
        #         # load params
        #         backbone.load_state_dict(new_state_dict)
        # elif cfg['name'] == 'Resnet50':
        #     import torchvision.models as models
        #     backbone = models.resnet50(pretrained=cfg['pretrain'])

        self.cfg = cfg
        self.calculate_prior_boxes = calculate_prior_boxes

        self.body = _utils.IntermediateLayerGetter(backbone,
                                                   cfg['return_layers'])
        in_channels_stage2 = cfg['in_channel']
        in_channels_list = [
            in_channels_stage2 * 2,
            in_channels_stage2 * 4,
            in_channels_stage2 * 8,
        ]
        out_channels = cfg['out_channel']
        self.fpn = FPN(in_channels_list, out_channels)
        self.ssh1 = SSH(out_channels, out_channels)
        self.ssh2 = SSH(out_channels, out_channels)
        self.ssh3 = SSH(out_channels, out_channels)

        self.ClassHead = self._make_class_head(fpn_num=3,
                                               inchannels=cfg['out_channel'])
        self.BboxHead = self._make_bbox_head(fpn_num=3,
                                             inchannels=cfg['out_channel'])
        self.LandmarkHead = self._make_landmark_head(
            fpn_num=3, inchannels=cfg['out_channel'])
Exemplo n.º 19
0
    def __init__(self, cfg=None, phase='train'):
        """
        :param cfg:  Network related settings.
        :param phase: train or test.
        """
        super(RetinaTrackNet, self).__init__()
        self.phase = phase
        backbone = None

        if cfg['name'] == 'Resnet50':
            import torchvision.models as models
            backbone = models.resnet50(pretrained=cfg['pretrain'])
            self.body = _utils.IntermediateLayerGetter(backbone,
                                                       cfg['return_layers'])
        else:
            pass

        anchorNum = cfg[
            'anchorNum_per_stage']  ##not total anchor,indicate per stage anchors
        in_channels_stage2 = cfg['in_channel']
        in_channels_list = [
            in_channels_stage2 * 2,
            in_channels_stage2 * 4,
            in_channels_stage2 * 8,
        ]
        out_channels = cfg['out_channel']
        self.fpn = FPN(in_channels_list, out_channels)
        self.ssh1 = SSH(out_channels, out_channels)
        self.ssh2 = SSH(out_channels, out_channels)
        self.ssh3 = SSH(out_channels, out_channels)
        ##task shared
        self.task_shared = task_shared(out_channels,
                                       out_channels,
                                       anchorNum=anchorNum)
        ##task specific
        self.cls_task = task_specific_cls(out_channels, out_channels)
        self.loc_task = task_specific_loc(out_channels, out_channels)
        self.emb_task = task_specific_emb(out_channels, out_channels)
        ###head
        self.cls_heads = make_cls_head(inp=out_channels,
                                       fpnNum=len(in_channels_list),
                                       anchorNum=anchorNum)
        self.loc_heads = make_loc_head(inp=out_channels,
                                       fpnNum=len(in_channels_list),
                                       anchorNum=anchorNum)
        self.emb_heads = make_emb_head(inp=out_channels,
                                       fpnNum=len(in_channels_list),
                                       anchorNum=anchorNum)

        ###classifier ,ndim of emb dims and num of ids
        self.classifier = nn.Linear(256, 7)
Exemplo n.º 20
0
    def __init__(self, cfg=None, phase="train"):
        """
        :param cfg:  Network related settings.
        :param phase: train or test.
        """
        super(RetinaFace, self).__init__()
        self.phase = phase
        backbone = None
        if cfg["name"] == "mobilenet0.25":
            backbone = MobileNetV1()
            if cfg["pretrain"]:
                checkpoint = torch.load(
                    "./weights/mobilenetV1X0.25_pretrain.tar",
                    map_location=torch.device("cpu"),
                )
                from collections import OrderedDict

                new_state_dict = OrderedDict()
                for k, v in checkpoint["state_dict"].items():
                    name = k[7:]  # remove module.
                    new_state_dict[name] = v
                # load params
                backbone.load_state_dict(new_state_dict)
        elif cfg["name"] == "Resnet50":
            import torchvision.models as models

            backbone = models.resnet50(pretrained=cfg["pretrain"])

        self.body = _utils.IntermediateLayerGetter(backbone,
                                                   cfg["return_layers"])
        in_channels_stage2 = cfg["in_channel"]
        in_channels_list = [
            in_channels_stage2 * 2,
            in_channels_stage2 * 4,
            in_channels_stage2 * 8,
        ]
        out_channels = cfg["out_channel"]
        self.fpn = FPN(in_channels_list, out_channels)
        self.ssh1 = SSH(out_channels, out_channels)
        self.ssh2 = SSH(out_channels, out_channels)
        self.ssh3 = SSH(out_channels, out_channels)

        self.ClassHead = self._make_class_head(fpn_num=3,
                                               inchannels=cfg["out_channel"])
        self.BboxHead = self._make_bbox_head(fpn_num=3,
                                             inchannels=cfg["out_channel"])
        self.LandmarkHead = self._make_landmark_head(
            fpn_num=3, inchannels=cfg["out_channel"])
Exemplo n.º 21
0
    def __init__(self, cfg=None, phase='train'):
        """
        :param cfg:  Network related settings.
        :param phase: train or test.
        """
        super(RetinaFace, self).__init__()
        self.phase = phase
        backbone = None
        if cfg['name'] == 'mobilenet0.25':
            backbone = MobileNetV1()
            if cfg['pretrain']:
                # 改成绝对路径
                checkpoint = torch.load(os.path.join(
                    base_config.root_dir,
                    r"core\weights\retinaface\mobilenetV1X0.25_pretrain.tar"),
                                        map_location=torch.device('cpu'))
                from collections import OrderedDict
                new_state_dict = OrderedDict()
                for k, v in checkpoint['state_dict'].items():
                    name = k[7:]  # remove module.
                    new_state_dict[name] = v
                # load params
                backbone.load_state_dict(new_state_dict)
        elif cfg['name'] == 'Resnet50':
            import torchvision.models as models
            backbone = models.resnet50(pretrained=cfg['pretrain'])

        self.body = _utils.IntermediateLayerGetter(backbone,
                                                   cfg['return_layers'])
        in_channels_stage2 = cfg['in_channel']
        in_channels_list = [
            in_channels_stage2 * 2,
            in_channels_stage2 * 4,
            in_channels_stage2 * 8,
        ]
        out_channels = cfg['out_channel']
        self.fpn = FPN(in_channels_list, out_channels)
        self.ssh1 = SSH(out_channels, out_channels)
        self.ssh2 = SSH(out_channels, out_channels)
        self.ssh3 = SSH(out_channels, out_channels)

        self.ClassHead = self._make_class_head(fpn_num=3,
                                               inchannels=cfg['out_channel'])
        self.BboxHead = self._make_bbox_head(fpn_num=3,
                                             inchannels=cfg['out_channel'])
        self.LandmarkHead = self._make_landmark_head(
            fpn_num=3, inchannels=cfg['out_channel'])
Exemplo n.º 22
0
    def __init__(self,
                 cfg=None,
                 fpn_level=5,
                 head=HeadBasic,
                 outputs=[4, 2, 10]):
        """
        :param cfg:  Network related settings.
        :param phase: train or test.
        """
        super(SinvNet, self).__init__()
        backbone = None
        if cfg['name'] == 'mobilenet0.25':
            backbone = MobileNetV1()
            if cfg['pretrain']:
                checkpoint = torch.load(
                    "./weights/mobilenetV1X0.25_pretrain.tar",
                    map_location=torch.device('cpu'))
                from collections import OrderedDict
                new_state_dict = OrderedDict()
                for k, v in checkpoint['state_dict'].items():
                    name = k[7:]  # remove module.
                    new_state_dict[name] = v
                # load params
                backbone.load_state_dict(new_state_dict)
        elif cfg['name'] == 'Resnet50':
            import torchvision.models as models
            backbone = models.resnet50(pretrained=cfg['pretrain'])

        self.body = _utils.IntermediateLayerGetter(backbone,
                                                   cfg['return_layers'])
        in_channels = cfg['in_channel'] * 2
        out_channels = cfg['out_channel']
        self.downsample = nn.AvgPool2d(3, stride=2, padding=1)
        self.conv = conv_bn1X1(in_channels, out_channels, stride=1, leaky=0)
        self.fpn_level = fpn_level
        self.fpn = FPNLevel(out_channels, fpn_level)
        self.ssh = SSHLight(out_channels, out_channels)
        self.dropout = nn.Dropout(p=0.3)

        self.Heads = nn.ModuleList()
        for num_output in outputs:
            self.Heads.append(
                head(inchannels=out_channels,
                     num_anchors=1,
                     num_output=num_output))
Exemplo n.º 23
0
    def __init__(self, cfg=None, phase='train'):
        """
        phase: train / test
        """
        super(RetinaFace, self).__init__()
        print('RetinaFace work model : {}'.format(phase))
        self.phase = phase

        backbone = None
        if cfg['name'] == 'mobilenet0.25':
            backbone = MobileNetV1()
            if cfg['pretrain']:
                print(
                    'Loading pretrain model: ./weights/mobilenetV1X0.25_pretrain.tar'
                )
                checkpoint = torch.load(
                    "./weights/mobilenetV1X0.25_pretrain.tar",
                    map_location=torch.device('cpu'))
                from collections import OrderedDict
                new_state_dict = OrderedDict()
                for k, v in checkpoint['state_dict'].items():
                    name = k[7:]  # remove module.
                    new_state_dict[name] = v
                # load params
                backbone.load_state_dict(new_state_dict)
        self.body = _utils.IntermediateLayerGetter(backbone,
                                                   cfg['return_layers'])
        in_channels_stage2 = cfg['in_channel']
        in_channels_list = [
            in_channels_stage2 * 2,
            in_channels_stage2 * 4,
            in_channels_stage2 * 8,
        ]
        out_channels = cfg['out_channel']
        self.fpn = FPN(in_channels_list, out_channels)
        self.ssh1 = SSH(out_channels, out_channels)
        self.ssh2 = SSH(out_channels, out_channels)
        self.ssh3 = SSH(out_channels, out_channels)
        self.ClassHead = self._make_class_head(fpn_num=3,
                                               inchannels=cfg['out_channel'])
        self.BboxHead = self._make_bbox_head(fpn_num=3,
                                             inchannels=cfg['out_channel'])
        self.LandmarkHead = self._make_landmark_head(
            fpn_num=3, inchannels=cfg['out_channel'])
Exemplo n.º 24
0
    def __init__(self, class_num=3, cfg=None, phase='train'):
        """
        :param cfg:  Network related settings.
        :param phase: train or test.
        """
        super(RetinaFace, self).__init__()
        self.phase = phase
        backbone = None
        if cfg['name'] == 'mobilenet':
            backbone = MobileNetV1()
        elif cfg['name'] == 'Resnet50':
            import torchvision.models as models
            backbone = models.resnet50(pretrained=cfg['pretrain'])
        elif cfg['name'] == 'resnet18':
            import torchvision.models as models
            backbone = models.resnet18(pretrained=cfg['pretrain'])

        self.body = _utils.IntermediateLayerGetter(
            backbone, cfg['return_layers'])  # get output of each layers

        in_channels_stage2 = cfg['in_channel']

        in_channels_list = [
            in_channels_stage2 * 2,
            in_channels_stage2 * 4,
            in_channels_stage2 * 8,
        ]

        out_channels = cfg['out_channel']

        # in_channel 256 512 1024
        self.fpn = FPN(in_channels_list, out_channels)

        self.ssh1 = SSH(out_channels, out_channels)
        self.ssh2 = SSH(out_channels, out_channels)
        self.ssh3 = SSH(out_channels, out_channels)

        self.ClassHead = self._make_class_head(class_num,
                                               fpn_num=3,
                                               inchannels=cfg['out_channel'],
                                               anchor_num=5)
        self.BboxHead = self._make_bbox_head(fpn_num=3,
                                             inchannels=cfg['out_channel'],
                                             anchor_num=5)
Exemplo n.º 25
0
 def __init__(self):
     super(RetinaFaceModel, self).__init__()
     backbone = models.resnet50(pretrained=False)
     self.body = _utils.IntermediateLayerGetter(
         model=backbone,
         return_layers=CONFIG_RESNET_50['backbone_layers_to_extract'])
     in_channels_stage2 = CONFIG_RESNET_50['in_channel']
     in_channels_list = (in_channels_stage2 * 2, in_channels_stage2 * 4,
                         in_channels_stage2 * 8)
     out_channels = CONFIG_RESNET_50['out_channel']
     self.fpn = FPN(in_channels_list=in_channels_list,
                    out_channels=out_channels)
     self.ssh1 = SSH(in_channels=out_channels, out_channels=out_channels)
     self.ssh2 = SSH(in_channels=out_channels, out_channels=out_channels)
     self.ssh3 = SSH(in_channels=out_channels, out_channels=out_channels)
     self.ClassHead = self.__make_class_head(
         heads_num=3, in_channels=CONFIG_RESNET_50['out_channel'])
     self.BboxHead = self.__make_bbox_head(
         heads_num=3, in_channels=CONFIG_RESNET_50['out_channel'])
     self.LandmarkHead = self.__make_landmark_head(
         heads_num=3, in_channels=CONFIG_RESNET_50['out_channel'])
Exemplo n.º 26
0
    def __init__(self, cfg=None):
        super(Retina, self).__init__()
        if cfg['name'] == 'mobilenet0.25':
            backbone = MobileNetV1()

            self.body = _utils.IntermediateLayerGetter(backbone, cfg['return_layers'])

        in_channels_stage2 = cfg['in_channel']
        in_channels_list = [
            in_channels_stage2 * 2,
            in_channels_stage2 * 4,
            in_channels_stage2 * 8,
        ]
        out_channels = cfg['out_channel']
        self.fpn = FPN(in_channels_list, out_channels)
        self.ssh1 = SSH(out_channels, out_channels)
        self.ssh2 = SSH(out_channels, out_channels)
        self.ssh3 = SSH(out_channels, out_channels)

        self.ClassHead = self._make_class_head(fpn_num=3, inchannels=cfg['out_channel'])
        self.BboxHead = self._make_bbox_head(fpn_num=3, inchannels=cfg['out_channel'])
        self.LandmarkHead = self._make_landmark_head(fpn_num=3, inchannels=cfg['out_channel'])
Exemplo n.º 27
0
    def __init__(self, phase='test', weights=None):
        """
            :param phase: train or test.
        """
        super(RetinaFace, self).__init__()
        self.phase = phase

        backbone = MobileNetV1()
        # if weights is not None:
        #     checkpoint = torch.load("./weights/mobilenetV1X0.25_pretrain.tar", map_location=torch.device('cpu'))
        #     from collections import OrderedDict
        #     new_state_dict = OrderedDict()
        #     for k, v in checkpoint['state_dict'].items():
        #         name = k[7:]  # remove module.
        #         new_state_dict[name] = v
        #     # load params
        #     backbone.load_state_dict(new_state_dict)

        self.body = _utils.IntermediateLayerGetter(backbone, {
            'stage1': 1,
            'stage2': 2,
            'stage3': 3
        })
        in_channels_stage2 = 32
        in_channels_list = [
            in_channels_stage2 * 2,
            in_channels_stage2 * 4,
            in_channels_stage2 * 8,
        ]
        out_channels = 64
        self.fpn = FPN(in_channels_list, out_channels)
        self.ssh1 = SSH(out_channels, out_channels)
        self.ssh2 = SSH(out_channels, out_channels)
        self.ssh3 = SSH(out_channels, out_channels)

        self.ClassHead = self._make_class_head(fpn_num=3, inchannels=64)
        self.BboxHead = self._make_bbox_head(fpn_num=3, inchannels=64)
        self.LandmarkHead = self._make_landmark_head(fpn_num=3, inchannels=64)
Exemplo n.º 28
0
    def __init__(self):
        super(RetinaFace, self).__init__()
        backbone = models.resnet50(pretrained=False)

        self.body = _utils.IntermediateLayerGetter(backbone, {
            'layer2': 1,
            'layer3': 2,
            'layer4': 3
        })
        in_channels_stage2 = 256
        in_channels_list = [
            in_channels_stage2 * 2,
            in_channels_stage2 * 4,
            in_channels_stage2 * 8,
        ]
        out_channels = 256
        self.fpn = FPN(in_channels_list, out_channels)
        self.ssh1 = SSH(out_channels, out_channels)
        self.ssh2 = SSH(out_channels, out_channels)
        self.ssh3 = SSH(out_channels, out_channels)

        self.ClassHead = self._make_class_head(fpn_num=3, inchannels=256)
        self.BboxHead = self._make_bbox_head(fpn_num=3, inchannels=256)
        self.LandmarkHead = self._make_landmark_head(fpn_num=3, inchannels=256)
Exemplo n.º 29
0
    def __init__(self, cfg=None, phase='train'):
        super(RetinaFace, self).__init__()
        self.phase = phase
        backbone = None
        if cfg['name'] == 'mobilenet0.25_retinaface':
            backbone = MobileNetV1()
            if cfg['pretrain']:
                checkpoint = torch.load('', map_location=torch.device('cpu'))
                from collections import OrderedDict
                new_state_dict = OrderedDict()
                for k, v in checkpoint['state_dict'].items():
                    name = k[7:]  # remove module.
                    new_state_dict[name] = v
                # load params
                backbone.load_state_dict(new_state_dict)
        elif cfg['name'] == 'resnet50_ratinaface':
            import torchvision.models as models
            backbone = models.resnet50(pretrained=cfg['pretrain'])

        self.body = _utils.IntermediateLayerGetter(backbone, cfg['return_layers'])
        in_channels_stage2 = cfg['in_channel']
        in_channels_list = [
            in_channels_stage2 * 2,
            in_channels_stage2 * 4,
            in_channels_stage2 * 8,
        ]

        out_channels = cfg['out_channel']
        self.fpn = FPN(in_channels_list, out_channels)
        self.ssh1 = SSH(out_channels, out_channels)
        self.ssh2 = SSH(out_channels, out_channels)
        self.ssh3 = SSH(out_channels, out_channels)

        self.BoxHead = self._make_bbox_head(fpn_num=3, inchannels=cfg['out_channel'])
        self.ClassHead = self._make_class_head(fpn_num=3, inchannels=cfg['out_channel'])
        self.LandmarkHead = self._make_landmark_head(fpn_num=3, inchannels=cfg['out_channel'])
Exemplo n.º 30
0
    def __init__(self, model, phase='train'):
        """
        :param model: name of features extractor model.
        :param phase: train or test.
        """
        super(RetinaFace, self).__init__()
        self.phase = phase

        backbone = model

        self.body = _utils.IntermediateLayerGetter(backbone, {
            'stage2': 1,
            'stage3': 2,
            'stage4': 3
        })

        in_channels_stage2 = 58
        in_channels_list = [
            in_channels_stage2 * 2, in_channels_stage2 * 4,
            in_channels_stage2 * 8
        ]

        out_channels = 116

        self.fpn = FPN(in_channels_list, out_channels)

        self.ssh1 = SSH(out_channels, out_channels)
        self.ssh2 = SSH(out_channels, out_channels)
        self.ssh3 = SSH(out_channels, out_channels)

        self.ClassHead = self._make_class_head(fpn_num=3,
                                               inchannels=out_channels)
        self.BboxHead = self._make_bbox_head(fpn_num=3,
                                             inchannels=out_channels)
        self.LandmarkHead = self._make_landmark_head(fpn_num=3,
                                                     inchannels=out_channels)