forked from aztc/EtinyNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetinynet.py
196 lines (166 loc) · 8.31 KB
/
etinynet.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
from mxnet.gluon import nn
from mxnet.gluon.nn import BatchNorm
from mxnet.context import cpu
from mxnet.gluon.block import HybridBlock
from gluoncv.nn import ReLU6
import mxnet as mx
from gluoncv.model_zoo.ssd import get_ssd
from gluoncv.model_zoo import get_model
from gluoncv.nn.coder import MultiPerClassDecoder, NormalizedBoxCenterDecoder
from gluoncv.model_zoo.ssd.anchor import SSDAnchorGenerator
from mxnet import autograd
import numpy as np
class Clip(HybridBlock):
def __init__(self, **kwargs):
super(Clip, self).__init__(**kwargs)
def hybrid_forward(self, F, x):
return F.clip(x, -12, 12, name="clip")
# pylint: disable= too-many-arguments
def _add_conv(out, channels=1, kernel=1, stride=1, pad=0, norm=True, in_channels=0, quantized=True,
num_group=1, active=True, relu6=False, norm_layer=BatchNorm, norm_kwargs=None):
out.add(nn.Conv2D(channels, kernel, stride, pad, groups=num_group, use_bias=False,in_channels=in_channels))
if active:
out.add(ReLU6() if relu6 else nn.Activation('relu'))
if norm:
out.add(norm_layer(scale=True, center=True, **({} if norm_kwargs is None else norm_kwargs)))
class LinearBottleneck(nn.HybridBlock):
r"""LinearBottleneck used in MobileNetV2 model from the
`"Inverted Residuals and Linear Bottlenecks:
Mobile Networks for Classification, Detection and Segmentation"
<https://arxiv.org/abs/1801.04381>`_ paper.
Parameters
----------
in_channels : int
Number of input channels.
channels : int
Number of output channels.
t : int
Layer expansion ratio.
stride : int
stride
norm_layer : object
Normalization layer used (default: :class:`mxnet.gluon.nn.BatchNorm`)
Can be :class:`mxnet.gluon.nn.BatchNorm` or :class:`mxnet.gluon.contrib.nn.SyncBatchNorm`.
norm_kwargs : dict
Additional `norm_layer` arguments, for example `num_devices=4`
for :class:`mxnet.gluon.contrib.nn.SyncBatchNorm`.
"""
def __init__(self, channels, stride, shortcut,
norm_layer=BatchNorm, norm_kwargs=None, **kwargs):
super(LinearBottleneck, self).__init__(**kwargs)
self.use_shortcut1 = (stride == 1 and channels[0] == channels[1] and shortcut)
self.use_shortcut2 = shortcut
with self.name_scope():
self.out1 = nn.HybridSequential() # 1x1
self.out2 = nn.HybridSequential() # 1x1
_add_conv(self.out1,
in_channels=channels[0],
channels=channels[0],
kernel=3,
stride=stride,
pad=1,
num_group=channels[0],
active=False,
relu6=False,
norm_layer=norm_layer, norm_kwargs=norm_kwargs)
_add_conv(self.out1,
in_channels=channels[0],
channels=channels[1],
active=True,
relu6=False,
norm_layer=norm_layer, norm_kwargs=norm_kwargs)
_add_conv(self.out2,
in_channels=channels[1],
channels=channels[1],
kernel=3,
stride=1,
pad=1,
num_group=channels[1],
active=True,
relu6=False,
norm_layer=norm_layer, norm_kwargs=norm_kwargs)
def hybrid_forward(self, F, x):
out = self.out1(x)
if self.use_shortcut1:
out = F.elemwise_add(out, x)
x = out
out = self.out2(out)
if self.use_shortcut2:
out = F.elemwise_add(out, x)
return out
class Etinynet(nn.HybridBlock):
r"""MobileNetV2 model from the
`"Inverted Residuals and Linear Bottlenecks:
Mobile Networks for Classification, Detection and Segmentation"
<https://arxiv.org/abs/1801.04381>`_ paper.
Parameters
----------
multiplier : float, default 1.0
The width multiplier for controlling the model size. The actual number of channels
is equal to the original channel size multiplied by this multiplier.
classes : int, default 1000
Number of classes for the output layer.
norm_layer : object
Normalization layer used (default: :class:`mxnet.gluon.nn.BatchNorm`)
Can be :class:`mxnet.gluon.nn.BatchNorm` or :class:`mxnet.gluon.contrib.nn.SyncBatchNorm`.
norm_kwargs : dict
Additional `norm_layer` arguments, for example `num_devices=4`
for :class:`mxnet.gluon.contrib.nn.SyncBatchNorm`.
"""
def __init__(self, multiplier=1.0, classes=1000, norm_layer=BatchNorm, norm_kwargs=None,
ctx=cpu(), root='', pretrained=False, **kwargs):
super(Etinynet, self).__init__(**kwargs)
with self.name_scope():
self.features1 = nn.HybridSequential(prefix='features1_')
self.features2 = nn.HybridSequential(prefix='features2_')
self.features3 = nn.HybridSequential(prefix='features2_')
with self.features1.name_scope():
_add_conv(self.features1, int(32 * multiplier), kernel=3,
stride=(2,2), pad=1, relu6=False,in_channels=3, quantized=True,
norm_layer=norm_layer, norm_kwargs=norm_kwargs)
self.features1.add(nn.MaxPool2D((2,2)))
channels_group = [[32, 32], [32, 32], [32, 32], [32, 32],
[32, 128], [128, 128], [128, 128], [128, 128]]
strides = [1,1,1,1] + [2,1,1,1]
shortcuts = [0,0,0,0] + [0,0,0,0]
for cg, s, sc in zip(channels_group, strides, shortcuts):
self.features1.add(LinearBottleneck(channels=np.int32(np.array(cg)*multiplier),
stride=s,
shortcut=sc,
norm_layer=norm_layer,
norm_kwargs=norm_kwargs))
channels_group = [[128, 192], [192, 192], [192, 192]]
strides = [2,1,1]
shortcuts = [1,1,1]
for cg, s, sc in zip(channels_group, strides, shortcuts):
self.features2.add(LinearBottleneck(channels=np.int32(np.array(cg)*multiplier),
stride=s,
shortcut=sc,
norm_layer=norm_layer,
norm_kwargs=norm_kwargs))
channels_group = [[192, 256], [256, 256], [256, 512]]
strides = [2,1,1]
shortcuts = [1,1,1]
for cg, s, sc in zip(channels_group, strides, shortcuts):
self.features3.add(LinearBottleneck(channels=np.int32(np.array(cg)*multiplier),
stride=s,
shortcut=sc,
norm_layer=norm_layer,
norm_kwargs=norm_kwargs))
self.avg = nn.GlobalAvgPool2D()
self.output = nn.HybridSequential(prefix='output_')
with self.output.name_scope():
self.output.add(
nn.Conv2D(classes, 1, in_channels=int(512*multiplier),prefix='pred_'),
nn.Flatten())
def hybrid_forward(self, F, x):
x1 = self.features1(x)
x2 = self.features2(x1)
x3 = self.features3(x2)
x = self.avg(x3)
x = self.output(x)
return x
if __name__ == "__main__":
model = Etinynet()
model.initialize()
model.summary(mx.ndarray.zeros((1,3,256,256)))