yolov7_basic.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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='lrelu', # activation
  35. norm_type='BN', # 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. # ELAN Block
  63. class ELANBlock(nn.Module):
  64. def __init__(self, in_dim, out_dim, expand_ratio=0.5, act_type='silu', norm_type='BN', depthwise=False):
  65. super(ELANBlock, self).__init__()
  66. inter_dim = int(in_dim * expand_ratio)
  67. self.cv1 = Conv(in_dim, inter_dim, k=1, act_type=act_type, norm_type=norm_type)
  68. self.cv2 = Conv(in_dim, inter_dim, k=1, act_type=act_type, norm_type=norm_type)
  69. self.cv3 = nn.Sequential(*[
  70. Conv(inter_dim, inter_dim, k=3, p=1, act_type=act_type, norm_type=norm_type, depthwise=depthwise)
  71. for _ in range(2)
  72. ])
  73. self.cv4 = nn.Sequential(*[
  74. Conv(inter_dim, inter_dim, k=3, p=1, act_type=act_type, norm_type=norm_type, depthwise=depthwise)
  75. for _ in range(2)
  76. ])
  77. self.out = Conv(inter_dim*4, out_dim, k=1, act_type=act_type, norm_type=norm_type)
  78. def forward(self, x):
  79. x1 = self.cv1(x)
  80. x2 = self.cv2(x)
  81. x3 = self.cv3(x2)
  82. x4 = self.cv4(x3)
  83. out = self.out(torch.cat([x1, x2, x3, x4], dim=1))
  84. return out
  85. # DownSample Block
  86. class DownSample(nn.Module):
  87. def __init__(self, in_dim, act_type='silu', norm_type='BN'):
  88. super().__init__()
  89. inter_dim = in_dim // 2
  90. self.mp = nn.MaxPool2d((2, 2), 2)
  91. self.cv1 = Conv(in_dim, inter_dim, k=1, act_type=act_type, norm_type=norm_type)
  92. self.cv2 = nn.Sequential(
  93. Conv(in_dim, inter_dim, k=1, act_type=act_type, norm_type=norm_type),
  94. Conv(inter_dim, inter_dim, k=3, p=1, s=2, act_type=act_type, norm_type=norm_type)
  95. )
  96. def forward(self, x):
  97. x1 = self.cv1(self.mp(x))
  98. x2 = self.cv2(x)
  99. out = torch.cat([x1, x2], dim=1)
  100. return out
  101. # ELAN Block for PaFPN
  102. class ELANBlockFPN(nn.Module):
  103. def __init__(self, in_dim, out_dim, act_type='silu', norm_type='BN', depthwise=False):
  104. super(ELANBlockFPN, self).__init__()
  105. # Basic parameters
  106. e1, e2 = 0.5, 0.5
  107. width = 4
  108. depth = 1
  109. inter_dim = int(in_dim * e1)
  110. inter_dim2 = int(inter_dim * e2)
  111. # Network structure
  112. self.cv1 = Conv(in_dim, inter_dim, k=1, act_type=act_type, norm_type=norm_type)
  113. self.cv2 = Conv(in_dim, inter_dim, k=1, act_type=act_type, norm_type=norm_type)
  114. self.cv3 = nn.ModuleList()
  115. for idx in range(width):
  116. if idx == 0:
  117. cvs = [Conv(inter_dim, inter_dim2, k=3, p=1, act_type=act_type, norm_type=norm_type, depthwise=depthwise)]
  118. else:
  119. cvs = [Conv(inter_dim2, inter_dim2, k=3, p=1, act_type=act_type, norm_type=norm_type, depthwise=depthwise)]
  120. # deeper
  121. if depth > 1:
  122. for _ in range(1, depth):
  123. cvs.append(Conv(inter_dim2, inter_dim2, k=3, p=1, act_type=act_type, norm_type=norm_type, depthwise=depthwise))
  124. self.cv3.append(nn.Sequential(*cvs))
  125. else:
  126. self.cv3.append(cvs[0])
  127. self.out = Conv(inter_dim*2+inter_dim2*len(self.cv3), out_dim, k=1, act_type=act_type, norm_type=norm_type)
  128. def forward(self, x):
  129. x1 = self.cv1(x)
  130. x2 = self.cv2(x)
  131. inter_outs = [x1, x2]
  132. for m in self.cv3:
  133. y1 = inter_outs[-1]
  134. y2 = m(y1)
  135. inter_outs.append(y2)
  136. out = self.out(torch.cat(inter_outs, dim=1))
  137. return out
  138. # DownSample Block for PaFPN
  139. class DownSampleFPN(nn.Module):
  140. def __init__(self, in_dim, act_type='silu', norm_type='BN', depthwise=False):
  141. super().__init__()
  142. inter_dim = in_dim
  143. self.mp = nn.MaxPool2d((2, 2), 2)
  144. self.cv1 = Conv(in_dim, inter_dim, k=1, act_type=act_type, norm_type=norm_type)
  145. self.cv2 = nn.Sequential(
  146. Conv(in_dim, inter_dim, k=1, act_type=act_type, norm_type=norm_type),
  147. Conv(inter_dim, inter_dim, k=3, p=1, s=2, act_type=act_type, norm_type=norm_type, depthwise=depthwise)
  148. )
  149. def forward(self, x):
  150. x1 = self.cv1(self.mp(x))
  151. x2 = self.cv2(x)
  152. out = torch.cat([x1, x2], dim=1)
  153. return out