| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  | # copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  | # Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  | # you may not use this file except in compliance with the License. | 
					
						
							|  |  |  | # You may obtain a copy of the License at | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  | # | 
					
						
							|  |  |  | #    http://www.apache.org/licenses/LICENSE-2.0 | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  | # Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  | # distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  | # See the License for the specific language governing permissions and | 
					
						
							|  |  |  | # limitations under the License. | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | from __future__ import absolute_import | 
					
						
							|  |  |  | from __future__ import division | 
					
						
							|  |  |  | from __future__ import print_function | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  | import paddle | 
					
						
							|  |  |  | from paddle import nn | 
					
						
							|  |  |  | import paddle.nn.functional as F | 
					
						
							|  |  |  | from paddle import ParamAttr | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | __all__ = ['MobileNetV3'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  | def make_divisible(v, divisor=8, min_value=None): | 
					
						
							|  |  |  |     if min_value is None: | 
					
						
							|  |  |  |         min_value = divisor | 
					
						
							|  |  |  |     new_v = max(min_value, int(v + divisor / 2) // divisor * divisor) | 
					
						
							|  |  |  |     if new_v < 0.9 * v: | 
					
						
							|  |  |  |         new_v += divisor | 
					
						
							|  |  |  |     return new_v | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MobileNetV3(nn.Layer): | 
					
						
							| 
									
										
										
										
											2020-12-09 14:59:04 +08:00
										 |  |  |     def __init__(self, | 
					
						
							|  |  |  |                  in_channels=3, | 
					
						
							|  |  |  |                  model_name='large', | 
					
						
							|  |  |  |                  scale=0.5, | 
					
						
							|  |  |  |                  disable_se=False, | 
					
						
							|  |  |  |                  **kwargs): | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         the MobilenetV3 backbone network for detection module. | 
					
						
							|  |  |  |         Args: | 
					
						
							|  |  |  |             params(dict): the super parameters for build network | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |         super(MobileNetV3, self).__init__() | 
					
						
							| 
									
										
										
										
											2020-12-09 14:59:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.disable_se = disable_se | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  |         if model_name == "large": | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             cfg = [ | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  |                 # k, exp, c,  se,     nl,  s, | 
					
						
							|  |  |  |                 [3, 16, 16, False, 'relu', 1], | 
					
						
							|  |  |  |                 [3, 64, 24, False, 'relu', 2], | 
					
						
							|  |  |  |                 [3, 72, 24, False, 'relu', 1], | 
					
						
							|  |  |  |                 [5, 72, 40, True, 'relu', 2], | 
					
						
							|  |  |  |                 [5, 120, 40, True, 'relu', 1], | 
					
						
							|  |  |  |                 [5, 120, 40, True, 'relu', 1], | 
					
						
							| 
									
										
										
										
											2021-01-28 19:14:50 +08:00
										 |  |  |                 [3, 240, 80, False, 'hardswish', 2], | 
					
						
							|  |  |  |                 [3, 200, 80, False, 'hardswish', 1], | 
					
						
							|  |  |  |                 [3, 184, 80, False, 'hardswish', 1], | 
					
						
							|  |  |  |                 [3, 184, 80, False, 'hardswish', 1], | 
					
						
							|  |  |  |                 [3, 480, 112, True, 'hardswish', 1], | 
					
						
							|  |  |  |                 [3, 672, 112, True, 'hardswish', 1], | 
					
						
							|  |  |  |                 [5, 672, 160, True, 'hardswish', 2], | 
					
						
							|  |  |  |                 [5, 960, 160, True, 'hardswish', 1], | 
					
						
							|  |  |  |                 [5, 960, 160, True, 'hardswish', 1], | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  |             ] | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             cls_ch_squeeze = 960 | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  |         elif model_name == "small": | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             cfg = [ | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  |                 # k, exp, c,  se,     nl,  s, | 
					
						
							|  |  |  |                 [3, 16, 16, True, 'relu', 2], | 
					
						
							|  |  |  |                 [3, 72, 24, False, 'relu', 2], | 
					
						
							|  |  |  |                 [3, 88, 24, False, 'relu', 1], | 
					
						
							| 
									
										
										
										
											2021-01-28 19:14:50 +08:00
										 |  |  |                 [5, 96, 40, True, 'hardswish', 2], | 
					
						
							|  |  |  |                 [5, 240, 40, True, 'hardswish', 1], | 
					
						
							|  |  |  |                 [5, 240, 40, True, 'hardswish', 1], | 
					
						
							|  |  |  |                 [5, 120, 48, True, 'hardswish', 1], | 
					
						
							|  |  |  |                 [5, 144, 48, True, 'hardswish', 1], | 
					
						
							|  |  |  |                 [5, 288, 96, True, 'hardswish', 2], | 
					
						
							|  |  |  |                 [5, 576, 96, True, 'hardswish', 1], | 
					
						
							|  |  |  |                 [5, 576, 96, True, 'hardswish', 1], | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  |             ] | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             cls_ch_squeeze = 576 | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  |         else: | 
					
						
							|  |  |  |             raise NotImplementedError("mode[" + model_name + | 
					
						
							|  |  |  |                                       "_model] is not implemented!") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         supported_scale = [0.35, 0.5, 0.75, 1.0, 1.25] | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |         assert scale in supported_scale, \ | 
					
						
							|  |  |  |             "supported scale are {} but input scale is {}".format(supported_scale, scale) | 
					
						
							|  |  |  |         inplanes = 16 | 
					
						
							|  |  |  |         # conv1 | 
					
						
							|  |  |  |         self.conv = ConvBNLayer( | 
					
						
							|  |  |  |             in_channels=in_channels, | 
					
						
							|  |  |  |             out_channels=make_divisible(inplanes * scale), | 
					
						
							|  |  |  |             kernel_size=3, | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  |             stride=2, | 
					
						
							|  |  |  |             padding=1, | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             groups=1, | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  |             if_act=True, | 
					
						
							| 
									
										
										
										
											2021-06-02 08:31:57 +00:00
										 |  |  |             act='hardswish') | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.stages = [] | 
					
						
							|  |  |  |         self.out_channels = [] | 
					
						
							|  |  |  |         block_list = [] | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  |         i = 0 | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |         inplanes = make_divisible(inplanes * scale) | 
					
						
							|  |  |  |         for (k, exp, c, se, nl, s) in cfg: | 
					
						
							| 
									
										
										
										
											2020-12-09 20:26:40 +08:00
										 |  |  |             se = se and not self.disable_se | 
					
						
							| 
									
										
										
										
											2021-01-26 12:58:38 +08:00
										 |  |  |             start_idx = 2 if model_name == 'large' else 0 | 
					
						
							|  |  |  |             if s == 2 and i > start_idx: | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |                 self.out_channels.append(inplanes) | 
					
						
							|  |  |  |                 self.stages.append(nn.Sequential(*block_list)) | 
					
						
							|  |  |  |                 block_list = [] | 
					
						
							|  |  |  |             block_list.append( | 
					
						
							|  |  |  |                 ResidualUnit( | 
					
						
							|  |  |  |                     in_channels=inplanes, | 
					
						
							|  |  |  |                     mid_channels=make_divisible(scale * exp), | 
					
						
							|  |  |  |                     out_channels=make_divisible(scale * c), | 
					
						
							|  |  |  |                     kernel_size=k, | 
					
						
							|  |  |  |                     stride=s, | 
					
						
							|  |  |  |                     use_se=se, | 
					
						
							| 
									
										
										
										
											2021-06-02 08:31:57 +00:00
										 |  |  |                     act=nl)) | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             inplanes = make_divisible(scale * c) | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  |             i += 1 | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |         block_list.append( | 
					
						
							|  |  |  |             ConvBNLayer( | 
					
						
							|  |  |  |                 in_channels=inplanes, | 
					
						
							|  |  |  |                 out_channels=make_divisible(scale * cls_ch_squeeze), | 
					
						
							|  |  |  |                 kernel_size=1, | 
					
						
							|  |  |  |                 stride=1, | 
					
						
							|  |  |  |                 padding=0, | 
					
						
							|  |  |  |                 groups=1, | 
					
						
							|  |  |  |                 if_act=True, | 
					
						
							| 
									
										
										
										
											2021-06-02 08:31:57 +00:00
										 |  |  |                 act='hardswish')) | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |         self.stages.append(nn.Sequential(*block_list)) | 
					
						
							|  |  |  |         self.out_channels.append(make_divisible(scale * cls_ch_squeeze)) | 
					
						
							|  |  |  |         for i, stage in enumerate(self.stages): | 
					
						
							|  |  |  |             self.add_sublayer(sublayer=stage, name="stage{}".format(i)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def forward(self, x): | 
					
						
							|  |  |  |         x = self.conv(x) | 
					
						
							|  |  |  |         out_list = [] | 
					
						
							|  |  |  |         for stage in self.stages: | 
					
						
							|  |  |  |             x = stage(x) | 
					
						
							|  |  |  |             out_list.append(x) | 
					
						
							|  |  |  |         return out_list | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ConvBNLayer(nn.Layer): | 
					
						
							|  |  |  |     def __init__(self, | 
					
						
							|  |  |  |                  in_channels, | 
					
						
							|  |  |  |                  out_channels, | 
					
						
							|  |  |  |                  kernel_size, | 
					
						
							|  |  |  |                  stride, | 
					
						
							|  |  |  |                  padding, | 
					
						
							|  |  |  |                  groups=1, | 
					
						
							|  |  |  |                  if_act=True, | 
					
						
							| 
									
										
										
										
											2021-06-02 08:31:57 +00:00
										 |  |  |                  act=None): | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |         super(ConvBNLayer, self).__init__() | 
					
						
							|  |  |  |         self.if_act = if_act | 
					
						
							|  |  |  |         self.act = act | 
					
						
							| 
									
										
										
										
											2020-11-05 15:13:36 +08:00
										 |  |  |         self.conv = nn.Conv2D( | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             in_channels=in_channels, | 
					
						
							|  |  |  |             out_channels=out_channels, | 
					
						
							|  |  |  |             kernel_size=kernel_size, | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  |             stride=stride, | 
					
						
							|  |  |  |             padding=padding, | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             groups=groups, | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  |             bias_attr=False) | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 08:31:57 +00:00
										 |  |  |         self.bn = nn.BatchNorm(num_channels=out_channels, act=None) | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def forward(self, x): | 
					
						
							|  |  |  |         x = self.conv(x) | 
					
						
							|  |  |  |         x = self.bn(x) | 
					
						
							|  |  |  |         if self.if_act: | 
					
						
							|  |  |  |             if self.act == "relu": | 
					
						
							|  |  |  |                 x = F.relu(x) | 
					
						
							| 
									
										
										
										
											2021-01-28 19:14:50 +08:00
										 |  |  |             elif self.act == "hardswish": | 
					
						
							|  |  |  |                 x = F.hardswish(x) | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											2021-01-28 19:14:50 +08:00
										 |  |  |                 print("The activation function({}) is selected incorrectly.". | 
					
						
							|  |  |  |                       format(self.act)) | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |                 exit() | 
					
						
							|  |  |  |         return x | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ResidualUnit(nn.Layer): | 
					
						
							|  |  |  |     def __init__(self, | 
					
						
							|  |  |  |                  in_channels, | 
					
						
							|  |  |  |                  mid_channels, | 
					
						
							|  |  |  |                  out_channels, | 
					
						
							|  |  |  |                  kernel_size, | 
					
						
							|  |  |  |                  stride, | 
					
						
							|  |  |  |                  use_se, | 
					
						
							| 
									
										
										
										
											2021-06-02 08:31:57 +00:00
										 |  |  |                  act=None): | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |         super(ResidualUnit, self).__init__() | 
					
						
							|  |  |  |         self.if_shortcut = stride == 1 and in_channels == out_channels | 
					
						
							|  |  |  |         self.if_se = use_se | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.expand_conv = ConvBNLayer( | 
					
						
							|  |  |  |             in_channels=in_channels, | 
					
						
							|  |  |  |             out_channels=mid_channels, | 
					
						
							|  |  |  |             kernel_size=1, | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  |             stride=1, | 
					
						
							|  |  |  |             padding=0, | 
					
						
							|  |  |  |             if_act=True, | 
					
						
							| 
									
										
										
										
											2021-06-02 08:31:57 +00:00
										 |  |  |             act=act) | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |         self.bottleneck_conv = ConvBNLayer( | 
					
						
							|  |  |  |             in_channels=mid_channels, | 
					
						
							|  |  |  |             out_channels=mid_channels, | 
					
						
							|  |  |  |             kernel_size=kernel_size, | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  |             stride=stride, | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             padding=int((kernel_size - 1) // 2), | 
					
						
							|  |  |  |             groups=mid_channels, | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  |             if_act=True, | 
					
						
							| 
									
										
										
										
											2021-06-02 08:31:57 +00:00
										 |  |  |             act=act) | 
					
						
							| 
									
										
										
										
											2020-12-09 20:26:40 +08:00
										 |  |  |         if self.if_se: | 
					
						
							| 
									
										
										
										
											2021-06-02 08:31:57 +00:00
										 |  |  |             self.mid_se = SEModule(mid_channels) | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |         self.linear_conv = ConvBNLayer( | 
					
						
							|  |  |  |             in_channels=mid_channels, | 
					
						
							|  |  |  |             out_channels=out_channels, | 
					
						
							|  |  |  |             kernel_size=1, | 
					
						
							| 
									
										
										
										
											2020-05-10 16:26:57 +08:00
										 |  |  |             stride=1, | 
					
						
							|  |  |  |             padding=0, | 
					
						
							|  |  |  |             if_act=False, | 
					
						
							| 
									
										
										
										
											2021-06-02 08:31:57 +00:00
										 |  |  |             act=None) | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def forward(self, inputs): | 
					
						
							|  |  |  |         x = self.expand_conv(inputs) | 
					
						
							|  |  |  |         x = self.bottleneck_conv(x) | 
					
						
							| 
									
										
										
										
											2020-12-09 20:26:40 +08:00
										 |  |  |         if self.if_se: | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             x = self.mid_se(x) | 
					
						
							|  |  |  |         x = self.linear_conv(x) | 
					
						
							|  |  |  |         if self.if_shortcut: | 
					
						
							| 
									
										
										
										
											2020-11-05 15:13:36 +08:00
										 |  |  |             x = paddle.add(inputs, x) | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |         return x | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SEModule(nn.Layer): | 
					
						
							| 
									
										
										
										
											2021-06-02 08:31:57 +00:00
										 |  |  |     def __init__(self, in_channels, reduction=4): | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |         super(SEModule, self).__init__() | 
					
						
							| 
									
										
										
										
											2020-11-05 15:13:36 +08:00
										 |  |  |         self.avg_pool = nn.AdaptiveAvgPool2D(1) | 
					
						
							|  |  |  |         self.conv1 = nn.Conv2D( | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             in_channels=in_channels, | 
					
						
							|  |  |  |             out_channels=in_channels // reduction, | 
					
						
							|  |  |  |             kernel_size=1, | 
					
						
							|  |  |  |             stride=1, | 
					
						
							| 
									
										
										
										
											2021-06-02 08:31:57 +00:00
										 |  |  |             padding=0) | 
					
						
							| 
									
										
										
										
											2020-11-05 15:13:36 +08:00
										 |  |  |         self.conv2 = nn.Conv2D( | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             in_channels=in_channels // reduction, | 
					
						
							|  |  |  |             out_channels=in_channels, | 
					
						
							|  |  |  |             kernel_size=1, | 
					
						
							|  |  |  |             stride=1, | 
					
						
							| 
									
										
										
										
											2021-06-02 08:31:57 +00:00
										 |  |  |             padding=0) | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def forward(self, inputs): | 
					
						
							|  |  |  |         outputs = self.avg_pool(inputs) | 
					
						
							|  |  |  |         outputs = self.conv1(outputs) | 
					
						
							|  |  |  |         outputs = F.relu(outputs) | 
					
						
							|  |  |  |         outputs = self.conv2(outputs) | 
					
						
							| 
									
										
										
										
											2021-01-28 19:14:50 +08:00
										 |  |  |         outputs = F.hardsigmoid(outputs, slope=0.2, offset=0.5) | 
					
						
							| 
									
										
										
										
											2020-12-09 14:59:04 +08:00
										 |  |  |         return inputs * outputs |