def __init__(self, conf, verbose=True): self.device = conf.device self.threshold = conf.threshold try: if conf.use_mobilfacenet: self.model = MobileFaceNet(conf.embedding_size).to(conf.device) # download the default weigth file_name = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'mobilenet.pth') weight_path = os.path.join(os.path.dirname(file_name), 'weights/mobilenet.pth') if os.path.isfile(weight_path) == False: os.makedirs(os.path.split(weight_path)[0], exist_ok=True) download_weight( link= 'https://drive.google.com/uc?export=download&id=1W9nM7LE6zUKQ4tncL6OnBn-aXNyiRPNH', file_name=file_name, verbose=verbose) os.rename(file_name, weight_path) self.model.load_state_dict( torch.load(weight_path, map_location=conf.device)) else: self.model = Backbone(conf.net_depth, conf.drop_ratio, conf.net_mode).to(conf.device) # download the default weigth file_name = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'ir_se50.pth') weight_path = os.path.join(os.path.dirname(file_name), 'weights/ir_se50.pth') if os.path.isfile(weight_path) == False: os.makedirs(os.path.split(weight_path)[0], exist_ok=True) download_weight( link= 'https://www.dropbox.com/s/a570ucg6a5z22zf/ir_se50.pth?dl=1', file_name=file_name, verbose=verbose) os.rename(file_name, weight_path) self.model.load_state_dict( torch.load(weight_path, map_location=conf.device)) except IOError as e: exit( f'from FaceRecognizer Exit: the weight does not exist,' f' \n download and putting up in "{conf.work_path}" folder \n {e}' ) self.model.eval() torch.set_grad_enabled(False)
def __init__(self, name='mobilenet', weight_path=None, device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu"), confidence_threshold=0.99, top_k=5000, nms_threshold=0.4, keep_top_k=750, face_size=(112, 112)): """ RetinaFace Detector with 5points landmarks Args: name: name of backbone (resnet, mobilenet, slim, rfb) weight_path: path of network weight device: running device (cuda, cpu) face_size: final face size face_padding: padding for bounding boxes """ model, cfg = None, None if name == 'mobilenet': cfg = cfg_mnet model = RetinaFace(cfg=cfg, phase='test') elif name == 'resnet': cfg = cfg_re50 model = RetinaFace(cfg=cfg, phase='test') else: exit('from FaceDetector Exit: model not support \n just(mobilenet, resnet)') # download the default weigth if weight_path is None: file_name = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'mobilenet.pth') weight_path = os.path.join(os.path.dirname(file_name), 'weights/mobilenet.pth') if os.path.isfile(weight_path) == False: print('from FaceDetector: download defualt weight started') os.makedirs(os.path.split(weight_path)[0], exist_ok=True) download_weight(link='https://drive.google.com/uc?export=download&id=15zP8BP-5IvWXWZoYTNdvUJUiBqZ1hxu1', file_name=file_name) os.rename(file_name, weight_path) # setting for model model.load_state_dict(torch.load(weight_path, map_location=device)) model.to(device).eval() self.model = model self.device = device self.cfg = cfg # setting for face detection self.thresh = confidence_threshold self.top_k = top_k self.nms_thresh = nms_threshold self.keep_top_k = keep_top_k # setting for face align self.trans = transform.SimilarityTransform() self.out_size = face_size self.ref_pts = get_reference_facial_points(output_size=face_size) print('from FaceDetector: weights loaded') return
def __init__(self, name='full', weight_path=None, device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu")): """ Age and gender Detector :param name: name of backbone (full or tiny) :param device: model run in cpu or gpu (cuda, cpu) :param weight_path: path of network weight Notice: image size must be 112x112 but cun run with 224x224 Method detect: :param faces: 4d tensor of face for example size(1, 3, 112, 112) :returns genders list and ages list """ if name == 'tiny': model = ShuffleneTiny() elif name == 'full': model = ShuffleneFull() else: exit('from AgeGender Detector: model dose not support just(tiny, full)') # download the default weigth if weight_path is None: file_name = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ShufflenetFull.pth') weight_path = os.path.join(os.path.dirname(file_name), 'weights/ShufflenetFull.pth') if os.path.isfile(weight_path) == False: print('from AgeGenderEstimator: download defualt weight started') os.makedirs(os.path.split(weight_path)[0], exist_ok=True) download_weight(link='https://drive.google.com/uc?export=download&id=1rnOZo46RjGZYrUb6Wup6sSOP37ol5I9E', file_name=file_name) os.rename(file_name, weight_path) model.load_state_dict(torch.load(weight_path, map_location=device)) model.to(device).eval() self.model = model self.device = device print('from AgeGenderEstimator: weights loaded')
def __init__(self, name='densnet121', weight_path=None, device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu")): """ Residual Masking Emotion Detector from a list of labels :param name: name of backbone of networks (resnet34, densenet121) :param device: model run in cpu or gpu (cuda, cpu) :param weight_path: path of network weight Notice: image size must be 224x224 Method detect_emotion: :param faces: 4d tensor of face for example size(1, 3, 224, 224) :returns emotions list and probability of emotions """ self.device = device self.model = None if name == 'resnet34': self.model = resnet34() elif name == 'densnet121': self.model = densenet121() else: exit('from EmotionDetector: Network does not support!! \n just(resnet34, densnet121)') # download the default weigth if weight_path is None: file_name = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'densnet121.pth') weight_path = os.path.join(os.path.dirname(file_name), 'weights/densnet121.pth') if os.path.isfile(weight_path) == False: print('from EmotionDetector: download defualt weight started') os.makedirs(os.path.split(weight_path)[0], exist_ok=True) download_weight(link='https://drive.google.com/uc?export=download&id=1G3VsfgiQb16VyFnOwEVDgm2g8-9qN0-9', file_name=file_name) os.rename(file_name, weight_path) self.model.load_state_dict(torch.load(weight_path, map_location=device)) self.model.to(device).eval() print('from EmotionDetector: weights loaded')
def __init__(self, conf, inference=True): if conf.use_mobilfacenet: self.model = MobileFaceNet(conf.embedding_size).to(conf.device) else: self.model = Backbone(conf.net_depth, conf.drop_ratio, conf.net_mode).to(conf.device) if not inference: self.milestones = conf.milestones self.loader, self.class_num = get_train_loader(conf) self.step = 0 self.head = Arcface(embedding_size=conf.embedding_size, classnum=self.class_num).to(conf.device) print('two model heads generated') paras_only_bn, paras_wo_bn = separate_bn_paras(self.model) if conf.use_mobilfacenet: self.optimizer = optim.SGD( [{ 'params': paras_wo_bn[:-1], 'weight_decay': 4e-5 }, { 'params': [paras_wo_bn[-1]] + [self.head.kernel], 'weight_decay': 4e-4 }, { 'params': paras_only_bn }], lr=conf.lr, momentum=conf.momentum) else: self.optimizer = optim.SGD( [{ 'params': paras_wo_bn + [self.head.kernel], 'weight_decay': 5e-4 }, { 'params': paras_only_bn }], lr=conf.lr, momentum=conf.momentum) print(self.optimizer) print('optimizers generated') self.board_loss_every = len(self.loader) // 100 self.evaluate_every = len(self.loader) // 10 self.save_every = len(self.loader) // 5 self.agedb_30, self.cfp_fp, self.lfw, self.agedb_30_issame, self.cfp_fp_issame, self.lfw_issame = get_val_data( self.loader.dataset.root.parent) else: self.threshold = conf.threshold try: if conf.use_mobilfacenet: # download the default weigth file_name = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'mobilenet.pth') weight_path = os.path.join(os.path.dirname(file_name), 'weights/mobilenet.pth') if os.path.isfile(weight_path) == False: print( 'from FaceRecognizer: download defualt weight started' ) os.makedirs(os.path.split(weight_path)[0], exist_ok=True) download_weight( link= 'https://drive.google.com/uc?export=download&id=1W9nM7LE6zUKQ4tncL6OnBn-aXNyiRPNH', file_name=file_name) os.rename(file_name, weight_path) self.model.load_state_dict( torch.load(weight_path, map_location=conf.device)) print('from FaceRecognizer: MobileFaceNet Loaded') else: self.model.load_state_dict( torch.load(f'{conf.work_path}/ir_se50.pth')) print('from FaceRecognizer: IR_SE_50 Loaded') except IOError as e: exit( f'from FaceRecognizer Exit: the weight does not exist,' f' \n download and putting up in "{conf.work_path}" folder \n {e}' )