forked from MingSun-Tse/Regularization-Pruning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
1142 lines (990 loc) · 40.2 KB
/
utils.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
import torch
import torch.nn as nn
import torch.nn.init as init
from torch.utils.data import Dataset
import torch.nn.functional as F
import torchvision
from torch.autograd import Variable
from pprint import pprint
import time, math, os, sys, copy, numpy as np, shutil as sh
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
from collections import OrderedDict
import glob
from PIL import Image
import pickle
def _weights_init(m):
if isinstance(m, (nn.Conv2d, nn.Linear)):
init.kaiming_normal(m.weight)
if m.bias is not None:
m.bias.data.fill_(0)
elif isinstance(m, nn.BatchNorm2d):
if m.weight is not None:
m.weight.data.fill_(1.0)
m.bias.data.zero_()
def _weights_init_orthogonal(m, act='relu'):
if isinstance(m, (nn.Conv2d, nn.Linear)):
init.orthogonal_(m.weight, gain=init.calculate_gain(act))
if m.bias is not None:
m.bias.data.fill_(0)
elif isinstance(m, nn.BatchNorm2d):
if m.weight is not None:
m.weight.data.fill_(1.0)
m.bias.data.zero_()
# Modify the orthogonal initialization
# refer to: https://pytorch.org/docs/stable/_modules/torch/nn/init.html#orthogonal_
def orthogonalize_weights(tensor, act):
r"""Fills the input `Tensor` with a (semi) orthogonal matrix, as
described in `Exact solutions to the nonlinear dynamics of learning in deep
linear neural networks` - Saxe, A. et al. (2013). The input tensor must have
at least 2 dimensions, and for tensors with more than 2 dimensions the
trailing dimensions are flattened.
Args:
tensor: an n-dimensional `torch.Tensor`, where :math:`n \geq 2`
gain: optional scaling factor
Examples:
>>> w = torch.empty(3, 5)
>>> nn.init.orthogonal_(w)
"""
tensor = tensor.clone().detach() # @mst: avoid modifying the original tensor
gain = init.calculate_gain(act)
if tensor.ndimension() < 2:
raise ValueError("Only tensors with 2 or more dimensions are supported")
rows = tensor.size(0)
cols = tensor.numel() // rows
# flattened = tensor.new(rows, cols).normal_(0, 1)
flattened = tensor.view(rows, cols) # @mst: do NOT reinit the tensor
if rows < cols:
flattened.t_()
# Compute the qr factorization
q, r = torch.qr(flattened) # q: (rows, cols), r: (cols, cols) if rows > cols.
# Make Q uniform according to https://arxiv.org/pdf/math-ph/0609050.pdf
d = torch.diag(r, 0) # d: (cols)
ph = d.sign()
q *= ph # (rows, cols) * (cols) => (rows, cols)
if rows < cols:
q.t_()
with torch.no_grad():
tensor.view_as(q).copy_(q)
tensor.mul_(gain)
return tensor
# refer to: https://github.com/JiJingYu/delta_orthogonal_init_pytorch/blob/master/demo.py
def genOrthgonal(dim):
a = torch.zeros((dim, dim)).normal_(0, 1)
q, r = torch.qr(a)
d = torch.diag(r, 0).sign()
diag_size = d.size(0)
d_exp = d.view(1, diag_size).expand(diag_size, diag_size)
q.mul_(d_exp)
return q
def delta_orthogonalize_weights(weights, act):
weights = copy.deepcopy(weights)
gain = init.calculate_gain(act)
rows = weights.size(0)
cols = weights.size(1)
if rows > cols:
print("In_filters should not be greater than out_filters.")
weights.data.fill_(0)
dim = max(rows, cols)
q = genOrthgonal(dim)
mid1 = weights.size(2) // 2
mid2 = weights.size(3) // 2
weights[:, :, mid1, mid2] = q[:weights.size(0), :weights.size(1)]
weights.mul_(gain)
return weights
# refer to: https://github.com/Eric-mingjie/rethinking-network-pruning/blob/master/imagenet/l1-norm-pruning/compute_flops.py
def get_n_params(model):
total = sum([param.nelement() if param.requires_grad else 0 for param in model.parameters()])
total /= 1e6
return total
# The above 'get_n_params' requires 'param.requires_grad' to be true. In KD, for the teacher, this is not the case.
def get_n_params_(model):
n_params = 0
for _, module in model.named_modules():
if isinstance(module, nn.Conv2d) or isinstance(module, nn.Linear): # only consider Conv2d and Linear, no BN
n_params += module.weight.numel()
if hasattr(module, 'bias') and type(module.bias) != type(None):
n_params += module.bias.numel()
return n_params
def get_n_flops(model=None, input_res=224, multiply_adds=True, n_channel=3):
model = copy.deepcopy(model)
prods = {}
def save_hook(name):
def hook_per(self, input, output):
prods[name] = np.prod(input[0].shape)
return hook_per
list_1=[]
def simple_hook(self, input, output):
list_1.append(np.prod(input[0].shape))
list_2={}
def simple_hook2(self, input, output):
list_2['names'] = np.prod(input[0].shape)
list_conv=[]
def conv_hook(self, input, output):
batch_size, input_channels, input_height, input_width = input[0].size()
output_channels, output_height, output_width = output[0].size()
kernel_ops = self.kernel_size[0] * self.kernel_size[1] * (self.in_channels / self.groups)
bias_ops = 0 if self.bias is not None else 0
# params = output_channels * (kernel_ops + bias_ops) # @mst: commented since not used
# flops = (kernel_ops * (2 if multiply_adds else 1) + bias_ops) * output_channels * output_height * output_width * batch_size
num_weight_params = (self.weight.data != 0).float().sum() # @mst: this should be considering the pruned model
# could be problematic if some weights happen to be 0.
flops = (num_weight_params * (2 if multiply_adds else 1) + bias_ops * output_channels) * output_height * output_width * batch_size
list_conv.append(flops)
list_linear=[]
def linear_hook(self, input, output):
batch_size = input[0].size(0) if input[0].dim() == 2 else 1
weight_ops = self.weight.nelement() * (2 if multiply_adds else 1)
bias_ops = self.bias.nelement()
flops = batch_size * (weight_ops + bias_ops)
list_linear.append(flops)
list_bn=[]
def bn_hook(self, input, output):
list_bn.append(input[0].nelement() * 2)
list_relu=[]
def relu_hook(self, input, output):
list_relu.append(input[0].nelement())
list_pooling=[]
def pooling_hook(self, input, output):
batch_size, input_channels, input_height, input_width = input[0].size()
output_channels, output_height, output_width = output[0].size()
kernel_ops = self.kernel_size * self.kernel_size
bias_ops = 0
params = 0
flops = (kernel_ops + bias_ops) * output_channels * output_height * output_width * batch_size
list_pooling.append(flops)
list_upsample=[]
# For bilinear upsample
def upsample_hook(self, input, output):
batch_size, input_channels, input_height, input_width = input[0].size()
output_channels, output_height, output_width = output[0].size()
flops = output_height * output_width * output_channels * batch_size * 12
list_upsample.append(flops)
def foo(net):
childrens = list(net.children())
if not childrens:
if isinstance(net, torch.nn.Conv2d):
net.register_forward_hook(conv_hook)
if isinstance(net, torch.nn.Linear):
net.register_forward_hook(linear_hook)
if isinstance(net, torch.nn.BatchNorm2d):
net.register_forward_hook(bn_hook)
if isinstance(net, torch.nn.ReLU):
net.register_forward_hook(relu_hook)
if isinstance(net, torch.nn.MaxPool2d) or isinstance(net, torch.nn.AvgPool2d):
net.register_forward_hook(pooling_hook)
if isinstance(net, torch.nn.Upsample):
net.register_forward_hook(upsample_hook)
return
for c in childrens:
foo(c)
if model == None:
model = torchvision.models.alexnet()
foo(model)
input = Variable(torch.rand(n_channel,input_res,input_res).unsqueeze(0), requires_grad = True)
out = model(input)
total_flops = (sum(list_conv) + sum(list_linear)) # + sum(list_bn) + sum(list_relu) + sum(list_pooling) + sum(list_upsample))
total_flops /= 1e9
# print(' Number of FLOPs: %.2fG' % total_flops)
return total_flops
# The above version is redundant. Get a neat version as follow.
def get_n_flops_(model=None, img_size=(224,224), n_channel=3, count_adds=True, idx_scale=None):
'''Only count the FLOPs of conv and linear layers (no BN layers etc.).
Only count the weight computation (bias not included since it is negligible)
'''
if hasattr(img_size, '__len__'):
height, width = img_size
else:
assert isinstance(img_size, int)
height, width = img_size, img_size
model = copy.deepcopy(model)
list_conv = []
def conv_hook(self, input, output):
flops = np.prod(self.weight.data.shape) * output.size(2) * output.size(3) / self.groups
list_conv.append(flops)
list_linear = []
def linear_hook(self, input, output):
flops = np.prod(self.weight.data.shape)
list_linear.append(flops)
def register_hooks(net):
childrens = list(net.children())
if not childrens:
if isinstance(net, torch.nn.Conv2d):
net.register_forward_hook(conv_hook)
if isinstance(net, torch.nn.Linear):
net.register_forward_hook(linear_hook)
return
for c in childrens:
register_hooks(c)
register_hooks(model)
use_cuda = next(model.parameters()).is_cuda
input = torch.rand(1, n_channel, height, width)
if use_cuda:
input = input.cuda()
# forward
try:
model(input)
except:
model(input, {'idx_scale': idx_scale})
# @mst (TODO): for SR network, there may be an extra argument for scale. Here set it to 2 to make it run normally.
# -- An ugly solution. Probably will be improved later.
total_flops = (sum(list_conv) + sum(list_linear))
if count_adds:
total_flops *= 2
return total_flops
# refer to: https://github.com/alecwangcq/EigenDamage-Pytorch/blob/master/utils/common_utils.py
class PresetLRScheduler(object):
"""Using a manually designed learning rate schedule rules.
"""
def __init__(self, decay_schedule):
# decay_schedule is a dictionary
# which is for specifying iteration -> lr
self.decay_schedule = {}
for k, v in decay_schedule.items(): # a dict, example: {"0":0.001, "30":0.00001, "45":0.000001}
self.decay_schedule[int(k)] = v
# print('Using a preset learning rate schedule:')
# print(self.decay_schedule)
def __call__(self, optimizer, e):
epochs = list(self.decay_schedule.keys())
epochs = sorted(epochs) # example: [0, 30, 45]
lr = self.decay_schedule[epochs[-1]]
for i in range(len(epochs) - 1):
if epochs[i] <= e < epochs[i+1]:
lr = self.decay_schedule[epochs[i]]
break
for param_group in optimizer.param_groups:
param_group['lr'] = lr
return lr
def get_lr(optimizer):
for param_group in optimizer.param_groups:
lr = param_group['lr']
return lr
def plot_weights_heatmap(weights, out_path):
'''
weights: [N, C, H, W]. Torch tensor
averaged in dim H, W so that we get a 2-dim color map of size [N, C]
'''
w_abs = weights.abs()
w_abs = w_abs.data.cpu().numpy()
fig, ax = plt.subplots()
im = ax.imshow(w_abs, cmap='jet')
# make a beautiful colorbar
divider = make_axes_locatable(ax)
cax = divider.append_axes('right', size=0.05, pad=0.05)
fig.colorbar(im, cax=cax, orientation='vertical')
ax.set_xlabel("Channel")
ax.set_ylabel("Filter")
fig.savefig(out_path, dpi=200)
plt.close(fig)
def strlist_to_list(sstr, ttype):
'''
example:
# self.args.stage_pr = [0, 0.3, 0.3, 0.3, 0, ]
# self.args.skip_layers = ['1.0', '2.0', '2.3', '3.0', '3.5', ]
turn these into a list of <ttype> (float or str or int etc.)
'''
if not sstr:
return sstr
out = []
sstr = sstr.strip()
if sstr.startswith('[') and sstr.endswith(']'):
sstr = sstr[1:-1]
for x in sstr.split(','):
x = x.strip()
if x:
x = ttype(x)
out.append(x)
return out
def strdict_to_dict(sstr, ttype):
'''
'{"1": 0.04, "2": 0.04, "4": 0.03, "5": 0.02, "7": 0.03, }'
'''
if not sstr:
return sstr
out = {}
sstr = sstr.strip()
if sstr.startswith('{') and sstr.endswith('}'):
sstr = sstr[1:-1]
for x in sstr.split(','):
x = x.strip()
if x:
k = x.split(':')[0]
v = ttype(x.split(':')[1].strip())
out[k] = v
return out
def check_path(x):
if x:
complete_path = glob.glob(x)
assert(len(complete_path) == 1)
x = complete_path[0]
return x
def parse_prune_ratio_vgg(sstr, num_layers=20):
# example: [0-4:0.5, 5:0.6, 8-10:0.2]
out = np.zeros(num_layers)
if '[' in sstr:
sstr = sstr.split("[")[1].split("]")[0]
else:
sstr = sstr.strip()
for x in sstr.split(','):
k = x.split(":")[0].strip()
v = x.split(":")[1].strip()
if k.isdigit():
out[int(k)] = float(v)
else:
begin = int(k.split('-')[0].strip())
end = int(k.split('-')[1].strip())
out[begin : end+1] = float(v)
return list(out)
def kronecker(A, B):
return torch.einsum("ab,cd->acbd", A, B).view(A.size(0) * B.size(0), A.size(1) * B.size(1))
def np_to_torch(x):
'''
np array to pytorch float tensor
'''
x = np.array(x)
x= torch.from_numpy(x).float()
return x
def kd_loss(student_scores, teacher_scores, temp=1, weights=None):
'''Knowledge distillation loss: soft target
'''
p = F.log_softmax(student_scores / temp, dim=1)
q = F.softmax(teacher_scores / temp, dim=1)
# l_kl = F.kl_div(p, q, size_average=False) / student_scores.shape[0] # previous working loss
if isinstance(weights, type(None)):
l_kl = F.kl_div(p, q, reduction='batchmean') # 2020-06-21 @mst: Since 'size_average' is deprecated, use 'reduction' instead.
else:
l_kl = (F.kl_div(p, q, reduction='none').sum(dim=1) * weights).sum()
return l_kl
def test(net, test_loader):
n_example_test = 0
total_correct = 0
avg_loss = 0
is_train = net.training
net.eval()
with torch.no_grad():
pred_total = []
label_total = []
for _, (images, labels) in enumerate(test_loader):
n_example_test += images.size(0)
images = images.cuda()
labels = labels.cuda()
output = net(images)
avg_loss += nn.CrossEntropyLoss()(output, labels).sum()
pred = output.data.max(1)[1]
total_correct += pred.eq(labels.data.view_as(pred)).sum()
pred_total.extend(list(pred.data.cpu().numpy()))
label_total.extend(list(labels.data.cpu().numpy()))
acc = float(total_correct) / n_example_test
avg_loss /= n_example_test
# get accuracy per class
n_class = output.size(1)
acc_test = [0] * n_class
cnt_test = [0] * n_class
for p, l in zip(pred_total, label_total):
acc_test[l] += int(p == l)
cnt_test[l] += 1
acc_per_class = []
for c in range(n_class):
acc_test[c] = 0 if cnt_test[c] == 0 else acc_test[c] / float(cnt_test[c])
acc_per_class.append(acc_test[c])
# return to the train state if necessary
if is_train:
net.train()
return acc, avg_loss.item(), acc_per_class
def get_project_path(ExpID):
full_path = glob.glob("Experiments/*%s*" % ExpID)
assert(len(full_path) == 1) # There should be only ONE folder with <ExpID> in its name.
return full_path[0]
def parse_ExpID(path):
'''parse out the ExpID from 'path', which can be a file or directory.
Example: Experiments/AE__ckpt_epoch_240.pth__LR1.5__originallabel__vgg13_SERVER138-20200829-202307/gen_img
Example: Experiments/AE__ckpt_epoch_240.pth__LR1.5__originallabel__vgg13_SERVER-20200829-202307/gen_img
'''
return 'SERVER' + path.split('_SERVER')[1].split('/')[0]
def mkdirs(*paths):
for p in paths:
if not os.path.exists(p):
os.makedirs(p)
class EMA():
'''
Exponential Moving Average for pytorch tensor
'''
def __init__(self, mu):
self.mu = mu
self.history = {}
def __call__(self, name, x):
'''
Note: this func will modify x directly, no return value.
x is supposed to be a pytorch tensor.
'''
if self.mu > 0:
assert(0 < self.mu < 1)
if name in self.history.keys():
new_average = self.mu * self.history[name] + (1.0 - self.mu) * x.clone()
else:
new_average = x.clone()
self.history[name] = new_average.clone()
return new_average.clone()
else:
return x.clone()
# Exponential Moving Average
class EMA2():
def __init__(self, mu):
self.mu = mu
self.shadow = {}
def register(self, name, value):
self.shadow[name] = value.clone()
def __call__(self, name, x):
assert name in self.shadow
new_average = (1.0 - self.mu) * x + self.mu * self.shadow[name]
self.shadow[name] = new_average.clone()
return new_average
def register_ema(emas):
for net, ema in emas:
for name, param in net.named_parameters():
if param.requires_grad:
ema.register(name, param.data)
def apply_ema(emas):
for net, ema in emas:
for name, param in net.named_parameters():
if param.requires_grad:
param.data = ema(name, param.data)
colors = ["gray", "blue", "black", "yellow", "green", "yellowgreen", "gold", "royalblue", "peru", "purple"]
def feat_visualize(ax, feat, label):
'''
feat: N x 2 # 2-d feature, N: number of examples
label: N x 1
'''
for ix in range(len(label)):
x = feat[ix]
y = label[ix]
ax.scatter(x[0], x[1], color=colors[y], marker=".")
return ax
def _remove_module_in_name(name):
''' remove 'module.' in the module name, caused by DataParallel, if any
'''
module_name_parts = name.split(".")
module_name_parts_new = []
for x in module_name_parts:
if x != 'module':
module_name_parts_new.append(x)
new_name = '.'.join(module_name_parts_new)
return new_name
def smart_weights_load(net, w_path, key=None, load_mode='exact'):
'''
This func is to load the weights of <w_path> into <net>.
'''
common_weights_keys = ['T', 'S', 'G', 'model', 'state_dict', 'state_dict_t']
ckpt = torch.load(w_path, map_location=lambda storage, location: storage)
# get state_dict
if isinstance(ckpt, OrderedDict):
state_dict = ckpt
else:
if key:
state_dict = ckpt[key]
else:
intersection = [k for k in ckpt.keys() if k in common_weights_keys and isinstance(ckpt[k], OrderedDict)]
if len(intersection) == 1:
k = intersection[0]
state_dict = ckpt[k]
else:
print('Error: multiple or no model keys found in ckpt: %s. Please explicitly appoint one' % intersection)
exit(1)
if load_mode == 'exact': # net and state_dict have exactly the same architecture (layer names etc. are exactly same)
try:
net.load_state_dict(state_dict)
except:
ckpt_data_parallel = False
for k, v in state_dict.items():
if k.startswith('module.'):
ckpt_data_parallel = True # DataParallel was used in the ckpt
break
if ckpt_data_parallel:
# If ckpt used DataParallel, then the reason of the load failure above should be that the <net> does not use
# DataParallel. Therefore, remove the surfix 'module.' in ckpt.
new_state_dict = OrderedDict()
for k, v in state_dict.items():
param_name = k.split("module.")[-1]
new_state_dict[param_name] = v
else:
# Similarly, if ckpt didn't use DataParallel, here we add the surfix 'module.'.
new_state_dict = OrderedDict()
for k, v in state_dict.items():
param_name = 'module.' + k
new_state_dict[param_name] = v
net.load_state_dict(new_state_dict)
else:
# Here is the case that <net> and ckpt only have part of weights in common. Then load them by module name:
# for every named module in <net>, if ckpt has a module of the same (or contextually similar) name, then they are matched and weights are loaded from ckpt to <net>.
for name, m in net.named_modules():
print(name)
for name, m in net.named_modules():
if name:
print('loading weights for module "%s" in the network' % name)
new_name = _remove_module_in_name(name)
# find the matched module name
matched_param_name = ''
for k in ckpt.keys():
new_k = _remove_module_in_name(k)
if new_name == new_k:
matched_param_name = k
break
# load weights
if matched_param_name:
m.weight.copy_(ckpt[matched_param_name])
print("net module name: '%s' <- '%s' (ckpt module name)" % (name, matched_param_name))
else:
print("Error: cannot find matched module in ckpt for module '%s' in net. Please check manually." % name)
exit(1)
# parse wanted value from accuracy print log
def parse_acc_log(line, key, type_func=float):
line_seg = line.strip().lower().split()
for i in range(len(line_seg)):
if key in line_seg[i]:
break
if i == len(line_seg) - 1:
return None # did not find the <key> in this line
try:
value = type_func(line_seg[i+1])
except:
value = type_func(line_seg[i+2])
return value
def get_layer_by_index(net, index):
cnt = -1
for _, m in net.named_modules():
if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
cnt += 1
if cnt == index:
return m
return None
def get_total_index_by_learnable_index(net, learnable_index):
'''
learnable_index: index when only counting learnable layers (conv or fc, no bn);
total_index: count relu, pooling etc in.
'''
layer_type_considered = [nn.Conv2d, nn.ReLU, nn.LeakyReLU, nn.PReLU,
nn.BatchNorm2d, nn.MaxPool2d, nn.AvgPool2d, nn.Linear]
cnt_total = -1
cnt_learnable = -1
for _, m in net.named_modules():
cond = [isinstance(m, x) for x in layer_type_considered]
if any(cond):
cnt_total += 1
if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
cnt_learnable += 1
if cnt_learnable == learnable_index:
return cnt_total
return None
def cal_correlation(x, coef=False):
'''Calculate the correlation matrix for a pytorch tensor.
Input shape: [n_sample, n_attr]
Output shape: [n_attr, n_attr]
Refer to: https://github.com/pytorch/pytorch/issues/1254
'''
# calculate covariance matrix
y = x - x.mean(dim=0)
c = y.t().mm(y) / (y.size(0) - 1)
if coef:
# normalize covariance matrix
d = torch.diag(c)
stddev = torch.pow(d, 0.5)
c = c.div(stddev.expand_as(c))
c = c.div(stddev.expand_as(c).t())
# clamp between -1 and 1
# probably not necessary but numpy does it
c = torch.clamp(c, -1.0, 1.0)
return c
def get_class_corr(loader, model):
model.eval().cuda()
logits = 0
n_batch = len(loader)
with torch.no_grad():
for ix, data in enumerate(loader):
input = data[0]
print('[%d/%d] -- forwarding' % (ix, n_batch))
input = input.float().cuda()
if type(logits) == int:
logits = model(input) # [batch_size, n_class]
else:
logits = torch.cat([logits, model(input)], dim=0)
# Use numpy:
# logits -= logits.mean(dim=0)
# logits = logits.data.cpu().numpy()
# corr = np.corrcoef(logits, rowvar=False)
# Use pytorch
corr = cal_correlation(logits, coef=True)
return corr
def cal_acc(logits, y):
pred = logits.argmax(dim=1)
acc = pred.eq(y.data.view_as(pred)).sum().float() / y.size(0)
return acc
class Timer():
'''Log down iteration time and predict the left time for the left iterations
'''
def __init__(self, total_epoch):
self.total_epoch = total_epoch
self.time_stamp = []
def predict_finish_time(self, ave_window=3):
self.time_stamp.append(time.time()) # update time stamp
if len(self.time_stamp) == 1:
return 'only one time stamp, not enough to predict'
interval = []
for i in range(len(self.time_stamp) - 1):
t = self.time_stamp[i + 1] - self.time_stamp[i]
interval.append(t)
sec_per_epoch = np.mean(interval[-ave_window:])
left_t = sec_per_epoch * (self.total_epoch - len(interval))
finish_t = left_t + time.time()
finish_t = time.strftime('%Y/%m/%d-%H:%M', time.localtime(finish_t))
total_t = '%.2fh' % ((np.sum(interval) + left_t) / 3600.)
return finish_t + ' (speed: %.2fs per timing, total_time: %s)' % (sec_per_epoch, total_t)
def __call__(self):
return(self.predict_finish_time())
class Dataset_npy_batch(Dataset):
def __init__(self, npy_dir, transform, f='batch.npy'):
self.data = np.load(os.path.join(npy_dir, f), allow_pickle=True)
self.transform = transform
def __getitem__(self, index):
img = Image.fromarray(self.data[index][0])
img = self.transform(img)
label = self.data[index][1]
label = torch.LongTensor([label])[0]
return img.squeeze(0), label
def __len__(self):
return len(self.data)
class Dataset_lmdb_batch(Dataset):
'''Dataset to load a lmdb data file.
'''
def __init__(self, lmdb_path, transform):
import lmdb
env = lmdb.open(lmdb_path, readonly=True)
with env.begin() as txn:
self.data = [value for key, value in txn.cursor()]
self.transform = transform
def __getitem__(self, index):
img, label = pickle.loads(self.data[index]) # PIL image
if self.transform:
img = self.transform(img)
return img, label
def __len__(self):
return len(self.data)
def merge_args(args, params_json):
import json, yaml
'''<args> is from argparser. <params_json> is a json/yaml file.
merge them, if there is collision, the param in <params_json> has a higher priority.
'''
with open(params_json) as f:
if params_json.endswith('.json'):
params = json.load(f)
elif params_json.endswith('.yaml'):
params = yaml.load(f, Loader=yaml.FullLoader)
else:
raise NotImplementedError
for k, v in params.items():
args.__dict__[k] = v
return args
class AccuracyManager():
def __init__(self):
import pandas as pd
self.accuracy = pd.DataFrame()
def update(self, time, acc1, acc5=None):
acc = pd.DataFrame([[time, acc1, acc5]], columns=['time', 'acc1', 'acc5']) # time can be epoch or step
self.accuracy = self.accuracy.append(acc, ignore_index=True)
def get_best_acc(self, criterion='acc1'):
assert criterion in ['acc1', 'acc5']
acc = self.accuracy.sort_values(by=criterion) # ascending sort
best = acc.iloc[-1] # the last row
time, acc1, acc5 = best.time, best.acc1, best.acc5
return time, acc1, acc5
def get_last_acc(self):
last = self.accuracy.iloc[-1]
time, acc1, acc5 = last.time, last.acc1, last.acc5
return time, acc1, acc5
def format_acc_log(acc1_set, lr, acc5=None, time_unit='Epoch'):
'''return uniform format for the accuracy print
'''
acc1, acc1_time, acc1_best, acc1_best_time = acc1_set
if acc5:
line = 'Acc1 %.4f Acc5 %.4f @ %s %d (Best_Acc1 %.4f @ %s %d) LR %s' % (acc1, acc5, time_unit, acc1_time, acc1_best, time_unit, acc1_best_time, lr)
else:
line = 'Acc1 %.4f @ %s %d (Best_Acc1 %.4f @ %s %d) LR %s' % (acc1, time_unit, acc1_time, acc1_best, time_unit, acc1_best_time, lr)
return line
def get_lambda(alpha=1.0):
'''Return lambda'''
if alpha > 0.:
lam = np.random.beta(alpha, alpha)
else:
lam = 1.
return lam
# refer to: 2018-ICLR-mixup
# https://github.com/facebookresearch/mixup-cifar10/blob/eaff31ab397a90fbc0a4aac71fb5311144b3608b/train.py#L119
def mixup_data(x, y, alpha=1.0, use_cuda=True):
'''Returns mixed inputs, pairs of targets, and lambda'''
if alpha > 0:
lam = np.random.beta(alpha, alpha)
else:
lam = 1
batch_size = x.size()[0]
if use_cuda:
index = torch.randperm(batch_size).cuda()
else:
index = torch.randperm(batch_size)
mixed_x = lam * x + (1 - lam) * x[index, :]
y_a, y_b = y, y[index]
return mixed_x, y_a, y_b, lam
def mixup_criterion(criterion, pred, y_a, y_b, lam):
return lam * criterion(pred, y_a) + (1 - lam) * criterion(pred, y_b)
def visualize_filter(layer, layer_id, save_dir, n_filter_plot=16, n_channel_plot=16, pick_mode='rand', plot_abs=True, prefix='', ext='.pdf'):
'''layer is a pytorch model layer
'''
w = layer.weight.data.cpu().numpy() # shape: [N, C, H, W]
if plot_abs:
w = np.abs(w)
n, c = w.shape[0], w.shape[1]
n_filter_plot = min(n_filter_plot, n)
n_channel_plot = min(n_channel_plot, c)
if pick_mode == 'rand':
filter_ix = np.random.permutation(n)[:n_filter_plot] # filter indexes to plot
channel_ix = np.random.permutation(c)[:n_channel_plot] # channel indexes to plot
else:
filter_ix = list(range(n_filter_plot))
channel_ix = list(range(n_channel_plot))
# iteration for plotting
for i in filter_ix:
f_avg = np.mean(w[i], axis=0)
fig, ax = plt.subplots()
im = ax.imshow(f_avg, cmap='jet')
# make a beautiful colorbar
divider = make_axes_locatable(ax)
cax = divider.append_axes('right', size=0.05, pad=0.05)
fig.colorbar(im, cax=cax, orientation='vertical')
save_path = '%s/filter_visualize__%s__layer%s__filter%s__average_cross_channel' % (save_dir, prefix, layer_id, i) # prefix is usually a net name
fig.savefig(save_path + ext, bbox_inches='tight')
plt.close(fig)
for j in channel_ix:
f = w[i][j]
fig, ax = plt.subplots()
im = ax.imshow(f, cmap='jet')
# make a beautiful colorbar
divider = make_axes_locatable(ax)
cax = divider.append_axes('right', size=0.05, pad=0.05)
fig.colorbar(im, cax=cax, orientation='vertical')
save_path = '%s/filter_visualize__%s__layer%s__filter%s__channel%s' % (save_dir, prefix, layer_id, i, j)
fig.savefig(save_path + ext, bbox_inches='tight')
plt.close(fig)
def visualize_feature_map(fm, layer_id, save_dir, n_channel_plot=16, pick_mode='rand', plot_abs=True, prefix='', ext='.pdf'):
fm = fm.clone().detach()
fm = fm.data.cpu().numpy()[0] # shape: [N, C, H, W], N is batch size. Default: batch size should be 1
if plot_abs:
fm = np.abs(fm)
c = fm.shape[0]
n_channel_plot = min(n_channel_plot, c)
if pick_mode == 'rand':
channel_ix = np.random.permutation(c)[:n_channel_plot] # channel indexes to plot
else:
channel_ix = list(range(n_channel_plot))
# iteration for plotting
fm_avg = np.mean(fm, axis=0)
fig, ax = plt.subplots()
im = ax.imshow(fm_avg, cmap='jet')
# make a beautiful colorbar
divider = make_axes_locatable(ax)
cax = divider.append_axes('right', size=0.05, pad=0.05)
fig.colorbar(im, cax=cax, orientation='vertical')
save_path = '%s/featmap_visualization__%s__layer%s__average_cross_channel' % (save_dir, prefix, layer_id) # prefix is usually a net name
fig.savefig(save_path + ext, bbox_inches='tight')
plt.close(fig)
for j in channel_ix:
f = fm[j]
fig, ax = plt.subplots()
im = ax.imshow(f, cmap='jet')
# make a beautiful colorbar
divider = make_axes_locatable(ax)
cax = divider.append_axes('right', size=0.05, pad=0.05)
fig.colorbar(im, cax=cax, orientation='vertical')
save_path = '%s/featmap_visualization__%s__layer%s__channel%s' % (save_dir, prefix, layer_id, j)
fig.savefig(save_path + ext, bbox_inches='tight')
plt.close(fig)
def add_noise_to_model(model, std=0.01):
model = copy.deepcopy(model) # do not modify the original model
for name, module in model.named_modules():
if isinstance(module, (nn.Conv2d, nn.Linear, nn.BatchNorm2d)): # all learnable params for a typical DNN
w = module.weight
w.data += torch.randn_like(w) * std
return model
# Refer to: https://github.com/ast0414/adversarial-example/blob/26ee4144a1771d3a565285e0a631056a6f42d49c/craft.py#L6
def compute_jacobian(inputs, output):
"""
:param inputs: Batch X Size (e.g. Depth X Width X Height)
:param output: Batch X Classes
:return: jacobian: Batch X Classes X Size
"""
from torch.autograd.gradcheck import zero_gradients
assert inputs.requires_grad
num_classes = output.size()[1]
jacobian = torch.zeros(num_classes, *inputs.size())
grad_output = torch.zeros(*output.size())
if inputs.is_cuda:
grad_output = grad_output.cuda()
jacobian = jacobian.cuda()
for i in range(num_classes):
zero_gradients(inputs)
grad_output.zero_()
grad_output[:, i] = 1
output.backward(grad_output, retain_graph=True)
jacobian[i] = inputs.grad.data
return torch.transpose(jacobian, dim0=0, dim1=1)
def get_jacobian_singular_values(model, data_loader, num_classes, n_loop=20, print_func=print, rand_data=False):
jsv, condition_number = [], []
if rand_data:
picked_batch = np.random.permutation(len(data_loader))[:n_loop]
else:
picked_batch = list(range(n_loop))
for i, (images, target) in enumerate(data_loader):
if i in picked_batch:
images, target = images.cuda(), target.cuda()
batch_size = images.size(0)
images.requires_grad = True # for Jacobian computation
output = model(images)
jacobian = compute_jacobian(images, output) # shape [batch_size, num_classes, num_channels, input_width, input_height]
jacobian = jacobian.view(batch_size, num_classes, -1) # shape [batch_size, num_classes, num_channels*input_width*input_height]
u, s, v = torch.svd(jacobian) # u: [batch_size, num_channels*input_width*input_height, num_classes], s: [batch_size, num_classes], v: [batch_size, num_channels*input_width*input_height, num_classes]
s = s.data.cpu().numpy()
jsv.append(s)
condition_number.append(s.max(axis=1) / s.min(axis=1))
print_func('[%3d/%3d] calculating Jacobian...' % (i, len(data_loader)))
jsv = np.concatenate(jsv)
condition_number = np.concatenate(condition_number)
return jsv, condition_number
def approximate_entropy(X, num_bins=10, esp=1e-30):
'''X shape: [num_sample, n_var], numpy array.
'''
entropy = []
for di in range(X.shape[1]):
samples = X[:, di]
bins = np.linspace(samples.min(), samples.max(), num=num_bins+1)
prob = np.histogram(samples, bins=bins, density=False)[0] / len(samples)
entropy.append((-np.log2(prob + esp) * prob).sum()) # esp for numerical stability when prob = 0
return np.mean(entropy)