raise NameError('model dir exists!') os.makedirs(save_dir) logging = init_log(save_dir) _print = logging.info # define trainloader and testloader trainset = CASIA_Face(root=CASIA_DATA_DIR) trainloader = torch.utils.data.DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True, num_workers=8, drop_last=False) # nl: left_image_path # nr: right_image_path nl, nr, folds, flags = parseList(root=LFW_DATA_DIR) testdataset = LFW(nl, nr) testloader = torch.utils.data.DataLoader(testdataset, batch_size=32, shuffle=False, num_workers=8, drop_last=False) # define model net = model.MobileFacenet() ArcMargin = model.ArcMarginProduct(128, trainset.class_nums) if RESUME: ckpt = torch.load(RESUME) net.load_state_dict(ckpt['net_state_dict']) start_epoch = ckpt['epoch'] + 1
imgr = imageio.imread(self.imgr_list[index]) if len(imgr.shape) == 2: imgr = np.stack([imgr] * 3, 2) # imgl = imgl[:, :, ::-1] # imgr = imgr[:, :, ::-1] imglist = [imgl, imgl[:, ::-1, :], imgr, imgr[:, ::-1, :]] for i in range(len(imglist)): imglist[i] = (imglist[i] - 127.5) / 128.0 imglist[i] = imglist[i].transpose(2, 0, 1) imgs = [torch.from_numpy(i).float() for i in imglist] return imgs def __len__(self): return len(self.imgl_list) if __name__ == '__main__': data_dir = '/home/users/keiller/recfaces/datasets/LFW/' from lfw_eval import parseList nl, nr, folds, flags = parseList(root=data_dir) dataset = LFW(nl, nr) trainloader = torch.utils.data.DataLoader(dataset, batch_size=32, shuffle=True, num_workers=8, drop_last=False) print(len(dataset)) for data in trainloader: print(data[0].shape)
def main(): # 加载训练好的模型 net = model.MobileFacenet() ckpt = torch.load("model/best/068.ckpt", map_location='cpu') net.load_state_dict(ckpt['net_state_dict']) net.eval() # 加载数据集 nl, nr, flods, flags = parseList(LFW_DATA_DIR) # 得到左边人和右边人 lfw_dataset = LFW(nl, nr) lfw_loader = torch.utils.data.DataLoader(lfw_dataset, batch_size=32, shuffle=False, num_workers=8, drop_last=False) featureLs = None featureRs = None count = 0 # 图片总数 # 这一步是在网络算出每个人的人脸特征,得到了左边的人脸特征和右边的特征,做余弦相似度,就能判断是不是同一个人 for i, data in tqdm(enumerate(lfw_loader)): # data (4, 32, 3, 112, 96) count += data[0].size(0) # batch的大小 # print('extracing deep features from the face pair {}...'.format(count)) res = [net(d).data.cpu().numpy() for d in data] # d 表示一个图像 featureL = np.concatenate((res[0], res[1]), 1) # 纵向拼接,也就是接在列后面 featureR = np.concatenate((res[2], res[3]), 1) if featureLs is None: featureLs = featureL else: featureLs = np.concatenate((featureLs, featureL), 0) # 横向拼接,也就是接在行后面 if featureRs is None: featureRs = featureR else: featureRs = np.concatenate((featureRs, featureR), 0) # print(featureRs.shape) # (6000, 256)这里256的原因是在加载数据集时将逆转宽度的向量也加入进来了,类似与得到了一张左右对称的图 result = {'fl': featureLs, 'fr': featureRs, 'fold': flods, 'flag': flags} scipy.io.savemat("result.mat", result) ACCs = np.zeros(10) result = scipy.io.loadmat("result.mat") for i in range(10): fold = result['fold'] flags = result['flag'] featureLs = result['fl'] featureRs = result['fr'] valFold = fold != i testFold = fold == i flags = np.squeeze(flags) mu = np.mean( np.concatenate( (featureLs[valFold[0], :], featureRs[valFold[0], :]), 0), 0) # 获得人脸特征的均值 mu = np.expand_dims(mu, 0) featureLs = featureLs - mu # 减去均值 featureRs = featureRs - mu featureLs = featureLs / np.expand_dims( np.sqrt(np.sum(np.power(featureLs, 2), 1)), 1) # 除以根号下的平方 featureRs = featureRs / np.expand_dims( np.sqrt(np.sum(np.power(featureRs, 2), 1)), 1) # 其实上面几步就是归一化,映射到-1到1之间 scores = np.sum(np.multiply(featureLs, featureRs), 1) # 求向量内积 threshold = getThreshold(scores[valFold[0]], flags[valFold[0]], 10000) # 从验证集中获得精度最高的阈值 ACCs[i] = getAccuracy(scores[testFold[0]], flags[testFold[0]], threshold) # 根据阈值,算在测试机中的准确率 return ACCs
save_dir = os.path.join(SAVE_DIR, MODEL_PRE + 'v2_' + datetime.now().strftime('%Y%m%d_%H%M%S')) if os.path.exists(save_dir): raise NameError('model dir exists!') os.makedirs(save_dir) logging = init_log(save_dir) _print = logging.info # define trainloader and testloader trainset = CASIA_Face(root=CASIA_DATA_DIR) trainloader = torch.utils.data.DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True, num_workers=8, drop_last=False) # nl: left_image_path # nr: right_image_path nl, nr, folds, flags = parseList(root=LFW_DATA_DIR) testdataset = LFW(nl, nr) testloader = torch.utils.data.DataLoader(testdataset, batch_size=32, shuffle=False, num_workers=8, drop_last=False) # define model net = model.MobileFacenet() ArcMargin = model.ArcMarginProduct(128, trainset.class_nums) if RESUME: ckpt = torch.load(RESUME) net.load_state_dict(ckpt['net_state_dict']) start_epoch = ckpt['epoch'] + 1 # define optimizers