-
Notifications
You must be signed in to change notification settings - Fork 11
/
conv_type.py
108 lines (84 loc) · 3.39 KB
/
conv_type.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
from torch.nn import init
import torch
import torch.autograd as autograd
import torch.nn as nn
import torch.nn.functional as F
import math
from args import args as parser_args
import numpy as np
DenseConv = nn.Conv2d
def sparseFunction(x, s, activation=torch.relu, f=torch.sigmoid):
return torch.sign(x)*activation(torch.abs(x)-f(s))
def initialize_sInit():
if parser_args.sInit_type == "constant":
return parser_args.sInit_value*torch.ones([1, 1])
class STRConv(nn.Conv2d):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.activation = torch.relu
if parser_args.sparse_function == 'sigmoid':
self.f = torch.sigmoid
self.sparseThreshold = nn.Parameter(initialize_sInit())
else:
self.sparseThreshold = nn.Parameter(initialize_sInit())
def forward(self, x):
# In case STR is not training for the hyperparameters given in the paper, change sparseWeight to self.sparseWeight if it is a problem of backprop.
# However, that should not be the case according to graph computation.
sparseWeight = sparseFunction(self.weight, self.sparseThreshold, self.activation, self.f)
x = F.conv2d(
x, sparseWeight, self.bias, self.stride, self.padding, self.dilation, self.groups
)
return x
def getSparsity(self, f=torch.sigmoid):
sparseWeight = sparseFunction(self.weight, self.sparseThreshold, self.activation, self.f)
temp = sparseWeight.detach().cpu()
temp[temp!=0] = 1
return (100 - temp.mean().item()*100), temp.numel(), f(self.sparseThreshold).item()
class ChooseEdges(autograd.Function):
@staticmethod
def forward(ctx, weight, prune_rate):
output = weight.clone()
_, idx = weight.flatten().abs().sort()
p = int(prune_rate * weight.numel())
# flat_oup and output access the same memory.
flat_oup = output.flatten()
flat_oup[idx[:p]] = 0
return output
@staticmethod
def backward(ctx, grad_output):
return grad_output, None
class DNWConv(nn.Conv2d):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def set_prune_rate(self, prune_rate):
self.prune_rate = prune_rate
print(f"=> Setting prune rate to {prune_rate}")
def forward(self, x):
w = ChooseEdges.apply(self.weight, self.prune_rate)
x = F.conv2d(
x, w, self.bias, self.stride, self.padding, self.dilation, self.groups
)
return x
def GMPChooseEdges(weight, prune_rate):
output = weight.clone()
_, idx = weight.flatten().abs().sort()
p = int(prune_rate * weight.numel())
# flat_oup and output access the same memory.
flat_oup = output.flatten()
flat_oup[idx[:p]] = 0
return output
class GMPConv(nn.Conv2d):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def set_prune_rate(self, prune_rate):
self.prune_rate = prune_rate
self.curr_prune_rate = 0.0
print(f"=> Setting prune rate to {prune_rate}")
def set_curr_prune_rate(self, curr_prune_rate):
self.curr_prune_rate = curr_prune_rate
def forward(self, x):
w = GMPChooseEdges(self.weight, self.curr_prune_rate)
x = F.conv2d(
x, w, self.bias, self.stride, self.padding, self.dilation, self.groups
)
return x