model.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. import torch
  2. from collections import OrderedDict
  3. import torch
  4. import torch.nn as nn
  5. def make_layers(block, no_relu_layers):
  6. layers = []
  7. for layer_name, v in block.items():
  8. if 'pool' in layer_name:
  9. layer = nn.MaxPool2d(kernel_size=v[0], stride=v[1],
  10. padding=v[2])
  11. layers.append((layer_name, layer))
  12. else:
  13. conv2d = nn.Conv2d(in_channels=v[0], out_channels=v[1],
  14. kernel_size=v[2], stride=v[3],
  15. padding=v[4])
  16. layers.append((layer_name, conv2d))
  17. if layer_name not in no_relu_layers:
  18. layers.append(('relu_' + layer_name, nn.ReLU(inplace=True)))
  19. return nn.Sequential(OrderedDict(layers))
  20. class bodypose_model(nn.Module):
  21. def __init__(self):
  22. super(bodypose_model, self).__init__()
  23. # these layers have no relu layer
  24. no_relu_layers = ['conv5_5_CPM_L1', 'conv5_5_CPM_L2', 'Mconv7_stage2_L1', \
  25. 'Mconv7_stage2_L2', 'Mconv7_stage3_L1', 'Mconv7_stage3_L2', \
  26. 'Mconv7_stage4_L1', 'Mconv7_stage4_L2', 'Mconv7_stage5_L1', \
  27. 'Mconv7_stage5_L2', 'Mconv7_stage6_L1', 'Mconv7_stage6_L1']
  28. blocks = {}
  29. block0 = OrderedDict([
  30. ('conv1_1', [3, 64, 3, 1, 1]),
  31. ('conv1_2', [64, 64, 3, 1, 1]),
  32. ('pool1_stage1', [2, 2, 0]),
  33. ('conv2_1', [64, 128, 3, 1, 1]),
  34. ('conv2_2', [128, 128, 3, 1, 1]),
  35. ('pool2_stage1', [2, 2, 0]),
  36. ('conv3_1', [128, 256, 3, 1, 1]),
  37. ('conv3_2', [256, 256, 3, 1, 1]),
  38. ('conv3_3', [256, 256, 3, 1, 1]),
  39. ('conv3_4', [256, 256, 3, 1, 1]),
  40. ('pool3_stage1', [2, 2, 0]),
  41. ('conv4_1', [256, 512, 3, 1, 1]),
  42. ('conv4_2', [512, 512, 3, 1, 1]),
  43. ('conv4_3_CPM', [512, 256, 3, 1, 1]),
  44. ('conv4_4_CPM', [256, 128, 3, 1, 1])
  45. ])
  46. # Stage 1
  47. block1_1 = OrderedDict([
  48. ('conv5_1_CPM_L1', [128, 128, 3, 1, 1]),
  49. ('conv5_2_CPM_L1', [128, 128, 3, 1, 1]),
  50. ('conv5_3_CPM_L1', [128, 128, 3, 1, 1]),
  51. ('conv5_4_CPM_L1', [128, 512, 1, 1, 0]),
  52. ('conv5_5_CPM_L1', [512, 38, 1, 1, 0])
  53. ])
  54. block1_2 = OrderedDict([
  55. ('conv5_1_CPM_L2', [128, 128, 3, 1, 1]),
  56. ('conv5_2_CPM_L2', [128, 128, 3, 1, 1]),
  57. ('conv5_3_CPM_L2', [128, 128, 3, 1, 1]),
  58. ('conv5_4_CPM_L2', [128, 512, 1, 1, 0]),
  59. ('conv5_5_CPM_L2', [512, 19, 1, 1, 0])
  60. ])
  61. blocks['block1_1'] = block1_1
  62. blocks['block1_2'] = block1_2
  63. self.model0 = make_layers(block0, no_relu_layers)
  64. # Stages 2 - 6
  65. for i in range(2, 7):
  66. blocks['block%d_1' % i] = OrderedDict([
  67. ('Mconv1_stage%d_L1' % i, [185, 128, 7, 1, 3]),
  68. ('Mconv2_stage%d_L1' % i, [128, 128, 7, 1, 3]),
  69. ('Mconv3_stage%d_L1' % i, [128, 128, 7, 1, 3]),
  70. ('Mconv4_stage%d_L1' % i, [128, 128, 7, 1, 3]),
  71. ('Mconv5_stage%d_L1' % i, [128, 128, 7, 1, 3]),
  72. ('Mconv6_stage%d_L1' % i, [128, 128, 1, 1, 0]),
  73. ('Mconv7_stage%d_L1' % i, [128, 38, 1, 1, 0])
  74. ])
  75. blocks['block%d_2' % i] = OrderedDict([
  76. ('Mconv1_stage%d_L2' % i, [185, 128, 7, 1, 3]),
  77. ('Mconv2_stage%d_L2' % i, [128, 128, 7, 1, 3]),
  78. ('Mconv3_stage%d_L2' % i, [128, 128, 7, 1, 3]),
  79. ('Mconv4_stage%d_L2' % i, [128, 128, 7, 1, 3]),
  80. ('Mconv5_stage%d_L2' % i, [128, 128, 7, 1, 3]),
  81. ('Mconv6_stage%d_L2' % i, [128, 128, 1, 1, 0]),
  82. ('Mconv7_stage%d_L2' % i, [128, 19, 1, 1, 0])
  83. ])
  84. for k in blocks.keys():
  85. blocks[k] = make_layers(blocks[k], no_relu_layers)
  86. self.model1_1 = blocks['block1_1']
  87. self.model2_1 = blocks['block2_1']
  88. self.model3_1 = blocks['block3_1']
  89. self.model4_1 = blocks['block4_1']
  90. self.model5_1 = blocks['block5_1']
  91. self.model6_1 = blocks['block6_1']
  92. self.model1_2 = blocks['block1_2']
  93. self.model2_2 = blocks['block2_2']
  94. self.model3_2 = blocks['block3_2']
  95. self.model4_2 = blocks['block4_2']
  96. self.model5_2 = blocks['block5_2']
  97. self.model6_2 = blocks['block6_2']
  98. def forward(self, x):
  99. out1 = self.model0(x)
  100. out1_1 = self.model1_1(out1)
  101. out1_2 = self.model1_2(out1)
  102. out2 = torch.cat([out1_1, out1_2, out1], 1)
  103. out2_1 = self.model2_1(out2)
  104. out2_2 = self.model2_2(out2)
  105. out3 = torch.cat([out2_1, out2_2, out1], 1)
  106. out3_1 = self.model3_1(out3)
  107. out3_2 = self.model3_2(out3)
  108. out4 = torch.cat([out3_1, out3_2, out1], 1)
  109. out4_1 = self.model4_1(out4)
  110. out4_2 = self.model4_2(out4)
  111. out5 = torch.cat([out4_1, out4_2, out1], 1)
  112. out5_1 = self.model5_1(out5)
  113. out5_2 = self.model5_2(out5)
  114. out6 = torch.cat([out5_1, out5_2, out1], 1)
  115. out6_1 = self.model6_1(out6)
  116. out6_2 = self.model6_2(out6)
  117. return out6_1, out6_2
  118. class handpose_model(nn.Module):
  119. def __init__(self):
  120. super(handpose_model, self).__init__()
  121. # these layers have no relu layer
  122. no_relu_layers = ['conv6_2_CPM', 'Mconv7_stage2', 'Mconv7_stage3', \
  123. 'Mconv7_stage4', 'Mconv7_stage5', 'Mconv7_stage6']
  124. # stage 1
  125. block1_0 = OrderedDict([
  126. ('conv1_1', [3, 64, 3, 1, 1]),
  127. ('conv1_2', [64, 64, 3, 1, 1]),
  128. ('pool1_stage1', [2, 2, 0]),
  129. ('conv2_1', [64, 128, 3, 1, 1]),
  130. ('conv2_2', [128, 128, 3, 1, 1]),
  131. ('pool2_stage1', [2, 2, 0]),
  132. ('conv3_1', [128, 256, 3, 1, 1]),
  133. ('conv3_2', [256, 256, 3, 1, 1]),
  134. ('conv3_3', [256, 256, 3, 1, 1]),
  135. ('conv3_4', [256, 256, 3, 1, 1]),
  136. ('pool3_stage1', [2, 2, 0]),
  137. ('conv4_1', [256, 512, 3, 1, 1]),
  138. ('conv4_2', [512, 512, 3, 1, 1]),
  139. ('conv4_3', [512, 512, 3, 1, 1]),
  140. ('conv4_4', [512, 512, 3, 1, 1]),
  141. ('conv5_1', [512, 512, 3, 1, 1]),
  142. ('conv5_2', [512, 512, 3, 1, 1]),
  143. ('conv5_3_CPM', [512, 128, 3, 1, 1])
  144. ])
  145. block1_1 = OrderedDict([
  146. ('conv6_1_CPM', [128, 512, 1, 1, 0]),
  147. ('conv6_2_CPM', [512, 22, 1, 1, 0])
  148. ])
  149. blocks = {}
  150. blocks['block1_0'] = block1_0
  151. blocks['block1_1'] = block1_1
  152. # stage 2-6
  153. for i in range(2, 7):
  154. blocks['block%d' % i] = OrderedDict([
  155. ('Mconv1_stage%d' % i, [150, 128, 7, 1, 3]),
  156. ('Mconv2_stage%d' % i, [128, 128, 7, 1, 3]),
  157. ('Mconv3_stage%d' % i, [128, 128, 7, 1, 3]),
  158. ('Mconv4_stage%d' % i, [128, 128, 7, 1, 3]),
  159. ('Mconv5_stage%d' % i, [128, 128, 7, 1, 3]),
  160. ('Mconv6_stage%d' % i, [128, 128, 1, 1, 0]),
  161. ('Mconv7_stage%d' % i, [128, 22, 1, 1, 0])
  162. ])
  163. for k in blocks.keys():
  164. blocks[k] = make_layers(blocks[k], no_relu_layers)
  165. self.model1_0 = blocks['block1_0']
  166. self.model1_1 = blocks['block1_1']
  167. self.model2 = blocks['block2']
  168. self.model3 = blocks['block3']
  169. self.model4 = blocks['block4']
  170. self.model5 = blocks['block5']
  171. self.model6 = blocks['block6']
  172. def forward(self, x):
  173. out1_0 = self.model1_0(x)
  174. out1_1 = self.model1_1(out1_0)
  175. concat_stage2 = torch.cat([out1_1, out1_0], 1)
  176. out_stage2 = self.model2(concat_stage2)
  177. concat_stage3 = torch.cat([out_stage2, out1_0], 1)
  178. out_stage3 = self.model3(concat_stage3)
  179. concat_stage4 = torch.cat([out_stage3, out1_0], 1)
  180. out_stage4 = self.model4(concat_stage4)
  181. concat_stage5 = torch.cat([out_stage4, out1_0], 1)
  182. out_stage5 = self.model5(concat_stage5)
  183. concat_stage6 = torch.cat([out_stage5, out1_0], 1)
  184. out_stage6 = self.model6(concat_stage6)
  185. return out_stage6
  186. def make_layers_Mconv(block, no_relu_layers):
  187. modules = []
  188. for layer_name, v in block.items():
  189. layers = []
  190. if 'pool' in layer_name:
  191. layer = nn.MaxPool2d(kernel_size=v[0], stride=v[1],
  192. padding=v[2])
  193. layers.append((layer_name, layer))
  194. else:
  195. conv2d = nn.Conv2d(in_channels=v[0], out_channels=v[1],
  196. kernel_size=v[2], stride=v[3],
  197. padding=v[4])
  198. layers.append((layer_name, conv2d))
  199. if layer_name not in no_relu_layers:
  200. layers.append(('Mprelu' + layer_name[5:], nn.PReLU(v[1])))
  201. modules.append(nn.Sequential(OrderedDict(layers)))
  202. return nn.ModuleList(modules)
  203. class bodypose_25_model(nn.Module):
  204. def __init__(self):
  205. super(bodypose_25_model, self).__init__()
  206. # these layers have no relu layer
  207. no_relu_layers = ['Mconv7_stage0_L1', 'Mconv7_stage0_L2', \
  208. 'Mconv7_stage1_L1', 'Mconv7_stage1_L2', \
  209. 'Mconv7_stage2_L2', 'Mconv7_stage3_L2']
  210. prelu_layers = ['conv4_2', 'conv4_3_CPM', 'conv4_4_CPM']
  211. blocks = {}
  212. block0 = OrderedDict([
  213. ('conv1_1', [3, 64, 3, 1, 1]),
  214. ('conv1_2', [64, 64, 3, 1, 1]),
  215. ('pool1_stage1', [2, 2, 0]),
  216. ('conv2_1', [64, 128, 3, 1, 1]),
  217. ('conv2_2', [128, 128, 3, 1, 1]),
  218. ('pool2_stage1', [2, 2, 0]),
  219. ('conv3_1', [128, 256, 3, 1, 1]),
  220. ('conv3_2', [256, 256, 3, 1, 1]),
  221. ('conv3_3', [256, 256, 3, 1, 1]),
  222. ('conv3_4', [256, 256, 3, 1, 1]),
  223. ('pool3_stage1', [2, 2, 0]),
  224. ('conv4_1', [256, 512, 3, 1, 1]),
  225. ('conv4_2', [512, 512, 3, 1, 1]),
  226. ('conv4_3_CPM', [512, 256, 3, 1, 1]),
  227. ('conv4_4_CPM', [256, 128, 3, 1, 1])
  228. ])
  229. self.model0 = make_layers(block0, no_relu_layers, prelu_layers)
  230. # L2
  231. # stage0
  232. blocks['Mconv1_stage0_L2'] = OrderedDict([
  233. ('Mconv1_stage0_L2_0', [128, 96, 3, 1, 1]),
  234. ('Mconv1_stage0_L2_1', [96, 96, 3, 1, 1]),
  235. ('Mconv1_stage0_L2_2', [96, 96, 3, 1, 1])
  236. ])
  237. for i in range(2, 6):
  238. blocks['Mconv%d_stage0_L2' % i] = OrderedDict([
  239. ('Mconv%d_stage0_L2_0' % i, [288, 96, 3, 1, 1]),
  240. ('Mconv%d_stage0_L2_1' % i, [96, 96, 3, 1, 1]),
  241. ('Mconv%d_stage0_L2_2' % i, [96, 96, 3, 1, 1])
  242. ])
  243. blocks['Mconv6_7_stage0_L2'] = OrderedDict([
  244. ('Mconv6_stage0_L2', [288, 256, 1, 1, 0]),
  245. ('Mconv7_stage0_L2', [256, 52, 1, 1, 0])
  246. ])
  247. # stage1~3
  248. for s in range(1, 4):
  249. blocks['Mconv1_stage%d_L2' % s] = OrderedDict([
  250. ('Mconv1_stage%d_L2_0' % s, [180, 128, 3, 1, 1]),
  251. ('Mconv1_stage%d_L2_1' % s, [128, 128, 3, 1, 1]),
  252. ('Mconv1_stage%d_L2_2' % s, [128, 128, 3, 1, 1])
  253. ])
  254. for i in range(2, 6):
  255. blocks['Mconv%d_stage%d_L2' % (i, s)] = OrderedDict([
  256. ('Mconv%d_stage%d_L2_0' % (i, s), [384, 128, 3, 1, 1]),
  257. ('Mconv%d_stage%d_L2_1' % (i, s), [128, 128, 3, 1, 1]),
  258. ('Mconv%d_stage%d_L2_2' % (i, s), [128, 128, 3, 1, 1])
  259. ])
  260. blocks['Mconv6_7_stage%d_L2' % s] = OrderedDict([
  261. ('Mconv6_stage%d_L2' % s, [384, 512, 1, 1, 0]),
  262. ('Mconv7_stage%d_L2' % s, [512, 52, 1, 1, 0])
  263. ])
  264. # L1
  265. # stage0
  266. blocks['Mconv1_stage0_L1'] = OrderedDict([
  267. ('Mconv1_stage0_L1_0', [180, 96, 3, 1, 1]),
  268. ('Mconv1_stage0_L1_1', [96, 96, 3, 1, 1]),
  269. ('Mconv1_stage0_L1_2', [96, 96, 3, 1, 1])
  270. ])
  271. for i in range(2, 6):
  272. blocks['Mconv%d_stage0_L1' % i] = OrderedDict([
  273. ('Mconv%d_stage0_L1_0' % i, [288, 96, 3, 1, 1]),
  274. ('Mconv%d_stage0_L1_1' % i, [96, 96, 3, 1, 1]),
  275. ('Mconv%d_stage0_L1_2' % i, [96, 96, 3, 1, 1])
  276. ])
  277. blocks['Mconv6_7_stage0_L1'] = OrderedDict([
  278. ('Mconv6_stage0_L1', [288, 256, 1, 1, 0]),
  279. ('Mconv7_stage0_L1', [256, 26, 1, 1, 0])
  280. ])
  281. # stage1
  282. blocks['Mconv1_stage1_L1'] = OrderedDict([
  283. ('Mconv1_stage1_L1_0', [206, 128, 3, 1, 1]),
  284. ('Mconv1_stage1_L1_1', [128, 128, 3, 1, 1]),
  285. ('Mconv1_stage1_L1_2', [128, 128, 3, 1, 1])
  286. ])
  287. for i in range(2, 6):
  288. blocks['Mconv%d_stage1_L1' % i] = OrderedDict([
  289. ('Mconv%d_stage1_L1_0' % i, [384, 128, 3, 1, 1]),
  290. ('Mconv%d_stage1_L1_1' % i, [128, 128, 3, 1, 1]),
  291. ('Mconv%d_stage1_L1_2' % i, [128, 128, 3, 1, 1])
  292. ])
  293. blocks['Mconv6_7_stage1_L1'] = OrderedDict([
  294. ('Mconv6_stage1_L1', [384, 512, 1, 1, 0]),
  295. ('Mconv7_stage1_L1', [512, 26, 1, 1, 0])
  296. ])
  297. for k in blocks.keys():
  298. blocks[k] = make_layers_Mconv(blocks[k], no_relu_layers)
  299. self.models = nn.ModuleDict(blocks)
  300. # self.model_L2_S0_mconv1 = blocks['Mconv1_stage0_L2']
  301. def _Mconv_forward(self, x, models):
  302. outs = []
  303. out = x
  304. for m in models:
  305. out = m(out)
  306. outs.append(out)
  307. return torch.cat(outs, 1)
  308. def forward(self, x):
  309. out0 = self.model0(x)
  310. # L2
  311. tout = out0
  312. for s in range(4):
  313. tout = self._Mconv_forward(tout, self.models['Mconv1_stage%d_L2' % s])
  314. for v in range(2, 6):
  315. tout = self._Mconv_forward(tout, self.models['Mconv%d_stage%d_L2' % (v, s)])
  316. tout = self.models['Mconv6_7_stage%d_L2' % s][0](tout)
  317. tout = self.models['Mconv6_7_stage%d_L2' % s][1](tout)
  318. outL2 = tout
  319. tout = torch.cat([out0, tout], 1)
  320. # L1 stage0
  321. # tout = torch.cat([out0,outL2],1)
  322. tout = self._Mconv_forward(tout, self.models['Mconv1_stage0_L1'])
  323. for v in range(2, 6):
  324. tout = self._Mconv_forward(tout, self.models['Mconv%d_stage0_L1' % v])
  325. tout = self.models['Mconv6_7_stage0_L1'][0](tout)
  326. tout = self.models['Mconv6_7_stage0_L1'][1](tout)
  327. outS0L1 = tout
  328. tout = torch.cat([out0, outS0L1, outL2], 1)
  329. # L1 stage1
  330. tout = self._Mconv_forward(tout, self.models['Mconv1_stage1_L1'])
  331. for v in range(2, 6):
  332. tout = self._Mconv_forward(tout, self.models['Mconv%d_stage1_L1' % v])
  333. tout = self.models['Mconv6_7_stage1_L1'][0](tout)
  334. outS1L1 = self.models['Mconv6_7_stage1_L1'][1](tout)
  335. return outS1L1, outL2