-
Notifications
You must be signed in to change notification settings - Fork 1
/
shufflenetv2.py
244 lines (209 loc) · 8.79 KB
/
shufflenetv2.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
#!/usr/bin/env python
# type: ignore
"""Implementation of the pre-trained ShuffleNetv2 architectures using PyTorch and torchvision.
Contains:
- ShuffleNetv2_x0_5
- ShuffleNetv2_x1_0
- ShuffleNetv2_x1_5
- ShuffleNetv2_x2_0
"""
from types import SimpleNamespace
import torch.nn as nn
from torchvision import models
from torchfl.compatibility import ACTIVATION_FUNCTIONS_BY_NAME
class ShuffleNetv2_x0_5(models.ShuffleNetV2):
"""ShuffleNetv2_x0_5 base definition"""
def __init__(
self,
pre_trained=True,
feature_extract=True,
num_classes=10,
num_channels=3,
act_fn_name="relu",
**kwargs
) -> None:
"""Constructor
Args:
- pre_trained (bool, optional): Use the model pre-trained on the ImageNet dataset. Defaults to True.
- feature_extract (bool, optional): Only trains the sequential layers of the pre-trained model. If False, the entire model is finetuned. Defaults to True.
- num_classes (int, optional): Number of classification outputs. Defaults to 10.
- num_channels (int, optional): Number of incoming channels. Defaults to 3.
- act_fn_name (str, optional): Activation function to be used. Defaults to "relu". Accepted: ["tanh", "relu", "leakyrelu", "gelu"].
"""
super().__init__(
stages_repeats=[4, 8, 4],
stages_out_channels=[24, 48, 96, 192, 1024],
)
self.hparams = SimpleNamespace(
model_name="shufflenet_v2_x0_5",
pre_trained=pre_trained,
feature_extract=bool(pre_trained and feature_extract),
finetune=bool(not feature_extract),
quantized=False,
num_classes=num_classes,
num_channels=num_channels,
act_fn_name=act_fn_name,
act_fn=ACTIVATION_FUNCTIONS_BY_NAME[act_fn_name],
)
if pre_trained:
pretrained_model = models.shufflenet_v2_x0_5(
pretrained=True, progress=True
)
self.load_state_dict(pretrained_model.state_dict())
if feature_extract:
for param in self.parameters():
param.requires_grad = False
if num_channels != 3:
self.conv1 = nn.Sequential(
nn.Conv2d(num_channels, 24, 3, 2, 1, bias=False),
nn.BatchNorm2d(24),
nn.ReLU(inplace=True),
)
in_features = self.fc.in_features
self.fc = nn.Linear(in_features, self.hparams.num_classes)
class ShuffleNetv2_x1_0(models.ShuffleNetV2):
"""ShuffleNetv2_x1_0 base definition"""
def __init__(
self,
pre_trained=True,
feature_extract=True,
num_classes=10,
num_channels=3,
act_fn_name="relu",
**kwargs
) -> None:
"""Constructor
Args:
- pre_trained (bool, optional): Use the model pre-trained on the ImageNet dataset. Defaults to True.
- feature_extract (bool, optional): Only trains the sequential layers of the pre-trained model. If False, the entire model is finetuned. Defaults to True.
- num_classes (int, optional): Number of classification outputs. Defaults to 10.
- num_channels (int, optional): Number of incoming channels. Defaults to 3.
- act_fn_name (str, optional): Activation function to be used. Defaults to "relu". Accepted: ["tanh", "relu", "leakyrelu", "gelu"].
"""
super().__init__(
stages_repeats=[4, 8, 4],
stages_out_channels=[24, 116, 232, 464, 1024],
)
self.hparams = SimpleNamespace(
model_name="shufflenet_v2_x1_0",
pre_trained=pre_trained,
feature_extract=bool(pre_trained and feature_extract),
finetune=bool(not feature_extract),
quantized=False,
num_classes=num_classes,
num_channels=num_channels,
act_fn_name=act_fn_name,
act_fn=ACTIVATION_FUNCTIONS_BY_NAME[act_fn_name],
)
if pre_trained:
pretrained_model = models.shufflenet_v2_x1_0(
pretrained=True, progress=True
)
self.load_state_dict(pretrained_model.state_dict())
if feature_extract:
for param in self.parameters():
param.requires_grad = False
if num_channels != 3:
self.conv1 = nn.Sequential(
nn.Conv2d(num_channels, 24, 3, 2, 1, bias=False),
nn.BatchNorm2d(24),
nn.ReLU(inplace=True),
)
in_features = self.fc.in_features
self.fc = nn.Linear(in_features, self.hparams.num_classes)
class ShuffleNetv2_x1_5(models.ShuffleNetV2):
"""ShuffleNetv2_x1_5 base definition"""
def __init__(
self,
pre_trained=True,
feature_extract=True,
num_classes=10,
num_channels=3,
act_fn_name="relu",
**kwargs
) -> None:
"""Constructor
Args:
- pre_trained (bool, optional): Use the model pre-trained on the ImageNet dataset. Defaults to True.
- feature_extract (bool, optional): Only trains the sequential layers of the pre-trained model. If False, the entire model is finetuned. Defaults to True.
- num_classes (int, optional): Number of classification outputs. Defaults to 10.
- num_channels (int, optional): Number of incoming channels. Defaults to 3.
- act_fn_name (str, optional): Activation function to be used. Defaults to "relu". Accepted: ["tanh", "relu", "leakyrelu", "gelu"].
Raise:
- NotImplementedError: pretrained models not implemented.
"""
super().__init__(
stages_repeats=[4, 8, 4],
stages_out_channels=[24, 176, 352, 704, 1024],
)
self.hparams = SimpleNamespace(
model_name="shufflenet_v2_x1_5",
pre_trained=pre_trained,
feature_extract=bool(pre_trained and feature_extract),
finetune=bool(not feature_extract),
quantized=False,
num_classes=num_classes,
num_channels=num_channels,
act_fn_name=act_fn_name,
act_fn=ACTIVATION_FUNCTIONS_BY_NAME[act_fn_name],
)
if pre_trained:
raise NotImplementedError(
"pretrained shufflenetv2_x2.0 is not supported as of now"
)
if num_channels != 3:
self.conv1 = nn.Sequential(
nn.Conv2d(num_channels, 24, 3, 2, 1, bias=False),
nn.BatchNorm2d(24),
nn.ReLU(inplace=True),
)
in_features = self.fc.in_features
self.fc = nn.Linear(in_features, self.hparams.num_classes)
class ShuffleNetv2_x2_0(models.ShuffleNetV2):
"""ShuffleNetv2_x2_0 base definition"""
def __init__(
self,
pre_trained=True,
feature_extract=True,
num_classes=10,
num_channels=3,
act_fn_name="relu",
**kwargs
) -> None:
"""Constructor
Args:
- pre_trained (bool, optional): Use the model pre-trained on the ImageNet dataset. Defaults to True.
- feature_extract (bool, optional): Only trains the sequential layers of the pre-trained model. If False, the entire model is finetuned. Defaults to True.
- num_classes (int, optional): Number of classification outputs. Defaults to 10.
- num_channels (int, optional): Number of incoming channels. Defaults to 3.
- act_fn_name (str, optional): Activation function to be used. Defaults to "relu". Accepted: ["tanh", "relu", "leakyrelu", "gelu"].
Raise:
- NotImplementedError: pretrained models not implemented.
"""
super().__init__(
stages_repeats=[4, 8, 4],
stages_out_channels=[24, 244, 488, 976, 2048],
)
self.hparams = SimpleNamespace(
model_name="shufflenet_v2_x2_0",
pre_trained=pre_trained,
feature_extract=bool(pre_trained and feature_extract),
finetune=bool(not feature_extract),
quantized=False,
num_classes=num_classes,
num_channels=num_channels,
act_fn_name=act_fn_name,
act_fn=ACTIVATION_FUNCTIONS_BY_NAME[act_fn_name],
)
if pre_trained:
raise NotImplementedError(
"pretrained shufflenetv2_x2.0 is not supported as of now"
)
if num_channels != 3:
self.conv1 = nn.Sequential(
nn.Conv2d(num_channels, 24, 3, 2, 1, bias=False),
nn.BatchNorm2d(24),
nn.ReLU(inplace=True),
)
in_features = self.fc.in_features
self.fc = nn.Linear(in_features, self.hparams.num_classes)