-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot.py
More file actions
51 lines (41 loc) · 1.37 KB
/
plot.py
File metadata and controls
51 lines (41 loc) · 1.37 KB
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
import numpy as np
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.nn.utils.prune as prune
import custom_model
plt.switch_backend('agg')
title = '139'
def add_prune_mask(model):
print('add prune mask to model')
for name, module in model.named_modules():
if isinstance(module, torch.nn.BatchNorm2d):
prune.l1_unstructured(module, name='weight', amount=0)
prune.l1_unstructured(module, name='bias', amount=0)
model = custom_model.cifar10_resnet56()
model = model.cuda()
add_prune_mask(model)
model.load_state_dict(torch.load('final_{}.pth'.format(title))['state_dict'])
total = 0
for name,m in model.named_modules():
if isinstance(m,nn.BatchNorm2d):
dt = m.weight_orig.view(-1)
total += dt.shape[0]
stat = torch.zeros(total)
index = 0
for name,m in model.named_modules():
if isinstance(m,nn.BatchNorm2d):
dt = m.weight_orig.view(-1).abs()
#print(m.weight)
size = dt.shape[0]
stat[index:index+size] = dt
index += size
stat = stat.cpu().detach().numpy()
print('num_parameters:',len(stat))
fig = plt.figure()
plt.ylim(0,300)
small = len(np.where(stat < 0.001)[0])
plt.hist(stat,100,range=[0,1])
plt.text(0.125,8000,'#para<0.001 = {}'.format(small))
plt.text(0.125,9000,'#para = {}'.format(len(stat)))
plt.savefig('distribution_margin_channel_{}'.format(title))