def __init__(self, conf, inference=False): pprint.pprint(conf) if conf.use_mobilfacenet: self.model = MobileFaceNet(conf.embedding_size).to(conf.device) print('MobileFaceNet model generated') else: self.model = Backbone(conf.net_depth, conf.drop_ratio, conf.net_mode).to(conf.device) print('{}_{} model generated'.format(conf.net_mode, conf.net_depth)) if not inference: self.milestones = conf.milestones self.loader, self.class_num = get_train_loader(conf) self.writer = SummaryWriter(conf.log_path) 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) # self.scheduler = optim.lr_scheduler.ReduceLROnPlateau(self.optimizer, patience=40, verbose=True) print('optimizers generated') self.board_loss_every = len(self.loader) // 5 # originally, 100 self.evaluate_every = len(self.loader) // 5 # originally, 10 self.save_every = len(self.loader) // 2 # originally, 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) self.val_112, self.val_112_issame = get_val_pair( self.loader.dataset.root.parent, 'val_112') else: self.threshold = conf.threshold
def verify(learner): vgg2_fp, vgg2_fp_issame = get_val_pair(conf.emore_folder, 'vgg2_fp') accuracy, best_threshold, roc_curve_tensor = learner.evaluate( conf, vgg2_fp, vgg2_fp_issame, nrof_folds=10, tta=True) print('vgg2_fp - accuray:{}, threshold:{}'.format(accuracy, best_threshold)) trans.ToPILImage()(roc_curve_tensor) agedb_30, agedb_30_issame = get_val_pair(conf.emore_folder, 'agedb_30') accuracy, best_threshold, roc_curve_tensor = learner.evaluate( conf, agedb_30, agedb_30_issame, nrof_folds=10, tta=True) print('agedb_30 - accuray:{}, threshold:{}'.format(accuracy, best_threshold)) trans.ToPILImage()(roc_curve_tensor) calfw, calfw_issame = get_val_pair(conf.emore_folder, 'calfw') accuracy, best_threshold, roc_curve_tensor = learner.evaluate( conf, calfw, calfw_issame, nrof_folds=10, tta=True) print('calfw - accuray:{}, threshold:{}'.format(accuracy, best_threshold)) trans.ToPILImage()(roc_curve_tensor) cfp_ff, cfp_ff_issame = get_val_pair(conf.emore_folder, 'cfp_ff') accuracy, best_threshold, roc_curve_tensor = learner.evaluate( conf, cfp_ff, cfp_ff_issame, nrof_folds=10, tta=True) print('cfp_ff - accuray:{}, threshold:{}'.format(accuracy, best_threshold)) trans.ToPILImage()(roc_curve_tensor) cfp_fp, cfp_fp_issame = get_val_pair(conf.emore_folder, 'cfp_fp') accuracy, best_threshold, roc_curve_tensor = learner.evaluate( conf, cfp_fp, cfp_fp_issame, nrof_folds=10, tta=True) print('cfp_fp - accuray:{}, threshold:{}'.format(accuracy, best_threshold)) trans.ToPILImage()(roc_curve_tensor) cplfw, cplfw_issame = get_val_pair(conf.emore_folder, 'cplfw') accuracy, best_threshold, roc_curve_tensor = learner.evaluate( conf, cplfw, cplfw_issame, nrof_folds=10, tta=True) print('cplfw - accuray:{}, threshold:{}'.format(accuracy, best_threshold)) trans.ToPILImage()(roc_curve_tensor) lfw, lfw_issame = get_val_pair(conf.emore_folder, 'lfw') accuracy, best_threshold, roc_curve_tensor = learner.evaluate( conf, lfw, lfw_issame, nrof_folds=10, tta=True) print('lfw - accuray:{}, threshold:{}'.format(accuracy, best_threshold)) trans.ToPILImage()(roc_curve_tensor)
from config import get_config import argparse from Learner import face_learner from data.data_pipe import get_val_pair from torchvision import transforms as trans import time import pdb conf = get_config(training=False) learner = face_learner(conf, inference=True) learner.load_state(conf, 'ir_se50.pth', model_only=True, from_save_folder=True) before_time = time.time() lfw, lfw_issame = get_val_pair(conf.emore_folder, 'lfw') accuracy, best_threshold, roc_curve_tensor = learner.evaluate(conf, lfw, lfw_issame, nrof_folds=10, tta=True) print('lfw - accuracy:{}, threshold:{}'.format(accuracy, best_threshold)) # trans.ToPILImage()(roc_curve_tensor) after_time = time.time() print(f'time_consumed: {after_time - before_time}') vgg2_fp, vgg2_fp_issame = get_val_pair(conf.emore_folder, 'vgg2_fp') accuracy, best_threshold, roc_curve_tensor = learner.evaluate(conf, vgg2_fp, vgg2_fp_issame, nrof_folds=10, tta=True)
from config import get_config from Learner import face_learner from data.data_pipe import get_val_pair from torchvision import transforms as trans conf = get_config(training=False) learner = face_learner(conf, inference=True) learner.load_state(conf, 'ir_se50.pth', model_only=True) # LFW evaluation lfw, lfw_issame = get_val_pair(conf.emore_folder, 'lfw') accuracy, best_threshold, roc_curve_tensor = learner.evaluate(conf, lfw, lfw_issame, nrof_folds=10, tta=False) print('lfw - accuray:{}, threshold:{}'.format(accuracy, best_threshold)) trans.ToPILImage()(roc_curve_tensor)
def evaluate_ori(model, path, name, nrof_folds=10, tta=True): from utils import ccrop_batch, hflip_batch from models.model import l2_norm from verifacation import evaluate idx = 0 from data.data_pipe import get_val_pair carray, issame = get_val_pair(path, name) carray = carray[:, ::-1, :, :] # BGR 2 RGB! if use_mxnet: carray *= 0.5 carray += 0.5 carray *= 255. embeddings = np.zeros([len(carray), 512]) if not use_mxnet: with torch.no_grad(): while idx + bs <= len(carray): batch = torch.tensor(carray[idx:idx + bs]) if tta: # batch = ccrop_batch(batch) fliped = hflip_batch(batch) emb_batch = model(batch.cuda()) + model(fliped.cuda()) emb_batch = emb_batch.cpu() embeddings[idx:idx + bs] = l2_norm(emb_batch) else: embeddings[idx:idx + bs] = model(batch.cuda()).cpu() idx += bs if idx < len(carray): batch = torch.tensor(carray[idx:]) if tta: # batch = ccrop_batch(batch) fliped = hflip_batch(batch) emb_batch = model(batch.cuda()) + model(fliped.cuda()) emb_batch = emb_batch.cpu() embeddings[idx:] = l2_norm(emb_batch) else: embeddings[idx:] = model(batch.cuda()).cpu() else: from sklearn.preprocessing import normalize while idx + bs <= len(carray): batch = torch.tensor(carray[idx:idx + bs]) if tta: fliped = hflip_batch(batch) emb_batch = model(batch) + model(fliped) embeddings[idx:idx + bs] = normalize(emb_batch) else: embeddings[idx:idx + bs] = model(batch) idx += bs if idx < len(carray): batch = torch.tensor(carray[idx:]) if tta: fliped = hflip_batch(batch) emb_batch = model(batch) + model(fliped) embeddings[idx:] = normalize(emb_batch) else: embeddings[idx:] = model(batch) tpr, fpr, accuracy, best_thresholds = evaluate(embeddings, issame, nrof_folds) roc_curve_tensor = None # buf = gen_plot(fpr, tpr) # roc_curve = Image.open(buf) # roc_curve_tensor = trans.ToTensor()(roc_curve) return accuracy.mean(), best_thresholds.mean(), roc_curve_tensor