-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselection_methods.py
374 lines (306 loc) · 14.9 KB
/
selection_methods.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
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
import torch.optim as optim
# Custom
from config import *
from models.query_models import VAE, Discriminator, GCN
# from data.sampler import SubsetSequentialSampler
from sampler import SubsetSequentialSampler
from kcenterGreedy import kCenterGreedy
import argparse
# parser = argparse.ArgumentParser()
# parser.add_argument("-bs", "--batch_size", type=int, default='128', help='Batch size')
# args = parser.parse_args()
# BATCH = args.batch_size
def BCEAdjLoss(scores, lbl, nlbl, l_adj):
lnl = torch.log(scores[lbl])
lnu = torch.log(1 - scores[nlbl])
labeled_score = torch.mean(lnl)
unlabeled_score = torch.mean(lnu)
bce_adj_loss = -labeled_score - l_adj * unlabeled_score
return bce_adj_loss
def aff_to_adj(x, y=None):
x = x.detach().cpu().numpy()
adj = np.matmul(x, x.transpose())
adj += -1.0 * np.eye(adj.shape[0])
adj_diag = np.sum(adj, axis=0) # rowise sum
adj = np.matmul(adj, np.diag(1 / adj_diag))
adj = adj + np.eye(adj.shape[0])
adj = torch.Tensor(adj).cuda()
return adj
def read_data(dataloader, labels=True):
if labels:
while True:
for img, label, _ in dataloader:
yield img, label
else:
while True:
for img, _, _ in dataloader:
yield img
def vae_loss(x, recon, mu, logvar, beta):
mse_loss = nn.MSELoss()
MSE = mse_loss(recon, x)
KLD = -0.5 * torch.mean(1 + logvar - mu.pow(2) - logvar.exp())
KLD = KLD * beta
return MSE + KLD
def train_vaal(models, optimizers, labeled_dataloader, unlabeled_dataloader, cycle, args):
vae = models['vae']
discriminator = models['discriminator']
task_model = models['backbone']
ranker = models['module']
task_model.eval()
ranker.eval()
vae.train()
discriminator.train()
with torch.cuda.device(CUDA_VISIBLE_DEVICES):
vae = vae.cuda()
discriminator = discriminator.cuda()
task_model = task_model.cuda()
ranker = ranker.cuda()
adversary_param = 1
beta = 1
num_adv_steps = 1
num_vae_steps = 1
bce_loss = nn.BCELoss()
labeled_data = read_data(labeled_dataloader)
unlabeled_data = read_data(unlabeled_dataloader)
train_iterations = int((ADDENDUM * cycle + SUBSET) * EPOCHV / args.batch_size) # 16667
# train_iterations = 1000
for iter_count in range(train_iterations):
labeled_imgs, labels = next(labeled_data)
unlabeled_imgs = next(unlabeled_data)[0]
with torch.cuda.device(CUDA_VISIBLE_DEVICES):
labeled_imgs = labeled_imgs.cuda()
unlabeled_imgs = unlabeled_imgs.cuda()
labels = labels.cuda()
if iter_count == 0:
r_l_0 = torch.from_numpy(np.random.uniform(0, 1, size=(labeled_imgs.shape[0], 1))).type(
torch.FloatTensor).cuda()
r_u_0 = torch.from_numpy(np.random.uniform(0, 1, size=(unlabeled_imgs.shape[0], 1))).type(
torch.FloatTensor).cuda()
else:
with torch.no_grad():
_, _, features_l = task_model(labeled_imgs)
_, _, feature_u = task_model(unlabeled_imgs)
r_l = ranker(features_l)
r_u = ranker(feature_u)
if iter_count == 0:
r_l = r_l_0.detach()
r_u = r_u_0.detach()
r_l_s = r_l_0.detach()
r_u_s = r_u_0.detach()
else:
r_l_s = torch.sigmoid(r_l).detach()
r_u_s = torch.sigmoid(r_u).detach()
# VAE step
for count in range(num_vae_steps): # num_vae_steps
recon, _, mu, logvar = vae(r_l_s, labeled_imgs)
unsup_loss = vae_loss(labeled_imgs, recon, mu, logvar, beta)
unlab_recon, _, unlab_mu, unlab_logvar = vae(r_u_s, unlabeled_imgs)
transductive_loss = vae_loss(unlabeled_imgs,
unlab_recon, unlab_mu, unlab_logvar, beta)
labeled_preds = discriminator(r_l, mu)
unlabeled_preds = discriminator(r_u, unlab_mu)
lab_real_preds = torch.ones(labeled_imgs.size(0))
unlab_real_preds = torch.ones(unlabeled_imgs.size(0))
with torch.cuda.device(CUDA_VISIBLE_DEVICES):
lab_real_preds = lab_real_preds.cuda()
unlab_real_preds = unlab_real_preds.cuda()
dsc_loss = bce_loss(labeled_preds[:, 0], lab_real_preds) + \
bce_loss(unlabeled_preds[:, 0], unlab_real_preds)
total_vae_loss = unsup_loss + transductive_loss + adversary_param * dsc_loss
optimizers['vae'].zero_grad()
total_vae_loss.backward()
optimizers['vae'].step()
# sample new batch if needed to train the adversarial network
if count < (num_vae_steps - 1):
labeled_imgs, _ = next(labeled_data)
unlabeled_imgs = next(unlabeled_data)[0]
with torch.cuda.device(CUDA_VISIBLE_DEVICES):
labeled_imgs = labeled_imgs.cuda()
unlabeled_imgs = unlabeled_imgs.cuda()
labels = labels.cuda()
# Discriminator step
for count in range(num_adv_steps):
with torch.no_grad():
_, _, mu, _ = vae(r_l_s, labeled_imgs)
_, _, unlab_mu, _ = vae(r_u_s, unlabeled_imgs)
labeled_preds = discriminator(r_l, mu)
unlabeled_preds = discriminator(r_u, unlab_mu)
lab_real_preds = torch.ones(labeled_imgs.size(0))
unlab_fake_preds = torch.zeros(unlabeled_imgs.size(0))
with torch.cuda.device(CUDA_VISIBLE_DEVICES):
lab_real_preds = lab_real_preds.cuda()
unlab_fake_preds = unlab_fake_preds.cuda()
dsc_loss = bce_loss(labeled_preds[:, 0], lab_real_preds) + \
bce_loss(unlabeled_preds[:, 0], unlab_fake_preds)
optimizers['discriminator'].zero_grad()
dsc_loss.backward()
optimizers['discriminator'].step()
# sample new batch if needed to train the adversarial network
if count < (num_adv_steps - 1):
labeled_imgs, _ = next(labeled_data)
unlabeled_imgs = next(unlabeled_data)[0]
with torch.cuda.device(CUDA_VISIBLE_DEVICES):
labeled_imgs = labeled_imgs.cuda()
unlabeled_imgs = unlabeled_imgs.cuda()
labels = labels.cuda()
if iter_count % 1000 == 0:
print(
"Iteration: " + str(iter_count) + " vae_loss: " + str(total_vae_loss.item()) + " dsc_loss: " + str(
dsc_loss.item()))
def get_uncertainty(models, unlabeled_loader):
models['backbone'].eval()
models['module'].eval()
with torch.cuda.device(CUDA_VISIBLE_DEVICES):
uncertainty = torch.tensor([]).cuda()
with torch.no_grad():
for inputs, _, _ in unlabeled_loader:
with torch.cuda.device(CUDA_VISIBLE_DEVICES):
inputs = inputs.cuda()
_, _, features = models['backbone'](inputs)
pred_loss = models['module'](features) # pred_loss = criterion(scores, labels) # ground truth loss
pred_loss = pred_loss.view(pred_loss.size(0))
uncertainty = torch.cat((uncertainty, pred_loss), 0)
return uncertainty.cpu()
def get_features(models, unlabeled_loader):
models['backbone'].eval()
with torch.cuda.device(CUDA_VISIBLE_DEVICES):
features = torch.tensor([]).cuda()
with torch.no_grad():
for inputs, _, _ in unlabeled_loader:
with torch.cuda.device(CUDA_VISIBLE_DEVICES):
inputs = inputs.cuda()
_, features_batch, _ = models['backbone'](inputs)
features = torch.cat((features, features_batch), 0)
feat = features # .detach().cpu().numpy()
return feat
def get_kcg(models, labeled_data_size, unlabeled_loader):
models['backbone'].eval()
with torch.cuda.device(CUDA_VISIBLE_DEVICES):
features = torch.tensor([]).cuda()
with torch.no_grad():
for inputs, _, _ in unlabeled_loader:
with torch.cuda.device(CUDA_VISIBLE_DEVICES):
inputs = inputs.cuda()
_, features_batch, _ = models['backbone'](inputs)
features = torch.cat((features, features_batch), 0)
feat = features.detach().cpu().numpy()
new_av_idx = np.arange(SUBSET, (SUBSET + labeled_data_size))
sampling = kCenterGreedy(feat)
batch = sampling.select_batch_(new_av_idx, ADDENDUM)
other_idx = [x for x in range(SUBSET) if x not in batch]
return other_idx + batch
# Select the indices of the unlabeled data according to the methods
def query_samples(model, method, data_unlabeled, subset, labeled_set, cycle, args):
if method == 'Random':
arg = np.random.randint(SUBSET, size=SUBSET)
if (method == 'UncertainGCN') or (method == 'CoreGCN'):
# Create unlabeled dataloader for the unlabeled subset
unlabeled_loader = DataLoader(data_unlabeled, batch_size=args.batch_size,
sampler=SubsetSequentialSampler(subset + labeled_set),
# more convenient if we maintain the order of subset
pin_memory=True)
binary_labels = torch.cat((torch.zeros([SUBSET, 1]), (torch.ones([len(labeled_set), 1]))), 0)
features = get_features(model, unlabeled_loader)
features = nn.functional.normalize(features)
adj = aff_to_adj(features)
gcn_module = GCN(nfeat=features.shape[1],
nhid=args.hidden_units,
nclass=1,
dropout=args.dropout_rate).cuda()
models = {'gcn_module': gcn_module}
optim_backbone = optim.Adam(models['gcn_module'].parameters(), lr=LR_GCN, weight_decay=WDECAY)
optimizers = {'gcn_module': optim_backbone}
lbl = np.arange(SUBSET, SUBSET + (cycle + 1) * ADDENDUM, 1)
nlbl = np.arange(0, SUBSET, 1)
############
for _ in range(200):
optimizers['gcn_module'].zero_grad()
outputs, _, _ = models['gcn_module'](features, adj)
lamda = args.lambda_loss
loss = BCEAdjLoss(outputs, lbl, nlbl, lamda)
loss.backward()
optimizers['gcn_module'].step()
models['gcn_module'].eval()
with torch.no_grad():
with torch.cuda.device(CUDA_VISIBLE_DEVICES):
inputs = features.cuda()
labels = binary_labels.cuda()
scores, _, feat = models['gcn_module'](inputs, adj)
if method == "CoreGCN":
feat = features.detach().cpu().numpy()
new_av_idx = np.arange(SUBSET, (SUBSET + (cycle + 1) * ADDENDUM))
sampling2 = kCenterGreedy(feat)
batch2 = sampling2.select_batch_(new_av_idx, ADDENDUM)
other_idx = [x for x in range(SUBSET) if x not in batch2]
arg = other_idx + batch2
else:
s_margin = args.s_margin
scores_median = np.squeeze(torch.abs(scores[:SUBSET] - s_margin).detach().cpu().numpy())
arg = np.argsort(-(scores_median))
print("Max confidence value: ", torch.max(scores.data))
print("Mean confidence value: ", torch.mean(scores.data))
preds = torch.round(scores)
correct_labeled = (preds[SUBSET:, 0] == labels[SUBSET:, 0]).sum().item() / ((cycle + 1) * ADDENDUM)
correct_unlabeled = (preds[:SUBSET, 0] == labels[:SUBSET, 0]).sum().item() / SUBSET
correct = (preds[:, 0] == labels[:, 0]).sum().item() / (SUBSET + (cycle + 1) * ADDENDUM)
print("Labeled classified: ", correct_labeled)
print("Unlabeled classified: ", correct_unlabeled)
print("Total classified: ", correct)
if method == 'CoreSet':
# Create unlabeled dataloader for the unlabeled subset
unlabeled_loader = DataLoader(data_unlabeled, batch_size=args.batch_size,
sampler=SubsetSequentialSampler(subset + labeled_set),
# more convenient if we maintain the order of subset
pin_memory=True)
arg = get_kcg(models, ADDENDUM * (cycle + 1), unlabeled_loader)
if method == 'lloss':
# Create unlabeled dataloader for the unlabeled subset
unlabeled_loader = DataLoader(data_unlabeled, batch_size=args.batch_size,
sampler=SubsetSequentialSampler(subset),
pin_memory=True)
# Measure uncertainty of each data points in the subset
uncertainty = get_uncertainty(model, unlabeled_loader)
arg = np.argsort(uncertainty)
if method == 'TA-VAAL':
# Create unlabeled dataloader for the unlabeled subset
unlabeled_loader = DataLoader(data_unlabeled, batch_size=args.batch_size,
sampler=SubsetSequentialSampler(subset),
pin_memory=True)
labeled_loader = DataLoader(data_unlabeled, batch_size=args.batch_size,
sampler=SubsetSequentialSampler(labeled_set),
pin_memory=True)
if args.dataset == 'fashionmnist':
vae = VAE(28, 1, 3)
discriminator = Discriminator(28)
else:
vae = VAE()
discriminator = Discriminator(32)
models = {'backbone': model['backbone'], 'module': model['module'], 'vae': vae, 'discriminator': discriminator}
optim_vae = optim.Adam(vae.parameters(), lr=5e-4)
optim_discriminator = optim.Adam(discriminator.parameters(), lr=5e-4)
optimizers = {'vae': optim_vae, 'discriminator': optim_discriminator}
train_vaal(models, optimizers, labeled_loader, unlabeled_loader, cycle + 1, args)
task_model = models['backbone']
ranker = models['module'] # 这里根本就没有用到ranking module, 用的是lossnet!!!
all_preds, all_indices = [], []
for images, _, indices in unlabeled_loader:
images = images.cuda()
with torch.no_grad():
_, _, features = task_model(images)
r = ranker(features) # 所以这里的r就是lossnet的输出,应该用
_, _, mu, _ = vae(torch.sigmoid(r), images)
preds = discriminator(r, mu)
preds = preds.cpu().data
all_preds.extend(preds)
all_indices.extend(indices)
all_preds = torch.stack(all_preds)
all_preds = all_preds.view(-1)
# need to multiply by -1 to be able to use torch.topk
all_preds *= -1
# select the points which the discriminator things are the most likely to be unlabeled
_, arg = torch.sort(all_preds)
return arg