-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcompute_FVA.py
125 lines (96 loc) · 3.74 KB
/
compute_FVA.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
import os
import torch
import argparse
import itertools
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import os.path as osp
from PIL import Image
from tqdm import tqdm
from torch.utils import data
from torchvision import transforms
from eval_utils.resnet50_facevgg2_FVA import resnet50, load_state_dict
# create dataset to read the counterfactual results images
class CFDataset():
mean_bgr = np.array([91.4953, 103.8827, 131.0912])
def __init__(self, path, exp_name):
self.images = []
self.path = path
self.exp_name = exp_name
for CL, CF in itertools.product(['CC', 'IC'], ['CCF']):
self.images += [(CL, CF, I) for I in os.listdir(osp.join(path, 'Results', self.exp_name, CL, CF, 'CF'))]
def __len__(self):
return len(self.images)
def switch(self, partition):
if partition == 'C':
LCF = ['CCF']
elif partition == 'I':
LCF = ['ICF']
else:
LCF = ['CCF', 'ICF']
self.images = []
for CL, CF in itertools.product(['CC', 'IC'], LCF):
self.images += [(CL, CF, I) for I in os.listdir(osp.join(self.path, 'Results', self.exp_name, CL, CF, 'CF'))]
def __getitem__(self, idx):
CL, CF, I = self.images[idx]
# get paths
cl_path = osp.join(self.path, 'Original', 'Correct' if CL == 'CC' else 'Incorrect', I)
cf_path = osp.join(self.path, 'Results', self.exp_name, CL, CF, 'CF', I)
cl = self.load_img(cl_path)
cf = self.load_img(cf_path)
return cl, cf
def load_img(self, path):
img = Image.open(os.path.join(path))
img = transforms.Resize(224)(img)
img = np.array(img, dtype=np.uint8)
return self.transform(img)
def transform(self, img):
img = img[:, :, ::-1] # RGB -> BGR
img = img.astype(np.float32)
img -= self.mean_bgr
img = img.transpose(2, 0, 1) # C x H x W
img = torch.from_numpy(img).float()
return img
@torch.no_grad()
def compute_FVA(oracle,
path,
exp_name):
dataset = CFDataset(path, exp_name)
cosine_similarity = torch.nn.CosineSimilarity()
FVAS = []
dists = []
loader = data.DataLoader(dataset, batch_size=15,
shuffle=False,
num_workers=4, pin_memory=True)
for cl, cf in tqdm(loader):
cl = cl.to(device, dtype=torch.float)
cf = cf.to(device, dtype=torch.float)
cl_feat = oracle(cl)
cf_feat = oracle(cf)
dist = cosine_similarity(cl_feat, cf_feat)
FVAS.append((dist > 0.5).cpu().numpy())
dists.append(dist.cpu().numpy())
return np.concatenate(FVAS), np.concatenate(dists)
def arguments():
parser = argparse.ArgumentParser(description='FVA arguments.')
parser.add_argument('--gpu', default='0', type=str,
help='GPU id')
parser.add_argument('--exp-name', required=True, type=str,
help='Experiment Name')
parser.add_argument('--output-path', required=True, type=str,
help='Results Path')
parser.add_argument('--weights-path', default='models/resnet50_vggface2_model.pkl', type=str,
help='ResNet50 VGGFace2 model weights')
return parser.parse_args()
if __name__ == '__main__':
args = arguments()
device = torch.device('cuda:' + args.gpu)
oracle = resnet50(num_classes=8631, include_top=False).to(device)
load_state_dict(oracle, args.weights_path)
oracle.eval()
results = compute_FVA(oracle,
args.output_path,
args.exp_path)
print('FVA', np.mean(results[0]))
print('mean dist', np.mean(results[1]))