Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

#609 Ci fix

Merged
Ghost merged 1 commits into Deci-AI:master from deci-ai:bugfix/infra-000_ci
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
  1. """
  2. Creates a MobileNetV3 Model as defined in:
  3. Andrew Howard, Mark Sandler, Grace Chu, Liang-Chieh Chen, Bo Chen, Mingxing Tan, Weijun Wang, Yukun Zhu,
  4. Ruoming Pang, Vijay Vasudevan, Quoc V. Le, Hartwig Adam. (2019).
  5. Searching for MobileNetV3
  6. arXiv preprint arXiv:1905.02244.
  7. """
  8. import torch.nn as nn
  9. import math
  10. from super_gradients.training.models.classification_models.mobilenetv2 import MobileNetBase
  11. from super_gradients.training.utils import get_param
  12. def _make_divisible(v, divisor, min_value=None):
  13. """
  14. This function is taken from the original tf repo.
  15. It ensures that all layers have a channel number that is divisible by 8
  16. It can be seen here:
  17. https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py
  18. """
  19. if min_value is None:
  20. min_value = divisor
  21. new_v = max(min_value, int(v + divisor / 2) // divisor * divisor)
  22. # Make sure that round down does not go down by more than 10%.
  23. if new_v < 0.9 * v:
  24. new_v += divisor
  25. return new_v
  26. class h_sigmoid(nn.Module):
  27. def __init__(self, inplace=True):
  28. super(h_sigmoid, self).__init__()
  29. self.relu = nn.ReLU6(inplace=inplace)
  30. def forward(self, x):
  31. return self.relu(x + 3) / 6
  32. class h_swish(nn.Module):
  33. def __init__(self, inplace=True):
  34. super(h_swish, self).__init__()
  35. self.sigmoid = h_sigmoid(inplace=inplace)
  36. def forward(self, x):
  37. return x * self.sigmoid(x)
  38. class SELayer(nn.Module):
  39. def __init__(self, channel, reduction=4):
  40. super(SELayer, self).__init__()
  41. self.avg_pool = nn.AdaptiveAvgPool2d(1)
  42. self.fc = nn.Sequential(
  43. nn.Linear(channel, _make_divisible(channel // reduction, 8)),
  44. nn.ReLU(inplace=True),
  45. nn.Linear(_make_divisible(channel // reduction, 8), channel),
  46. h_sigmoid()
  47. )
  48. def forward(self, x):
  49. b, c, _, _ = x.size()
  50. y = self.avg_pool(x).view(b, c)
  51. y = self.fc(y).view(b, c, 1, 1)
  52. return x * y
  53. def conv_3x3_bn(inp, oup, stride):
  54. return nn.Sequential(
  55. nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
  56. nn.BatchNorm2d(oup),
  57. h_swish()
  58. )
  59. def conv_1x1_bn(inp, oup):
  60. return nn.Sequential(
  61. nn.Conv2d(inp, oup, 1, 1, 0, bias=False),
  62. nn.BatchNorm2d(oup),
  63. h_swish()
  64. )
  65. class InvertedResidual(nn.Module):
  66. def __init__(self, inp, hidden_dim, oup, kernel_size, stride, use_se, use_hs):
  67. super(InvertedResidual, self).__init__()
  68. assert stride in [1, 2]
  69. self.identity = stride == 1 and inp == oup
  70. if inp == hidden_dim:
  71. self.conv = nn.Sequential(
  72. # dw
  73. nn.Conv2d(hidden_dim, hidden_dim, kernel_size, stride, (kernel_size - 1) // 2, groups=hidden_dim,
  74. bias=False),
  75. nn.BatchNorm2d(hidden_dim),
  76. h_swish() if use_hs else nn.ReLU(inplace=True),
  77. # Squeeze-and-Excite
  78. SELayer(hidden_dim) if use_se else nn.Identity(),
  79. # pw-linear
  80. nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
  81. nn.BatchNorm2d(oup),
  82. )
  83. else:
  84. self.conv = nn.Sequential(
  85. # pw
  86. nn.Conv2d(inp, hidden_dim, 1, 1, 0, bias=False),
  87. nn.BatchNorm2d(hidden_dim),
  88. h_swish() if use_hs else nn.ReLU(inplace=True),
  89. # dw
  90. nn.Conv2d(hidden_dim, hidden_dim, kernel_size, stride, (kernel_size - 1) // 2, groups=hidden_dim,
  91. bias=False),
  92. nn.BatchNorm2d(hidden_dim),
  93. # Squeeze-and-Excite
  94. SELayer(hidden_dim) if use_se else nn.Identity(),
  95. h_swish() if use_hs else nn.ReLU(inplace=True),
  96. # pw-linear
  97. nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
  98. nn.BatchNorm2d(oup),
  99. )
  100. def forward(self, x):
  101. if self.identity:
  102. return x + self.conv(x)
  103. else:
  104. return self.conv(x)
  105. class MobileNetV3(MobileNetBase):
  106. def __init__(self, cfgs, mode, num_classes=1000, width_mult=1., in_channels=3):
  107. super(MobileNetV3, self).__init__()
  108. # setting of inverted residual blocks
  109. self.cfgs = cfgs
  110. assert mode in ['large', 'small']
  111. # building first layer
  112. curr_channels = _make_divisible(16 * width_mult, 8)
  113. layers = [conv_3x3_bn(in_channels, curr_channels, 2)]
  114. # building inverted residual blocks
  115. block = InvertedResidual
  116. for k, t, c, use_se, use_hs, s in self.cfgs:
  117. output_channel = _make_divisible(c * width_mult, 8)
  118. exp_size = _make_divisible(curr_channels * t, 8)
  119. layers.append(block(curr_channels, exp_size, output_channel, k, s, use_se, use_hs))
  120. curr_channels = output_channel
  121. self.features = nn.Sequential(*layers)
  122. # building last several layers
  123. self.conv = conv_1x1_bn(curr_channels, exp_size)
  124. self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
  125. output_channel = {'large': 1280, 'small': 1024}
  126. output_channel = _make_divisible(output_channel[mode] * width_mult, 8) if width_mult > 1.0 else output_channel[
  127. mode]
  128. self.classifier = nn.Sequential(
  129. nn.Linear(exp_size, output_channel),
  130. h_swish(),
  131. nn.Dropout(0.2),
  132. nn.Linear(output_channel, num_classes),
  133. )
  134. self._initialize_weights()
  135. def forward(self, x):
  136. x = self.features(x)
  137. x = self.conv(x)
  138. x = self.avgpool(x)
  139. x = x.view(x.size(0), -1)
  140. x = self.classifier(x)
  141. return x
  142. def _initialize_weights(self):
  143. for m in self.modules():
  144. if isinstance(m, nn.Conv2d):
  145. n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
  146. m.weight.data.normal_(0, math.sqrt(2. / n))
  147. if m.bias is not None:
  148. m.bias.data.zero_()
  149. elif isinstance(m, nn.BatchNorm2d):
  150. m.weight.data.fill_(1)
  151. m.bias.data.zero_()
  152. elif isinstance(m, nn.Linear):
  153. n = m.weight.size(1)
  154. m.weight.data.normal_(0, 0.01)
  155. m.bias.data.zero_()
  156. class mobilenetv3_large(MobileNetV3):
  157. """
  158. Constructs a MobileNetV3-Large model
  159. """
  160. def __init__(self, arch_params):
  161. width_mult = arch_params.width_mult if hasattr(arch_params, 'width_mult') else 1.
  162. cfgs = [
  163. # k, t, c, SE, HS, s
  164. [3, 1, 16, 0, 0, 1],
  165. [3, 4, 24, 0, 0, 2],
  166. [3, 3, 24, 0, 0, 1],
  167. [5, 3, 40, 1, 0, 2],
  168. [5, 3, 40, 1, 0, 1],
  169. [5, 3, 40, 1, 0, 1],
  170. [3, 6, 80, 0, 1, 2],
  171. [3, 2.5, 80, 0, 1, 1],
  172. [3, 2.3, 80, 0, 1, 1],
  173. [3, 2.3, 80, 0, 1, 1],
  174. [3, 6, 112, 1, 1, 1],
  175. [3, 6, 112, 1, 1, 1],
  176. [5, 6, 160, 1, 1, 2],
  177. [5, 6, 160, 1, 1, 1],
  178. [5, 6, 160, 1, 1, 1]
  179. ]
  180. super().__init__(cfgs, mode='large', num_classes=arch_params.num_classes, width_mult=width_mult,
  181. in_channels=get_param(arch_params, "in_channels", 3))
  182. class mobilenetv3_small(MobileNetV3):
  183. """
  184. Constructs a MobileNetV3-Small model
  185. """
  186. def __init__(self, arch_params):
  187. width_mult = arch_params.width_mult if hasattr(arch_params, 'width_mult') else 1.
  188. cfgs = [
  189. # k, t, c, SE, HS, s
  190. [3, 1, 16, 1, 0, 2],
  191. [3, 4.5, 24, 0, 0, 2],
  192. [3, 3.67, 24, 0, 0, 1],
  193. [5, 4, 40, 1, 1, 2],
  194. [5, 6, 40, 1, 1, 1],
  195. [5, 6, 40, 1, 1, 1],
  196. [5, 3, 48, 1, 1, 1],
  197. [5, 3, 48, 1, 1, 1],
  198. [5, 6, 96, 1, 1, 2],
  199. [5, 6, 96, 1, 1, 1],
  200. [5, 6, 96, 1, 1, 1],
  201. ]
  202. super().__init__(cfgs, mode='small', num_classes=arch_params.num_classes, width_mult=width_mult,
  203. in_channels=get_param(arch_params, "in_channels", 3))
  204. class mobilenetv3_custom(MobileNetV3):
  205. """
  206. Constructs a MobileNetV3-Customized model
  207. """
  208. def __init__(self, arch_params):
  209. super().__init__(cfgs=arch_params.structure, mode=arch_params.mode, num_classes=arch_params.num_classes,
  210. width_mult=arch_params.width_mult,
  211. in_channels=get_param(arch_params, "in_channels", 3))
Discard
Tip!

Press p or to see the previous file or, n or to see the next file