-
Notifications
You must be signed in to change notification settings - Fork 0
/
c2p_test.py
136 lines (104 loc) · 4.15 KB
/
c2p_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'''
code version 1.0 by hjc (from nju to ucas)
'''
from __future__ import print_function
from torchvision import transforms
import argparse
import bisect
import datetime
import os
import pickle
import random
import sys
from PIL import Image
import pandas as pd
import cv2
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
from net_sphere import sphere20a
torch.backends.cudnn.bencmark = True
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='PyTorch sphereface wc')
parser.add_argument('--dataset_path', default='./test/', type=str)
parser.add_argument('--class_num', default=123, type=int)
# parser.add_argument('--model', '-m', default='../../support_material/sphere20a.pth', type=str)
parser.add_argument('--model', '-m', default='outputs\init_original_dataset\checkpoints/00012000.pth', type=str)
# parser.add_argument('--model', '-m', default='outputs/init_original_dataset_SE3out/checkpoints/00005000.pth', type=str)
parser.add_argument('--ipython', action='store_true')
parser.add_argument('--myGpu', default='0', help='GPU Number')
args = parser.parse_args()
os.environ["CUDA_VISIBLE_DEVICES"] = args.myGpu
if args.ipython:
from IPython import embed; embed()
exit(0)
# 加载网络
net = sphere20a(classnum=args.class_num)
net.load_state_dict(torch.load(args.model))
net.cuda()
net.eval()
net.feature = True
transform = transforms.Compose([
transforms.Resize((112, 96)),
])
# 加载Probe图片数据
with open('FR_Probe_C2P.txt') as f:
probe_lines = f.readlines()
# 对每一行进行处理
probes=[]
for line in probe_lines:
l = line.strip()
probes.append(l)
# 加载Gallery
with open('FR_Gallery_C2P.txt') as f:
gallery_lines = f.readlines()
# 对每一行进行处理
gallerys = []
for line in gallery_lines:
l = line.strip()
gallerys.append(l)
predicts=[]
gallerys_feature=[]
for k in range(len(gallerys)):
this_gallery_path = args.dataset_path + gallerys[k] + '.jpg'
gallery_img = np.array(transform(Image.open(this_gallery_path).convert('RGB')))
imglist = [gallery_img, cv2.flip(gallery_img, 1)] # cv2.flip(img1, 1):112*96*3
for i in range(len(imglist)):
imglist[i] = imglist[i].transpose(2, 0, 1).reshape((1, 3, 112, 96))
imglist[i] = (imglist[i] - 127.5) / 128.
img = np.vstack(imglist) # 2*3*112*96 (垂直将imglist中的2组数据合起来)
with torch.no_grad():
img = Variable(torch.from_numpy(img).float(), volatile=True).cuda()
output = net(img) # 2*512
f = output.data
f1 = f[0] # img1 output
gallerys_feature.append(f1)
for i in range(len(probes)):
print(str(i) + " start")
this_probe_path = args.dataset_path+probes[i]+'.jpg'
probe_img = np.array(transform(Image.open(this_probe_path).convert('RGB')))
imglist = [probe_img, cv2.flip(probe_img, 1)] # cv2.flip(img1, 1):112*96*3
for i in range(len(imglist)):
imglist[i] = imglist[i].transpose(2, 0, 1).reshape((1, 3, 112, 96))
imglist[i] = (imglist[i] - 127.5) / 128.
img = np.vstack(imglist) # 2*3*112*96 (垂直将imglist中的2组数据合起来)
with torch.no_grad():
img = Variable(torch.from_numpy(img).float(), volatile=True).cuda()
output = net(img) # 2*512
f = output.data
f1 = f[0] # img1 output
this_probe_scores=[]
for j in range(len(gallerys)):
f2=gallerys_feature[j]
cosdistance = f1.dot(f2) / (f1.norm() * f2.norm() + 1e-5)
this_probe_scores.append(cosdistance)
this_probe_scores=np.array(this_probe_scores)
max_index=this_probe_scores.argmax()
most_pos_gallery=gallerys[max_index]
predicts.append(most_pos_gallery)
predicts=np.array(predicts).reshape(len(probes),1)
predicts = pd.DataFrame(predicts)
predicts.to_csv('result.csv', header=0, index=0)