You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

csp_layer.py 5.1 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import torch
  3. import torch.nn as nn
  4. from mmcv.cnn import ConvModule, DepthwiseSeparableConvModule
  5. from mmcv.runner import BaseModule
  6. class DarknetBottleneck(BaseModule):
  7. """The basic bottleneck block used in Darknet.
  8. Each ResBlock consists of two ConvModules and the input is added to the
  9. final output. Each ConvModule is composed of Conv, BN, and LeakyReLU.
  10. The first convLayer has filter size of 1x1 and the second one has the
  11. filter size of 3x3.
  12. Args:
  13. in_channels (int): The input channels of this Module.
  14. out_channels (int): The output channels of this Module.
  15. expansion (int): The kernel size of the convolution. Default: 0.5
  16. add_identity (bool): Whether to add identity to the out.
  17. Default: True
  18. use_depthwise (bool): Whether to use depthwise separable convolution.
  19. Default: False
  20. conv_cfg (dict): Config dict for convolution layer. Default: None,
  21. which means using conv2d.
  22. norm_cfg (dict): Config dict for normalization layer.
  23. Default: dict(type='BN').
  24. act_cfg (dict): Config dict for activation layer.
  25. Default: dict(type='Swish').
  26. """
  27. def __init__(self,
  28. in_channels,
  29. out_channels,
  30. expansion=0.5,
  31. add_identity=True,
  32. use_depthwise=False,
  33. conv_cfg=None,
  34. norm_cfg=dict(type='BN', momentum=0.03, eps=0.001),
  35. act_cfg=dict(type='Swish'),
  36. init_cfg=None):
  37. super().__init__(init_cfg)
  38. hidden_channels = int(out_channels * expansion)
  39. conv = DepthwiseSeparableConvModule if use_depthwise else ConvModule
  40. self.conv1 = ConvModule(
  41. in_channels,
  42. hidden_channels,
  43. 1,
  44. conv_cfg=conv_cfg,
  45. norm_cfg=norm_cfg,
  46. act_cfg=act_cfg)
  47. self.conv2 = conv(
  48. hidden_channels,
  49. out_channels,
  50. 3,
  51. stride=1,
  52. padding=1,
  53. conv_cfg=conv_cfg,
  54. norm_cfg=norm_cfg,
  55. act_cfg=act_cfg)
  56. self.add_identity = \
  57. add_identity and in_channels == out_channels
  58. def forward(self, x):
  59. identity = x
  60. out = self.conv1(x)
  61. out = self.conv2(out)
  62. if self.add_identity:
  63. return out + identity
  64. else:
  65. return out
  66. class CSPLayer(BaseModule):
  67. """Cross Stage Partial Layer.
  68. Args:
  69. in_channels (int): The input channels of the CSP layer.
  70. out_channels (int): The output channels of the CSP layer.
  71. expand_ratio (float): Ratio to adjust the number of channels of the
  72. hidden layer. Default: 0.5
  73. num_blocks (int): Number of blocks. Default: 1
  74. add_identity (bool): Whether to add identity in blocks.
  75. Default: True
  76. use_depthwise (bool): Whether to depthwise separable convolution in
  77. blocks. Default: False
  78. conv_cfg (dict, optional): Config dict for convolution layer.
  79. Default: None, which means using conv2d.
  80. norm_cfg (dict): Config dict for normalization layer.
  81. Default: dict(type='BN')
  82. act_cfg (dict): Config dict for activation layer.
  83. Default: dict(type='Swish')
  84. """
  85. def __init__(self,
  86. in_channels,
  87. out_channels,
  88. expand_ratio=0.5,
  89. num_blocks=1,
  90. add_identity=True,
  91. use_depthwise=False,
  92. conv_cfg=None,
  93. norm_cfg=dict(type='BN', momentum=0.03, eps=0.001),
  94. act_cfg=dict(type='Swish'),
  95. init_cfg=None):
  96. super().__init__(init_cfg)
  97. mid_channels = int(out_channels * expand_ratio)
  98. self.main_conv = ConvModule(
  99. in_channels,
  100. mid_channels,
  101. 1,
  102. conv_cfg=conv_cfg,
  103. norm_cfg=norm_cfg,
  104. act_cfg=act_cfg)
  105. self.short_conv = ConvModule(
  106. in_channels,
  107. mid_channels,
  108. 1,
  109. conv_cfg=conv_cfg,
  110. norm_cfg=norm_cfg,
  111. act_cfg=act_cfg)
  112. self.final_conv = ConvModule(
  113. 2 * mid_channels,
  114. out_channels,
  115. 1,
  116. conv_cfg=conv_cfg,
  117. norm_cfg=norm_cfg,
  118. act_cfg=act_cfg)
  119. self.blocks = nn.Sequential(*[
  120. DarknetBottleneck(
  121. mid_channels,
  122. mid_channels,
  123. 1.0,
  124. add_identity,
  125. use_depthwise,
  126. conv_cfg=conv_cfg,
  127. norm_cfg=norm_cfg,
  128. act_cfg=act_cfg) for _ in range(num_blocks)
  129. ])
  130. def forward(self, x):
  131. x_short = self.short_conv(x)
  132. x_main = self.main_conv(x)
  133. x_main = self.blocks(x_main)
  134. x_final = torch.cat((x_main, x_short), dim=1)
  135. return self.final_conv(x_final)

No Description

Contributors (1)