-
Notifications
You must be signed in to change notification settings - Fork 2
/
extract.py
145 lines (115 loc) · 4.57 KB
/
extract.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os
import json
import argparse
import torch
import numpy as np
from data import AttributeDataset
from models import LinearProjection, MLP, Base
from models import ProxyNet, ProxyLoss
from validate import calibrate
# Training settings
parser = argparse.ArgumentParser(description='PyTorch DML')
parser.add_argument('--model-path', type=str, default='exp', metavar='ED',
help='model path')
args = parser.parse_args()
path = os.path.dirname(args.model_path)
with open(os.path.join(path, 'config.json')) as f:
tmp = json.load(f)
tmp['model_path'] = args.model_path
args = type('parser', (object,), tmp)
def main():
test_seen_loader = torch.utils.data.DataLoader(
AttributeDataset(args.data_dir, args.dataset,
features_path=args.gan_path,
mode='test_seen',
generalized=True,
normalize=args.normalize,
sentences=args.sentences),
batch_size=args.batch_size,
shuffle=False)
test_unseen_loader = torch.utils.data.DataLoader(
AttributeDataset(args.data_dir, args.dataset,
features_path=args.gan_path,
mode='test_unseen',
generalized=True,
normalize=args.normalize,
sentences=args.sentences),
batch_size=args.batch_size,
shuffle=False)
# instanciate the models
if args.mlp:
mlp = MLP(args.dim_input, [args.nhidden*2], args.nhidden)
else:
mlp = LinearProjection(args.dim_input, args.nhidden)
embed = LinearProjection(args.nhidden, args.dim_embed)
if args.sentences:
cam_key = 'sentences'
else:
cam_key = 'emb'
if args.gan_path is not None:
cam_key = 'full_' + cam_key
cam = torch.from_numpy(test_seen_loader.dataset.data[cam_key].T)
proxies = ProxyNet(args.n_classes, args.dim_embed, proxies=cam)
model = Base(mlp, embed, proxies)
criterion = ProxyLoss(temperature=args.temp)
if args.cuda:
mlp.cuda()
embed.cuda()
model.cuda()
proxies.cuda()
# loading
checkpoint = torch.load(args.model_path)
model.load_state_dict(checkpoint['state_dict'])
txt = ("=> loaded checkpoint '{}' (epoch {})"
.format(args.model_path, checkpoint['epoch']))
print(txt)
compute_scores(test_seen_loader, test_unseen_loader,
model, criterion)
def get_features(loader, model, criterion, proxies):
feats = []
probs = []
model.eval()
with torch.no_grad():
for i, (img, labels) in enumerate(loader):
if args.cuda:
img = img.cuda(non_blocking=True)
# compute output
output = model(img)
feats.append(output)
probs.append(criterion.classify(output, proxies).cpu())
return np.vstack(feats), np.vstack(probs)
def compute_scores(seen_loader, unseen_loader, model, criterion):
if args.sentences:
key = 'sentences'
else:
key = 'emb'
proxies = seen_loader.dataset.data['full_' + key].T
proxies = torch.from_numpy(proxies).cuda()
print('\nExtracting testing seen set...')
feats_seen, probs_seen = get_features(
seen_loader, model, criterion, proxies)
print('Extracting testing unseen set...\n')
feats_unseen, probs_unseen = get_features(
unseen_loader, model, criterion, proxies)
seen_idx = np.asarray([*seen_loader.dataset.data['classnames'].keys()])
dirname = os.path.dirname(args.model_path)
fname = os.path.join(dirname, 'feat.npz')
gamma = calibrate(probs_seen, probs_unseen,
seen_loader.dataset.data['Y_orig'],
unseen_loader.dataset.data['Y_orig'],
seen_idx)
np.savez(fname,
feats_seen=feats_seen, probs_seen=probs_seen,
feats_unseen=feats_unseen, probs_unseen=probs_unseen,
emb_seen=seen_loader.dataset.data[key].T,
emb_unseen=unseen_loader.dataset.data[key].T,
y_seen=seen_loader.dataset.data['Y_orig'],
y_unseen=unseen_loader.dataset.data['Y_orig'],
mapping_seen=seen_loader.dataset.data['mapping'],
mapping_unseen=unseen_loader.dataset.data['mapping'],
emb_full=seen_loader.dataset.data['full_' + key].T,
seen_idx=np.unique(seen_loader.dataset.data['Y_orig']),
unseen_idx=np.unique(unseen_loader.dataset.data['Y_orig']),
gamma=gamma)
if __name__ == '__main__':
main()