rec_resnet_vd.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import torch.nn as nn
  2. import torch.nn.functional as F
  3. from openrec.modeling.common import Activation
  4. class ConvBNLayer(nn.Module):
  5. def __init__(
  6. self,
  7. in_channels,
  8. out_channels,
  9. kernel_size,
  10. stride=1,
  11. groups=1,
  12. is_vd_mode=False,
  13. act=None,
  14. ):
  15. super(ConvBNLayer, self).__init__()
  16. self.act = act
  17. self.is_vd_mode = is_vd_mode
  18. self._pool2d_avg = nn.AvgPool2d(kernel_size=stride,
  19. stride=stride,
  20. padding=0,
  21. ceil_mode=False)
  22. self._conv = nn.Conv2d(
  23. in_channels=in_channels,
  24. out_channels=out_channels,
  25. kernel_size=kernel_size,
  26. stride=1 if is_vd_mode else stride,
  27. padding=(kernel_size - 1) // 2,
  28. groups=groups,
  29. bias=False,
  30. )
  31. self._batch_norm = nn.BatchNorm2d(out_channels, )
  32. if self.act is not None:
  33. self._act = Activation(act_type=act, inplace=True)
  34. def forward(self, inputs):
  35. if self.is_vd_mode:
  36. inputs = self._pool2d_avg(inputs)
  37. y = self._conv(inputs)
  38. y = self._batch_norm(y)
  39. if self.act is not None:
  40. y = self._act(y)
  41. return y
  42. class BottleneckBlock(nn.Module):
  43. def __init__(
  44. self,
  45. in_channels,
  46. out_channels,
  47. stride,
  48. shortcut=True,
  49. if_first=False,
  50. name=None,
  51. ):
  52. super(BottleneckBlock, self).__init__()
  53. self.scale = 4
  54. self.conv0 = ConvBNLayer(
  55. in_channels=in_channels,
  56. out_channels=out_channels,
  57. kernel_size=1,
  58. act='relu',
  59. )
  60. self.conv1 = ConvBNLayer(
  61. in_channels=out_channels,
  62. out_channels=out_channels,
  63. kernel_size=3,
  64. stride=stride,
  65. act='relu',
  66. )
  67. self.conv2 = ConvBNLayer(
  68. in_channels=out_channels,
  69. out_channels=out_channels * self.scale,
  70. kernel_size=1,
  71. act=None,
  72. )
  73. if not shortcut:
  74. self.short = ConvBNLayer(
  75. in_channels=in_channels,
  76. out_channels=out_channels * self.scale,
  77. kernel_size=1,
  78. stride=stride,
  79. is_vd_mode=not if_first and stride[0] != 1,
  80. )
  81. self.shortcut = shortcut
  82. self.out_channels = out_channels * self.scale
  83. def forward(self, inputs):
  84. y = self.conv0(inputs)
  85. conv1 = self.conv1(y)
  86. conv2 = self.conv2(conv1)
  87. if self.shortcut:
  88. short = inputs
  89. else:
  90. short = self.short(inputs)
  91. y = short + conv2
  92. y = F.relu(y)
  93. return y
  94. class BasicBlock(nn.Module):
  95. def __init__(
  96. self,
  97. in_channels,
  98. out_channels,
  99. stride,
  100. shortcut=True,
  101. if_first=False,
  102. name=None,
  103. ):
  104. super(BasicBlock, self).__init__()
  105. self.stride = stride
  106. self.scale = 1
  107. self.conv0 = ConvBNLayer(
  108. in_channels=in_channels,
  109. out_channels=out_channels,
  110. kernel_size=3,
  111. stride=stride,
  112. act='relu',
  113. )
  114. self.conv1 = ConvBNLayer(in_channels=out_channels,
  115. out_channels=out_channels,
  116. kernel_size=3,
  117. act=None)
  118. if not shortcut:
  119. self.short = ConvBNLayer(
  120. in_channels=in_channels,
  121. out_channels=out_channels,
  122. kernel_size=1,
  123. stride=stride,
  124. is_vd_mode=not if_first and stride[0] != 1,
  125. )
  126. self.shortcut = shortcut
  127. self.out_channels = out_channels * self.scale
  128. def forward(self, inputs):
  129. y = self.conv0(inputs)
  130. conv1 = self.conv1(y)
  131. if self.shortcut:
  132. short = inputs
  133. else:
  134. short = self.short(inputs)
  135. y = short + conv1
  136. y = F.relu(y)
  137. return y
  138. class ResNet(nn.Module):
  139. def __init__(self, in_channels=3, layers=50, **kwargs):
  140. super(ResNet, self).__init__()
  141. self.layers = layers
  142. supported_layers = [18, 34, 50, 101, 152, 200]
  143. assert layers in supported_layers, 'supported layers are {} but input layer is {}'.format(
  144. supported_layers, layers)
  145. if layers == 18:
  146. depth = [2, 2, 2, 2]
  147. elif layers == 34 or layers == 50:
  148. depth = [3, 4, 6, 3]
  149. elif layers == 101:
  150. depth = [3, 4, 23, 3]
  151. elif layers == 152:
  152. depth = [3, 8, 36, 3]
  153. elif layers == 200:
  154. depth = [3, 12, 48, 3]
  155. if layers >= 50:
  156. block_class = BottleneckBlock
  157. else:
  158. block_class = BasicBlock
  159. num_filters = [64, 128, 256, 512]
  160. self.conv1_1 = ConvBNLayer(
  161. in_channels=in_channels,
  162. out_channels=32,
  163. kernel_size=3,
  164. stride=1,
  165. act='relu',
  166. )
  167. self.conv1_2 = ConvBNLayer(in_channels=32,
  168. out_channels=32,
  169. kernel_size=3,
  170. stride=1,
  171. act='relu')
  172. self.conv1_3 = ConvBNLayer(in_channels=32,
  173. out_channels=64,
  174. kernel_size=3,
  175. stride=1,
  176. act='relu')
  177. self.pool2d_max = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
  178. # self.block_list = list()
  179. self.block_list = nn.Sequential()
  180. in_channels = 64
  181. for block in range(len(depth)):
  182. shortcut = False
  183. for i in range(depth[block]):
  184. if layers in [101, 152, 200] and block == 2:
  185. if i == 0:
  186. conv_name = 'res' + str(block + 2) + 'a'
  187. else:
  188. conv_name = 'res' + str(block + 2) + 'b' + str(i)
  189. else:
  190. conv_name = 'res' + str(block + 2) + chr(97 + i)
  191. if i == 0 and block != 0:
  192. stride = (2, 1)
  193. else:
  194. stride = (1, 1)
  195. block_instance = block_class(
  196. in_channels=in_channels,
  197. out_channels=num_filters[block],
  198. stride=stride,
  199. shortcut=shortcut,
  200. if_first=block == i == 0,
  201. name=conv_name,
  202. )
  203. shortcut = True
  204. in_channels = block_instance.out_channels
  205. # self.block_list.append(bottleneck_block)
  206. self.block_list.add_module('bb_%d_%d' % (block, i),
  207. block_instance)
  208. self.out_channels = num_filters[block]
  209. self.out_pool = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
  210. def forward(self, inputs):
  211. y = self.conv1_1(inputs)
  212. y = self.conv1_2(y)
  213. y = self.conv1_3(y)
  214. y = self.pool2d_max(y)
  215. for block in self.block_list:
  216. y = block(y)
  217. y = self.out_pool(y)
  218. return y