yolox_basic.py 4.5 KB

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