-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_fg_scgm_g.py
174 lines (137 loc) · 6.53 KB
/
test_fg_scgm_g.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import numpy as np
import torch
import torch.nn.functional as F
import os
import argparse
from sklearn import metrics
from time import time
from scgm_g.scgm_resnet import resnet50
# from utils.model_toolkit import identity_layer
from utils.utils import get_test_dataloader_breeds
from eval.eval_performance import classify, mean_confidence_interval
# import resource
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
# rlimit = resource.getrlimit(resource.RLIMIT_NOFILE)
# resource.setrlimit(resource.RLIMIT_NOFILE, (4096, rlimit[1]))
def parse_args():
parser = argparse.ArgumentParser(description='arguments for training')
parser.add_argument('data', metavar='DIR', help='path to dataset')
parser.add_argument('-b', '--batch-size', default=256, type=int, metavar='N', help='mini-batch size (default: 256)')
parser.add_argument('--n-test-runs', default=1000, type=int, help='the number of test runs')
parser.add_argument('--n-shots', default=1, type=int)
parser.add_argument('--n-queries', default=15, type=int)
parser.add_argument('--n-aug-support-samples', default=5, type=int, help='the number of support samples after augmentation')
parser.add_argument('--feat-norm', action='store_true', help='normalization on features')
parser.add_argument('--classifier', default='LR', choices=['LR', 'SGDLR', 'KNN'])
parser.add_argument('--hiddim', default=128, type=int, help='embedding dimension')
parser.add_argument('--mlp', action='store_false', help='use mlp head')
parser.add_argument('--kd-t', default=4.0, type=float, help='temperature of self-distillation')
parser.add_argument('--n-subclass', default=100, type=int, help='the number of subclasses')
parser.add_argument('--n-class', default=17, type=int, help='the number of superclasses, e.g., 17 for living17, 26 for nonliving26, 13 for entity13, 30 for entity30')
parser.add_argument('--dataset', default='living17', choices=['living17', 'nonliving26', 'entity13', 'entity30'])
args = parser.parse_args()
return args
def main():
args = parse_args()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
set_cuda = True
info_dir = os.path.join(args.data, 'BREEDS/')
data_dir = os.path.join(args.data, 'Data', 'CLS-LOC/')
breeds_test_loader = get_test_dataloader_breeds(
ds_name=args.dataset,
info_dir=info_dir,
data_dir=data_dir,
n_test_runs=args.n_test_runs,
n_ways=10000,
n_shots=args.n_shots,
n_queries=args.n_queries,
n_aug_support_samples=args.n_aug_support_samples,
fg=True,
batch_size=1,
num_workers=0)
# load model
# ---
net = resnet50(num_classes=args.n_class, num_subclasses=args.n_subclass, kd_t=args.kd_t, hiddim=args.hiddim, with_mlp=args.mlp)
weights_path = 'pretrain_model/scgm_g_' + args.dataset + '.pth'
net.load_state_dict(torch.load(weights_path, map_location='cpu'), strict=False)
# net.fc_enc = identity_layer()
if set_cuda is True:
net.to(device)
# net = torch.nn.DataParallel(net)
net.eval()
# evaluation
# ---
with torch.no_grad():
acc = []
t0 = time()
for (run_idx, batch_data) in enumerate(breeds_test_loader):
support_xs, support_ys, query_xs, query_ys = batch_data
support_xs = support_xs[0]
support_ys = support_ys[0]
query_xs = query_xs[0]
query_ys = query_ys[0]
# load support set embeddings
# ---
support_feats = []
if len(support_ys) > args.batch_size:
loop_range = range(0, (len(support_ys) - args.batch_size), args.batch_size)
else:
loop_range = [0]
for i in loop_range:
if (len(support_ys) - i) < 2 * args.batch_size:
batchsz_iter = len(support_ys) - i
else:
batchsz_iter = args.batch_size
batch_support_xs = support_xs[i:(i + batchsz_iter)]
if set_cuda is True:
batch_support_xs = batch_support_xs.to(device)
batch_support_xs = net(batch_support_xs)
# batch_support_xs = net.module.embed(batch_support_xs)
batch_support_xs = net.embed(batch_support_xs)
if args.feat_norm is True:
batch_support_xs = F.normalize(batch_support_xs, p=2, dim=1)
support_feats.append(batch_support_xs.detach().cpu().numpy())
support_feats = np.concatenate(support_feats, axis=0)
# load query set embeddings
# ---
query_feats = []
if len(query_ys) > args.batch_size:
loop_range = range(0, (len(query_ys) - args.batch_size), args.batch_size)
else:
loop_range = [0]
for i in loop_range:
if (len(query_ys) - i) < 2 * args.batch_size:
batchsz_iter = len(query_ys) - i
else:
batchsz_iter = args.batch_size
batch_query_xs = query_xs[i:(i + batchsz_iter)]
if set_cuda is True:
batch_query_xs = batch_query_xs.to(device)
batch_query_xs = net(batch_query_xs)
# batch_query_xs = net.module.embed(batch_query_xs)
batch_query_xs = net.embed(batch_query_xs)
if args.feat_norm is True:
batch_query_xs = F.normalize(batch_query_xs, p=2, dim=1)
query_feats.append(batch_query_xs.detach().cpu().numpy())
query_feats = np.concatenate(query_feats, axis=0)
# classification
# ---
clf = classify(args.classifier, support_feats, support_ys)
support_preds = clf.predict(support_feats)
query_preds = clf.predict(query_feats)
# evaluation
# ---
acc_tr = metrics.accuracy_score(support_ys, support_preds)
acc_te = metrics.accuracy_score(query_ys, query_preds)
acc.append(acc_te)
print('[{:d}'.format(run_idx),
'/ {:d}]'.format(args.n_test_runs),
'training: acc = {:.5f}'.format(acc_tr * 100),
'| testing: acc = {:.5f}'.format(acc_te * 100))
del clf
acc_mn, acc_std = mean_confidence_interval(acc)
print('accuracy={:.5f}'.format(acc_mn * 100),
'std={:.5f}'.format(acc_std * 100),
'time={:.5f}'.format(time() - t0))
if __name__ == '__main__':
main()