model.py 16 KB

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