-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
397 lines (278 loc) · 14.1 KB
/
solver.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import torch
import torch.nn as nn
import torch.optim as optim
import os
import numpy as np
from sklearn.metrics import accuracy_score
import sampler
from tqdm import tqdm
class Solver:
def __init__(self, args, test_dataloader):
self.args = args
self.test_dataloader = test_dataloader
self.bce_loss = nn.BCELoss()
self.mse_loss = nn.MSELoss()
self.ce_loss = nn.CrossEntropyLoss()
self.sampling_method = args.sampling_method
if self.sampling_method == "random":
self.sampler = sampler.RandomSampler(self.args.budget)
elif self.sampling_method == "adversary":
self.sampler = sampler.AdversarySampler(self.args.budget)
elif self.sampling_method == "uncertainty":
self.sampler = sampler.UncertaintySampler(self.args.budget)
elif self.sampling_method == "expected_error":
self.sampler = sampler.EESampler(self.args.budget)
elif self.sampling_method == "adversary_1c":
self.sample = sampler.AdversarySamplerSingleClass(self.args.budget)
else:
raise Exception("No valid sampling method provideds")
def read_data(self, dataloader, labels=True):
print(len(dataloader))
if labels:
while True:
for img, label, _ in dataloader:
yield img, label
else:
while True:
for img, _, _ in dataloader:
yield img
'''
'''
def train_without_adv_vae(self, querry_dataloader, task_model, vae, discriminator, unlabeled_dataloader, args):
labeled_data = self.read_data(querry_dataloader)
optim_task_model = optim.Adam(task_model.parameters(), lr=5e-3)
task_model.train()
if self.args.cuda:
task_model = task_model.cuda()
change_lr_iter = self.args.train_iterations // 25
for iter_count in tqdm(range(self.args.train_iterations)):
if iter_count is not 0 and iter_count % change_lr_iter == 0:
for param in optim_task_model.param_groups:
param['lr'] = param['lr'] * 0.9
labeled_imgs, labels = next(labeled_data)
if self.args.cuda:
labeled_imgs = labeled_imgs.cuda()
labels = labels.cuda()
# task_model step
preds = task_model(labeled_imgs)
task_loss = self.ce_loss(preds, labels)
optim_task_model.zero_grad()
task_loss.backward()
optim_task_model.step()
if iter_count % 100 == 0:
print('Current task model loss: {:.4f}'.format(task_loss.item()))
final_accuracy = self.test(task_model)
class_based_accs = self.class_based_test(task_model, args.num_classes)
return final_accuracy, vae, discriminator, class_based_accs
def train_ret_class_accs(self, querry_dataloader, task_model, vae, discriminator, unlabeled_dataloader):
labeled_data = self.read_data(querry_dataloader)
unlabeled_data = self.read_data(unlabeled_dataloader, labels=False)
optim_task_model = optim.Adam(task_model.parameters(), lr=5e-3)
task_model.train()
if self.args.cuda:
task_model = task_model.cuda()
change_lr_iter = self.args.train_iterations // 25
for iter_count in tqdm(range(self.args.train_iterations)):
if iter_count is not 0 and iter_count % change_lr_iter == 0:
for param in optim_task_model.param_groups:
param['lr'] = param['lr'] * 0.9
labeled_imgs, labels = next(labeled_data)
unlabeled_imgs = next(unlabeled_data)
if self.args.cuda:
labeled_imgs = labeled_imgs.cuda()
unlabeled_imgs = unlabeled_imgs.cuda()
labels = labels.cuda()
# task_model step
preds = task_model(labeled_imgs)
task_loss = self.ce_loss(preds, labels)
optim_task_model.zero_grad()
task_loss.backward()
optim_task_model.step()
if iter_count % 100 == 0:
print('Current task model loss: {:.4f}'.format(task_loss.item()))
final_accuracy = self.test(task_model)
return final_accuracy, vae, discriminator
'''
TEST
'''
def oracle_train_without_adv_vae(self, querry_dataloader, task_model, vae, discriminator, unlabeled_dataloader):
labeled_data = self.read_data(querry_dataloader)
unlabeled_data = self.read_data(unlabeled_dataloader, labels=False)
optim_task_model = optim.Adam(task_model.parameters(), lr=5e-3)
task_model.train()
if self.args.cuda:
task_model = task_model.cuda()
change_lr_iter = self.args.train_iterations // 25
for iter_count in tqdm(range(self.args.oracle_train_iterations)):
if iter_count is not 0 and iter_count % change_lr_iter == 0:
for param in optim_task_model.param_groups:
param['lr'] = param['lr'] * 0.9
labeled_imgs, labels = next(labeled_data)
unlabeled_imgs = next(unlabeled_data)
if self.args.cuda:
labeled_imgs = labeled_imgs.cuda()
unlabeled_imgs = unlabeled_imgs.cuda()
labels = labels.cuda()
# task_model step
preds = task_model(labeled_imgs)
task_loss = self.ce_loss(preds, labels)
optim_task_model.zero_grad()
task_loss.backward()
optim_task_model.step()
if iter_count % 100 == 0:
print('Current task model loss: {:.4f}'.format(task_loss.item()))
final_accuracy = self.test(task_model)
return final_accuracy, vae, discriminator
def train(self, querry_dataloader, task_model, vae, discriminator, unlabeled_dataloader):
labeled_data = self.read_data(querry_dataloader)
unlabeled_data = self.read_data(unlabeled_dataloader, labels=False)
optim_vae = optim.Adam(vae.parameters(), lr=5e-4)
optim_task_model = optim.Adam(task_model.parameters(), lr=5e-3)
optim_discriminator = optim.Adam(discriminator.parameters(), lr=5e-4)
vae.train()
discriminator.train()
task_model.train()
if self.args.cuda:
vae = vae.cuda()
discriminator = discriminator.cuda()
task_model = task_model.cuda()
change_lr_iter = self.args.train_iterations // 25
for iter_count in tqdm(range(self.args.train_iterations)):
if iter_count is not 0 and iter_count % change_lr_iter == 0:
for param in optim_vae.param_groups:
param['lr'] = param['lr'] * 0.9
for param in optim_task_model.param_groups:
param['lr'] = param['lr'] * 0.9
for param in optim_discriminator.param_groups:
param['lr'] = param['lr'] * 0.9
labeled_imgs, labels = next(labeled_data)
unlabeled_imgs = next(unlabeled_data)
if self.args.cuda:
labeled_imgs = labeled_imgs.cuda()
unlabeled_imgs = unlabeled_imgs.cuda()
labels = labels.cuda()
# task_model step
preds = task_model(labeled_imgs)
task_loss = self.ce_loss(preds, labels)
optim_task_model.zero_grad()
task_loss.backward()
optim_task_model.step()
# VAE step
for count in range(self.args.num_vae_steps):
recon, z, mu, logvar = vae(labeled_imgs)
unsup_loss = self.vae_loss(labeled_imgs, recon, mu, logvar, self.args.beta)
unlab_recon, unlab_z, unlab_mu, unlab_logvar = vae(unlabeled_imgs)
transductive_loss = self.vae_loss(unlabeled_imgs,
unlab_recon, unlab_mu, unlab_logvar, self.args.beta)
labeled_preds = discriminator(mu)
unlabeled_preds = discriminator(unlab_mu)
lab_real_preds = torch.zeros(labeled_imgs.size(0)).long()
unlab_real_preds = torch.zeros(unlabeled_imgs.size(0)).long()
if self.args.cuda:
lab_real_preds = lab_real_preds.cuda()
unlab_real_preds = unlab_real_preds.cuda()
dsc_loss = self.ce_loss(labeled_preds, lab_real_preds) + \
self.ce_loss(unlabeled_preds, unlab_real_preds)
total_vae_loss = unsup_loss + transductive_loss + self.args.adversary_param * dsc_loss
optim_vae.zero_grad()
total_vae_loss.backward()
optim_vae.step()
# sample new batch if needed to train the adversarial network
if count < (self.args.num_vae_steps - 1):
labeled_imgs, _ = next(labeled_data)
unlabeled_imgs = next(unlabeled_data)
if self.args.cuda:
labeled_imgs = labeled_imgs.cuda()
unlabeled_imgs = unlabeled_imgs.cuda()
labels = labels.cuda()
# Discriminator step
for count in range(self.args.num_adv_steps):
with torch.no_grad():
_, _, mu, _ = vae(labeled_imgs)
_, _, unlab_mu, _ = vae(unlabeled_imgs)
labeled_preds = discriminator(mu)
# labeled_preds = labeled_out.max(1)[1]
unlabeled_preds = discriminator(unlab_mu)
# unlabeled_preds = unlabeled_out.max(1)[1]
lab_real_preds = labels
unlab_fake_preds = torch.zeros(unlabeled_imgs.size(0)).long()
if self.args.cuda:
lab_real_preds = lab_real_preds.cuda()
unlab_fake_preds = unlab_fake_preds.cuda()
dsc_loss = self.ce_loss(labeled_preds, lab_real_preds) + \
self.ce_loss(unlabeled_preds, unlab_fake_preds)
optim_discriminator.zero_grad()
dsc_loss.backward()
optim_discriminator.step()
# sample new batch if needed to train the adversarial network
if count < (self.args.num_adv_steps - 1):
labeled_imgs, _ = next(labeled_data)
unlabeled_imgs = next(unlabeled_data)
if self.args.cuda:
labeled_imgs = labeled_imgs.cuda()
unlabeled_imgs = unlabeled_imgs.cuda()
labels = labels.cuda()
if iter_count % 100 == 0:
print('Current training iteration: {}'.format(iter_count))
print('Current task model loss: {:.4f}'.format(task_loss.item()))
print('Current vae model loss: {:.4f}'.format(total_vae_loss.item()))
print('Current discriminator model loss: {:.4f}'.format(dsc_loss.item()))
final_accuracy = self.test(task_model)
return final_accuracy, vae, discriminator
def sample_for_labeling(self, vae, discriminator, unlabeled_dataloader, task_learner):
if self.sampling_method == "random":
query_indices = self.sampler.sample(unlabeled_dataloader)
elif self.sampling_method == "uncertainty":
query_indices = self.sampler.sample(task_learner,
unlabeled_dataloader,
self.args.cuda)
elif self.sampling_method == "expected_error":
query_indices = self.sampler.sample(task_learner,
unlabeled_dataloader,
self.args.cuda)
elif self.sampling_method == "adversary" or self.sampling_method == "adversary_1c":
query_indices = self.sampler.sample(vae,
discriminator,
unlabeled_dataloader,
self.args.cuda)
# we can run some analysis on which indices were used in the query
# a couple of things: class based analysis. also: see how the losses distribute; and other interesting stuff
# print(query_indices)
# for x in query_indices:
# print(unlabeled_dataloader.dataset[x]) #(X, class label, index)
query_indices = np.asarray(query_indices).reshape(1,-1)[0,:]
return query_indices
def class_based_test(self, task_model, num_classes):
task_model.eval()
total, correct = torch.zeros((num_classes)), torch.zeros((num_classes))
for imgs, labels in self.test_dataloader:
if self.args.cuda:
imgs = imgs.cuda()
with torch.no_grad():
preds = task_model(imgs)
preds = torch.argmax(preds, dim=1).cpu().numpy()
# compute the accuracy per class. Assumes the labels go from 0 to k. This must be passed in
# good ol' fashioned iterating through the tensor will do
for y_pred, y in zip(preds, labels):
# y = y.item()
total[y.item()] += 1
if y_pred == y.item():
correct[y] += 1
return correct / total * 100
def test(self, task_model):
task_model.eval()
total, correct = 0, 0
for imgs, labels in self.test_dataloader:
if self.args.cuda:
imgs = imgs.cuda()
with torch.no_grad():
preds = task_model(imgs)
preds = torch.argmax(preds, dim=1).cpu().numpy()
correct += accuracy_score(labels, preds, normalize=False)
total += imgs.size(0)
return correct / total * 100
def vae_loss(self, x, recon, mu, logvar, beta):
MSE = self.mse_loss(recon, x)
KLD = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
KLD = KLD * beta
return MSE + KLD