rtrdet_basic.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import torch
  2. import torch.nn as nn
  3. from typing import List
  4. # ----------------- CNN modules -----------------
  5. def get_conv2d(c1, c2, k, p, s, d, g, bias=False):
  6. conv = nn.Conv2d(c1, c2, k, stride=s, padding=p, dilation=d, groups=g, bias=bias)
  7. return conv
  8. def get_activation(act_type=None):
  9. if act_type == 'relu':
  10. return nn.ReLU(inplace=True)
  11. elif act_type == 'lrelu':
  12. return nn.LeakyReLU(0.1, inplace=True)
  13. elif act_type == 'mish':
  14. return nn.Mish(inplace=True)
  15. elif act_type == 'silu':
  16. return nn.SiLU(inplace=True)
  17. elif act_type is None:
  18. return nn.Identity()
  19. else:
  20. raise NotImplementedError
  21. def get_norm(norm_type, dim):
  22. if norm_type == 'BN':
  23. return nn.BatchNorm2d(dim)
  24. elif norm_type == 'GN':
  25. return nn.GroupNorm(num_groups=32, num_channels=dim)
  26. elif norm_type is None:
  27. return nn.Identity()
  28. else:
  29. raise NotImplementedError
  30. class Conv(nn.Module):
  31. def __init__(self,
  32. c1, # in channels
  33. c2, # out channels
  34. k=1, # kernel size
  35. p=0, # padding
  36. s=1, # padding
  37. d=1, # dilation
  38. act_type :str = 'lrelu', # activation
  39. norm_type :str ='BN', # normalization
  40. depthwise :bool =False):
  41. super(Conv, self).__init__()
  42. convs = []
  43. add_bias = False if norm_type else True
  44. if depthwise:
  45. convs.append(get_conv2d(c1, c1, k=k, p=p, s=s, d=d, g=c1, bias=add_bias))
  46. # depthwise conv
  47. if norm_type:
  48. convs.append(get_norm(norm_type, c1))
  49. if act_type:
  50. convs.append(get_activation(act_type))
  51. # pointwise conv
  52. convs.append(get_conv2d(c1, c2, k=1, p=0, s=1, d=d, g=1, bias=add_bias))
  53. if norm_type:
  54. convs.append(get_norm(norm_type, c2))
  55. if act_type:
  56. convs.append(get_activation(act_type))
  57. else:
  58. convs.append(get_conv2d(c1, c2, k=k, p=p, s=s, d=d, g=1, bias=add_bias))
  59. if norm_type:
  60. convs.append(get_norm(norm_type, c2))
  61. if act_type:
  62. convs.append(get_activation(act_type))
  63. self.convs = nn.Sequential(*convs)
  64. def forward(self, x):
  65. return self.convs(x)
  66. class Bottleneck(nn.Module):
  67. def __init__(self,
  68. in_dim :int,
  69. out_dim :int,
  70. expand_ratio :float = 0.5,
  71. kernel_sizes :List = [3, 3],
  72. shortcut :bool = True,
  73. act_type :str = 'silu',
  74. norm_type :str = 'BN',
  75. depthwise :bool = False,):
  76. super(Bottleneck, self).__init__()
  77. inter_dim = int(out_dim * expand_ratio) # hidden channels
  78. self.cv1 = Conv(in_dim, inter_dim, k=kernel_sizes[0], p=kernel_sizes[0]//2, norm_type=norm_type, act_type=act_type, depthwise=depthwise)
  79. self.cv2 = Conv(inter_dim, out_dim, k=kernel_sizes[1], p=kernel_sizes[1]//2, norm_type=norm_type, act_type=act_type, depthwise=depthwise)
  80. self.shortcut = shortcut and in_dim == out_dim
  81. def forward(self, x):
  82. h = self.cv2(self.cv1(x))
  83. return x + h if self.shortcut else h
  84. class RTCBlock(nn.Module):
  85. def __init__(self,
  86. in_dim :int,
  87. out_dim :int,
  88. num_blocks :int = 1,
  89. shortcut :bool = False,
  90. act_type :str = 'silu',
  91. norm_type :str = 'BN',
  92. depthwise :bool = False,):
  93. super(RTCBlock, self).__init__()
  94. self.inter_dim = out_dim // 2
  95. self.input_proj = Conv(in_dim, out_dim, k=1, act_type=act_type, norm_type=norm_type)
  96. self.m = nn.Sequential(*(
  97. Bottleneck(self.inter_dim, self.inter_dim, 1.0, [3, 3], shortcut, act_type, norm_type, depthwise)
  98. for _ in range(num_blocks)))
  99. self.output_proj = Conv((2 + num_blocks) * self.inter_dim, out_dim, k=1, act_type=act_type, norm_type=norm_type)
  100. def forward(self, x):
  101. # Input proj
  102. x1, x2 = torch.chunk(self.input_proj(x), 2, dim=1)
  103. out = list([x1, x2])
  104. # Bottlenecl
  105. out.extend(m(out[-1]) for m in self.m)
  106. # Output proj
  107. out = self.output_proj(torch.cat(out, dim=1))
  108. return out
  109. # ----------------- Transformer modules -----------------