-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathVOS_virtual.py
1690 lines (1462 loc) · 73.1 KB
/
VOS_virtual.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#-*- coding: utf-8 -*-
import numpy as np
import os
import argparse
import time
import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
import torchvision.transforms as trn
import torchvision.datasets as dset
import torch.nn.functional as F
from tqdm import tqdm
from models.allconv import AllConvNet
from models.wrn_virtual import WideResNet
from torch.utils.data import Dataset, DataLoader
import copy
from sklearn import manifold
import matplotlib.pyplot as plt
import umap
import random
from torch.nn.functional import gelu
# go through rigamaroo to do ...utils.display_results import show_performance
if __package__ is None:
import sys
from os import path
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
from utils.validation_dataset import validation_split
class SimpleDataSet(Dataset):
""" load synthetic time series data"""
def __init__(self, x, y):
self.x = x
self.y = y
def __len__(self):
return self.x.shape[0]
def __dim__(self):
if len(self.x.shape) > 2:
raise Exception("only handles single channel data")
else:
return self.x.shape[1]
def __getitem__(self, idx):
return (
self.x[idx],
self.y[idx],
)
def get_weights(net):
weight_keys = []
for name, parameters in net.named_parameters():
weight_keys.append(name)
NUM_DISTORTIONS = 3
OPTION_LAYER_MAPPING = {0: 'fc_layers.0', 1: 'fc_layers.1', 2: 'representation_layer'}
#print random seed
def get_name(i, tpe):
return OPTION_LAYER_MAPPING[i] + "." + tpe
weights = net.state_dict()
for option in random.sample(range(NUM_DISTORTIONS), 1):
i = np.random.choice(range(len(OPTION_LAYER_MAPPING)))
j = np.random.choice(range(len(OPTION_LAYER_MAPPING)))
weight_i = get_name(i, "weight")
bias_i = get_name(i, "bias")
weight_j = get_name(j, "weight")
bias_j = get_name(j, "weight")
#print("changed para", weight_i, bias_i, weight_j, bias_j)
if option == 0:
weights[weight_i] = torch.flip(weights[weight_i], (0,))
weights[bias_i] = torch.flip(weights[bias_i], (0,))
weights[weight_j] = torch.flip(weights[weight_j], (0,))
weights[bias_j] = torch.flip(weights[bias_j], (0,))
elif option == 1:
for k in [np.random.choice(weights[weight_i].size()[0]) for _ in range(12)]:
weights[weight_i][k] = -weights[weight_i][k]
weights[bias_i][k] = -weights[bias_i][k]
#elif option == 2:
# for k in [np.random.choice(weights[weight_i].size()[0]) for _ in range(25)]:
# weights[weight_i][k] = 0 * weights[weight_i][k]
# weights[bias_i][k] = 0 * weights[bias_i][k]
#elif option == 3:
# for k in [np.random.choice(weights[weight_i].size()[0]) for _ in range(25)]:
# weights[weight_i][k] = -gelu(weights[weight_i][k])
# weights[bias_i][k] = -gelu(weights[bias_i][k])
elif option == 2:
weights[weight_i] = weights[weight_i] * \
(1 + 2 * np.float32(np.random.uniform()) * (
4 * torch.rand_like(weights[weight_i] - 1)))
weights[weight_j] = weights[weight_j] * \
(1 + 2 * np.float32(np.random.uniform()) * (
4 * torch.rand_like(weights[weight_j] - 1)))
#elif option == 5: ##### begin saurav #####
# if random.random() < 0.5:
# mask = torch.round(torch.rand_like(weights[weight_i]))
# else:
# mask = torch.round(torch.rand_like(weights[weight_i])) * 2 - 1
# weights[weight_i] *= mask
elif option == 3:
out_filters = weights[weight_i].shape[0]
to_zero = list(set([random.choice(list(range(out_filters))) for _ in range(out_filters // 5)]))
weights[weight_i][to_zero] = weights[weight_i][to_zero] * -1.0
return weights
def sample_ratios(train_sample,train_target, class_num, sample_ratio=None):
#print(train_sample.shape, train_target.shape)
if sample_ratio is not None:
selected_idx = []
for i in range(class_num):
images_i = [j for j in range(
len(train_sample.shape[0])) if train_target[j] == i]
num_ = len(images_i)
idx = np.random.choice(
num_, int(num_*sample_ratio), replace=False)
selected_idx.extend(np.array(images_i)[idx].tolist())
return train_sample[selected_idx], train_target[selected_idx]
return train_sample, train_target
def cosine_annealing(step, total_steps, lr_max, lr_min):
return lr_min + (lr_max - lr_min) * 0.5 * (
1 + np.cos(step / total_steps * np.pi))
def log_sum_exp(weight_energy, value, dim=None, keepdim=False):
"""Numerically stable implementation of the operation
value.exp().sum(dim, keepdim).log()
"""
import math
# TODO: torch.max(value, dim=None) threw an error at time of writing
if dim is not None:
m, _ = torch.max(value, dim=dim, keepdim=True)
value0 = value - m
if keepdim is False:
m = m.squeeze(dim)
return m + torch.log(torch.sum(
F.relu(weight_energy.weight) * torch.exp(value0), dim=dim, keepdim=keepdim))
else:
m = torch.max(value)
sum_exp = torch.sum(torch.exp(value - m))
# if isinstance(sum_exp, Number):
# return m + math.log(sum_exp)
# else:
return m + torch.log(sum_exp)
# /////////////// Training ///////////////
def exp_lr_scheduler(epoch, decay=0.98, init_lr=0.1, lr_decay_epoch=1):
"""Decay learning rate by a factor of 0.95 every lr_decay_epoch epochs."""
lr= max(0.1, init_lr * (decay ** (epoch // lr_decay_epoch)))
return lr
def train_gen(generator, start_iter, user_class, method, m_in, m_out, glob_iter, user_classifier, oe_batch_size, total_iter, state, max_iter, net, train_loader, optimizer,verbose=0):
net.train() # enter train mode
generator.eval()
data_iterator = iter(train_loader)
#print("total iter", total_iter)
#print("max iter", max_iter)
#print("start iter", start_iter)
generative_alpha = exp_lr_scheduler(glob_iter, decay=0.98, init_lr=0.1)
for i in tqdm(range(max_iter), disable=verbose < 1):
total_iter += 1
loss_avg = 0.0
try:
data, target = next(data_iterator)
except StopIteration:
data_iterator = iter(train_loader)
data, target = next(data_iterator)
data, target = data.cuda(), target.cuda()
# forward
x, output = net.forward_virtual(data)
optimizer.zero_grad()
loss = F.cross_entropy(x, target)
oodx = torch.rand(oe_batch_size, data.shape[1], data.shape[2], data.shape[3]).cuda()
## feed to generator
gen_result = generator(oodx).clone().detach()
logit_given_gen = user_classifier(gen_result)
if method == 'crossentropy':
oodclass = [i for i in range(10) if i not in user_class]
oody = np.random.choice(oodclass, oe_batch_size)
oody = torch.LongTensor(oody).cuda()
loss += generative_alpha * F.cross_entropy(oody, logit_given_gen) # encourage different outputs
elif method == 'energy':
idy = net(data)
Ec_out = -torch.logsumexp(logit_given_gen, dim=1)
Ec_in = -torch.logsumexp(idy, dim=1)
loss += generative_alpha * (torch.pow(F.relu(Ec_in - m_in), 2).mean() + torch.pow(F.relu(m_out - Ec_out),
2).mean())
elif method == 'OE':
loss += generative_alpha * (- (logit_given_gen.mean(1) - torch.logsumexp(logit_given_gen, dim=1)).mean())
loss.backward()
optimizer.step()
# exponential moving average
loss_avg = loss_avg * 0.8 + float(loss) * 0.2
state['train_loss'] = loss_avg
return loss_avg, total_iter
##inversion attack
def inversion_train(generator, start_iter, user_class, method, m_in, m_out, glob_iter, user_classifier, oe_batch_size, total_iter, state, max_iter, net, train_loader, optimizer,verbose=0, logistic_regression=None, weight_energy=None, numclasses=10):
net.train() # enter train mode
generator.eval()
data_iterator = iter(train_loader)
generative_alpha = exp_lr_scheduler(glob_iter, decay=0.98, init_lr=0.1)
for i in tqdm(range(max_iter), disable=verbose < 1):
total_iter += 1
loss_avg = 0.0
try:
data, target = next(data_iterator)
except StopIteration:
data_iterator = iter(train_loader)
data, target = next(data_iterator)
data, target = data.cuda(), target.cuda()
# forward
x, output = net.forward_virtual(data)
optimizer.zero_grad()
loss = F.cross_entropy(x, target)
oodclass = [i for i in range(numclasses) if i not in user_class]
oody = np.random.choice(oodclass, oe_batch_size)
oody = torch.LongTensor(oody).cuda()
ood_samples = generator(oody).clone().detach()
logit_given_gen = user_classifier(ood_samples)
if method == 'crossentropy':
loss += generative_alpha * F.cross_entropy(logit_given_gen, oody) # encourage different outputs
elif method == 'energy':
Ec_out = -torch.logsumexp(logit_given_gen, dim=1)
Ec_in = -torch.logsumexp(x, dim=1)
loss += generative_alpha * (torch.pow(F.relu(Ec_in - m_in), 2).mean() + torch.pow(F.relu(m_out - Ec_out),
2).mean())
elif method == 'OE':
loss += generative_alpha * (- (logit_given_gen.mean(1) - torch.logsumexp(logit_given_gen, dim=1)).mean())
elif method == 'energy_VOS':
energy_score_for_fg = log_sum_exp(weight_energy, x, 1)
# energy_score_for_bg = 1 * torch.logsumexp(predictions_ood[0][:, :-1] / 1, 1)
energy_score_for_bg = log_sum_exp(weight_energy, logit_given_gen, 1)
input_for_lr = torch.cat((energy_score_for_fg, energy_score_for_bg), -1)
labels_for_lr = torch.cat((torch.ones(len(x)).cuda(),
torch.zeros(len(logit_given_gen)).cuda()), -1)
criterion = torch.nn.CrossEntropyLoss()
output1 = logistic_regression(input_for_lr.view(-1, 1))
lr_reg_loss = criterion(output1, labels_for_lr.long())
loss += generative_alpha * lr_reg_loss
loss.backward()
optimizer.step()
# exponential moving average
loss_avg = loss_avg * 0.8 + float(loss) * 0.2
state['train_loss'] = loss_avg
return loss_avg, total_iter
##choose top pdf from z
def topk_inversion_train(num_classes, number_dict, data_dict, sample_number, eye_matrix, sample_from, generator, start_iter, user_class, method, m_in, m_out, glob_iter, user_classifier, total_iter, state, max_iter, net, train_loader, optimizer, verbose=0, logistic_regression=None, weight_energy=None, select=None, soft=False, optimizer_fc=None, optimizer_local=None):
net.train() # enter train mode
generator.eval()
generative_alpha = exp_lr_scheduler(glob_iter, decay=0.98, init_lr=0.1)
#load the parameter of fc_head to local model classifier head
#for k, v in fc_head.state_dict().items():
# if 'fc.weight' in k:
# net.state_dict()['fc.weight'].copy_(fc_head.state_dict()[k])
# if 'fc.bias' in k:
# net.state_dict()['fc.bias'].copy_(fc_head.state_dict()[k])
data_iterator = iter(train_loader)
for i in tqdm(range(max_iter), disable=verbose < 1):
total_iter += 1
loss_avg = 0.0
try:
data, target = next(data_iterator)
except StopIteration:
data_iterator = iter(train_loader)
data, target = next(data_iterator)
data, target = data.cuda(), target.cuda()
# forward
x, output = net.forward_virtual(data)
optimizer_fc.zero_grad()
optimizer_local.zero_grad()
loss = F.cross_entropy(x, target)
oodclass = [i for i in range(num_classes) if i not in user_class]
oody = np.random.choice(oodclass, sample_number)
oody = torch.LongTensor(oody).cuda()
oodz = generator(oody, soft=soft).clone().detach()
# energy regularization.
sum_temp = 0
for index in range(num_classes):
sum_temp += number_dict[index]
lr_reg_loss = torch.zeros(1).cuda()[0]
if sum_temp == num_classes * sample_number and total_iter < start_iter:
# maintaining an ID data queue for each class.
target_numpy = oody.cpu().data.numpy()
for index in range(len(oody)):
dict_key = target_numpy[index]
data_dict[dict_key] = torch.cat((data_dict[dict_key][1:],
oodz[index].detach().view(1, -1)), 0)
elif sum_temp == num_classes * sample_number and total_iter >= start_iter:
target_numpy = oody.cpu().data.numpy()
for index in range(len(oody)):
dict_key = target_numpy[index]
data_dict[dict_key] = torch.cat((data_dict[dict_key][1:],
oodz[index].detach().view(1, -1)), 0)
# the covariance finder needs the data to be centered.
for index in range(num_classes):
if index == 0:
X = data_dict[index] - data_dict[index].mean(0)
mean_embed_id = data_dict[index].mean(0).view(1, -1)
else:
X = torch.cat((X, data_dict[index] - data_dict[index].mean(0)), 0)
mean_embed_id = torch.cat((mean_embed_id,
data_dict[index].mean(0).view(1, -1)), 0)
## add the variance.
temp_precision = torch.mm(X.t(), X) / len(X)
temp_precision += 0.0001 * eye_matrix
start = False
for index in range(num_classes):
if index not in user_class:
new_dis = torch.distributions.multivariate_normal.MultivariateNormal(
mean_embed_id[index], covariance_matrix=temp_precision)
negative_samples = new_dis.rsample((sample_from,))
prob_density = new_dis.log_prob(negative_samples)
cur_samples, index_prob = torch.topk(- prob_density, select)
if start == False:
start = True
ood_samples = negative_samples[index_prob]
else:
ood_samples = torch.cat((ood_samples, negative_samples[index_prob]), 0)
if len(ood_samples) != 0:
# add some gaussian noise
# ood_samples = self.noise(ood_samples)
# energy_score_for_fg = 1 * torch.logsumexp(predictions[0][selected_fg_samples][:, :-1] / 1, 1)
logit_given_gen = user_classifier(ood_samples)
if method == 'energy':
Ec_out = -torch.logsumexp(logit_given_gen, dim=1)
Ec_in = -torch.logsumexp(x, dim=1)
loss += generative_alpha * (
torch.pow(F.relu(Ec_in - m_in), 2).mean() + torch.pow(F.relu(m_out - Ec_out),
2).mean())
elif method == 'OE':
loss += generative_alpha * (
- (logit_given_gen.mean(1) - torch.logsumexp(logit_given_gen, dim=1)).mean())
elif method == 'energy_VOS':
energy_score_for_fg = log_sum_exp(weight_energy, x, 1)
# energy_score_for_bg = 1 * torch.logsumexp(predictions_ood[0][:, :-1] / 1, 1)
energy_score_for_bg = log_sum_exp(weight_energy, logit_given_gen, 1)
input_for_lr = torch.cat((energy_score_for_fg, energy_score_for_bg), -1)
labels_for_lr = torch.cat((torch.ones(len(x)).cuda(),
torch.zeros(len(logit_given_gen)).cuda()), -1)
criterion = torch.nn.CrossEntropyLoss()
output1 = logistic_regression(input_for_lr.view(-1, 1))
lr_reg_loss = criterion(output1, labels_for_lr.long())
loss += generative_alpha * lr_reg_loss
else:
target_numpy = oody.cpu().data.numpy()
for index in range(len(oody)):
dict_key = target_numpy[index]
if number_dict[dict_key] < sample_number:
data_dict[dict_key][number_dict[dict_key]] = oodz[index].detach()
number_dict[dict_key] += 1
loss.backward()
optimizer_fc.step()
optimizer_local.step()
# exponential moving average
loss_avg = loss_avg * 0.8 + float(loss) * 0.2
state['train_loss'] = loss_avg
#for name, p in net.get_fc().named_parameters():
# param_norm = torch.norm(p.grad)
# print("torch.norm(fc_head.grad)", param_norm)
return loss_avg, total_iter
def topk_inversion_train_prox(num_classes, number_dict, data_dict, sample_number, eye_matrix, sample_from, generator, start_iter, user_class, method, m_in, m_out, glob_iter, user_classifier, total_iter, state, max_iter, server_model, net, train_loader, optimizer, verbose=0, logistic_regression=None, weight_energy=None, select=None, soft=False, optimizer_fc=None, optimizer_local=None, mu=1e-3):
net.train() # enter train mode
generator.eval()
generative_alpha = exp_lr_scheduler(glob_iter, decay=0.98, init_lr=0.1)
#load the parameter of fc_head to local model classifier head
#for k, v in fc_head.state_dict().items():
# if 'fc.weight' in k:
# net.state_dict()['fc.weight'].copy_(fc_head.state_dict()[k])
# if 'fc.bias' in k:
# net.state_dict()['fc.bias'].copy_(fc_head.state_dict()[k])
data_iterator = iter(train_loader)
for i in tqdm(range(max_iter), disable=verbose < 1):
total_iter += 1
loss_avg = 0.0
try:
data, target = next(data_iterator)
except StopIteration:
data_iterator = iter(train_loader)
data, target = next(data_iterator)
data, target = data.cuda(), target.cuda()
# forward
x, output = net.forward_virtual(data)
optimizer_fc.zero_grad()
optimizer_local.zero_grad()
loss = F.cross_entropy(x, target)
oodclass = [i for i in range(num_classes) if i not in user_class]
oody = np.random.choice(oodclass, sample_number)
oody = torch.LongTensor(oody).cuda()
oodz = generator(oody, soft=soft).clone().detach()
# energy regularization.
sum_temp = 0
for index in range(num_classes):
sum_temp += number_dict[index]
lr_reg_loss = torch.zeros(1).cuda()[0]
if sum_temp == num_classes * sample_number and total_iter < start_iter:
# maintaining an ID data queue for each class.
target_numpy = oody.cpu().data.numpy()
for index in range(len(oody)):
dict_key = target_numpy[index]
data_dict[dict_key] = torch.cat((data_dict[dict_key][1:],
oodz[index].detach().view(1, -1)), 0)
elif sum_temp == num_classes * sample_number and total_iter >= start_iter:
target_numpy = oody.cpu().data.numpy()
for index in range(len(oody)):
dict_key = target_numpy[index]
data_dict[dict_key] = torch.cat((data_dict[dict_key][1:],
oodz[index].detach().view(1, -1)), 0)
# the covariance finder needs the data to be centered.
for index in range(num_classes):
if index == 0:
X = data_dict[index] - data_dict[index].mean(0)
mean_embed_id = data_dict[index].mean(0).view(1, -1)
else:
X = torch.cat((X, data_dict[index] - data_dict[index].mean(0)), 0)
mean_embed_id = torch.cat((mean_embed_id,
data_dict[index].mean(0).view(1, -1)), 0)
## add the variance.
temp_precision = torch.mm(X.t(), X) / len(X)
temp_precision += 0.0001 * eye_matrix
start = False
for index in range(num_classes):
if index not in user_class:
new_dis = torch.distributions.multivariate_normal.MultivariateNormal(
mean_embed_id[index], covariance_matrix=temp_precision)
negative_samples = new_dis.rsample((sample_from,))
prob_density = new_dis.log_prob(negative_samples)
cur_samples, index_prob = torch.topk(- prob_density, select)
if start == False:
start = True
ood_samples = negative_samples[index_prob]
else:
ood_samples = torch.cat((ood_samples, negative_samples[index_prob]), 0)
if len(ood_samples) != 0:
# add some gaussian noise
# ood_samples = self.noise(ood_samples)
# energy_score_for_fg = 1 * torch.logsumexp(predictions[0][selected_fg_samples][:, :-1] / 1, 1)
logit_given_gen = user_classifier(ood_samples)
if method == 'energy':
Ec_out = -torch.logsumexp(logit_given_gen, dim=1)
Ec_in = -torch.logsumexp(x, dim=1)
loss += generative_alpha * (
torch.pow(F.relu(Ec_in - m_in), 2).mean() + torch.pow(F.relu(m_out - Ec_out),
2).mean())
elif method == 'OE':
loss += generative_alpha * (
- (logit_given_gen.mean(1) - torch.logsumexp(logit_given_gen, dim=1)).mean())
elif method == 'energy_VOS':
energy_score_for_fg = log_sum_exp(weight_energy, x, 1)
# energy_score_for_bg = 1 * torch.logsumexp(predictions_ood[0][:, :-1] / 1, 1)
energy_score_for_bg = log_sum_exp(weight_energy, logit_given_gen, 1)
input_for_lr = torch.cat((energy_score_for_fg, energy_score_for_bg), -1)
labels_for_lr = torch.cat((torch.ones(len(x)).cuda(),
torch.zeros(len(logit_given_gen)).cuda()), -1)
criterion = torch.nn.CrossEntropyLoss()
output1 = logistic_regression(input_for_lr.view(-1, 1))
lr_reg_loss = criterion(output1, labels_for_lr.long())
loss += generative_alpha * lr_reg_loss
else:
target_numpy = oody.cpu().data.numpy()
for index in range(len(oody)):
dict_key = target_numpy[index]
if number_dict[dict_key] < sample_number:
data_dict[dict_key][number_dict[dict_key]] = oodz[index].detach()
number_dict[dict_key] += 1
if i > 0:
w_diff = torch.tensor(0.).cuda()
if isinstance(server_model, dict): # state dict
for w_name, w_t in net.named_parameters():
w_diff += torch.pow(torch.norm(server_model[w_name] - w_t), 2)
else:
for w, w_t in zip(server_model.parameters(), net.parameters()):
w_diff += torch.pow(torch.norm(w - w_t), 2)
w_diff = torch.sqrt(w_diff)
loss += mu / 2. * w_diff
loss.backward()
optimizer_fc.step()
optimizer_local.step()
# exponential moving average
loss_avg = loss_avg * 0.8 + float(loss) * 0.2
state['train_loss'] = loss_avg
#for name, p in net.get_fc().named_parameters():
# param_norm = torch.norm(p.grad)
# print("torch.norm(fc_head.grad)", param_norm)
return loss_avg, total_iter
def plot_tsne(X1,X2,X3):
len1 = X1.shape[0]
len2 = X2.shape[0]
len3 = X3.shape[0]
X = torch.cat((X1, X2), 0)
X = torch.cat((X, X3), 0)
#X = torch.cat((X ,X4), 0)
X = X.detach().cpu().numpy()
tsne = manifold.TSNE(n_components=2, init='pca', random_state=501)
X_tsne1 = tsne.fit_transform(X)
print("Org data dimension is {}.Embedded data dimension is {}".format(X.shape[-1], X_tsne1.shape[-1]))
x_min, x_max = X_tsne1.min(0), X_tsne1.max(0)
X_norm1 = (X_tsne1 - x_min) / (x_max - x_min)
# ID
size0 = 2
marker0 = '.'
name0 = 'ID data'
color0 = 'coral'
# generated
size = 2
marker = '.'
name1 = 'Generated OoD data'
color = 'mediumaquamarine'
# virtual
name2 = 'Selected OoD sample'
color2 = 'midnightblue'
marker2 = '*'
size2 = 2
# real external
name3 = 'real external sample'
color3 = 'brown'
marker3 = '.'
size3= 2
plt.rcParams.update({'font.size': 15})
plt.scatter(X_norm1[:len1, 0], X_norm1[:len1, 1], label=name1, alpha=0.8, s=size, c=color, marker=marker)
plt.scatter(X_norm1[len1:len1+len2, 0], X_norm1[len1:len1+len2, 1], label=name3, alpha=0.8, s=size3, c=color3,
marker=marker3)
plt.scatter(X_norm1[len1 + len2:len1 + len2 + len3, 0], X_norm1[len1 + len2:len1 + len2 + len3, 1], label=name0, alpha=0.8, s=size0, c=color0,
marker=marker0)
#plt.scatter(X_norm1[len1 + len2 + len3:, 0], X_norm1[len1 + len2 + len3:, 1], label=name3,
# alpha=0.8, s=size3, c=color3,
# marker=marker3)
plt.xticks([])
plt.yticks([])
def plot_IDtsne(X, y):
X = X.detach().cpu().numpy()
y = y.detach().cpu().numpy()
tsne = manifold.TSNE(n_components=2, init='pca', random_state=501, perplexity=20)
X_tsne1 = tsne.fit_transform(X)
print("Org data dimension is {}.Embedded data dimension is {}".format(X.shape[-1], X_tsne1.shape[-1]))
x_min, x_max = X_tsne1.min(0), X_tsne1.max(0)
X_norm1 = (X_tsne1 - x_min) / (x_max - x_min)
plt.rcParams.update({'font.size': 15})
plt.scatter(X_norm1[:, 0], X_norm1[:, 1],s=2,c=y)
plt.xticks([])
plt.yticks([])
def plot_umap(X1,X2,X3=None):
len1 = X1.shape[0]
len2 = X2.shape[0]
X = torch.cat((X1,X2), 0)
X = torch.cat((X,X3), 0)
X = X.detach().cpu().numpy()
embedding = umap.UMAP(n_neighbors=5,
min_dist=0.8,
metric='correlation',
random_state=16).fit_transform(X)
print("Org data dimension is {}.Embedded data dimension is {}".format(X1.shape[-1], embedding.shape[-1]))
x_min, x_max = embedding.min(0), embedding.max(0)
X_norm1 = (embedding - x_min) / (x_max - x_min)
# ID
size0 = 2
marker0 = '.'
name0 = 'ID data'
color0 = 'coral'
# generated
size = 2
marker = '.'
name1 = 'Generated OoD data'
color = 'mediumaquamarine'
#virtual
name2 = 'Selected OoD sample'
color2 = 'midnightblue'
marker2 = '*'
size2 = 2
plt.rcParams.update({'font.size': 15})
#plt.scatter(X_norm1[:len1, 0], X_norm1[:len1, 1], label=name1, alpha=0.8, s=size, c=color, marker=marker)
plt.scatter(X_norm1[len1:len1+len2, 0], X_norm1[len1:len1+len2, 1], label=name2, alpha=0.8, s=size2, c=color2, marker=marker2)
plt.scatter(X_norm1[len1+len2:, 0], X_norm1[len1+len2:, 1], label=name0, alpha=0.8, s=size0, c=color0, marker=marker0)
plt.xticks([])
plt.yticks([])
def var(samples):
#get mean
smean = samples.mean(0)
def visualization(user_id, num_classes, number_dict, data_dict, sample_number, eye_matrix, sample_from, generator, start_iter, user_class, user_classifier, total_iter, state, max_iter, net, train_loader, verbose=0, logistic_regression=None, weight_energy=None, select=None, soft=0, external_loader=None,select_ID=1000):
net.eval() # enter train mode
generator.eval()
data_iterator = iter(train_loader)
#exdata_iterator = iter(external_loader)
start = True
start2 = False
for i in tqdm(range(max_iter), disable=verbose < 1):
total_iter += 1
loss_avg = 0.0
try:
data, target = next(data_iterator)
#exdata, extarget = next(exdata_iterator)
except StopIteration:
data_iterator = iter(train_loader)
data, target = next(data_iterator)
#exdata_iterator = iter(external_loader)
#exdata, extarget = next(exdata_iterator)
data, target = data.cuda(), target.cuda()
# forward
x, output = net.forward_virtual(data)
#exdata, extarget = exdata.cuda(), extarget.cuda()
#_, exoutput = net.forward_virtual(exdata)
loss = F.cross_entropy(x, target)
oodclass = [i for i in range(num_classes) if i not in user_class]
oody = np.random.choice(oodclass, sample_number)
oody = torch.LongTensor(oody).cuda()
oodz = generator(oody, soft=soft).clone().detach()
if start:
vx = output
vy = target
#exvx = exoutput
#exvy = extarget
start = False
else:
vx = torch.cat((vx, output), 0)
vy = torch.cat((vy, target), 0)
#exvx = torch.cat((exvx, output), 0)
#exvy = torch.cat((exvy, target), 0)
# energy regularization.
sum_temp = 0
for index in range(num_classes):
sum_temp += number_dict[index]
if sum_temp == num_classes * sample_number:
target_numpy = target.cpu().data.numpy()
for index in range(len(target)):
dict_key = target_numpy[index]
data_dict[dict_key] = torch.cat((data_dict[dict_key][1:],
output[index].detach().view(1, -1)), 0)
target_numpy = oody.cpu().data.numpy()
for index in range(len(oody)):
dict_key = target_numpy[index]
data_dict[dict_key] = torch.cat((data_dict[dict_key][1:],
oodz[index].detach().view(1, -1)), 0)
# the covariance finder needs the data to be centered.
for index in range(num_classes):
if index == 0:
X = data_dict[index] - data_dict[index].mean(0)
mean_embed_id = data_dict[index].mean(0).view(1, -1)
else:
X = torch.cat((X, data_dict[index] - data_dict[index].mean(0)), 0)
mean_embed_id = torch.cat((mean_embed_id,
data_dict[index].mean(0).view(1, -1)), 0)
else:
target_numpy = target.cpu().data.numpy()
for index in range(len(target)):
dict_key = target_numpy[index]
if number_dict[dict_key] < sample_number:
data_dict[dict_key][number_dict[dict_key]] = output[index].detach()
number_dict[dict_key] += 1
target_numpy = oody.cpu().data.numpy()
for index in range(len(oody)):
dict_key = target_numpy[index]
if number_dict[dict_key] < sample_number:
data_dict[dict_key][number_dict[dict_key]] = oodz[index].detach()
number_dict[dict_key] += 1
for index in range(num_classes):
if index == 0:
X = data_dict[index] - data_dict[index].mean(0)
mean_embed_id = data_dict[index].mean(0).view(1, -1)
else:
X = torch.cat((X, data_dict[index] - data_dict[index].mean(0)), 0)
mean_embed_id = torch.cat((mean_embed_id,
data_dict[index].mean(0).view(1, -1)), 0)
## add the variance.
temp_precision = torch.mm(X.t(), X) / len(X)
#temp_precision += 0.0001 * eye_matrix
temp_precision += 0.00001 * eye_matrix
#exood_sample = torch.load('vv {}'.format(user_id))
prob_origin = {}
prob_ex = {}
var_ex = {}
var_origin = {}
for index in range(num_classes):
prob_origin[index] = []
prob_ex[index] = []
var_ex[index] = []
var_origin[index] = []
start2 = False
for i in range(80):
oody = np.random.choice(oodclass, sample_number)
oody = torch.LongTensor(oody).cuda()
oodz = generator(oody, soft=soft).clone().detach()
if start2 == False:
start2 = True
vz = oodz
else:
vz = torch.cat((vz, oodz), 0)
start3 = False
for index in range(num_classes):
if index not in user_class:
new_dis = torch.distributions.multivariate_normal.MultivariateNormal(
mean_embed_id[index], covariance_matrix=temp_precision)
#negative_samples = new_dis.rsample((sample_from,))
prob_density = new_dis.log_prob(oodz)
# breakpoint()
# index_prob = (prob_density < - self.threshold).nonzero().view(-1)
# keep the data in the low density area.
cur_samples, index_prob = torch.topk(- prob_density, select)
if start3 == False:
start3 = True
ood_samples = oodz[index_prob]
#ood_samples = negative_samples[index_prob]
else:
ood_samples = torch.cat((ood_samples, oodz[index_prob]), 0)
#ood_samples = torch.cat((ood_samples, negative_samples[index_prob]), 0)
for index in range(num_classes):
if index in user_class:
new_dis = torch.distributions.multivariate_normal.MultivariateNormal(
mean_embed_id[index], covariance_matrix=temp_precision)
# negative_samples = new_dis.rsample((sample_from,))
prob_density = new_dis.log_prob(ood_samples)
prob_ex[index].append(prob_density.mean())
var_ex[index].append(torch.var(prob_density, 0))
prob_density_origin = new_dis.log_prob(vz)
prob_origin[index].append(prob_density_origin.mean())
var_origin[index].append(torch.var(prob_density_origin, 0))
print("user_class", index)
print("origin sample prob", sum(prob_origin[index])/len(prob_origin[index]))
print("origin sample var", sum(var_origin[index]) / len(var_origin[index]))
print("ex sample prob", sum(prob_ex[index])/len(prob_ex[index]))
print("ex sample var", sum(var_ex[index]) / len(var_ex[index]))
select_num = ood_samples.shape[0]
select_list = list(z for z in range(vz.shape[0]))
select_id = random.sample(select_list, min(select_num*5, vz.shape[0]))
#select_id2 = [i for i in range(vy.shape[0]) if vy[i] == user_class[0]]
vz = vz[select_id]
torch.save(ood_samples, 'vv')
torch.save(vx, 'vx')
torch.save(vz, 'vz')
torch.save(vy, 'vy')
#plot_umap(vz, ood_samples)
plot_IDtsne(vx, vy)
plt.savefig("distribution/v_user{}.png".format(user_id))
plt.close()
print("save ID figure {}.png".format(user_id))
#vx = vx[select_id2]
plot_tsne(vz, ood_samples, vx)
#plot_tsne(vz, vx)
#plot_tsne(vz, 'generated OoD data')
#name = 'virtual OoD data'
#plot_tsne(logit_given_gen, name)
plt.legend(loc="lower left", markerscale=4., framealpha=0.5)
plt.show()
plt.savefig("distribution/z_user{}.png".format(user_id))
plt.close()
print("save figure {}.png".format(user_id))
return vx, vz
def visualization_external(user_id, num_classes, number_dict, data_dict, sample_number, eye_matrix, sample_from, generator, start_iter, user_class, user_classifier, total_iter, state, max_iter, net, train_loader, verbose=0, logistic_regression=None, weight_energy=None, select=None, soft=False, external_loader=None):
net.eval() # enter train mode
generator.eval()
data_iterator = iter(train_loader)
exdata_iterator = iter(external_loader)
start = True
start2 = False
for i in tqdm(range(max_iter), disable=verbose < 1):
total_iter += 1
loss_avg = 0.0
try:
data, target = next(data_iterator)
exdata, extarget = next(exdata_iterator)
except StopIteration:
data_iterator = iter(train_loader)
data, target = next(data_iterator)
exdata_iterator = iter(external_loader)
exdata, extarget = next(exdata_iterator)
data, target = data.cuda(), target.cuda()
# forward
x, output = net.forward_virtual(data)
exdata, extarget = exdata.cuda(), extarget.cuda()
_, exoutput = net.forward_virtual(exdata)
loss = F.cross_entropy(x, target)
oodclass = [i for i in range(num_classes) if i not in user_class]
oody = np.random.choice(oodclass, sample_number)
oody = torch.LongTensor(oody).cuda()
oodz = generator(oody, soft=soft).clone().detach()
if start:
vx = output
vy = target
exvx = exoutput
exvy = extarget
start = False
else:
vx = torch.cat((vx, output), 0)
vy = torch.cat((vy, target), 0)
exvx = torch.cat((exvx, output), 0)
exvy = torch.cat((exvy, target), 0)
# energy regularization.
sum_temp = 0
for index in range(num_classes):
sum_temp += number_dict[index]
if sum_temp == num_classes * sample_number:
target_numpy = target.cpu().data.numpy()
for index in range(len(target)):
dict_key = target_numpy[index]
data_dict[dict_key] = torch.cat((data_dict[dict_key][1:],
output[index].detach().view(1, -1)), 0)
target_numpy = oody.cpu().data.numpy()
for index in range(len(oody)):
dict_key = target_numpy[index]
data_dict[dict_key] = torch.cat((data_dict[dict_key][1:],
oodz[index].detach().view(1, -1)), 0)
# the covariance finder needs the data to be centered.
for index in range(num_classes):
if index == 0:
X = data_dict[index] - data_dict[index].mean(0)
mean_embed_id = data_dict[index].mean(0).view(1, -1)
else:
X = torch.cat((X, data_dict[index] - data_dict[index].mean(0)), 0)
mean_embed_id = torch.cat((mean_embed_id,
data_dict[index].mean(0).view(1, -1)), 0)
else:
target_numpy = target.cpu().data.numpy()
for index in range(len(target)):
dict_key = target_numpy[index]
if number_dict[dict_key] < sample_number:
data_dict[dict_key][number_dict[dict_key]] = output[index].detach()
number_dict[dict_key] += 1
target_numpy = oody.cpu().data.numpy()
for index in range(len(oody)):
dict_key = target_numpy[index]
if number_dict[dict_key] < sample_number:
data_dict[dict_key][number_dict[dict_key]] = oodz[index].detach()
number_dict[dict_key] += 1
for index in range(num_classes):
if index == 0:
X = data_dict[index] - data_dict[index].mean(0)
mean_embed_id = data_dict[index].mean(0).view(1, -1)
else:
X = torch.cat((X, data_dict[index] - data_dict[index].mean(0)), 0)
mean_embed_id = torch.cat((mean_embed_id,
data_dict[index].mean(0).view(1, -1)), 0)
## add the variance.
temp_precision = torch.mm(X.t(), X) / len(X)
# temp_precision += 0.0001 * eye_matrix
temp_precision += 0.00001 * eye_matrix
# exood_sample = torch.load('vv {}'.format(user_id))
prob_origin = {}
prob_ex = {}
var_ex = {}
var_origin = {}
for index in range(num_classes):
prob_origin[index] = []
prob_ex[index] = []
var_ex[index] = []
var_origin[index] = []
start2 = False
start3 = False
for i in range(100):
oody = np.random.choice(oodclass, sample_number)
oody = torch.LongTensor(oody).cuda()
oodz = generator(oody, soft=soft).clone().detach()
if start2 == False:
start2 = True
vz = oodz
else:
vz = torch.cat((vz, oodz), 0)
for index in range(num_classes):
if index not in user_class:
new_dis = torch.distributions.multivariate_normal.MultivariateNormal(
mean_embed_id[index], covariance_matrix=temp_precision)
# negative_samples = new_dis.rsample((sample_from,))
prob_density = new_dis.log_prob(oodz)
# breakpoint()
# index_prob = (prob_density < - self.threshold).nonzero().view(-1)
# keep the data in the low density area.
cur_samples, index_prob = torch.topk(- prob_density, select)
if start3 == False:
start3 = True
ood_samples = oodz[index_prob]
# ood_samples = negative_samples[index_prob]
else:
ood_samples = torch.cat((ood_samples, oodz[index_prob]), 0)
# ood_samples = torch.cat((ood_samples, negative_samples[index_prob]), 0)
for index in range(num_classes):
if index in user_class:
new_dis = torch.distributions.multivariate_normal.MultivariateNormal(
mean_embed_id[index], covariance_matrix=temp_precision)
# negative_samples = new_dis.rsample((sample_from,))
prob_density = new_dis.log_prob(ood_samples)