def load_cosface_model(): model_path = 'CosFace_pytorch/cosface_model.pth' model = sphere().to(device) model.load_state_dict(torch.load(model_path, map_location=device)) model.eval() return model
def main(): # --------------------------------------model---------------------------------------- if args.network is 'sphere20': model = net.sphere(type=20, is_gray=args.is_gray) model_eval = net.sphere(type=20, is_gray=args.is_gray) elif args.network is 'sphere64': model = net.sphere(type=64, is_gray=args.is_gray) model_eval = net.sphere(type=64, is_gray=args.is_gray) elif args.network is 'LResNet50E_IR': model = net.LResNet50E_IR(is_gray=args.is_gray) model_eval = net.LResNet50E_IR(is_gray=args.is_gray) else: raise ValueError("NOT SUPPORT NETWORK! ") model = torch.nn.DataParallel(model).to(device) model_eval = model_eval.to(device) print(model) if not os.path.exists(args.save_path): os.makedirs(args.save_path) model.module.save(args.save_path + 'CosFace_0_checkpoint.pth') # 512 is dimension of feature classifier = { 'MCP': layer.MarginCosineProduct(512, args.num_class).to(device), 'AL': layer.AngleLinear(512, args.num_class).to(device), 'L': torch.nn.Linear(512, args.num_class, bias=False).to(device) }[args.classifier_type] # ------------------------------------load image--------------------------------------- if args.is_gray: train_transform = transforms.Compose([ transforms.Grayscale(), transforms.RandomHorizontalFlip(), transforms.ToTensor(), # range [0, 255] -> [0.0,1.0] transforms.Normalize(mean=(0.5, ), std=(0.5, )) ]) # gray else: train_transform = transforms.Compose([ transforms.RandomHorizontalFlip(), transforms.ToTensor(), # range [0, 255] -> [0.0,1.0] transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)) # range [0.0, 1.0] -> [-1.0,1.0] ]) train_loader = torch.utils.data.DataLoader(ImageList( root=args.root_path, fileList=args.train_list, transform=train_transform), batch_size=args.batch_size, shuffle=True, num_workers=args.workers, pin_memory=True, drop_last=True) print('length of train Database: ' + str(len(train_loader.dataset))) print('Number of Identities: ' + str(args.num_class)) # --------------------------------loss function and optimizer----------------------------- criterion = torch.nn.CrossEntropyLoss().to(device) optimizer = torch.optim.SGD([{ 'params': model.parameters() }, { 'params': classifier.parameters() }], lr=args.lr, momentum=args.momentum, weight_decay=args.weight_decay) # ----------------------------------------train---------------------------------------- # lfw_eval.eval(args.save_path + 'CosFace_0_checkpoint.pth') for epoch in range(1, args.epochs + 1): train(train_loader, model, classifier, criterion, optimizer, epoch) model.module.save(args.save_path + 'CosFace_' + str(epoch) + '_checkpoint.pth') lfw_eval.eval( model_eval, args.save_path + 'CosFace_' + str(epoch) + '_checkpoint.pth', args.is_gray) print('Finished Training')
return ft_dict def make_cosine_similarity_file(target_img, all_img_path, model, model_path): ''' file_dir = 'experiments' ''' img_list = os.listdir(all_img_path) ft_dict = {} txt_name = '_'.join(all_img_path.split('/')) fo = open('experiments/{}.txt'.format(txt_name), 'w') foo = open('experiments/min_max_{}.txt'.format(txt_name), 'w') ft_dict = extract_all_img_ft(model, model_path, all_img_path, img_list) make_cosine_similarity_matrix(ft_dict, img_list, fo, foo) if __name__ == '__main__': model = net.sphere().to('cuda') model_path = 'checkpoint/original.pth' img_path = 'lfw-112X96/Josh_Kronfeld/Josh_Kronfeld_0001.jpg' CASIA_dataset = '/data1/aipd_tuijian/charlesliu/dataset/CASIA-WebFace' lfw_dataset = '/data1/aipd_tuijian/charlesliu/dataset/lfw-112X96' img_dir_name_india_women = ['6432391'] img_dir_name_men = ['6418193'] img_dir_name_india_women_glasses = ['027.jpg', '054.jpg'] img_dir_name_india_women_make_up = ['043.jpg', '037.jpg', '028.jpg', '016.jpg', '007.jpg'] img_dir_name_india_women_occlusion = ['023.jpg', '017.jpg'] img_1 = os.path.join(lfw_dataset, 'Abel_Pacheco', 'Abel_Pacheco_0001.jpg') img_2 = os.path.join(lfw_dataset, 'Abel_Pacheco', 'Abel_Pacheco_0004.jpg') f1 = extract_one_img_ft(model, model_path, img_1) f2 = extract_one_img_ft(model, model_path, img_2) distance = f1.dot(f2) / (f1.norm() * f2.norm() + 1e-5) print distance
img1 = Image.open(f).convert('RGB') with open(root + name2, 'rb') as f: img2 = Image.open(f).convert('RGB') f1 = extractDeepFeature(img1, model, is_gray) f2 = extractDeepFeature(img2, model, is_gray) distance = f1.dot(f2) / (f1.norm() * f2.norm() + 1e-5) predicts.append('{}\t{}\t{}\t{}\n'.format(name1, name2, distance, sameflag)) accuracy = [] thd = [] folds = KFold(n=6000, n_folds=10) thresholds = np.arange(-1.0, 1.0, 0.005) predicts = np.array(map(lambda line: line.strip('\n').split(), predicts)) for idx, (train, test) in enumerate(folds): best_thresh = find_best_threshold(thresholds, predicts[train]) accuracy.append(eval_acc(best_thresh, predicts[test])) thd.append(best_thresh) print('LFWACC={:.4f} std={:.4f} thd={:.4f}'.format(np.mean(accuracy), np.std(accuracy), np.mean(thd))) return np.mean(accuracy), predicts if __name__ == '__main__': _, result = eval(net.sphere().to('cuda'), model_path='checkpoint/CosFace_24_checkpoint.pth') np.savetxt("result.txt", result, '%s')
else: raise ValueError("WRONG LINE IN 'pairs.txt! ") with open(root + name1, 'rb') as f: img1 = Image.open(f).convert('RGB') with open(root + name2, 'rb') as f: img2 = Image.open(f).convert('RGB') f1 = extractDeepFeature(img1, model, is_gray) f2 = extractDeepFeature(img2, model, is_gray) distance = f1.dot(f2) / (f1.norm() * f2.norm() + 1e-5) predicts.append('{}\t{}\t{}\t{}\n'.format(name1, name2, distance, sameflag)) accuracy = [] thd = [] folds = KFold(n=6000, n_folds=10) thresholds = np.arange(-1.0, 1.0, 0.005) predicts = np.array(map(lambda line: line.strip('\n').split(), predicts)) for idx, (train, test) in enumerate(folds): best_thresh = find_best_threshold(thresholds, predicts[train]) accuracy.append(eval_acc(best_thresh, predicts[test])) thd.append(best_thresh) print('LFWACC={:.4f} std={:.4f} thd={:.4f}'.format(np.mean(accuracy), np.std(accuracy), np.mean(thd))) return np.mean(accuracy), predicts if __name__ == '__main__': _, result = eval(net.sphere().to('cuda'), model_path='checkpoint/CosFace_24_checkpoint.pth') np.savetxt("result.txt", result, '%s')