yolov2_basic.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import torch
  2. import torch.nn as nn
  3. from typing import List
  4. # --------------------- Basic 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 BasicConv(nn.Module):
  31. def __init__(self,
  32. in_dim, # in channels
  33. out_dim, # out channels
  34. kernel_size=1, # kernel size
  35. padding=0, # padding
  36. stride=1, # padding
  37. dilation=1, # dilation
  38. act_type :str = 'lrelu', # activation
  39. norm_type :str = 'BN', # normalization
  40. depthwise :bool = False
  41. ):
  42. super(BasicConv, self).__init__()
  43. self.depthwise = depthwise
  44. use_bias = False if norm_type is not None else True
  45. if not depthwise:
  46. self.conv = get_conv2d(in_dim, out_dim, k=kernel_size, p=padding, s=stride, d=dilation, g=1, bias=use_bias)
  47. self.norm = get_norm(norm_type, out_dim)
  48. else:
  49. self.conv1 = get_conv2d(in_dim, in_dim, k=kernel_size, p=padding, s=stride, d=dilation, g=in_dim, bias=use_bias)
  50. self.norm1 = get_norm(norm_type, in_dim)
  51. self.conv2 = get_conv2d(in_dim, out_dim, k=1, p=0, s=1, d=1, g=1)
  52. self.norm2 = get_norm(norm_type, out_dim)
  53. self.act = get_activation(act_type)
  54. def forward(self, x):
  55. if not self.depthwise:
  56. return self.act(self.norm(self.conv(x)))
  57. else:
  58. # Depthwise conv
  59. x = self.act(self.norm1(self.conv1(x)))
  60. # Pointwise conv
  61. x = self.act(self.norm2(self.conv2(x)))
  62. return x
  63. # --------------------- ResNet modules ---------------------
  64. def conv3x3(in_planes, out_planes, stride=1):
  65. """3x3 convolution with padding"""
  66. return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
  67. padding=1, bias=False)
  68. def conv1x1(in_planes, out_planes, stride=1):
  69. """1x1 convolution"""
  70. return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)
  71. class BasicBlock(nn.Module):
  72. expansion = 1
  73. def __init__(self, inplanes, planes, stride=1, downsample=None):
  74. super(BasicBlock, self).__init__()
  75. self.conv1 = conv3x3(inplanes, planes, stride)
  76. self.bn1 = nn.BatchNorm2d(planes)
  77. self.relu = nn.ReLU(inplace=True)
  78. self.conv2 = conv3x3(planes, planes)
  79. self.bn2 = nn.BatchNorm2d(planes)
  80. self.downsample = downsample
  81. self.stride = stride
  82. def forward(self, x):
  83. identity = x
  84. out = self.conv1(x)
  85. out = self.bn1(out)
  86. out = self.relu(out)
  87. out = self.conv2(out)
  88. out = self.bn2(out)
  89. if self.downsample is not None:
  90. identity = self.downsample(x)
  91. out += identity
  92. out = self.relu(out)
  93. return out
  94. class Bottleneck(nn.Module):
  95. expansion = 4
  96. def __init__(self, inplanes, planes, stride=1, downsample=None):
  97. super(Bottleneck, self).__init__()
  98. self.conv1 = conv1x1(inplanes, planes)
  99. self.bn1 = nn.BatchNorm2d(planes)
  100. self.conv2 = conv3x3(planes, planes, stride)
  101. self.bn2 = nn.BatchNorm2d(planes)
  102. self.conv3 = conv1x1(planes, planes * self.expansion)
  103. self.bn3 = nn.BatchNorm2d(planes * self.expansion)
  104. self.relu = nn.ReLU(inplace=True)
  105. self.downsample = downsample
  106. self.stride = stride
  107. def forward(self, x):
  108. identity = x
  109. out = self.conv1(x)
  110. out = self.bn1(out)
  111. out = self.relu(out)
  112. out = self.conv2(out)
  113. out = self.bn2(out)
  114. out = self.relu(out)
  115. out = self.conv3(out)
  116. out = self.bn3(out)
  117. if self.downsample is not None:
  118. identity = self.downsample(x)
  119. out += identity
  120. out = self.relu(out)
  121. return out