-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.py
executable file
·212 lines (163 loc) · 7.03 KB
/
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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import glob
import json
import os
import pdb
import pprint
import random
import time
import h5py
import numpy as np
import torch
import torch.nn as nn
import torch.optim
import torch.optim.lr_scheduler as lr_scheduler
import torch.utils.data.sampler
import tqdm
from torch.autograd import Variable
from torchsummary import summary
import backbone
import configs
import data.feature_loader as feat_loader
import wandb
from data.datamgr import SetDataManager
from io_utils import (get_assigned_file, get_best_file,
model_dict, parse_args)
from methods.CTX import CTX
from methods.transformer import FewShotTransformer
global device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
def direct_test(test_loader, model, params):
correct = 0
count = 0
acc = []
iter_num = len(test_loader)
with tqdm.tqdm(total=len(test_loader)) as pbar:
for i, (x, _) in enumerate(test_loader):
scores = model.set_forward(x)
pred = scores.data.cpu().numpy().argmax(axis=1)
y = np.repeat(range(params.n_way), pred.shape[0]//params.n_way)
acc.append(np.mean(pred == y)*100)
pbar.set_description(
'Test | Acc {:.6f}'.format(np.mean(acc)))
pbar.update(1)
acc_all = np.asarray(acc)
acc_mean = np.mean(acc_all)
acc_std = np.std(acc_all)
return acc_mean, acc_std
def seed_func():
seed = 4040
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
np.random.seed(10)
random.seed(seed)
torch.manual_seed(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def change_model(model_name):
if model_name == 'Conv4':
model_name = 'Conv4NP'
elif model_name == 'Conv6':
model_name = 'Conv6NP'
elif model_name == 'Conv4S':
model_name = 'Conv4SNP'
elif model_name == 'Conv6S':
model_name = 'Conv6SNP'
return model_name
if __name__ == '__main__':
params = parse_args()
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(vars(params))
print()
if params.dataset == 'Omniglot': params.n_query = min(params.n_query, 15) #Omniglot only support maximum 15 samples/category as query
if params.dataset == "CIFAR":
image_size = 112 if 'ResNet' in params.backbone else 64
else:
image_size = 224 if 'ResNet' in params.backbone else 84
if params.dataset in ['Omniglot', 'cross_char']:
if params.backbone == 'Conv4': params.backbone = 'Conv4S'
if params.backbone == 'Conv6': params.backbone = 'Conv6S'
iter_num = params.test_iter
split = params.split
if params.dataset == 'cross':
if split == 'base':
testfile = configs.data_dir['miniImagenet'] + 'all.json'
else:
testfile = configs.data_dir['CUB'] + split + '.json'
elif params.dataset == 'cross_char':
if split == 'base':
testfile = configs.data_dir['Omniglot'] + 'noLatin.json'
else:
testfile = configs.data_dir['emnist'] + split + '.json'
else:
testfile = configs.data_dir[params.dataset] + split + '.json'
if params.method in ['FSCT_softmax', 'FSCT_cosine', 'CTX_softmax', 'CTX_cosine']:
seed_func()
few_shot_params = dict(
n_way=params.n_way, k_shot=params.k_shot, n_query=params.n_query)
if params.method in ['FSCT_softmax', 'FSCT_cosine']:
variant = 'cosine' if params.method == 'FSCT_cosine' else 'softmax'
def feature_model():
if params.dataset in ['Omniglot', 'cross_char']:
params.backbone = change_model(params.backbone)
return model_dict[params.backbone](params.FETI, params.dataset, flatten=True) if 'ResNet' in params.backbone else model_dict[params.backbone](params.dataset, flatten=True)
model = FewShotTransformer(feature_model, variant=variant, **few_shot_params)
elif params.method in ['CTX_softmax', 'CTX_cosine']:
variant = 'cosine' if params.method == 'CTX_cosine' else 'softmax'
input_dim = 512 if "ResNet" in params.backbone else 64
def feature_model():
if params.dataset in ['Omniglot', 'cross_char']:
params.backbone = change_model(params.backbone)
return model_dict[params.backbone](params.FETI, params.dataset, flatten=False) if 'ResNet' in params.backbone else model_dict[params.backbone](params.dataset, flatten=False)
model = CTX(feature_model, variant=variant, input_dim=input_dim, **few_shot_params)
else:
raise ValueError('Unknown method')
model = model.to(device)
params.checkpoint_dir = '%scheckpoints/%s/%s_%s' % (
configs.save_dir, params.dataset, params.backbone, params.method)
if params.train_aug:
params.checkpoint_dir += '_aug'
if params.FETI and 'ResNet' in params.backbone:
params.checkpoint_dir += '_FETI'
params.checkpoint_dir += '_%dway_%dshot' % (
params.n_way, params.k_shot)
if not os.path.isdir(params.checkpoint_dir):
raise ValueError('Can\'t find save model dir')
print("===================================")
print("Test phase: ")
if params.save_iter != -1:
modelfile = get_assigned_file(params.checkpoint_dir, params.save_iter)
else:
modelfile = get_best_file(params.checkpoint_dir)
test_datamgr = SetDataManager(
image_size, n_episode=iter_num, **few_shot_params)
test_loader = test_datamgr.get_data_loader(testfile, aug=False)
acc_all = []
model = model.to(device)
root = os.getcwd()
if params.save_iter != -1:
modelfile = get_assigned_file(params.checkpoint_dir, params.save_iter)
else:
modelfile = get_best_file(params.checkpoint_dir)
if modelfile is not None:
tmp = torch.load(modelfile)
model.load_state_dict(tmp['state'])
split = params.split
if params.save_iter != -1:
split_str = split + "_" + str(params.save_iter)
else:
split_str = split
acc_mean, acc_std = direct_test(test_loader, model, params)
print('%d Test Acc = %4.2f%% +- %4.2f%%' %
(iter_num, acc_mean, 1.96 * acc_std/np.sqrt(iter_num)))
with open('./record/results.txt', 'a') as f:
timestamp = params.datetime
aug_str = '-aug' if params.train_aug else ''
aug_str += '-FETI' if params.FETI and 'ResNet' in params.backbone else ''
if params.backbone == "Conv4SNP":
params.backbone = "Conv4"
elif params.backbone == "Conv6SNP":
params.backbone = "Conv6"
exp_setting = '%s-%s-%s%s-%sw%ss' % (params.dataset, params.backbone,
params.method, aug_str, params.n_way, params.k_shot)
acc_str = 'Test Acc = %4.2f%% +- %4.2f%%' % (acc_mean, 1.96 * acc_std/np.sqrt(iter_num))
f.write('Time: %s Setting: %s %s \n' % (timestamp, exp_setting.ljust(50), acc_str))