-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta_test_few_shot_models.py
204 lines (154 loc) · 7.99 KB
/
meta_test_few_shot_models.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
import numpy as np
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.optim
import torch.nn.functional as F
import torch.optim.lr_scheduler as lr_scheduler
import time
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '1'
import glob
from itertools import combinations
import configs
import backbone
from data.datamgr import SimpleDataManager, SetDataManager
from methods.protonet import ProtoNet
from methods.dtn_protonet import DTN_ProtoNet
from io_utils import model_dict, parse_args, get_resume_file, get_best_file, get_assigned_file
from utils import *
from datasets import miniImageNet_few_shot_DTN, EuroSAT_few_shot, ISIC_few_shot
from pseudo_query_generator import PseudoQeuryGenerator
def meta_test(novel_loader, novel_gen_loader, n_query = 15, pretrained_dataset='miniImageNet', freeze_backbone=False, n_pseudo=100, n_way = 5, n_support = 5):
correct = 0
count = 0
iter_num = len(novel_loader)
acc_all = []
for ti, ZIP in enumerate(zip(novel_loader, novel_gen_loader)):
train_data, gen_data = ZIP
x, y = train_data
gen_x, gen_label = gen_data
generated_support_1 = gen_x[:,0,:,:,:]
generated_support_2 = gen_x[:,1,:,:,:]
###############################################################################################
# load pretrained model on miniImageNet
if params.method == 'protonet':
pretrained_model = ProtoNet(model_dict[params.model], n_way = n_way, n_support = n_support)
elif params.method == 'dtn':
print('use dtn')
pretrained_model = DTN_ProtoNet(model_dict[params.model], n_way = n_way, n_support = n_support)
checkpoint_dir = '%s/checkpoints/%s/%s_%s' %(configs.save_dir, pretrained_dataset, params.model, params.method)
if params.train_aug:
checkpoint_dir += '_aug'
checkpoint_dir += '_5way_5shot'
params.save_iter = -1
#params.save_iter = 200
if params.save_iter != -1:
modelfile = get_assigned_file(checkpoint_dir, params.save_iter)
else:
modelfile = get_best_file(checkpoint_dir)
tmp = torch.load(modelfile)
state = tmp['state']
pretrained_model.load_state_dict(state)
pretrained_model.cuda()
###############################################################################################
# split data into support set and query set
n_query = x.size(1) - n_support
x = x.cuda()
x_var = Variable(x)
support_size = n_way * n_support
y_a_i = Variable( torch.from_numpy( np.repeat(range( n_way ), n_support ) )).cuda() # (25,)
x_b_i = x_var[:, n_support:,:,:,:].contiguous().view( n_way* n_query, *x.size()[2:]) # query set
x_a_i = x_var[:,:n_support,:,:,:].contiguous().view( n_way* n_support, *x.size()[2:]) # support set
if freeze_backbone == False:
###############################################################################################
# Finetune components initialization
pseudo_q_genrator = PseudoQeuryGenerator(n_way, n_support, n_pseudo)
delta_opt = torch.optim.Adam(filter(lambda p: p.requires_grad, pretrained_model.parameters()))
###############################################################################################
# finetune process
finetune_epoch = 100
fine_tune_n_query = n_pseudo // n_way
pretrained_model.n_query = fine_tune_n_query
pretrained_model.train()
z_support = x_a_i.view(n_way, n_support, *x_a_i.size()[1:])
for epoch in range(finetune_epoch):
delta_opt.zero_grad()
# generate pseudo query images
psedo_query_set, _ = pseudo_q_genrator.generate(x_a_i)
psedo_query_set = psedo_query_set.cuda().view(n_way, fine_tune_n_query, *x_a_i.size()[1:])
x = torch.cat((z_support, psedo_query_set), dim=1)
loss = pretrained_model.set_forward_loss(x, generated_support_1, generated_support_2)
loss.backward()
delta_opt.step()
###############################################################################################
# inference
pretrained_model.eval()
pretrained_model.n_query = n_query
with torch.no_grad():
scores = pretrained_model.set_forward(x_var.cuda(), generated_support_1, generated_support_2)
y_query = np.repeat(range( n_way ), n_query )
topk_scores, topk_labels = scores.data.topk(1, 1, True, True)
topk_ind = topk_labels.cpu().numpy()
top1_correct = np.sum(topk_ind[:,0] == y_query)
correct_this, count_this = float(top1_correct), len(y_query)
acc_all.append((correct_this/ count_this *100))
print("Task %d : %4.2f%% Now avg: %4.2f%%" %(ti, correct_this/ count_this *100, np.mean(acc_all) ))
###############################################################################################
acc_all = np.asarray(acc_all)
acc_mean = np.mean(acc_all)
acc_std = np.std(acc_all)
print('%d Test Acc = %4.2f%% +- %4.2f%%' %(iter_num, acc_mean, 1.96* acc_std/np.sqrt(iter_num)))
if __name__=='__main__':
np.random.seed(10)
torch.manual_seed(20)
params = parse_args('train')
task = params.task
##################################################################
image_size = 224
iter_num = 600
n_query = max(1, int(16* params.test_n_way/params.train_n_way))
few_shot_params = dict(n_way = params.test_n_way , n_support = params.n_shot)
# number of pseudo images
n_pseudo = 100
##################################################################
# loading dataset
pretrained_dataset = "miniImageNet"
dataset_names = ["EuroSAT", "ISIC"]
novel_loaders = []
novel_gen_loaders = []
if task == 'fsl':
freeze_backbone = True
dataset_names = ["miniImageNet"]
print ("Loading mini-ImageNet")
datamgr = miniImageNet_few_shot_DTN.SetDataManager_DTN(image_size, n_eposide = iter_num, n_query = 15, mode="test", **few_shot_params)
novel_loader = datamgr.get_data_loader(aug =False)
novel_loaders.append(novel_loader)
novel_gen_loader = datamgr.get_generation_loader(aug = False)
novel_gen_loaders.append(novel_gen_loader)
else:
freeze_backbone = params.freeze_backbone
dataset_names = ["EuroSAT", "ISIC"]
print ("Loading EuroSAT")
datamgr = EuroSAT_few_shot.SetDataManager_DTN(image_size, n_eposide = iter_num, n_query = 15, **few_shot_params)
novel_loader = datamgr.get_data_loader(aug =False)
novel_loaders.append(novel_loader)
novel_gen_loader = datamgr.get_generation_loader(aug = False)
novel_gen_loaders.append(novel_gen_loader)
print ("Loading ISIC")
datamgr = ISIC_few_shot.SetDataManager_DTN(image_size, n_eposide = iter_num, n_query = 15, **few_shot_params)
novel_loader = datamgr.get_data_loader(aug =False)
novel_loaders.append(novel_loader)
novel_gen_loader = datamgr.get_generation_loader(aug = False)
novel_gen_loaders.append(novel_gen_loader)
print('fine-tune: ', not freeze_backbone)
if not freeze_backbone:
print("n_pseudo: ", n_pseudo)
#########################################################################
# meta-test loop
for idx, All_loader in enumerate(zip(novel_loaders, novel_gen_loaders)):
novel_loader, novel_gen_loader = All_loader
print (dataset_names[idx])
start_epoch = params.start_epoch
stop_epoch = params.stop_epoch
meta_test(novel_loader, novel_gen_loader, n_query = 15, pretrained_dataset=pretrained_dataset, freeze_backbone=freeze_backbone, n_pseudo=n_pseudo, **few_shot_params)