-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmobilenetv2_hyper.py
192 lines (158 loc) · 6.34 KB
/
mobilenetv2_hyper.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
'''MobileNetV2 in PyTorch.
See the paper "Inverted Residuals and Linear Bottlenecks:
Mobile Networks for Classification, Detection and Segmentation" for more details.
'''
import torch
import torch.nn as nn
import torch.nn.functional as F
from .gate_function import virtual_gate
class Block(nn.Module):
'''expand + depthwise + pointwise'''
def __init__(self, in_planes, out_planes, expansion, stride, norm_layer= nn.BatchNorm2d, cfg=None):
super(Block, self).__init__()
self.stride = stride
if cfg is None:
planes = expansion * in_planes
else:
planes = cfg
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, stride=1, padding=0, bias=False)
# self.bn1 = nn.BatchNorm2d(planes)
if norm_layer == nn.modules.normalization.GroupNorm:
self.bn1 = norm_layer(1,planes)
else:
self.bn1 = norm_layer(planes)
self.gate = virtual_gate(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, groups=planes, bias=False)
# self.bn2 = nn.BatchNorm2d(planes)
if norm_layer == nn.modules.normalization.GroupNorm:
self.bn2 = norm_layer(1,planes)
else:
self.bn2 = norm_layer(planes)
self.conv3 = nn.Conv2d(planes, out_planes, kernel_size=1, stride=1, padding=0, bias=False)
# self.bn3 = nn.BatchNorm2d(out_planes)
if norm_layer == nn.modules.normalization.GroupNorm:
self.bn3 = norm_layer(1,out_planes)
else:
self.bn3 = norm_layer(out_planes)
self.shortcut = nn.Sequential()
if stride == 1 and in_planes != out_planes:
if norm_layer == nn.modules.normalization.GroupNorm:
self.shortcut = nn.Sequential(
nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=1, padding=0, bias=False),
norm_layer(1, out_planes),
)
else:
# self.bn3 = norm_layer(out_planes)
self.shortcut = nn.Sequential(
nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(out_planes),
)
def forward(self, x):
out = self.bn1(self.conv1(x))
out = F.relu(out)
#out = F.leaky_relu(out)
out = self.gate(out)
out = self.bn2(self.conv2(out))
out = F.relu(out)
#out = F.leaky_relu(out)
out = self.gate(out)
out = self.bn3(self.conv3(out))
out = out + self.shortcut(x) if self.stride==1 else out
return out
default_cfg = [(1, 16, 1, 1),
(6, 24, 2, 1), # NOTE: change stride 2 -> 1 for CIFAR10
(6, 32, 3, 1),
(6, 64, 4, 2),
(6, 96, 3, 1),
(6, 160, 3, 2),
(6, 320, 1, 1)]
class MobileNetV2(nn.Module):
# (expansion, out_planes, num_blocks, stride)
def __init__(self, num_classes=10, norm_layer= nn.BatchNorm2d, cfg = None):
super(MobileNetV2, self).__init__()
# NOTE: change conv1 stride 2 -> 1 for CIFAR10
self.norm_layer = norm_layer
if cfg is None:
self.cfg = default_cfg
cfg_flag = False
else:
self.cfg = cfg
cfg_flag = True
self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1, bias=False)
# self.bn1 = nn.BatchNorm2d(32)
if norm_layer == nn.modules.normalization.GroupNorm:
self.bn1 = norm_layer(1,32)
else:
self.bn1 = norm_layer(32)
self.layers = self._make_layers(in_planes=32, custom_cfg=cfg_flag)
#self.gate = soft_gate(320)
self.conv2 = nn.Conv2d(320, 1280, kernel_size=1, stride=1, padding=0, bias=False)
# self.bn2 = nn.BatchNorm2d(1280)
if norm_layer == nn.modules.normalization.GroupNorm:
self.bn2 = norm_layer(1,1280)
else:
self.bn2 = norm_layer(1280)
self.linear = nn.Linear(1280, num_classes)
def _make_layers(self, in_planes, custom_cfg = None):
layers = []
if custom_cfg is False:
for expansion, out_planes, num_blocks, stride in self.cfg:
strides = [stride] + [1]*(num_blocks-1)
for stride in strides:
layers.append(Block(in_planes, out_planes, expansion, stride, norm_layer=self.norm_layer))
in_planes = out_planes
else:
for expansion, out_planes, num_blocks, stride, planes_list in self.cfg:
strides = [stride] + [1]*(num_blocks-1)
idx = 0
for stride in strides:
layers.append(Block(in_planes, out_planes, expansion, stride, cfg=planes_list[idx],norm_layer=self.norm_layer))
in_planes = out_planes
idx+=1
return nn.Sequential(*layers)
def forward(self, x, feature_out=False):
out = F.relu(self.bn1(self.conv1(x)))
out = self.layers(out)
out = F.relu(self.bn2(self.conv2(out)))
# NOTE: change pooling kernel_size 7 -> 4 for CIFAR10
#out = F.avg_pool2d(out, 4)
#print(out.size())
if feature_out:
feature = out
out = F.adaptive_avg_pool2d(out, 1)
out = out.view(out.size(0), -1)
out = self.linear(out)
if feature_out:
return out, feature
else:
return out
def count_structure(self):
structure = []
for m in self.modules():
if isinstance(m, virtual_gate):
structure.append(m.width)
self.structure = structure
return sum(structure), structure
def set_vritual_gate(self, arch_vector):
i = 0
start = 0
for m in self.modules():
if isinstance(m, virtual_gate):
end = start + self.structure[i]
m.set_structure_value(arch_vector.squeeze()[start:end])
start = end
i+=1
def get_gate_grads(self):
all_grad = []
for m in self.modules():
if isinstance(m, virtual_gate):
#print(m.weights.grad.data)
all_grad.append(m.get_grads().clone())
#print(all_grad[0])
return all_grad
def test():
net = MobileNetV2()
x = torch.randn(2,3,32,32)
y = net(x)
print(y.size())
#test()