-
Notifications
You must be signed in to change notification settings - Fork 1
/
sampler.py
71 lines (47 loc) · 1.97 KB
/
sampler.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
import torch
import numpy as np
class AdversarySampler:
def __init__(self, budget):
self.budget = budget
def sample(self, vae, discriminator, data, unlabeled_indices, device):
vae.eval()
discriminator.eval()
all_preds = []
for images, _,_ in data:
images = images.to(device)
with torch.no_grad():
_, _, mu, _ = vae(images)
#_, _, mu = vae(images)
preds = discriminator(mu)
preds = preds.cpu().data
all_preds.extend(preds)
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 thinks are the most likely to be unlabeled
_, querry_indices = torch.topk(all_preds, int(self.budget))
querry_pool_indices = list(np.asarray(unlabeled_indices)[querry_indices])
return querry_pool_indices
class AdversarySampler_multimodal:
def __init__(self, budget):
self.budget = budget
def sample(self, vae, discriminator, data, unlabeled_indices, device):
vae.eval()
discriminator.eval()
all_preds = []
for images, _,_ in data:
images = images.to(device)
with torch.no_grad():
_, _, _, mu, _ = vae(images)
preds = discriminator(mu)
preds = preds.cpu().data
all_preds.extend(preds)
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 thinks are the most likely to be unlabeled
_, querry_indices = torch.topk(all_preds, int(self.budget))
querry_pool_indices = list(np.asarray(unlabeled_indices)[querry_indices])
return querry_pool_indices