transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))])
class ImdbValDataset(Dataset):
def __init__(self, root_dir,type="imdb",flip=False):
super(ImdbValDataset, self).__init__()
self.flip=flip
self.root_dir = root_dir
self.imgs = glob.glob(root_dir + "/*.jpg", recursive=False)
# self.imgs=self.imgs[:10000]
self.data_len=len(self.imgs)
self.type=type
def __getitem__(self, index):
id_path = self.imgs[index]
img_id=cv2.imread(id_path)
img_id=cv2.resize(img_id,(112,112))
img_id = cv2.cvtColor(img_id, cv2.COLOR_BGR2RGB)
if self.flip:
img_id_flip = np.fliplr(img_id)
data_id_flip=transform(img_id_flip.copy()).unsqueeze(0)
data_id=transform(img_id).unsqueeze(0)
if self.type=="imdb":
img_path=id_path.replace("Id","Life").replace("id","face")
else:
img_path=id_path.replace("Id","Life").replace("_IDCard","_Face")
img_face=cv2.imread(img_path)
img_face=cv2.resize(img_face,(112,112))
img_face = cv2.cvtColor(img_face, cv2.COLOR_BGR2RGB)
if self.flip:
img_face_flip = np.fliplr(img_face)
data_face_flip=transform(img_face_flip.copy()).unsqueeze(0)
data_face=transform(img_face).unsqueeze(0)
if self.flip:
data=torch.cat((data_id,data_id_flip,data_face,data_face_flip),0)
else:
data=torch.cat((data_id,data_face),0)
return data
def __len__(self):
return self.data_len
imdb_eval_img_find_err.py
import glob
import time
from collections import OrderedDict
import numpy as np
import os
import cv2
import torch
from torch.utils.data import dataloader, Dataset
from torchvision import transforms
from backbones.face_model3 import MobileFaceNet3
from backbones.face_model3_3 import MobileFaceNet3_3
from backbones.face_model_65_2 import MobileFace_65_2
from backbones.face_model_65_2_dep import MobileFace_65_2_dep
from backbones.face_model_71_2 import MobileFace_71_2
from calculate_acc_xudong import calculate_top_acc, get_sorted_file, cal_tar_far
from dataset import ImdbValDataset
from face_2022.face_model_83 import MobileFace_83
from face_2022.mfn_50m import MFN_50m
from face_model_72 import MobileFace_72
from face_model_837 import MobileFace_837
from face_model_837_s import MobileFace_837_s
from face_model_87 import MobileFace_87
# from train_83.face_model_83 import MobileFace_83
from nets.mfn_85m import MFN_85m
import numpy as np
path_id="/data/nvmeData1/zhaojia/Data/val/IMIDB_4w4w/Id/"
path_file="/data/nvmeData1/zhaojia/Data/val/IMIDB_4w4w/Life/"
path_id=r"G:\data\IMIDB_4w4w\Id_2\0/"
path_file=r"G:\data\IMIDB_4w4w\Life_2\0/"
path_id=r"G:\data\IMIDB_4w4w\Id_2\1/"
path_file=r"G:\data\IMIDB_4w4w\Life_2\1/"
def l2_norm(input,axis=1):
norm = torch.norm(input,2,axis,True)
output = torch.div(input, norm)
if torch.isnan(output[0][0]):
print('isnans')
return output
flip_flag=1
if __name__ == '__main__':
model_name="mfv2"
# model_name="mfv2_07"
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
model_name="mv_83"
model_name="face_83"
model_name="mfn_50"
# model_name="mv_87"
# model_name="mv_83_s"
# torch.set_num_threads(2)
print("model_name",model_name)
if model_name=="mv_87":
model = MobileFace_87(embedding_size=256).cuda()
# model.load_state_dict(torch.load('../recognition/train_83/train_5w_1/m_871/0.8763_153600_net.pth', map_location=lambda storage, loc: storage))#88.02 93.68
# model.load_state_dict(torch.load('../recognition/train_83/train_5w_1/m_871/0.8761_145600_net.pth', map_location=lambda storage, loc: storage))#87.99 93.70
# model.load_state_dict(torch.load('../recognition/train_83/train_5w_1/m_871/0.8768_200800_net.pth', map_location=lambda storage, loc: storage))#88.08 93.7
model.load_state_dict(torch.load('../recognition/train_83/train_5w_1/m_871/0.8766_49200_net.pth', map_location=lambda storage, loc: storage))#
model.eval()
elif model_name=="zhaojia":
# model=torch.jit.load("./eval_ssp/faceID_for_supei.pt").cuda()
model = MobileFace_837(embedding_size=256).cuda()
# model.load_state_dict(torch.load('../recognition/train_83/train_5w_1/m_83_drop/0.8395_20400_net.pth', map_location=lambda storage, loc: storage))#84.37
# model.load_state_dict(torch.load('../recognition/train_83/train_5w_1/m_83_drop/0.8408_28800_net.pth', map_location=lambda storage, loc: storage))#84.43 91.39
model.load_state_dict(torch.load('../recognition/train_83/train_5w_1/m_83_drop/0.8401_9600_net.pth', map_location=lambda storage, loc: storage))#
model.eval()
elif model_name=="mv_72":
model = MobileFace_72(embedding_size=256).cuda()
model.load_state_dict(torch.load('./eval_ssp/m72_bn.pth', map_location=lambda storage, loc: storage))
model.eval()
# model.half()
elif model_name=="mv_83":
model =MobileFace_83(256).cuda()
# state_dict=torch.load('/data/sharedata/lbg/reid/insightface-master/recognition/train_83/5w_noarg/m_83v3/0.8522_46400_net.pth', map_location=lambda storage, loc: storage)#85.98 92.56
# state_dict=torch.load('/data/sharedata/lbg/reid/insightface-master/recognition/train_83/5w_noarg/m_83v3/0.8529_49200_net.pth', map_location=lambda storage, loc: storage)#86.12 92.45
# state_dict=torch.load('/data/sharedata/lbg/reid/insightface-master/recognition/train_83/5w_noarg/m_83v3/0.8551_62800_net.pth', map_location=lambda storage, loc: storage)#86.28 92.54
state_dict=torch.load('/data/sharedata/lbg/reid/insightface-master/recognition/train_83/5w_noarg/m_83v3/0.8562_91600_net.pth', map_location=lambda storage, loc: storage)#86.42 92.64
new_state_dict = OrderedDict()
for k, v in state_dict.items():
head = k[:7]
if head == 'module.':
tmp_name = k[7:] # remove `module.`
else:
tmp_name = k
new_state_dict[tmp_name] = v
model.load_state_dict(new_state_dict)
model.eval()
elif model_name=="face_83":
model =MobileFace_83(256).cuda()
batchsize = 64
# state_dict=torch.load('/data/sharedata/lbg/reid/insightface-master/recognition/train_83/5w_noarg/m_83v3/0.8551_62800_net.pth', map_location=lambda storage, loc: storage)#86.28 92.54
state_dict=torch.load('face_2022/0.8953_73200_0.000050_net.pth', map_location=lambda storage, loc: storage)#86.42 92.64
new_state_dict = OrderedDict()
for k, v in state_dict.items():
head = k[:7]
if head == 'module.':
tmp_name = k[7:] # remove `module.`
else:
tmp_name = k
new_state_dict[tmp_name] = v
model.load_state_dict(new_state_dict)
model.eval()
elif model_name=="mfn_50":
model =MFN_85m().cuda()
batchsize = 16
# state_dict=torch.load('/data/sharedata/lbg/reid/insightface-master/recognition/train_83/5w_noarg/m_83v3/0.8551_62800_net.pth', map_location=lambda storage, loc: storage)#86.28 92.54
state_dict=torch.load('face_2022/0.9549_61800_net.pth', map_location=lambda storage, loc: storage)#86.42 92.64
new_state_dict = OrderedDict()
for k, v in state_dict.items():
head = k[:7]
if head == 'module.':
tmp_name = k[7:] # remove `module.`
else:
tmp_name = k
new_state_dict[tmp_name] = v
model.load_state_dict(new_state_dict)
model.eval()
if model_name=="mfv2_07":
model=MobileFace_71_2(256).cuda()
model.load_state_dict(torch.load('../recognition/train_71/m_72/0.9710_0_98000_net.pth', map_location=lambda storage, loc: storage))#59
model.eval()
# model.load_state_dict(torch.load('/data/sharedata/lbg/reid/insightface-master/recognition/arcface_torch/m_65_long/0.9714_0_441500_net.pth', map_location=lambda storage, loc: storage))#52
# model =MobileFace_65_2(embedding_size=256).cuda()
# model.eval()
print('MobileFaceNet face detection model generated')
save_err="err_img/"
err_list=[]
os.makedirs(save_err,exist_ok=True) if save_err else 1
model.eval()
flip=True
dataset=ImdbValDataset(root_dir=path_id, flip=flip)
val_loader = dataloader.DataLoader(dataset,shuffle=False,batch_size=batchsize,num_workers=0,pin_memory=True,drop_last=False)
feat_life=[]
feat_id=[]
name_life=[]
start=time.time()
for index,datas in enumerate(val_loader):
data_b=datas.size(0)
input_blob = datas.view(-1,3,112,112)
imgs = input_blob.cuda()
# imgs.sub_(0.5).div_(0.5)
with torch.no_grad():
feat = model(imgs)
print(index,feat.size())
res = feat
name_life.extend([i+index for i in range(data_b)])
for a_index in range(data_b):
if flip:
featureL = l2_norm((res[a_index*4+0] + res[a_index*4+1]).unsqueeze(0))
featureR = l2_norm((res[a_index*4+2] + res[a_index*4+3]).unsqueeze(0))
else:
featureL = l2_norm(res[a_index*2+0].unsqueeze(0))
featureR = l2_norm(res[a_index*2+1].unsqueeze(0))
feat_id.append(featureL.cpu())
feat_life.append(featureR.cpu())
# if index==14:
# break
print("data time",time.time()-start)
start1=time.time()
num_id=len(feat_life)
feat_life=torch.cat(feat_life,0)
print(feat_life.shape[0],num_id)
feat_id=torch.cat(feat_id,0)
inner_mat=torch.matmul(feat_life,feat_id.T)
norm_life=torch.norm(feat_life,p=2,dim=1)
del feat_life
norm_id=torch.norm(feat_id,p=2,dim=1)
del feat_id
norm_life=norm_life.reshape(num_id,1)
norm_id=norm_id.reshape(1,num_id)
denom=torch.matmul(norm_life,norm_id)
del norm_id
del norm_life
score_mat=inner_mat/denom
print(score_mat.shape,"mat time",time.time()-start1)
# index_score=score_mat.argsort(axis=1).flip(dims=[1])
values,index_score=score_mat.topk(5,dim=1)
print("start calcu index_score.shape",index_score.shape)
#max_score=np.max(score_mat,axis=1)
top1=0
top5=0
for i in range(index_score.shape[0]):
index_i=index_score[i]
if name_life[i]==name_life[index_i[0]]:
top1+=1
elif save_err:
img1=name_life[i]
id_1= dataset.imgs[img1]
face_1 = id_1.replace("Id", "Life").replace("id", "face")
img2 = name_life[index_i[0]]
id_2= dataset.imgs[img2]
err_list.extend([id_1,id_2])
face_2 = id_2.replace("Id", "Life").replace("id", "face")
face_img=np.hstack((cv2.imread(id_1),cv2.imread(face_1)))
face_img2=np.hstack((cv2.imread(id_2),cv2.imread(face_2)))
img_all=np.vstack((face_img,face_img2))
cv2.imwrite(save_err+os.path.basename(id_1),img_all)
# cv2.imshow("asdf",img_all)
# cv2.waitKey()
# cv2.imwrite("asdf'",img1)
if name_life[i]==name_life[index_i[0]] or name_life[i]==name_life[index_i[1]] or name_life[i]==name_life[index_i[2]] \
or name_life[i]==name_life[index_i[3]] or name_life[i]==name_life[index_i[4]] :
top5+=1
# fs.write(name_life[i]+":"+name_id[index_i[0]]+","+str(score_mat[i][index_i[0]])+","+name_id[index_i[1]]+","+str(score_mat[i][index_i[1]]) \
# +","+name_id[index_i[2]]+","+str(score_mat[i][index_i[2]])+","+name_id[index_i[3]]+","+str(score_mat[i][index_i[3]]) \
# +","+name_id[index_i[4]]+","+str(score_mat[i][index_i[4]])+"\n")
with open('err_list.txt', 'w') as f:
f.write("\n".join(err_list))
print("top1 acc: %f" %(float(top1)/num_id),time.time()-start)
print("top5 acc: %f" %(float(top5)/num_id))