model.py 15 KB

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