-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_samples.py
381 lines (285 loc) · 15.4 KB
/
get_samples.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
import matplotlib.pyplot as plt
import matplotlib
import torch
import torch.utils.data as data
import sampler as query_Sampler
import numpy as np
import random
import model
import multi_modal_model
import arguments
from sklearn.manifold import TSNE
import seaborn as sns
import torch.nn as nn
import torchvision
from brats_dataset import BraTSDataset, BraTSDataset_3channel_input
import torch.backends.cudnn as cudnn
''''
Use: This to investigate the discrimantor scores produces by VAAL and M-VAAL
OR to plot the TSNE of the latent features
You should pass the arguments similar to main.py
Features: Plot the discriminator scores.
Plot TSNE plots
'''
## Set Seed
def fix_seed(seed):
# random
random.seed(seed)
# Numpy
np.random.seed(seed)
# Pytorch
torch.manual_seed(seed)
cudnn.deterministic = True
def main(args):
if args.dataset == 'brats_MIUA_HGG':
crop = tuple(int(i) for i in args.crop.split(","))
if min(crop) == 0:
crop = None
train_dataset = BraTSDataset("data/BraTS_frames/MIUA/", flip = True, split_dir ="train/", resize= args.resize, crop = crop, version= 5,v_flip = True, brightness = True, rotation = True, random_crop= True, segmentation_type = args.segmentation_type)
query_train_dataset = BraTSDataset("data/BraTS_frames/MIUA/", flip = False, split_dir ="train/", resize= 128, crop = (210,210), version= 5,v_flip = False, brightness = False, rotation = False, random_crop= False,segmentation_type = args.segmentation_type)
test_dataset = BraTSDataset("data/BraTS_frames/MIUA/", flip = False, split_dir = "test/", resize= args.resize, crop = crop, version= 5,segmentation_type = args.segmentation_type)
val_dataset = BraTSDataset("data/BraTS_frames/MIUA/", flip = False, split_dir = "val/", resize= args.resize, crop = crop, version= 5,segmentation_type = args.segmentation_type)
args.num_val = len(val_dataset)
args.num_images = len(train_dataset)
print (args.num_val, args.num_images)
#args.budget = 300
args.budget = 100
args.initial_budget = 200
args.num_classes = 2
args.num_channels= 1
elif args.dataset == 'brats_MIUA_LGG':
crop = tuple(int(i) for i in args.crop.split(","))
if min(crop) == 0:
crop = None
train_dataset = BraTSDataset("data/BraTS_frames/MIUA/", flip = True, split_dir ="train/", resize= args.resize, crop = crop, version= 6,v_flip = True, brightness = True, rotation = True, random_crop= True,segmentation_type = args.segmentation_type)
query_train_dataset = BraTSDataset("data/BraTS_frames/MIUA/", flip = False, split_dir ="train/", resize= 128, crop = (210,210), version= 6,v_flip = False, brightness = False, rotation = False, random_crop= False,segmentation_type = args.segmentation_type)
test_dataset = BraTSDataset("data/BraTS_frames/MIUA/", flip = False, split_dir = "test/", resize= args.resize, crop = crop, version= 6,segmentation_type = args.segmentation_type)
val_dataset = BraTSDataset("data/BraTS_frames/MIUA/", flip = False, split_dir = "val/", resize= args.resize, crop = crop, version= 6,segmentation_type = args.segmentation_type)
args.num_val = len(val_dataset)
args.num_images = len(train_dataset)
# args.budget = 200
args.budget = 100
args.initial_budget = 200
args.num_classes = 2
args.num_channels= 1
elif args.dataset == 'brats_MIUA_HGG_and_LGG':
crop = tuple(int(i) for i in args.crop.split(","))
if min(crop) == 0:
crop = None
train_dataset = BraTSDataset("data/BraTS_frames/MIUA/", flip = True, split_dir ="train/", resize= args.resize, crop = crop, version= 7,v_flip = True, brightness = True, rotation = True, random_crop= True,segmentation_type = args.segmentation_type)
query_train_dataset = BraTSDataset("data/BraTS_frames/MIUA/", flip = False, split_dir ="train/", resize= 128, crop = (210,210), version= 7,v_flip = False, brightness = False, rotation = False, random_crop= False,segmentation_type = args.segmentation_type)
test_dataset = BraTSDataset("data/BraTS_frames/MIUA/", flip = False, split_dir = "test/", resize= args.resize, crop = crop, version= 7,segmentation_type = args.segmentation_type)
val_dataset = BraTSDataset("data/BraTS_frames/MIUA/", flip = False, split_dir = "val/", resize= args.resize, crop = crop, version= 7,segmentation_type = args.segmentation_type)
args.num_val = len(val_dataset)
args.num_images = len(train_dataset)
args.budget = 300
args.initial_budget = 200
args.num_classes = 2
args.num_channels= 1
elif args.dataset == 'brats_MIUA_HGG_3channel':
crop = tuple(int(i) for i in args.crop.split(","))
if min(crop) == 0:
crop = None
train_dataset = BraTSDataset("data/BraTS_frames/MIUA/", flip = True, split_dir ="train/", resize= args.resize, crop = crop, version= 5,v_flip = True, brightness = True, rotation = True, random_crop= True, segmentation_type = args.segmentation_type)
query_train_dataset = BraTSDataset_3channel_input("data/BraTS_frames/MIUA/", flip = False, split_dir ="train/", resize= 128, crop = (210,210), version= 5,v_flip = False, brightness = False, rotation = False, random_crop= False,segmentation_type = args.segmentation_type)
test_dataset = BraTSDataset("data/BraTS_frames/MIUA/", flip = False, split_dir = "test/", resize= args.resize, crop = crop, version= 5,segmentation_type = args.segmentation_type)
val_dataset = BraTSDataset("data/BraTS_frames/MIUA/", flip = False, split_dir = "val/", resize= args.resize, crop = crop, version= 5,segmentation_type = args.segmentation_type)
args.num_val = len(val_dataset)
args.num_images = len(train_dataset)
print (args.num_val, args.num_images)
args.budget = 100
args.initial_budget = 200
args.num_classes = 2
args.num_channels= 1
args.query_channels = 3
else:
raise NotImplementedError
all_indices = np.arange(args.num_images)
if args.train_full:
initial_indices = all_indices.tolist()
else:
initial_indices = random.sample(list(all_indices), args.initial_budget)
whole_dataloader = data.DataLoader(query_train_dataset,
batch_size=args.batch_size, drop_last=False)
args.device = torch.device('cuda:'+args.gpu_id if torch.cuda.is_available() else 'cpu')
splits = [0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4]
current_indices = list(initial_indices)
for i, split in enumerate(splits):
## all unlabeled train samples
unlabeled_indices = np.setdiff1d(list(all_indices), current_indices)
unlabeled_sampler = data.sampler.SubsetRandomSampler(unlabeled_indices)
unlabeled_dataloader = data.DataLoader(query_train_dataset,
sampler=unlabeled_sampler, batch_size=args.batch_size, drop_last=False)
if args.method == 'VAAL':
#### initilaize the VAAL models
discriminator = model.Discriminator(args.latent_dim)
vae = model.VAE(args.latent_dim,nc=args.query_channels)
# load the checkpoint models
discriminator.load_state_dict(torch.load('./checkpoints/'+'/'+args.expt +
'/'+ 'discriminator_checkpoint'+str(split)+'.pth'))
vae.load_state_dict(torch.load('./checkpoints/'+'/'+args.expt +
'/'+ 'vae_checkpoint'+str(split)+'.pth'))
# send model to gpu
discriminator = discriminator.to(device = args.device)
vae = vae.to(device = args.device)
VAAL_sampler = query_Sampler.AdversarySampler(args.budget)
sampled_indices = VAAL_sampler.sample(vae,
discriminator,
unlabeled_dataloader, unlabeled_indices,
args.device)
elif args.method == 'multimodal_VAAL':
#### initilaize the VAAL models
vae = multi_modal_model.VAE(args.latent_dim)
discriminator = multi_modal_model.Discriminator(args.latent_dim)
# load the checkpoint models
discriminator.load_state_dict(torch.load('./checkpoints/'+'/'+args.expt +
'/'+ 'discriminator_checkpoint'+str(split)+'.pth'))
vae.load_state_dict(torch.load('./checkpoints/'+'/'+args.expt +
'/'+ 'vae_checkpoint'+str(split)+'.pth'))
# send the model to gpu
vae = vae.to(device = args.device)
discriminator = discriminator.to(device = args.device)
multimodal_VAAL_sampler = query_Sampler.AdversarySampler_multimodal(args.budget)
sampled_indices = multimodal_VAAL_sampler.sample(vae,
discriminator,
unlabeled_dataloader, unlabeled_indices,
args.device)
elif args.method == "RandomSampling":
random.seed(args.random_sampling_seed)
random.shuffle(unlabeled_indices)
sampled_indices = unlabeled_indices[:args.budget]
old_indices = list(current_indices)
# select the split that you want to investigate
if split == 0.05:
#### for TSNE plots
'''
if args.method == "RandomSampling":
tsne(old_indices, sampled_indices, unlabeled_indices, whole_dataloader, vae = None, disc= None, method = "RandomSampling")
elif args.method == "VAAL":
tsne(old_indices, sampled_indices, unlabeled_indices, whole_dataloader, vae = vae, disc = discriminator, method = "VAAL")
elif args.method == "multimodal_VAAL":
tsne(old_indices, sampled_indices, unlabeled_indices, whole_dataloader, vae = vae, disc = discriminator, method = "multimodal_VAAL")
'''
#### for Discriminator Scores
if args.method == "VAAL":
discriminator_hist(old_indices, sampled_indices, unlabeled_indices, whole_dataloader, vae = vae, disc = discriminator, method = "VAAL")
elif args.method == "multimodal_VAAL":
discriminator_hist(old_indices, sampled_indices, unlabeled_indices, whole_dataloader, vae = vae, disc = discriminator, method = "multimodal_VAAL")
break
def extract_features (dataloader, model_name = "inception_v3", vae = None, disc = None):
features_data = []
if model_name == "inception_v3":
model = torchvision.models.inception_v3(pretrained=True)
model.fc = nn.Identity()
model.to(args.device)
model.eval()
elif (model_name == "multimodalvae") or (model_name == "vae"):
if disc:
model = vae
model.eval()
model_disc = disc
model_disc.eval()
else:
model = vae
model.eval()
# put features and labels into arrays
for batch_ix, (batch_image, batch_mask, batch_aux) in enumerate(dataloader):
batch_image = batch_image.to(args.device)
with torch.no_grad():
if disc:
if model_name =="multimodalvae":
_,_,_,batch_feature,_ = model(batch_image)
elif model_name =="vae":
_,_,batch_feature,_ = model(batch_image)
batch_feature = model_disc(batch_feature)
else:
if model_name == "inception_v3":
batch_feature = model(batch_image)
elif model_name =="multimodalvae":
_,_,_,batch_feature,_ = model(batch_image)
elif model_name =="vae":
_,_,batch_feature,_ = model(batch_image)
if disc:
ft = batch_feature.flatten().cpu().numpy()
else:
ft = batch_feature.cpu().numpy()
if disc:
features_data.extend(ft)
else:
if batch_ix == 0:
features_data = np.array(ft)
np.concatenate((features_data,ft), axis = 0)
if disc:
features_data = np.array(features_data)
return torch.Tensor(features_data)
def tsne(old_indices, sampled_indices, unlabeled_indices, whole_dataloader, vae = None, disc= None, method = "RandomSampling"):
# whole_features= extract_features(whole_dataloader)
if method == "VAAL":
whole_features = extract_features(whole_dataloader, model_name= "vae", vae = vae, disc= None)
elif method == "multimodal_VAAL" :
whole_features = extract_features(whole_dataloader, model_name= "multimodalvae", vae = vae,disc=None)
else:
whole_features= extract_features(whole_dataloader)
print (whole_features.shape)
labels = ["unlabelled" for i in range (len(unlabeled_indices))]
tsne = TSNE(n_components=2,n_iter=300)
tsne_results = tsne.fit_transform(whole_features)
print(len(tsne_results))
tsne_X = tsne_results[:,0][unlabeled_indices]
tsne_Y = tsne_results[:,1][unlabeled_indices]
fig, ax = plt.subplots(figsize=(10, 8))
sns.scatterplot(
x=tsne_X, y=tsne_Y,
hue= labels,
data=tsne_results,
legend="full",
alpha=0.3)
tsne_X = tsne_results[:,0][old_indices]
tsne_Y = tsne_results[:,1][old_indices]
labels = ["labelled" for i in range (len(old_indices))]
sns.scatterplot(
x=tsne_X, y=tsne_Y,
hue= labels,
data=tsne_results,
legend="full",
color=".2",
palette="rocket",
alpha=0.8)
tsne_X = tsne_results[:,0][sampled_indices]
tsne_Y = tsne_results[:,1][sampled_indices]
labels = ["selected" for i in range (len(sampled_indices))]
sns.scatterplot(
x=tsne_X, y=tsne_Y,
hue= labels,
data=tsne_results,
legend="full",
color=".5",
palette="viridis",
alpha=0.8)
save_name = "VAAL_TSNE.png"
plt.savefig(save_name)
plt.close(fig)
def discriminator_hist(old_indices, sampled_indices, unlabeled_indices, whole_dataloader, vae = None, disc= None, method = "VAAL"):
if method == "VAAL":
whole_features = extract_features(whole_dataloader, model_name= "vae", vae = vae, disc= disc)
elif method == "multimodal_VAAL" :
whole_features = extract_features(whole_dataloader, model_name= "multimodalvae", vae = vae,disc= disc)
fig, ax = plt.subplots(figsize=(20, 15))
matplotlib.rcParams.update({'font.size': 24})
plt.subplot(2,1,1)
plt.hist([i for i in whole_features[unlabeled_indices].tolist()], bins = 100, color = 'orange', label = "unlabelled data")
plt.ylabel("No. of Samples")
plt.xlabel("Discriminator Scores")
plt.legend()
plt.subplot(2,1,2)
plt.hist([i for i in whole_features[old_indices].tolist()], bins = 100, label = "labelled data")
plt.ylabel("No. of Samples")
plt.xlabel("Discriminator Scores")
plt.legend()
save_name = "VAAL_disc_prob.png"
plt.savefig(save_name)
plt.close(fig)
if __name__ == '__main__':
args = arguments.get_args()
fix_seed(args.seed)
main(args)