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.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'])
Beispiel #3
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
    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'])
Beispiel #6
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'])
    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"]
        )
Beispiel #8
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))
Beispiel #9
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'] == 'ShuffleNet':

            self.body = ShuffleNetV2(scale=1, in_channels=3, c_tag=0.5, num_classes=2, activation=nn.ReLU,
                                 SE=False, residual=False).cuda()

        elif cfg['name'] == 'mobilenet0.25':
            self.body = 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'])
Beispiel #10
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)
            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,
            ]
        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,
            ]
        elif cfg['name'] == 'selecsls60':
            self.body = timm.create_model('selecsls60',
                                          features_only=True,
                                          out_indices=(cfg['return_layers']),
                                          pretrained=cfg['pretrain'])
            in_channels_list = self.body.feature_info.channels()
        elif cfg['name'] == 'mobilenetv3_large_100':
            self.body = timm.create_model('mobilenetv3_large_100',
                                          features_only=True,
                                          out_indices=(cfg['return_layers']),
                                          pretrained=cfg['pretrain'])
            in_channels_list = self.body.feature_info.channels()
        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'])
Beispiel #11
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'] == 'mobilenetv3':
            backbone = MobileNetV3()
            # bbweight = backbone.state_dict()
            # layername = []
            # oldln = []
            # for i,key in enumerate(bbweight):
            #     print("Layer {} : {}; Size : {}".format(i,key,bbweight[key].size()))
            #     layername.append(key)
            # cpk = torch.load("/home/hwits/Documents/ImageRetrieval/mobilenetv3/mbv3_small.pth.tar")['state_dict']
            # # print(cpk.keys())
            # print(cpk['epoch'])
            # print("="*200)
            # for i,key in enumerate(cpk):
            #     print("Old Layer {} : {}; Size : {}".format(i,key,cpk[key].size()))
            #     if i < len(layername):
            #         oldln.append(key)
            # print(bbweight.keys())
            # print(oldln)
            #
            # import collections
            # mbv3_cpk = collections.OrderedDict()
            # for i in range(len(layername)):
            #     mbv3_cpk[layername[i]] = cpk[oldln[i]]
            #
            # torch.save(mbv3_cpk,"mobilenetv3_truck.pth")
            if cfg['pretrain']:
                checkpoint = torch.load("./weights/mobilenetv3_truck.pth",
                                        map_location=torch.device('cpu'))
                print("Loading Pretrained Weights ...")
                backbone.load_state_dict(checkpoint)
        elif cfg['name'] == 'mobilenetv2_0.1':
            backbone = MobileNetV2()
            if cfg['pretrain']:
                checkpoint = torch.load("./weights/mobilenetv2_0.1_face.pth",
                                        map_location=torch.device('cpu'))
                model_dict = backbone.state_dict()
                pretrained_dict = {
                    k: v
                    for k, v in checkpoint.items() if k in model_dict
                }  # 用于过滤掉修改结构处的权重
                model_dict.update(pretrained_dict)
                # print("Pre-Trained:",pretrained_dict.keys())
                backbone.load_state_dict(model_dict)
        elif cfg['name'] == 'efficientnetb0':
            backbone = EfficientNet.from_name("efficientnet-b0")
            if cfg['pretrain']:
                checkpoint = torch.load("./weights/efficientnetb0_face.pth",
                                        map_location=torch.device('cpu'))
                backbone.load_state_dict(checkpoint)
                print("succeed loaded weights...")
        self.body = _utils.IntermediateLayerGetter(backbone,
                                                   cfg['return_layers'])
        if cfg['name'] == 'mobilenet0.25':
            in_channels_stage2 = cfg['in_channel']
            in_channels_list = [
                # in_channels_stage2 * 2,
                in_channels_stage2 * 4,
                in_channels_stage2 * 8,
            ]
        elif cfg['name'] == 'mobilenetv2_0.1':
            in_channels_stage3 = cfg['in_channel3']
            in_channels_stage4 = cfg['in_channel4']
            in_channels_list = [in_channels_stage3, in_channels_stage4]
        elif cfg['name'] == 'mobilenetv3':
            in_channels_stage1 = cfg['in_channel0']
            in_channels_stage2 = cfg['in_channel1']
            in_channels_stage3 = cfg['in_channel2']
            in_channels_list = [
                in_channels_stage1,
                in_channels_stage2,
                in_channels_stage3,
            ]
        elif cfg['name'] == 'efficientnetb0':
            # in_channels_stage2 = cfg['in_channel1']
            in_channels_stage3 = cfg['in_channel2']
            in_channels_list = [
                # in_channels_stage2 * 2,
                # in_channels_stage2,
                in_channels_stage3,
            ]
        out_channels = cfg['out_channel']
        self.pan = PAN(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'])
Beispiel #12
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'] == 'mobilenetv3':
            backbone = MobileNetV3()
            # print(backbone)
            if cfg['pretrain']:
                checkpoint = torch.load("./weights/mobilenetv3.pth", map_location=torch.device('cpu'))
                print("Pretrained Weights : ",type(checkpoint))
                backbone.load_state_dict(checkpoint)
        elif cfg['name'] == 'mobilenetv2_0.1':
            backbone = MobileNetV2()
            if cfg['pretrain']:
                checkpoint = torch.load("./weights/mobilenetv2_0.1_face.pth", map_location=torch.device('cpu'))
                backbone.load_state_dict(checkpoint)
        elif cfg['name'] == 'efficientnetb0':
            backbone = EfficientNet.from_name("efficientnet-b0")
            if cfg['pretrain']:
                checkpoint = torch.load("./weights/efficientnetb0_face.pth", map_location=torch.device('cpu'))
                backbone.load_state_dict(checkpoint)
                print("succeed loaded weights...")
        self.body = _utils.IntermediateLayerGetter(backbone, cfg['return_layers'])
        if cfg['name'] == 'mobilenet0.25':
            in_channels_stage2 = cfg['in_channel']
            in_channels_list = [
                # in_channels_stage2 * 2,
                in_channels_stage2*4,
                in_channels_stage2*8,
            ]
        elif cfg['name'] == 'mobilenetv2_0.1':
            in_channels_stage2 = cfg['in_channel1']
            in_channels_stage3 = cfg['in_channel2']
            in_channels_list = [
                # in_channels_stage2 * 2,
                in_channels_stage2,
                in_channels_stage3,
            ]
        elif cfg['name'] == 'mobilenetv3':
            in_channels_stage2 = cfg['in_channel1']
            in_channels_stage3 = cfg['in_channel2']
            in_channels_list = [
                # in_channels_stage2 * 2,
                in_channels_stage2,
                in_channels_stage3,
            ]
        elif cfg['name'] == 'efficientnetb0':
            in_channels_stage2 = cfg['in_channel1']
            in_channels_stage3 = cfg['in_channel2']
            in_channels_list = [
                # in_channels_stage2 * 2,
                in_channels_stage2,
                in_channels_stage3,
            ]
        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=2, inchannels=cfg['out_channel'])
        self.BboxHead = self._make_bbox_head(fpn_num=2, inchannels=cfg['out_channel'])
        self.LandmarkHead = self._make_landmark_head(fpn_num=2, inchannels=cfg['out_channel'])