rtcdet_basic.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. from typing import List
  2. import numpy as np
  3. import torch
  4. import torch.nn as nn
  5. # ---------------------------- Base Conv Module ----------------------------
  6. class SiLU(nn.Module):
  7. """export-friendly version of nn.SiLU()"""
  8. @staticmethod
  9. def forward(x):
  10. return x * torch.sigmoid(x)
  11. def get_conv2d(c1, c2, k, p, s, d, g, bias=False):
  12. conv = nn.Conv2d(c1, c2, k, stride=s, padding=p, dilation=d, groups=g, bias=bias)
  13. return conv
  14. def get_activation(act_type=None):
  15. if act_type == 'relu':
  16. return nn.ReLU(inplace=True)
  17. elif act_type == 'lrelu':
  18. return nn.LeakyReLU(0.1, inplace=True)
  19. elif act_type == 'mish':
  20. return nn.Mish(inplace=True)
  21. elif act_type == 'silu':
  22. return nn.SiLU(inplace=True)
  23. elif act_type is None:
  24. return nn.Identity()
  25. def get_norm(norm_type, dim):
  26. if norm_type == 'BN':
  27. return nn.BatchNorm2d(dim)
  28. elif norm_type == 'GN':
  29. return nn.GroupNorm(num_groups=32, num_channels=dim)
  30. ## Basic Conv Module
  31. class Conv(nn.Module):
  32. def __init__(self,
  33. c1, # in channels
  34. c2, # out channels
  35. k=1, # kernel size
  36. p=0, # padding
  37. s=1, # padding
  38. d=1, # dilation
  39. act_type='lrelu', # activation
  40. norm_type='BN', # normalization
  41. depthwise=False):
  42. super(Conv, self).__init__()
  43. convs = []
  44. add_bias = False if norm_type else True
  45. p = p if d == 1 else d
  46. if depthwise:
  47. convs.append(get_conv2d(c1, c1, k=k, p=p, s=s, d=d, g=c1, bias=add_bias))
  48. # depthwise conv
  49. if norm_type is not None:
  50. convs.append(get_norm(norm_type, c1))
  51. if act_type is not None:
  52. convs.append(get_activation(act_type))
  53. # pointwise conv
  54. convs.append(get_conv2d(c1, c2, k=1, p=0, s=1, d=d, g=1, bias=add_bias))
  55. if norm_type is not None:
  56. convs.append(get_norm(norm_type, c2))
  57. if act_type is not None:
  58. convs.append(get_activation(act_type))
  59. else:
  60. convs.append(get_conv2d(c1, c2, k=k, p=p, s=s, d=d, g=1, bias=add_bias))
  61. if norm_type is not None:
  62. convs.append(get_norm(norm_type, c2))
  63. if act_type is not None:
  64. convs.append(get_activation(act_type))
  65. self.convs = nn.Sequential(*convs)
  66. def forward(self, x):
  67. return self.convs(x)
  68. ## Partial Conv Module
  69. class PartialConv(nn.Module):
  70. def __init__(self, in_dim, out_dim, split_ratio=0.25, kernel_size=1, stride=1, act_type=None, norm_type=None):
  71. super().__init__()
  72. # ----------- Basic Parameters -----------
  73. assert in_dim == out_dim
  74. self.in_dim = in_dim
  75. self.out_dim = out_dim
  76. self.split_ratio = split_ratio
  77. self.split_dim = round(in_dim * split_ratio)
  78. self.untouched_dim = in_dim - self.split_dim
  79. self.kernel_size = kernel_size
  80. self.padding = kernel_size // 2
  81. self.stride = stride
  82. self.act_type = act_type
  83. self.norm_type = norm_type
  84. # ----------- Network Parameters -----------
  85. self.partial_conv = Conv(self.split_dim, self.split_dim, self.kernel_size, self.padding, self.stride, act_type=act_type, norm_type=norm_type)
  86. def forward(self, x):
  87. x1, x2 = torch.split(x, [self.split_dim, self.untouched_dim], dim=1)
  88. x1 = self.partial_conv(x1)
  89. x = torch.cat((x1, x2), 1)
  90. return x
  91. ## Channel Shuffle
  92. class ChannelShuffle(nn.Module):
  93. def __init__(self, groups=1) -> None:
  94. super().__init__()
  95. self.groups = groups
  96. def forward(self, x):
  97. # type: (torch.Tensor, int) -> torch.Tensor
  98. batchsize, num_channels, height, width = x.data.size()
  99. channels_per_group = num_channels // self.groups
  100. # reshape
  101. x = x.view(batchsize, self.groups,
  102. channels_per_group, height, width)
  103. x = torch.transpose(x, 1, 2).contiguous()
  104. # flatten
  105. x = x.view(batchsize, -1, height, width)
  106. return x
  107. ## Inverse BottleNeck
  108. class InverseBottleneck(nn.Module):
  109. def __init__(self,
  110. in_dim,
  111. out_dim,
  112. expand_ratio=2.0,
  113. shortcut=False,
  114. act_type='silu',
  115. norm_type='BN',
  116. depthwise=False):
  117. super(InverseBottleneck, self).__init__()
  118. # ----------- Basic Parameters -----------
  119. self.in_dim = in_dim
  120. self.out_dim = out_dim
  121. self.expand_dim = int(in_dim * expand_ratio)
  122. # ----------- Network Parameters -----------
  123. self.cv1 = Conv(in_dim, in_dim, k=3, p=1, act_type=None, norm_type=norm_type, depthwise=depthwise)
  124. self.cv2 = Conv(in_dim, self.expand_dim, k=1, act_type=act_type, norm_type=norm_type, depthwise=depthwise)
  125. self.cv3 = Conv(self.expand_dim, out_dim, k=1, act_type=act_type, norm_type=norm_type, depthwise=depthwise)
  126. self.shortcut = shortcut and in_dim == out_dim
  127. def forward(self, x):
  128. h = self.cv3(self.cv2(self.cv1(x)))
  129. return x + h if self.shortcut else h
  130. ## YOLO-style BottleNeck
  131. class YoloBottleneck(nn.Module):
  132. def __init__(self,
  133. in_dim,
  134. out_dim,
  135. kernel_sizes :List[int] = [3, 3],
  136. expand_ratio :float = 0.5,
  137. shortcut :bool = False,
  138. act_type :str = 'silu',
  139. norm_type :str = 'BN',
  140. depthwise :bool = False):
  141. super(YoloBottleneck, self).__init__()
  142. # ------------------ Basic parameters ------------------
  143. self.in_dim = in_dim
  144. self.out_dim = out_dim
  145. self.inter_dim = int(out_dim * expand_ratio)
  146. self.shortcut = shortcut and in_dim == out_dim
  147. self.depthwise = []
  148. for ksize in kernel_sizes:
  149. if ksize > 1:
  150. self.depthwise.append(depthwise)
  151. else:
  152. self.depthwise.append(False)
  153. # ------------------ Network parameters ------------------
  154. self.cv1 = Conv(in_dim, self.inter_dim, k=kernel_sizes[0], p=kernel_sizes[0]//2, norm_type=norm_type, act_type=act_type, depthwise=self.depthwise[0])
  155. self.cv2 = Conv(self.inter_dim, out_dim, k=kernel_sizes[1], p=kernel_sizes[1]//2, norm_type=norm_type, act_type=act_type, depthwise=self.depthwise[1])
  156. def forward(self, x):
  157. h = self.cv2(self.cv1(x))
  158. return x + h if self.shortcut else h
  159. # ---------------------------- Base Modules ----------------------------
  160. ## ELAN Block for Backbone
  161. class ELANBlock(nn.Module):
  162. def __init__(self, in_dim, out_dim, expand_ratio :float=0.5, branch_depth :int=1, shortcut=False, act_type='silu', norm_type='BN', depthwise=False):
  163. super().__init__()
  164. # ----------- Basic Parameters -----------
  165. self.in_dim = in_dim
  166. self.out_dim = out_dim
  167. self.inter_dim = round(in_dim * expand_ratio)
  168. self.expand_ratio = expand_ratio
  169. self.branch_depth = branch_depth
  170. self.shortcut = shortcut
  171. # ----------- Network Parameters -----------
  172. ## branch-1
  173. self.cv1 = Conv(in_dim, self.inter_dim, k=1, act_type=act_type, norm_type=norm_type)
  174. ## branch-2
  175. self.cv2 = Conv(in_dim, self.inter_dim, k=1, act_type=act_type, norm_type=norm_type)
  176. ## branch-3
  177. self.cv3 = nn.Sequential(*[
  178. YoloBottleneck(self.inter_dim, self.inter_dim, [1, 3], 1.0, shortcut, act_type, norm_type, depthwise)
  179. for _ in range(branch_depth)
  180. ])
  181. ## branch-4
  182. self.cv4 = nn.Sequential(*[
  183. YoloBottleneck(self.inter_dim, self.inter_dim, [1, 3], 1.0, shortcut, act_type, norm_type, depthwise)
  184. for _ in range(branch_depth)
  185. ])
  186. ## output proj
  187. self.out = Conv(self.inter_dim*4, out_dim, k=1, act_type=act_type, norm_type=norm_type)
  188. def forward(self, x):
  189. x1 = self.cv1(x)
  190. x2 = self.cv2(x)
  191. x3 = self.cv3(x2)
  192. x4 = self.cv4(x3)
  193. # [B, C, H, W] -> [B, 2C, H, W]
  194. out = self.out(torch.cat([x1, x2, x3, x4], dim=1))
  195. return out
  196. ## ELAN Block for FPN
  197. class ELANBlockFPN(nn.Module):
  198. def __init__(self, in_dim, out_dim, expand_ratio :float=0.5, branch_depth :int=1, shortcut=False, act_type='silu', norm_type='BN', depthwise=False):
  199. super().__init__()
  200. # ----------- Basic Parameters -----------
  201. self.in_dim = in_dim
  202. self.out_dim = out_dim
  203. self.inter_dim1 = round(out_dim * expand_ratio)
  204. self.inter_dim2 = round(self.inter_dim1 * expand_ratio)
  205. self.expand_ratio = expand_ratio
  206. self.branch_depth = branch_depth
  207. self.shortcut = shortcut
  208. # ----------- Network Parameters -----------
  209. ## branch-1
  210. self.cv1 = Conv(in_dim, self.inter_dim1, k=1, act_type=act_type, norm_type=norm_type)
  211. ## branch-2
  212. self.cv2 = Conv(in_dim, self.inter_dim1, k=1, act_type=act_type, norm_type=norm_type)
  213. ## branch-3
  214. self.cv3 = []
  215. for i in range(branch_depth):
  216. if i == 0:
  217. self.cv3.append(YoloBottleneck(self.inter_dim1, self.inter_dim2, [1, 3], 1.0, shortcut, act_type, norm_type, depthwise))
  218. else:
  219. self.cv3.append(YoloBottleneck(self.inter_dim2, self.inter_dim2, [1, 3], 1.0, shortcut, act_type, norm_type, depthwise))
  220. self.cv3 = nn.Sequential(*self.cv3)
  221. ## branch-4
  222. self.cv4 = nn.Sequential(*[
  223. YoloBottleneck(self.inter_dim2, self.inter_dim2, [1, 3], 1.0, shortcut, act_type, norm_type, depthwise)
  224. for _ in range(branch_depth)
  225. ])
  226. ## branch-5
  227. self.cv5 = nn.Sequential(*[
  228. YoloBottleneck(self.inter_dim2, self.inter_dim2, [1, 3], 1.0, shortcut, act_type, norm_type, depthwise)
  229. for _ in range(branch_depth)
  230. ])
  231. ## branch-6
  232. self.cv6 = nn.Sequential(*[
  233. YoloBottleneck(self.inter_dim2, self.inter_dim2, [1, 3], 1.0, shortcut, act_type, norm_type, depthwise)
  234. for _ in range(branch_depth)
  235. ])
  236. ## output proj
  237. self.out = Conv(self.inter_dim1*2 + self.inter_dim2*4, out_dim, k=1, act_type=act_type, norm_type=norm_type)
  238. def forward(self, x):
  239. x1 = self.cv1(x)
  240. x2 = self.cv2(x)
  241. x3 = self.cv3(x2)
  242. x4 = self.cv4(x3)
  243. x5 = self.cv5(x4)
  244. x6 = self.cv6(x5)
  245. # [B, C, H, W] -> [B, 2C, H, W]
  246. out = self.out(torch.cat([x1, x2, x3, x4, x5, x6], dim=1))
  247. return out
  248. ## DownSample Block
  249. class DSBlock(nn.Module):
  250. def __init__(self, in_dim, out_dim, act_type='silu', norm_type='BN', depthwise=False):
  251. super().__init__()
  252. inter_dim = out_dim // 2
  253. self.branch_1 = nn.Sequential(
  254. nn.MaxPool2d((2, 2), 2),
  255. Conv(in_dim, inter_dim, k=1, act_type=act_type, norm_type=norm_type)
  256. )
  257. self.branch_2 = nn.Sequential(
  258. Conv(in_dim, inter_dim, k=1, act_type=act_type, norm_type=norm_type),
  259. Conv(inter_dim, inter_dim, k=3, p=1, s=2, act_type=act_type, norm_type=norm_type, depthwise=depthwise)
  260. )
  261. def forward(self, x):
  262. x1 = self.branch_1(x)
  263. x2 = self.branch_2(x)
  264. out = torch.cat([x1, x2], dim=1)
  265. return out
  266. # ---------------------------- FPN Modules ----------------------------
  267. ## build fpn's core block
  268. def build_fpn_block(cfg, in_dim, out_dim):
  269. if cfg['fpn_core_block'] == 'elan_block':
  270. layer = ELANBlockFPN(in_dim = in_dim,
  271. out_dim = out_dim,
  272. expand_ratio = cfg['fpn_expand_ratio'],
  273. branch_depth = round(3 * cfg['depth']),
  274. shortcut = False,
  275. act_type = cfg['fpn_act'],
  276. norm_type = cfg['fpn_norm'],
  277. depthwise = cfg['fpn_depthwise']
  278. )
  279. return layer
  280. ## build fpn's reduce layer
  281. def build_reduce_layer(cfg, in_dim, out_dim):
  282. if cfg['fpn_reduce_layer'] == 'conv':
  283. layer = Conv(in_dim, out_dim, k=1, act_type=cfg['fpn_act'], norm_type=cfg['fpn_norm'])
  284. return layer
  285. ## build fpn's downsample layer
  286. def build_downsample_layer(cfg, in_dim, out_dim):
  287. if cfg['fpn_downsample_layer'] == 'conv':
  288. layer = Conv(in_dim, out_dim, k=3, s=2, p=1,
  289. act_type=cfg['fpn_act'], norm_type=cfg['fpn_norm'], depthwise=cfg['fpn_depthwise'])
  290. elif cfg['fpn_downsample_layer'] == 'maxpool':
  291. assert in_dim == out_dim
  292. layer = nn.MaxPool2d((2, 2), stride=2)
  293. elif cfg['fpn_downsample_layer'] == 'dsblock':
  294. layer = DSBlock(in_dim, out_dim, cfg['fpn_act'], cfg['fpn_norm'], cfg['fpn_depthwise'])
  295. return layer