├── .ipynb_checkpoints ├── TestAugmentation-checkpoint.ipynb ├── TestSymbol-checkpoint.ipynb ├── TestWeight-checkpoint.ipynb ├── testModel-checkpoint.ipynb └── trainModel-checkpoint.ipynb ├── README.md ├── TrainWeight.py ├── config ├── __init__.py ├── __init__.pyc └── config.py ├── cython ├── __init__.py ├── __init__.pyc ├── heatmap.c ├── heatmap.pyx ├── heatmap.so ├── pafmap.c ├── pafmap.pyx ├── rebuild.sh └── setup.py ├── evaluate.py ├── evaluation_Pose_testImage_easy.ipynb ├── evaluation_Pose_trainImage_easy.ipynb ├── generateLabelCPMWeight.py ├── modelCPMWeight.py ├── pose_io ├── .ipynb_checkpoints │ └── annotation-checkpoint.ipynb ├── AI_data_val.json └── parse_label.py ├── sample_image ├── multiperson.jpg └── ski.jpg └── test_generatelabel.ipynb /.ipynb_checkpoints/TestSymbol-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/trainModel-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true, 8 | "deletable": true, 9 | "editable": true 10 | }, 11 | "outputs": [], 12 | "source": [ 13 | "import sys\n", 14 | "sys.path.append('../../practice_demo')" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "base_lr: 0.000040" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 2, 27 | "metadata": { 28 | "collapsed": false, 29 | "deletable": true, 30 | "editable": true 31 | }, 32 | "outputs": [], 33 | "source": [ 34 | "from GenerateLabelCPM import *\n", 35 | "from modelCPM import *" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 3, 41 | "metadata": { 42 | "collapsed": true, 43 | "deletable": true, 44 | "editable": true 45 | }, 46 | "outputs": [], 47 | "source": [ 48 | "sym = mxnetModule()" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 4, 54 | "metadata": { 55 | "collapsed": false, 56 | "deletable": true, 57 | "editable": true 58 | }, 59 | "outputs": [ 60 | { 61 | "name": "stdout", 62 | "output_type": "stream", 63 | "text": [ 64 | "self._data_shapes: [DataDesc[data,(1, 3, 368, 368),,NCHW]]\n", 65 | "self._label_shapes: [DataDesc[heatmaplabel,(1, 19, 46, 46),,NCHW], DataDesc[partaffinityglabel,(1, 38, 46, 46),,NCHW]]\n" 66 | ] 67 | } 68 | ], 69 | "source": [ 70 | "cmodel = intializeModel(sym)" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 5, 76 | "metadata": { 77 | "collapsed": false, 78 | "deletable": true, 79 | "editable": true 80 | }, 81 | "outputs": [], 82 | "source": [ 83 | "cocodata = cocoIter('pose_io/data.json',\n", 84 | " 'data', (1,3, 368,368),\n", 85 | " ['heatmaplabel','partaffinityglabel'],\n", 86 | " [(1, 19, 46, 46),(1,38,46,46)])" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 6, 92 | "metadata": { 93 | "collapsed": false, 94 | "deletable": true, 95 | "editable": true 96 | }, 97 | "outputs": [ 98 | { 99 | "name": "stdout", 100 | "output_type": "stream", 101 | "text": [ 102 | "-------------\n", 103 | "1.05934e-08\n", 104 | "3.5435e-08\n", 105 | "-------------\n", 106 | "0.000615781\n", 107 | "0.00125011\n", 108 | "-------------\n", 109 | "0.000571712\n", 110 | "0.00169049\n", 111 | "-------------\n", 112 | "0.00196791\n", 113 | "0.00841065\n", 114 | "-------------\n", 115 | "0.00296555\n", 116 | "0.0287082\n", 117 | "-------------\n", 118 | "0.000649921\n", 119 | "0.00293906\n", 120 | "-------------\n", 121 | "0.00079231\n", 122 | "0.00291298\n", 123 | "-------------\n", 124 | "0.000618997\n", 125 | "0.00346037\n", 126 | "-------------\n", 127 | "0.000495606\n", 128 | "0.00148083\n", 129 | "-------------\n", 130 | "2.88742e-06\n", 131 | "1.80777e-07\n", 132 | "-------------\n", 133 | "0.000710441\n", 134 | "0.00439732\n", 135 | "-------------\n", 136 | "0.000111578\n", 137 | "0.000323599\n", 138 | "-------------\n", 139 | "3.63359e-05\n", 140 | "2.1786e-06\n", 141 | "-------------\n", 142 | "0.00113143\n", 143 | "0.00535995\n", 144 | "-------------\n", 145 | "0.000612072\n", 146 | "0.00176775\n", 147 | "-------------\n", 148 | "0.000292848\n", 149 | "0.000567834\n", 150 | "-------------\n", 151 | "0.000487854\n", 152 | "0.00164848\n", 153 | "-------------\n", 154 | "0.000386941\n", 155 | "0.00109444\n", 156 | "-------------\n", 157 | "2.1515e-07\n", 158 | "4.21183e-08\n", 159 | "-------------\n", 160 | "8.94964e-08\n", 161 | "4.56508e-08\n", 162 | "-------------\n", 163 | "0.00032792\n", 164 | "0.00330106\n", 165 | "-------------\n", 166 | "0.00158406\n", 167 | "0.00634685\n", 168 | "-------------\n", 169 | "0.000428516\n", 170 | "0.00336787\n", 171 | "-------------\n", 172 | "0.000347164\n", 173 | "0.00344747\n", 174 | "-------------\n", 175 | "0.000423805\n", 176 | "0.00104877\n", 177 | "-------------\n", 178 | "0.00074387\n", 179 | "0.00548392\n", 180 | "-------------\n", 181 | "0.000325731\n", 182 | "0.00109593\n", 183 | "-------------\n", 184 | "0.000287429\n", 185 | "0.00131158\n", 186 | "-------------\n", 187 | "0.000215979\n", 188 | "0.00269716\n", 189 | "-------------\n", 190 | "2.46646e-08\n", 191 | "3.54313e-08\n", 192 | "-------------\n", 193 | "5.56715e-07\n", 194 | "3.54357e-08\n", 195 | "-------------\n", 196 | "0.000176681\n", 197 | "0.00315297\n", 198 | "-------------\n", 199 | "0.000671439\n", 200 | "0.00410188\n", 201 | "-------------\n", 202 | "0.00107246\n", 203 | "0.0159403\n", 204 | "-------------\n", 205 | "0.00039778\n", 206 | "0.00433199\n", 207 | "-------------\n", 208 | "5.65321e-06\n", 209 | "9.59642e-08\n", 210 | "-------------\n", 211 | "0.00157653\n", 212 | "0.0133146\n", 213 | "-------------\n", 214 | "0.00103656\n", 215 | "0.00469402\n", 216 | "-------------\n", 217 | "0.00063753\n", 218 | "0.00174854\n", 219 | "-------------\n", 220 | "0.000685532\n", 221 | "0.00197498\n", 222 | "-------------\n", 223 | "2.31636e-08\n", 224 | "0.00909295\n", 225 | "-------------\n", 226 | "1.21051e-08\n", 227 | "3.54423e-08\n", 228 | "-------------\n", 229 | "0.00101551\n", 230 | "0.00303588\n", 231 | "-------------\n", 232 | "0.00100649\n", 233 | "0.00290133\n", 234 | "-------------\n", 235 | "1.05966e-08\n", 236 | "3.54297e-08\n", 237 | "-------------\n", 238 | "1.05966e-08\n", 239 | "3.54296e-08\n", 240 | "-------------\n", 241 | "1.05966e-08\n", 242 | "3.54295e-08\n", 243 | "-------------\n", 244 | "0.00682892\n", 245 | "0.032725\n", 246 | "-------------\n", 247 | "0.000523719\n", 248 | "0.0025379\n", 249 | "-------------\n", 250 | "0.0012056\n", 251 | "0.00147341\n", 252 | "1736.69319701\n" 253 | ] 254 | } 255 | ], 256 | "source": [ 257 | "import time\n", 258 | "startt = time.time()\n", 259 | "\n", 260 | "cmodel.init_optimizer(optimizer='sgd', \n", 261 | " optimizer_params=(('learning_rate', 0.00004), ))\n", 262 | "\n", 263 | "# train one epoch, i.e. going over the data iter one pass\n", 264 | "for batch in cocodata:\n", 265 | " cmodel.forward(batch, is_train=True) # compute predictions\n", 266 | " prediction=cmodel.get_outputs()\n", 267 | " #print prediction\n", 268 | " #print prediction[11].asnumpy().reshape(-1).shape\n", 269 | " #print batch.label[0].asnumpy().reshape(-1).shape\n", 270 | " print '-------------'\n", 271 | " print np.mean((prediction[11].asnumpy().reshape(-1)-batch.label[0].asnumpy().reshape(-1))**2)\n", 272 | " print np.mean((prediction[10].asnumpy().reshape(-1)-batch.label[1].asnumpy().reshape(-1))**2)\n", 273 | " cmodel.backward() # compute gradients\n", 274 | " cmodel.update() # update parameters using SGD\n", 275 | " \n", 276 | "endt = time.time()\n", 277 | "print endt-startt" 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": null, 283 | "metadata": { 284 | "collapsed": true 285 | }, 286 | "outputs": [], 287 | "source": [ 288 | "1736/60" 289 | ] 290 | }, 291 | { 292 | "cell_type": "markdown", 293 | "metadata": { 294 | "deletable": true, 295 | "editable": true 296 | }, 297 | "source": [ 298 | "### Fake Data" 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "execution_count": null, 304 | "metadata": { 305 | "collapsed": true, 306 | "deletable": true, 307 | "editable": true 308 | }, 309 | "outputs": [], 310 | "source": [ 311 | "cocoIterFake = cocoIter('pose_io/data.json',\n", 312 | " 'data', (1,3, 368,368),\n", 313 | " ['heatmaplabel','partaffinityglabel'],\n", 314 | " [(1, 19, 46, 46),(1,38,46,46)])" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": null, 320 | "metadata": { 321 | "collapsed": false, 322 | "deletable": true, 323 | "editable": true 324 | }, 325 | "outputs": [], 326 | "source": [ 327 | "import time\n", 328 | "startt = time.time()\n", 329 | "\n", 330 | "cmodel.init_optimizer(optimizer='sgd', \n", 331 | " optimizer_params=(('learning_rate', 0.01), ))\n", 332 | "\n", 333 | "# train one epoch, i.e. going over the data iter one pass\n", 334 | "for batch in cocoIterFake:\n", 335 | " cmodel.forward(batch, is_train=True) # compute predictions\n", 336 | " prediction=cmodel.get_outputs()\n", 337 | " #print prediction\n", 338 | " #print prediction[11].asnumpy().reshape(-1).shape\n", 339 | " #print batch.label[0].asnumpy().reshape(-1).shape\n", 340 | " print '-------------'\n", 341 | " print np.mean((prediction[11].asnumpy().reshape(-1)-batch.label[0].asnumpy().reshape(-1))**2)\n", 342 | " print np.mean((prediction[10].asnumpy().reshape(-1)-batch.label[1].asnumpy().reshape(-1))**2)\n", 343 | " cmodel.backward() # compute gradients\n", 344 | " cmodel.update() # update parameters using SGD\n", 345 | " \n", 346 | "endt = time.time()\n", 347 | "print endt-startt" 348 | ] 349 | }, 350 | { 351 | "cell_type": "markdown", 352 | "metadata": { 353 | "collapsed": true, 354 | "deletable": true, 355 | "editable": true 356 | }, 357 | "source": [ 358 | "### Comparsion" 359 | ] 360 | }, 361 | { 362 | "cell_type": "code", 363 | "execution_count": null, 364 | "metadata": { 365 | "collapsed": false 366 | }, 367 | "outputs": [], 368 | "source": [ 369 | "import matplotlib.pylab as plt\n", 370 | "%matplotlib inline\n", 371 | "\n", 372 | "cocodata = cocoIter('pose_io/data.json',\n", 373 | " 'data', (1,3, 368,368),\n", 374 | " ['heatmaplabel','partaffinityglabel'],\n", 375 | " [(1, 19, 46, 46),(1,38,46,46)])\n", 376 | "\n", 377 | "dataitera = cocodata.next()\n", 378 | "\n", 379 | "newsimage = dataitera.data[0].asnumpy()[0]\n", 380 | "\n", 381 | "heatmap1 = dataitera.label[0].asnumpy()[0]\n", 382 | "\n", 383 | "pagmap1 = dataitera.label[1].asnumpy()[0]\n", 384 | "\n", 385 | "#oriImg[:,:,[2,1,0]]\n", 386 | "f = plt.imshow(newsimage[0,:,:]) # reorder it before displaying\n", 387 | "\n", 388 | "plt.figure(); # plt.axis('off')" 389 | ] 390 | }, 391 | { 392 | "cell_type": "code", 393 | "execution_count": null, 394 | "metadata": { 395 | "collapsed": false 396 | }, 397 | "outputs": [], 398 | "source": [ 399 | "heatmap1.shape" 400 | ] 401 | }, 402 | { 403 | "cell_type": "code", 404 | "execution_count": null, 405 | "metadata": { 406 | "collapsed": false 407 | }, 408 | "outputs": [], 409 | "source": [ 410 | "pagmap1.shape" 411 | ] 412 | }, 413 | { 414 | "cell_type": "code", 415 | "execution_count": null, 416 | "metadata": { 417 | "collapsed": true 418 | }, 419 | "outputs": [], 420 | "source": [ 421 | "import matplotlib" 422 | ] 423 | }, 424 | { 425 | "cell_type": "code", 426 | "execution_count": null, 427 | "metadata": { 428 | "collapsed": false 429 | }, 430 | "outputs": [], 431 | "source": [ 432 | "for i in range(1,2):\n", 433 | " plt.imshow(cv.resize(heatmap1[i,:,:], (368, 368)))\n", 434 | "\n", 435 | "plt.imshow(newsimage[0,:,:], alpha=0.5)\n", 436 | "\n", 437 | "fig = matplotlib.pyplot.gcf()\n", 438 | "fig.set_size_inches(5, 5)\n", 439 | "\n", 440 | "plt.show()" 441 | ] 442 | }, 443 | { 444 | "cell_type": "code", 445 | "execution_count": null, 446 | "metadata": { 447 | "collapsed": false 448 | }, 449 | "outputs": [], 450 | "source": [ 451 | "cmodel.forward(dataitera) # compute predictions\n", 452 | "prediction=cmodel.get_outputs()\n", 453 | "\n", 454 | "heatmap2 = prediction[11].asnumpy()[0]\n", 455 | "\n", 456 | "plt.figure(); # plt.axis('off')\n", 457 | "\n", 458 | "for i in range(18,19):\n", 459 | " plt.imshow(cv.resize(heatmap2[i,:,:], (368, 368)), alpha=(1 - 1.0 * i / 10))\n", 460 | "\n", 461 | "plt.imshow(newsimage[0,:,:], alpha=0.1)\n", 462 | "\n", 463 | "plt.show()" 464 | ] 465 | }, 466 | { 467 | "cell_type": "code", 468 | "execution_count": null, 469 | "metadata": { 470 | "collapsed": false 471 | }, 472 | "outputs": [], 473 | "source": [ 474 | "differentmap = heatmap2[7,:,:]-heatmap1[7,:,:]\n", 475 | "\n", 476 | "plt.figure(); # plt.axis('off')\n", 477 | "\n", 478 | "plt.imshow(cv.resize(differentmap, (368, 368)))\n", 479 | "\n", 480 | "plt.imshow(newsimage[0,:,:], alpha=0.1)\n", 481 | "\n", 482 | "plt.show()" 483 | ] 484 | }, 485 | { 486 | "cell_type": "code", 487 | "execution_count": null, 488 | "metadata": { 489 | "collapsed": false 490 | }, 491 | "outputs": [], 492 | "source": [ 493 | "differentmap[0,1]" 494 | ] 495 | }, 496 | { 497 | "cell_type": "code", 498 | "execution_count": null, 499 | "metadata": { 500 | "collapsed": false 501 | }, 502 | "outputs": [], 503 | "source": [ 504 | "differentmap = heatmap2[10,:,:]-heatmap1[10,:,:]\n", 505 | "\n", 506 | "plt.figure(); # plt.axis('off')\n", 507 | "\n", 508 | "plt.imshow(cv.resize(differentmap, (368, 368)))\n", 509 | "\n", 510 | "plt.imshow(newsimage[0,:,:], alpha=0.1)\n", 511 | "\n", 512 | "plt.show()" 513 | ] 514 | }, 515 | { 516 | "cell_type": "code", 517 | "execution_count": null, 518 | "metadata": { 519 | "collapsed": false 520 | }, 521 | "outputs": [], 522 | "source": [ 523 | "differentmap = heatmap2[18,:,:]-heatmap1[18,:,:]\n", 524 | "\n", 525 | "plt.figure(); # plt.axis('off')\n", 526 | "\n", 527 | "plt.imshow(cv.resize(differentmap, (368, 368)))\n", 528 | "\n", 529 | "plt.imshow(newsimage[0,:,:], alpha=0.1)\n", 530 | "\n", 531 | "plt.show()" 532 | ] 533 | }, 534 | { 535 | "cell_type": "code", 536 | "execution_count": null, 537 | "metadata": { 538 | "collapsed": true, 539 | "deletable": true, 540 | "editable": true 541 | }, 542 | "outputs": [], 543 | "source": [] 544 | } 545 | ], 546 | "metadata": { 547 | "kernelspec": { 548 | "display_name": "Python 2", 549 | "language": "python", 550 | "name": "python2" 551 | }, 552 | "language_info": { 553 | "codemirror_mode": { 554 | "name": "ipython", 555 | "version": 2 556 | }, 557 | "file_extension": ".py", 558 | "mimetype": "text/x-python", 559 | "name": "python", 560 | "nbconvert_exporter": "python", 561 | "pygments_lexer": "ipython2", 562 | "version": "2.7.12" 563 | } 564 | }, 565 | "nbformat": 4, 566 | "nbformat_minor": 2 567 | } 568 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Implementation of "Realtime Multi-Person 2D Pose Estimation using Part Affinity Fields" for [AI challenger 2 | keypoint competition](https://challenger.ai/competition/keypoint/subject) 3 | 4 | ## Train demo 5 | 6 | 1. install cython package 7 | 8 | ``` 9 | ./cython/rebuild.sh 10 | 11 | ``` 12 | 13 | 2. Generate intermediate files 14 | 15 | change folder name and json name in pose_io/parse_label.py 16 | 17 | ``` 18 | path1 = '/data/guest_users/liangdong/liangdong/practice_demo/AIchallenger/keypoint_validation_annotations_20170911.json' 19 | trainimagepath = '/data/guest_users/liangdong/liangdong/practice_demo/AIchallenger/validation_image/keypoint_validation_images_20170911/' 20 | ``` 21 | 22 | ``` 23 | python pose_io/parse_label.py 24 | ``` 25 | 26 | 3. Train 27 | 28 | ``` 29 | python TrainWeight.py 30 | ``` 31 | 32 | You can download mxnet model and parameters for vgg19 from [here](http://data.mxnet.io/models/imagenet/vgg/) 33 | 34 | 35 | ### Cite paper Realtime Multi-Person 2D Pose Estimation using Part Affinity Fields 36 | 37 | ``` 38 | @article{cao2016realtime, 39 | title={Realtime Multi-Person 2D Pose Estimation using Part Affinity Fields}, 40 | author={Zhe Cao and Tomas Simon and Shih-En Wei and Yaser Sheikh}, 41 | journal={arXiv preprint arXiv:1611.08050}, 42 | year={2016} 43 | } 44 | ``` 45 | 46 | ## Other implementations of Realtime Multi-Person 2D Pose Estimation 47 | 48 | [Original caffe training model](https://github.com/CMU-Perceptual-Computing-Lab/caffe_rtpose) 49 | 50 | [Original data preparation and demo](https://github.com/ZheC/Realtime_Multi-Person_Pose_Estimation) 51 | 52 | [Pytorch](https://github.com/tensorboy/pytorch_Realtime_Multi-Person_Pose_Estimation) 53 | 54 | [keras](https://github.com/raymon-tian/keras_Realtime_Multi-Person_Pose_Estimation) 55 | 56 | [mxnet](https://github.com/dragonfly90/mxnet_Realtime_Multi-Person_Pose_Estimation) 57 | -------------------------------------------------------------------------------- /TrainWeight.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.append('/data/guest_users/liangdong/liangdong/practice_demo') 3 | from modelCPMWeight import * 4 | from config.config import config 5 | 6 | class AIChallengerIterweightBatch: 7 | def __init__(self, datajson, 8 | data_names, data_shapes, label_names, 9 | label_shapes, batch_size = 1): 10 | 11 | self._data_shapes = data_shapes 12 | self._label_shapes = label_shapes 13 | self._provide_data = zip([data_names], [data_shapes]) 14 | self._provide_label = zip(label_names, label_shapes) * 6 15 | self._batch_size = batch_size 16 | 17 | with open(datajson, 'r') as f: 18 | data = json.load(f) 19 | 20 | self.num_batches = len(data)/20*20 21 | 22 | self.data = data 23 | 24 | self.cur_batch = 0 25 | 26 | self.keys = data.keys() 27 | 28 | def __iter__(self): 29 | return self 30 | 31 | def reset(self): 32 | self.cur_batch = 0 33 | 34 | def __next__(self): 35 | return self.next() 36 | 37 | @property 38 | def provide_data(self): 39 | return self._provide_data 40 | 41 | @property 42 | def provide_label(self): 43 | return self._provide_label 44 | 45 | def next(self): 46 | if self.cur_batch < self.num_batches: 47 | 48 | transposeImage_batch = [] 49 | heatmap_batch = [] 50 | pagmap_batch = [] 51 | heatweight_batch = [] 52 | vecweight_batch = [] 53 | 54 | for i in range(batch_size): 55 | ''' 56 | if self.cur_batch >= 45174: 57 | break 58 | ''' 59 | image, mask, heatmap, pagmap = getImageandLabel(self.data[self.keys[self.cur_batch]]) 60 | maskscale = mask[0:368:8, 0:368:8, 0] 61 | heatweight = np.ones((numofparts, 46, 46)) 62 | vecweight = np.ones((numoflinks*2, 46, 46)) 63 | 64 | for i in range(numofparts): 65 | heatweight[i,:,:] = maskscale 66 | 67 | for i in range(numoflinks*2): 68 | vecweight[i,:,:] = maskscale 69 | 70 | transposeImage = np.transpose(np.float32(image), (2,0,1))/256 - 0.5 71 | 72 | self.cur_batch += 1 73 | 74 | transposeImage_batch.append(transposeImage) 75 | heatmap_batch.append(heatmap) 76 | pagmap_batch.append(pagmap) 77 | heatweight_batch.append(heatweight) 78 | vecweight_batch.append(vecweight) 79 | 80 | return DataBatchweight(mx.nd.array(transposeImage_batch), 81 | mx.nd.array(heatmap_batch), 82 | mx.nd.array(pagmap_batch), 83 | mx.nd.array(heatweight_batch), 84 | mx.nd.array(vecweight_batch)) 85 | else: 86 | raise StopIteration 87 | 88 | start_prefix = 47 89 | class poseModule(mx.mod.Module): 90 | 91 | def fit(self, train_data, num_epoch, batch_size, carg_params=None, begin_epoch=0): 92 | 93 | assert num_epoch is not None, 'please specify number of epochs' 94 | 95 | self.bind(data_shapes=[('data', (batch_size, 3, 368, 368))], label_shapes=[ 96 | ('heatmaplabel', (batch_size, numofparts, 46, 46)), 97 | ('partaffinityglabel', (batch_size, numoflinks*2, 46, 46)), 98 | ('heatweight', (batch_size, numofparts, 46, 46)), 99 | ('vecweight', (batch_size, numoflinks*2, 46, 46))]) 100 | 101 | 102 | # self.init_params(mx.initializer.Xavier(rnd_type='uniform', factor_type='avg', magnitude=1)) 103 | # mx.initializer.Uniform(scale=0.07), 104 | # mx.initializer.Uniform(scale=0.01) 105 | # mx.initializer.Xavier(rnd_type='uniform', factor_type='avg', magnitude=0.01) 106 | self.init_params(arg_params = carg_params, aux_params={}, allow_missing = True) 107 | #self.set_params(arg_params = carg_params, aux_params={}, 108 | # allow_missing = True) 109 | self.init_optimizer(optimizer='sgd', optimizer_params=(('learning_rate', 0.00004), )) 110 | losserror_list = [] 111 | 112 | for epoch in range(begin_epoch, num_epoch): 113 | tic = time.time() 114 | nbatch = 0 115 | data_iter = iter(train_data) 116 | end_of_batch = False 117 | next_data_batch = next(data_iter) 118 | i=0 119 | sumerror=0 120 | while not end_of_batch: 121 | data_batch = next_data_batch 122 | cmodel.forward(data_batch, is_train=True) # compute predictions 123 | prediction=cmodel.get_outputs() 124 | i=i+1 125 | sumloss=0 126 | numpixel=0 127 | print 'iteration: ', i 128 | 129 | ''' 130 | print 'length of prediction:', len(prediction) 131 | for j in range(len(prediction)): 132 | 133 | lossiter = prediction[j].asnumpy() 134 | cls_loss = np.sum(lossiter) 135 | print 'loss: ', cls_loss 136 | sumloss += cls_loss 137 | numpixel +=lossiter.shape[0] 138 | 139 | ''' 140 | 141 | 142 | 143 | lossiter = prediction[1].asnumpy() 144 | cls_loss = np.sum(lossiter)/batch_size 145 | sumerror = sumerror + cls_loss 146 | print 'start heat: ', cls_loss 147 | 148 | lossiter = prediction[0].asnumpy() 149 | cls_loss = np.sum(lossiter)/batch_size 150 | sumerror = sumerror + cls_loss 151 | print 'start paf: ', cls_loss 152 | 153 | lossiter = prediction[-1].asnumpy() 154 | cls_loss = np.sum(lossiter)/batch_size 155 | sumerror = sumerror + cls_loss 156 | print 'end heat: ', cls_loss 157 | 158 | lossiter = prediction[-2].asnumpy() 159 | cls_loss = np.sum(lossiter)/batch_size 160 | sumerror = sumerror + cls_loss 161 | print 'end paf: ', cls_loss 162 | 163 | 164 | cmodel.backward() 165 | self.update() 166 | ''' 167 | if i > 10: 168 | break 169 | ''' 170 | try: 171 | next_data_batch = next(data_iter) 172 | self.prepare(next_data_batch) 173 | except StopIteration: 174 | end_of_batch = True 175 | nbatch += 1 176 | 177 | 178 | print '------Error-------' 179 | print sumerror/i 180 | losserror_list.append(sumerror/i) 181 | 182 | toc = time.time() 183 | self.logger.info('Epoch[%d] Time cost=%.3f', epoch, (toc-tic)) 184 | 185 | arg_params, aux_params = self.get_params() 186 | self.set_params(arg_params, aux_params) 187 | self.save_checkpoint(config.TRAIN.output_model, start_prefix+epoch+1) 188 | 189 | train_data.reset() 190 | print losserror_list 191 | text_file = open("OutputLossError.txt", "w") 192 | text_file.write(' '.join([str(i) for i in losserror_list])) 193 | text_file.close() 194 | 195 | sym = '' 196 | if config.TRAIN.head == 'vgg': 197 | sym = CPMModel() 198 | 199 | ## Load parameters from vgg 200 | warmupModel = '/data/guest_users/liangdong/liangdong/practice_demo/mxnet_CPM/model/vgg19' 201 | testsym, arg_params, aux_params = mx.model.load_checkpoint(warmupModel, 0) 202 | newargs = {} 203 | for ikey in config.TRAIN.vggparams: 204 | newargs[ikey] = arg_params[ikey] 205 | 206 | batch_size = 10 207 | aidata = AIChallengerIterweightBatch('pose_io/AI_data_val.json', # 'pose_io/COCO_data.json', 208 | 'data', (batch_size, 3, 368, 368), 209 | ['heatmaplabel','partaffinityglabel','heatweight','vecweight'], 210 | [(batch_size, numofparts, 46, 46), 211 | (batch_size, numoflinks*2, 46, 46), 212 | (batch_size, numofparts, 46, 46), 213 | (batch_size, numoflinks*2, 46, 46)]) 214 | 215 | cmodel = poseModule(symbol=sym, context=mx.gpu(0), 216 | label_names=['heatmaplabel', 217 | 'partaffinityglabel', 218 | 'heatweight', 219 | 'vecweight']) 220 | starttime = time.time() 221 | 222 | 223 | output_prefix = config.TRAIN.output_model 224 | testsym, newargs, aux_params = mx.model.load_checkpoint(output_prefix, start_prefix) 225 | 226 | iteration = 10 227 | cmodel.fit(aidata, num_epoch = iteration, batch_size = batch_size, carg_params = newargs) 228 | cmodel.save_checkpoint(config.TRAIN.output_model, start_prefix + iteration) 229 | endtime = time.time() 230 | 231 | print 'cost time: ', (endtime-starttime)/60 232 | 233 | -------------------------------------------------------------------------------- /config/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PoseAIChallenger/mxnet_pose_for_AI_challenger/d81c7a2aa83768134e53101e451c25e122ed799f/config/__init__.py -------------------------------------------------------------------------------- /config/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PoseAIChallenger/mxnet_pose_for_AI_challenger/d81c7a2aa83768134e53101e451c25e122ed799f/config/__init__.pyc -------------------------------------------------------------------------------- /config/config.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from easydict import EasyDict as edict 3 | 4 | config = edict() 5 | 6 | config.boxsize = 368 7 | 8 | config.TEST = edict() 9 | 10 | config.TEST.scale_search = [0.5, 1, 1.5, 2, 2.3] 11 | 12 | config.TEST.imgIds_num = -1 13 | 14 | config.TEST.model_path = 'testConfigModel' 15 | 16 | #config.TEST.model_path = '../realtimePose' 17 | 18 | config.TEST.epoch = 0 19 | 20 | config.TEST.imgIds_num = 5 21 | 22 | config.TRAIN = edict() 23 | 24 | config.TRAIN.num_epoch = 1 25 | 26 | config.TRAIN.initial_model = '/data/guest_users/liangdong/liangdong/practice_demo/realtimePose' 27 | 28 | config.TRAIN.output_model = 'testConfigModel' 29 | 30 | config.TRAIN.crop_size_x = 368 31 | 32 | config.TRAIN.crop_size_y = 368 33 | 34 | config.TRAIN.center_perterb_max = 0 35 | 36 | config.TRAIN.scale_prob = 1 37 | 38 | config.TRAIN.scale_min = 0.9 39 | 40 | config.TRAIN.scale_max = 1.2 41 | 42 | config.TRAIN.flip = False 43 | 44 | config.TRAIN.target_dist = 0.6 45 | 46 | config.TRAIN.max_rotate_degree = 0 47 | 48 | config.TRAIN.scale_set = False 49 | 50 | config.TRAIN.head = 'vgg' 51 | 52 | config.TRAIN.vggparams = ['conv1_1_weight', 53 | 'conv1_1_bias', 54 | 'conv1_2_weight', 55 | 'conv1_2_bias', 56 | 'conv2_1_weight', 57 | 'conv2_1_bias', 58 | 'conv2_2_weight', 59 | 'conv2_2_bias', 60 | 'conv3_1_weight', 61 | 'conv3_1_bias', 62 | 'conv3_2_weight', 63 | 'conv3_2_bias', 64 | 'conv3_3_weight', 65 | 'conv3_3_bias', 66 | 'conv3_4_weight', 67 | 'conv3_4_bias', 68 | 'conv4_1_weight', 69 | 'conv4_1_bias', 70 | 'conv4_2_weight', 71 | 'conv4_2_bias'] 72 | -------------------------------------------------------------------------------- /cython/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PoseAIChallenger/mxnet_pose_for_AI_challenger/d81c7a2aa83768134e53101e451c25e122ed799f/cython/__init__.py -------------------------------------------------------------------------------- /cython/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PoseAIChallenger/mxnet_pose_for_AI_challenger/d81c7a2aa83768134e53101e451c25e122ed799f/cython/__init__.pyc -------------------------------------------------------------------------------- /cython/heatmap.pyx: -------------------------------------------------------------------------------- 1 | cimport cython 2 | import numpy as np 3 | cimport numpy as np 4 | 5 | DTYPE = np.float 6 | ctypedef np.float_t DTYPE_t 7 | 8 | def putGaussianMaps(np.ndarray[DTYPE_t, ndim = 2] entry, DTYPE_t rows, 9 | DTYPE_t cols, DTYPE_t center_x, DTYPE_t center_y, DTYPE_t stride, 10 | int grid_x, int grid_y, DTYPE_t sigma): 11 | cdef DTYPE_t start = stride / 2.0 - 0.5 12 | cdef DTYPE_t x, y, d2 13 | 14 | for g_y in range(grid_y): 15 | for g_x in range(grid_x): 16 | x = start + g_x * stride 17 | y = start + g_y * stride 18 | d2 = (x - center_x) * (x - center_x) + (y - center_y) * (y - center_y) 19 | exponent = d2 / 2.0 / sigma / sigma 20 | if (exponent > 4.6052): 21 | continue 22 | entry[g_y, g_x] += np.exp(-exponent) 23 | if (entry[g_y, g_x] > 1): 24 | entry[g_y, g_x] = 1 25 | return entry 26 | -------------------------------------------------------------------------------- /cython/heatmap.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PoseAIChallenger/mxnet_pose_for_AI_challenger/d81c7a2aa83768134e53101e451c25e122ed799f/cython/heatmap.so -------------------------------------------------------------------------------- /cython/pafmap.pyx: -------------------------------------------------------------------------------- 1 | cimport cython 2 | import numpy as np 3 | cimport numpy as np 4 | 5 | DTYPE = np.float 6 | ctypedef np.float_t DTYPE_t 7 | 8 | def putVecMaps(np.ndarray[DTYPE_t, ndim = 2] entryX, np.ndarray[DTYPE_t, ndim = 2] entryY, 9 | np.ndarray[DTYPE_t, ndim = 2] count, DTYPE_t center1_x, DTYPE_t center1_y, 10 | DTYPE_t center2_x, DTYPE_t center2_y, DTYPE_t stride, DTYPE_t grid_x, DTYPE_t grid_y, 11 | DTYPE_t sigma, DTYPE_t thre): 12 | 13 | cdef DTYPE_t centerA_x = 0.125 * center1_x 14 | cdef DTYPE_t centerA_y = 0.125 * center1_y 15 | 16 | cdef DTYPE_t centerB_x = 0.125 * center2_x 17 | cdef DTYPE_t centerB_y = 0.125 * center2_y 18 | 19 | cdef DTYPE_t bc_x = centerB_x - centerA_x 20 | cdef DTYPE_t bc_y = centerB_y - centerA_y 21 | 22 | cdef int min_x = max(int(round(min(centerA_x, centerB_x)) - thre), 0) 23 | cdef int max_x = min(int(round(max(centerA_x, centerB_x))) + thre, grid_x) 24 | cdef int min_y = max(int(round(min(centerA_y, centerB_y) - thre)), 0) 25 | cdef int max_y = min(int(round(max(centerA_y, centerB_y) + thre)), grid_y) 26 | 27 | cdef DTYPE_t norm_bc = np.sqrt(bc_x * bc_x + bc_y * bc_y) 28 | if norm_bc == 0: 29 | return 30 | 31 | bc_x = bc_x/norm_bc 32 | bc_y = bc_y/norm_bc 33 | 34 | cdef DTYPE_t ba_x, ba_y 35 | cdef DTYPE_t dist 36 | cdef DTYPE_t cnt 37 | 38 | for g_y in range(min_y, max_y): 39 | for g_x in range(min_x, max_x): 40 | ba_x = g_x - centerA_x 41 | ba_y = g_y - centerA_y 42 | dist = np.absolute(ba_x * bc_y - ba_y * bc_x) 43 | 44 | if (dist <= thre): 45 | cnt = count[g_y, g_x] 46 | if (cnt == 0): 47 | entryX[g_y, g_x] = bc_x 48 | entryY[g_y, g_x] = bc_y 49 | else: 50 | entryX[g_y, g_x] = (entryX[g_y, g_x] * cnt + bc_x) / (cnt + 1) 51 | entryY[g_y, g_x] = (entryY[g_y, g_x] * cnt + bc_y) / (cnt + 1) 52 | count[g_y, g_x] = cnt + 1 53 | -------------------------------------------------------------------------------- /cython/rebuild.sh: -------------------------------------------------------------------------------- 1 | rm *.so 2 | python setup.py build_ext --inplace 3 | rm -rf build 4 | -------------------------------------------------------------------------------- /cython/setup.py: -------------------------------------------------------------------------------- 1 | import os 2 | from os.path import join as pjoin 3 | from setuptools import setup 4 | from distutils.core import setup 5 | from distutils.extension import Extension 6 | from Cython.Build import cythonize 7 | from Cython.Distutils import build_ext 8 | import numpy as np 9 | 10 | def find_in_path(name, path): 11 | "Find a file in a search path" 12 | # Adapted fom 13 | # http://code.activestate.com/recipes/52224-find-a-file-given-a-search-path/ 14 | for dir in path.split(os.pathsep): 15 | binpath = pjoin(dir, name) 16 | if os.path.exists(binpath): 17 | return os.path.abspath(binpath) 18 | return None 19 | 20 | # Obtain the numpy include directory. This logic works across numpy versions. 21 | try: 22 | numpy_include = np.get_include() 23 | except AttributeError: 24 | numpy_include = np.get_numpy_include() 25 | 26 | def customize_compiler_for_nvcc(self): 27 | # tell the compiler it can processes .cu 28 | self.src_extensions.append('.cu') 29 | 30 | # save references to the default compiler_so and _comple methods 31 | default_compiler_so = self.compiler_so 32 | super = self._compile 33 | 34 | # now redefine the _compile method. This gets executed for each 35 | # object but distutils doesn't have the ability to change compilers 36 | # based on source extension: we add it. 37 | def _compile(obj, src, ext, cc_args, extra_postargs, pp_opts): 38 | if os.path.splitext(src)[1] == '.cu': 39 | # use the cuda for .cu files 40 | self.set_executable('compiler_so', CUDA['nvcc']) 41 | # use only a subset of the extra_postargs, which are 1-1 translated 42 | # from the extra_compile_args in the Extension class 43 | postargs = extra_postargs['nvcc'] 44 | else: 45 | postargs = extra_postargs['gcc'] 46 | 47 | super(obj, src, ext, cc_args, postargs, pp_opts) 48 | # reset the default compiler_so, which we might have changed for cuda 49 | self.compiler_so = default_compiler_so 50 | 51 | # inject our redefined _compile method into the class 52 | self._compile = _compile 53 | 54 | ext_modules = [ 55 | Extension( 56 | "heatmap", 57 | ["heatmap.pyx"], 58 | extra_compile_args = {'gcc': ["-Wno-cpp", "-Wno-unused-function"]}, 59 | include_dirs = [numpy_include] 60 | ), 61 | Extension( 62 | "pafmap", 63 | ["pafmap.pyx"], 64 | extra_compile_args = {'gcc': ["-Wno-cpp", "-Wno-unused-function"]}, 65 | include_dirs = [numpy_include] 66 | ), 67 | ] 68 | 69 | # run the customize_compiler 70 | class custom_build_ext(build_ext): 71 | def build_extensions(self): 72 | customize_compiler_for_nvcc(self.compiler) 73 | build_ext.build_extensions(self) 74 | 75 | setup( 76 | name = 'fpose_cython', 77 | ext_modules = ext_modules, 78 | # inject our custom trigger 79 | cmdclass = {'build_ext': custom_build_ext}, 80 | ) 81 | -------------------------------------------------------------------------------- /evaluate.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import os 3 | import mxnet as mx 4 | import numpy as np 5 | import copy 6 | import re 7 | import json 8 | from google.protobuf import text_format 9 | import json 10 | import cv2 as cv 11 | import scipy 12 | import PIL.Image 13 | import math 14 | import time 15 | import scipy 16 | import matplotlib 17 | import pylab as plt 18 | from scipy.ndimage.filters import gaussian_filter 19 | from modelCPMWeight import * 20 | from collections import namedtuple 21 | Point = namedtuple('Point', 'x y') 22 | crop_size_x = 368 23 | crop_size_y = 368 24 | center_perterb_max = 40 25 | 26 | scale_prob = 1 27 | scale_min = 0.5 28 | scale_max = 1.1 29 | target_dist = 0.6 30 | 31 | 32 | output_prefix='model/testConfigModel' 33 | sym, arg_params, aux_params = mx.model.load_checkpoint(output_prefix, 44) 34 | 35 | csym = CPMModel_test() 36 | 37 | def padRightDownCorner(img, stride, padValue): 38 | h = img.shape[0] 39 | w = img.shape[1] 40 | 41 | pad = 4 * [None] 42 | pad[0] = 0 # up 43 | pad[1] = 0 # left 44 | pad[2] = 0 if (h%stride==0) else stride - (h % stride) # down 45 | pad[3] = 0 if (w%stride==0) else stride - (w % stride) # right 46 | 47 | img_padded = img 48 | pad_up = np.tile(img_padded[0:1,:,:]*0 + padValue, (pad[0], 1, 1)) 49 | img_padded = np.concatenate((pad_up, img_padded), axis=0) 50 | pad_left = np.tile(img_padded[:,0:1,:]*0 + padValue, (1, pad[1], 1)) 51 | img_padded = np.concatenate((pad_left, img_padded), axis=1) 52 | pad_down = np.tile(img_padded[-2:-1,:,:]*0 + padValue, (pad[2], 1, 1)) 53 | img_padded = np.concatenate((img_padded, pad_down), axis=0) 54 | pad_right = np.tile(img_padded[:,-2:-1,:]*0 + padValue, (1, pad[3], 1)) 55 | img_padded = np.concatenate((img_padded, pad_right), axis=1) 56 | 57 | return img_padded, pad 58 | 59 | class DataBatch(object): 60 | def __init__(self, data, label, pad=0): 61 | self.data = [data] 62 | self.label = [label] 63 | self.pad = pad 64 | 65 | def applyDNN(oriImg, images, sym1, arg_params1, aux_params1): 66 | 67 | imageToTest_padded, pad = padRightDownCorner(images, 8, 128) 68 | transposeImage = np.transpose(np.float32(imageToTest_padded[:,:,:]), (2,0,1))/256 - 0.5 69 | testimage = transposeImage 70 | # print testimage.shape 71 | cmodel = mx.mod.Module(symbol=csym, label_names=[], context=mx.gpu(1)) 72 | # print testimage.shape 73 | cmodel.bind(data_shapes=[('data', (1, 3, testimage.shape[1], testimage.shape[2]))]) 74 | cmodel.init_params(arg_params=arg_params1, aux_params=aux_params1) 75 | # print 'init_params failed' 76 | onedata = DataBatch(mx.nd.array([testimage[:,:,:]]), 0) 77 | #print 'batch' 78 | cmodel.forward(onedata) 79 | #print 'forward' 80 | result = cmodel.get_outputs() 81 | 82 | heatmap = np.moveaxis(result[11].asnumpy()[0], 0, -1) 83 | heatmap = cv.resize(heatmap, (0,0), fx=8, fy=8, interpolation=cv.INTER_CUBIC) 84 | heatmap = heatmap[:imageToTest_padded.shape[0]-pad[2], :imageToTest_padded.shape[1]-pad[3], :] 85 | heatmap = cv.resize(heatmap, (oriImg.shape[1], oriImg.shape[0]), interpolation=cv.INTER_CUBIC) 86 | 87 | pagmap = np.moveaxis(result[10].asnumpy()[0], 0, -1) 88 | pagmap = cv.resize(pagmap, (0,0), fx=8, fy=8, interpolation=cv.INTER_CUBIC) 89 | pagmap = pagmap[:imageToTest_padded.shape[0]-pad[2], :imageToTest_padded.shape[1]-pad[3], :] 90 | pagmap = cv.resize(pagmap, (oriImg.shape[1], oriImg.shape[0]), interpolation=cv.INTER_CUBIC) 91 | 92 | # print heatmap.shape 93 | # print pagmap.shape 94 | return heatmap, pagmap 95 | 96 | def applyModel(oriImg, param, sym, arg_params, aux_params): 97 | model = param['model'] 98 | model = model[param['modelID']] 99 | boxsize = model['boxsize'] 100 | makeFigure = 0 101 | numberPoints = 1 102 | octave = param['octave'] 103 | starting_range = param['starting_range'] 104 | ending_range = param['ending_range'] 105 | boxsize = 368 106 | scale_search = [0.5, 1, 1.5, 2] 107 | multiplier = [x * boxsize*1.0/ oriImg.shape[0] for x in scale_search] 108 | 109 | 110 | heatmap_avg = np.zeros((oriImg.shape[0], oriImg.shape[1], numofparts)) 111 | pag_avg = np.zeros((oriImg.shape[0], oriImg.shape[1], numoflinks*2)) 112 | for i in range(len(multiplier)): 113 | # print i 114 | cscale = multiplier[i] 115 | imageToTest = cv.resize(oriImg, (0,0), fx=cscale, fy=cscale, interpolation=cv.INTER_CUBIC) 116 | 117 | heatmap, pagmap = applyDNN(oriImg, imageToTest, sym, arg_params, aux_params) 118 | # print(heatmap.shape) 119 | # print(pagmap.shape) 120 | heatmap_avg = heatmap_avg + heatmap / len(multiplier) 121 | pag_avg = pag_avg + pagmap / len(multiplier) 122 | # print 'add one layer' 123 | return heatmap_avg, pag_avg 124 | 125 | 126 | 127 | 128 | 129 | modelId = 1 130 | # set this part 131 | param = dict() 132 | # GPU device number (doesn't matter for CPU mode) 133 | GPUdeviceNumber = 0 134 | # Select model (default: 5) 135 | param['modelID'] = modelId 136 | # Use click mode or not. If yes (1), you will be asked to click on the center 137 | # of person to be pose-estimated (for multiple people image). If not (0), 138 | # the model will simply be applies on the whole image. 139 | param['click'] = 1 140 | # Scaling paramter: starting and ending ratio of person height to image 141 | # height, and number of scales per octave 142 | # warning: setting too small starting value on non-click mode will take 143 | # large memory 144 | # CPU mode or GPU mode 145 | param['use_gpu'] = 1 146 | param['test_mode'] = 3 147 | param['vis'] = 1 148 | param['octave'] = 6 149 | param['starting_range'] = 0.8 150 | param['ending_range'] = 2 151 | param['min_num'] = 4 152 | param['mid_num'] = 10 153 | # the larger the crop_ratio, the smaller the windowsize 154 | param['crop_ratio'] = 2.5 # 2 155 | param['bbox_ratio'] = 0.25 # 0.5 156 | # applyModel_max 157 | param['max'] = 0 158 | # use average heatmap 159 | param['merge'] = 'avg' 160 | 161 | # path of your caffe 162 | caffepath = '/home/zhecao/caffe/matlab'; 163 | 164 | if modelId == 1: 165 | param['scale_search'] = [0.5, 1, 1.5, 2] 166 | param['thre1'] = 0.1 167 | param['thre2'] = 0.05 168 | param['thre3'] = 0.5 169 | 170 | param['model'] = dict() 171 | param['model'][1] = dict() 172 | param['model'][1]['caffemodel'] = '../model/_trained_COCO/pose_iter_440000.caffemodel' 173 | param['model'][1]['deployFile'] = '../model/_trained_COCO/pose_deploy.prototxt' 174 | param['model'][1]['description'] = 'COCO Pose56 Two-level Linevec' 175 | param['model'][1]['boxsize'] = 368 176 | param['model'][1]['padValue'] = 128 177 | param['model'][1]['np'] = 18 178 | param['model'][1]['part_str'] = ['nose', 'neck', 'Rsho', 'Relb', 'Rwri', 179 | 'Lsho', 'Lelb', 'Lwri', 180 | 'Rhip', 'Rkne', 'Rank', 181 | 'Lhip', 'Lkne', 'Lank', 182 | 'Leye', 'Reye', 'Lear', 'Rear', 'pt19'] 183 | if modelId == 2: 184 | param['scale_search'] = [0.7, 1, 1.3] 185 | param['thre1'] = 0.05 186 | param['thre2'] = 0.01 187 | param['thre3'] = 3 188 | param['thre4'] = 0.1 189 | 190 | param['model'] = dict() 191 | param['model'][2] = dict() 192 | param['model'][2]['caffemodel'] = '../model/_trained_MPI/pose_iter_146000.caffemodel' 193 | param['model'][2]['deployFile'] = '../model/_trained_MPI/pose_deploy.prototxt' 194 | param['model'][2]['description'] = 'COCO Pose56 Two-level Linevec' 195 | param['model'][2]['boxsize'] = 368 196 | param['model'][2]['padValue'] = 128 197 | param['model'][2]['np'] = 18 198 | param['model'][2]['part_str'] = ['nose', 'neck', 'Rsho', 'Relb', 'Rwri', 199 | 'Lsho', 'Lelb', 'Lwri', 200 | 'Rhip', 'Rkne', 'Rank', 201 | 'Lhip', 'Lkne', 'Lank', 202 | 'Leye', 'Reye', 'Lear', 'Rear', 'pt19'] 203 | 204 | 205 | mid_1 = [1, 2, 4, 5, 1, 7, 8, 4, 10, 11, 13, 1, 4] 206 | mid_2 = [2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 14, 14, 14] 207 | newslist = [[x,y] for x, y in zip(mid_1, mid_2)] 208 | 209 | 210 | mapidx1 = range(1, 27, 2) 211 | mapidx2 = range(2, 28, 2) 212 | mapid = [[x,y] for x, y in zip(mapidx1, mapidx2)] 213 | mapid = mapid[3:]+mapid[0:3] 214 | 215 | 216 | # test AI challenge validation dataset 217 | # rootdir= '../ai_dataset/ai_challenger_keypoint_validation_20170911/keypoint_validation_images_20170911/' 218 | rootdir = '../ai_dataset/ai_challenger_keypoint_test_a_20170923/keypoint_test_a_images_20170923/' 219 | images = os.listdir(rootdir) 220 | count = 0 221 | 222 | joints_all = [] 223 | 224 | for image in images: 225 | count += 1 226 | cimage = cv.imread(rootdir + str(image)) 227 | 228 | 229 | heatmap_avg, paf_avg = applyModel(cimage, param, sym, arg_params, aux_params) 230 | 231 | 232 | all_peaks = [] 233 | peak_counter = 0 234 | 235 | for part in range(14): 236 | x_list = [] 237 | y_list = [] 238 | map_ori = heatmap_avg[:,:,part] 239 | map = gaussian_filter(map_ori, sigma=3) 240 | 241 | map_left = np.zeros(map.shape) 242 | map_left[1:,:] = map[:-1,:] 243 | map_right = np.zeros(map.shape) 244 | map_right[:-1,:] = map[1:,:] 245 | map_up = np.zeros(map.shape) 246 | map_up[:,1:] = map[:,:-1] 247 | map_down = np.zeros(map.shape) 248 | map_down[:,:-1] = map[:,1:] 249 | 250 | peaks_binary = np.logical_and.reduce((map>=map_left, map>=map_right, map>=map_up, map>=map_down, map > param['thre1'])) 251 | peaks = zip(np.nonzero(peaks_binary)[1], np.nonzero(peaks_binary)[0]) # note reverse 252 | peaks_with_score = [x + (map_ori[x[1],x[0]],) for x in peaks] 253 | cid = range(peak_counter, peak_counter + len(peaks)) 254 | peaks_with_score_and_id = [peaks_with_score[i] + (cid[i],) for i in range(len(cid))] 255 | 256 | all_peaks.append(peaks_with_score_and_id) 257 | peak_counter += len(peaks) 258 | 259 | 260 | # find connection in the specified sequence, center 29 is in the position 15 261 | limbSeq = newslist 262 | # the middle joints heatmap correpondence 263 | mapIdx = mapid 264 | 265 | connection_all = [] 266 | special_k = [] 267 | special_non_zero_index = [] 268 | mid_num = 11 269 | 270 | for k in range(len(mapIdx)): 271 | 272 | score_mid = paf_avg[:,:,[x-1 for x in mapIdx[k]]] 273 | candA = all_peaks[limbSeq[k][0]-1] 274 | candB = all_peaks[limbSeq[k][1]-1] 275 | # print(k) 276 | # print(candA) 277 | # print('---------') 278 | # print(candB) 279 | # print limbSeq[k][0], limbSeq[k][1] 280 | nA = len(candA) 281 | nB = len(candB) 282 | indexA, indexB = limbSeq[k] 283 | if(nA != 0 and nB != 0): 284 | connection_candidate = [] 285 | for i in range(nA): 286 | for j in range(nB): 287 | vec = np.subtract(candB[j][:2], candA[i][:2]) 288 | # print('vec: ',vec) 289 | norm = math.sqrt(vec[0]*vec[0] + vec[1]*vec[1]) 290 | if norm == 0: 291 | norm = 0.1 292 | # print('norm: ', norm) 293 | vec = np.divide(vec, norm) 294 | # print('normalized vec: ', vec) 295 | startend = zip(np.linspace(candA[i][0], candB[j][0], num=mid_num), \ 296 | np.linspace(candA[i][1], candB[j][1], num=mid_num)) 297 | # print('startend: ', startend) 298 | vec_x = np.array([score_mid[int(round(startend[I][1])), int(round(startend[I][0])), 0] \ 299 | for I in range(len(startend))]) 300 | # print('vec_x: ', vec_x) 301 | vec_y = np.array([score_mid[int(round(startend[I][1])), int(round(startend[I][0])), 1] \ 302 | for I in range(len(startend))]) 303 | # print('vec_y: ', vec_y) 304 | score_midpts = np.multiply(vec_x, vec[0]) + np.multiply(vec_y, vec[1]) 305 | # print(score_midpts) 306 | # print('score_midpts: ', score_midpts) 307 | score_with_dist_prior = sum(score_midpts)/len(score_midpts) + min(0.5*cimage.shape[0]/norm-1, 0) 308 | 309 | # print('score_with_dist_prior: ', score_with_dist_prior) 310 | criterion1 = len(np.nonzero(score_midpts > param['thre2'])[0]) > 0.5 * len(score_midpts) 311 | # print('score_midpts > param["thre2"]: ', len(np.nonzero(score_midpts > param['thre2'])[0])) 312 | criterion2 = score_with_dist_prior > 0 313 | 314 | ''' 315 | if k==1 or k==2: 316 | criterion2 = score_with_dist_prior > 1 317 | if k == 2: 318 | print '-----------', i, j 319 | print criterion1 320 | print criterion2 321 | print score_with_dist_prior 322 | print score_midpts 323 | ''' 324 | if criterion1 and criterion2: 325 | # print('match') 326 | # print(i, j, score_with_dist_prior, score_with_dist_prior+candA[i][2]+candB[j][2]) 327 | connection_candidate.append([i, j, score_with_dist_prior, score_with_dist_prior+candA[i][2]+candB[j][2]]) 328 | # print('--------end-----------') 329 | connection_candidate = sorted(connection_candidate, key=lambda x: x[2], reverse=True) 330 | # print('-------------connection_candidate---------------') 331 | # print(connection_candidate) 332 | # print('------------------------------------------------') 333 | connection = np.zeros((0,5)) 334 | for c in range(len(connection_candidate)): 335 | i,j,s = connection_candidate[c][0:3] 336 | if(i not in connection[:,3] and j not in connection[:,4]): 337 | connection = np.vstack([connection, [candA[i][3], candB[j][3], s, i, j]]) 338 | # print('----------connection-----------') 339 | # print(connection) 340 | # print('-------------------------------') 341 | if(len(connection) >= min(nA, nB)): 342 | break 343 | 344 | connection_all.append(connection) 345 | elif(nA != 0 or nB != 0): 346 | #print 'k: ', special_k 347 | special_k.append(k) 348 | special_non_zero_index.append(indexA if nA != 0 else indexB) 349 | connection_all.append([]) 350 | 351 | #print connection_all 352 | # last number in each row is the total parts number of that person 353 | # the second last number in each row is the score of the overall configuration 354 | subset = -1 * np.ones((0, 16)) 355 | 356 | candidate = np.array([item for sublist in all_peaks for item in sublist]) 357 | 358 | # print len(connection_all) 359 | # print len(mapIdx) 360 | print 'special_k:', special_k 361 | for k in range(len(mapIdx)): 362 | if k not in special_k: 363 | 364 | try: 365 | partAs = connection_all[k][:,0] 366 | partBs = connection_all[k][:,1] 367 | indexA, indexB = np.array(limbSeq[k]) - 1 368 | 369 | for i in range(len(connection_all[k])): #= 1:size(temp,1) 370 | 371 | found = 0 372 | subset_idx = [-1, -1] 373 | for j in range(len(subset)): #1:size(subset,1): 374 | if subset[j][indexA] == partAs[i] or subset[j][indexB] == partBs[i]: 375 | subset_idx[found] = j 376 | found += 1 377 | 378 | if found == 1: 379 | j = subset_idx[0] 380 | # if k==2: 381 | # print 'j ', j 382 | if subset[j][indexB] != partBs[i]: 383 | # if k==2: 384 | # print 'j ', j 385 | subset[j][indexB] = partBs[i] 386 | subset[j][-1] += 1 387 | subset[j][-2] += candidate[partBs[i].astype(int), 2] + connection_all[k][i][2] 388 | elif found == 2: # if found 2 and disjoint, merge them 389 | j1, j2 = subset_idx 390 | # print "found = 2" 391 | membership = ((subset[j1]>=0).astype(int) + (subset[j2]>=0).astype(int))[:-2] 392 | if len(np.nonzero(membership == 2)[0]) == 0: #merge 393 | subset[j1][:-2] += (subset[j2][:-2] + 1) 394 | subset[j1][-2:] += subset[j2][-2:] 395 | subset[j1][-2] += connection_all[k][i][2] 396 | subset = np.delete(subset, j2, 0) 397 | else: # as like found == 1 398 | subset[j1][indexB] = partBs[i] 399 | subset[j1][-1] += 1 400 | subset[j1][-2] += candidate[partBs[i].astype(int), 2] + connection_all[k][i][2] 401 | 402 | # if find no partA in the subset, create a new subset 403 | elif not found and k < 13: 404 | row = -1 * np.ones(16) 405 | row[indexA] = partAs[i] 406 | row[indexB] = partBs[i] 407 | row[-1] = 2 408 | row[-2] = sum(candidate[connection_all[k][i,:2].astype(int), 2]) + connection_all[k][i][2] 409 | subset = np.vstack([subset, row]) 410 | # if k==2: 411 | # print 'k ', found 412 | except: 413 | print k 414 | 415 | # delete some rows of subset which has few parts occur 416 | deleteIdx = []; 417 | 418 | for i in range(len(subset)): 419 | if subset[i][-1] < 5 or subset[i][-2]/subset[i][-1] < 0.2: 420 | deleteIdx.append(i) 421 | subset = np.delete(subset, deleteIdx, axis=0) 422 | 423 | 424 | keypoint = dict() 425 | 426 | for i in range(subset.shape[0]): 427 | joints = [0 for k in range(14*3)] 428 | for j in range(len(subset[i])-2): 429 | if int(subset[i][j]) == -1: 430 | joints[j*3] = 0 431 | joints[j*3+1] = 0 432 | joints[j*3+2] = 0 433 | else: 434 | for m in range(14): 435 | for n in range(len(all_peaks[m])): 436 | if int(subset[i][j]) == all_peaks[m][n][3]: 437 | #print([all_peaks[m][n][0], all_peaks[m][n][1], 1]) 438 | joints[j*3] = int(all_peaks[m][n][0]) 439 | joints[j*3+1] = int(all_peaks[m][n][1]) 440 | joints[j*3+2] = 1 441 | break 442 | keypoint['human'+str(subset.shape[0]-i)] = joints 443 | 444 | 445 | # sort keypoint 446 | current_joints = {'image_id':image[:-4], 'keypoint_annotations':keypoint} 447 | joints_all.append(current_joints) 448 | 449 | 450 | print 'steps:', count 451 | 452 | 453 | with open('AI_pose_mxnet.json', 'w') as f: 454 | json.dump(joints_all, f) 455 | 456 | 457 | -------------------------------------------------------------------------------- /generateLabelCPMWeight.py: -------------------------------------------------------------------------------- 1 | ## author: Liang Dong 2 | ## Generate heat map and part affinity map 3 | 4 | import os 5 | import mxnet as mx 6 | import numpy as np 7 | import copy 8 | import re 9 | import json 10 | import cv2 as cv 11 | 12 | import scipy 13 | import PIL.Image 14 | import math 15 | import time 16 | from PIL import Image, ImageDraw 17 | 18 | #import caffe 19 | #from caffe.proto import caffe_pb2 20 | from config.config import config 21 | from collections import namedtuple 22 | 23 | from cython.heatmap import putGaussianMaps 24 | from cython.pafmap import putVecMaps 25 | 26 | Point = namedtuple('Point', 'x y') 27 | 28 | crop_size_x = 368 29 | crop_size_y = 368 30 | center_perterb_max = 40 31 | scale_prob = 1 32 | scale_min = 0.5 33 | scale_max = 1.1 34 | target_dist = 0.6 35 | 36 | numofparts = 14 37 | numoflinks = 13 38 | 39 | def rotatePoint(R, pointDict): 40 | NewPoint = {'x':0, 'y':0} 41 | NewPoint['x'] = R[0,0]*pointDict['x']+R[0,1]*pointDict['y']+R[0,2] 42 | NewPoint['y'] = R[1,0]*pointDict['x']+R[1,1]*pointDict['y']+R[1,2] 43 | return NewPoint 44 | 45 | def readmeta(data): 46 | meta = copy.deepcopy(data) 47 | meta['img_size'] = {'width': data['img_width'], 'height': data['img_height']} 48 | 49 | joint_self = data['joint_self'] 50 | meta['joint_self'] = {'joints': list(), 'isVisible': list()} 51 | for i in range(len(joint_self)): 52 | currentdict = {'x': joint_self[i][0], 'y': joint_self[i][1]} 53 | meta['joint_self']['joints'].append(currentdict) 54 | 55 | # meta['joint_self']['isVisible'].append(joint_self[i][2]) 56 | if joint_self[i][2] == 3: 57 | meta['joint_self']['isVisible'].append(3) 58 | elif joint_self[i][2] == 2: 59 | meta['joint_self']['isVisible'].append(2) 60 | elif joint_self[i][2] == 0: 61 | meta['joint_self']['isVisible'].append(0) 62 | else: 63 | meta['joint_self']['isVisible'].append(1) 64 | if (meta['joint_self']['joints'][i]['x'] < 0 or meta['joint_self']['joints'][i]['y'] < 0 65 | or meta['joint_self']['joints'][i]['x'] >= meta['img_size']['width'] or 66 | meta['joint_self']['joints'][i]['y'] >= meta['img_size']['height']): 67 | meta['joint_self']['isVisible'][i] = 2 68 | 69 | for key in data['joint_others']: 70 | joint_other = data['joint_others'][key] 71 | #print joint_other 72 | meta['joint_others'][key] = {'joints': list(), 'isVisible': list()} 73 | 74 | for i in range(len(joint_self)): 75 | currentdict = {'x': joint_other[i][0], 'y': joint_other[i][1]} 76 | meta['joint_others'][key]['joints'].append(currentdict) 77 | 78 | # meta['joint_self']['isVisible'].append(joint_self[i][2]) 79 | if joint_other[i][2] == 3: 80 | meta['joint_others'][key]['isVisible'].append(3) 81 | elif joint_other[i][2] == 2: 82 | meta['joint_others'][key]['isVisible'].append(2) 83 | elif joint_other[i][2] == 0: 84 | meta['joint_others'][key]['isVisible'].append(0) 85 | else: 86 | meta['joint_others'][key]['isVisible'].append(1) 87 | if (meta['joint_others'][key]['joints'][i]['x'] < 0 or meta['joint_others'][key]['joints'][i]['y'] < 0 88 | or meta['joint_others'][key]['joints'][i]['x'] >= meta['img_size']['width'] or 89 | meta['joint_others'][key]['joints'][i]['y'] >= meta['img_size']['height']): 90 | meta['joint_others'][key]['isVisible'][i] = 2 91 | 92 | return meta 93 | 94 | 95 | def TransformJointsSelf(meta): 96 | jo = meta['joint_self'].copy() 97 | newjo = {'joints': list(), 'isVisible': list()} 98 | #COCO_to_ours_1 = [1, 6, 7, 9, 11, 6, 8, 10, 13, 15, 17, 12, 14, 16, 3, 2, 5, 4] 99 | #COCO_to_ours_2 = [1, 7, 7, 9, 11, 6, 8, 10, 13, 15, 17, 12, 14, 16, 3, 2, 5, 4] 100 | 101 | COCO_to_ours_1 = list(range(1,15)) # [14, 6, 8, 10, 5, 7, 9, 12, 13, 11, 2, 1, 4, 3] 102 | COCO_to_ours_2 = list(range(1,15)) # [14, 6, 8, 10, 5, 7, 9, 12, 13, 11, 2, 1, 4, 3] 103 | 104 | for i in range(numofparts): 105 | currentdict = {'x': (jo['joints'][COCO_to_ours_1[i] - 1]['x'] + jo['joints'][COCO_to_ours_2[i] - 1]['x']) * 0.5, 106 | 'y': (jo['joints'][COCO_to_ours_1[i] - 1]['y'] + jo['joints'][COCO_to_ours_2[i] - 1]['y']) * 0.5} 107 | newjo['joints'].append(currentdict) 108 | 109 | if (jo['isVisible'][COCO_to_ours_1[i] - 1] == 2 or jo['isVisible'][COCO_to_ours_2[i] - 1] == 2): 110 | newjo['isVisible'].append(2) 111 | elif (jo['isVisible'][COCO_to_ours_1[i] - 1] == 3 or jo['isVisible'][COCO_to_ours_2[i] - 1] == 3): 112 | newjo['isVisible'].append(3) 113 | else: 114 | isVisible = jo['isVisible'][COCO_to_ours_1[i] - 1] and jo['isVisible'][COCO_to_ours_2[i] - 1] 115 | newjo['isVisible'].append(isVisible) 116 | 117 | meta['joint_self'] = newjo 118 | 119 | 120 | def TransformJointsOther(meta): 121 | for key in meta['joint_others']: 122 | jo = meta['joint_others'][key].copy() 123 | 124 | newjo = {'joints': list(), 'isVisible': list()} 125 | #COCO_to_ours_1 = [1, 6, 7, 9, 11, 6, 8, 10, 13, 15, 17, 12, 14, 16, 3, 2, 5, 4] 126 | #COCO_to_ours_2 = [1, 7, 7, 9, 11, 6, 8, 10, 13, 15, 17, 12, 14, 16, 3, 2, 5, 4] 127 | 128 | COCO_to_ours_1 = list(range(1, 15)) #[14, 6, 8, 10, 5, 7, 9, 12, 13, 11, 2, 1, 4, 3] 129 | COCO_to_ours_2 = list(range(1, 15)) # [14, 6, 8, 10, 5, 7, 9, 12, 13, 11, 2, 1, 4, 3] 130 | 131 | for i in range(numofparts): 132 | currentdict = { 133 | 'x': (jo['joints'][COCO_to_ours_1[i] - 1]['x'] + jo['joints'][COCO_to_ours_2[i] - 1]['x']) * 0.5, 134 | 'y': (jo['joints'][COCO_to_ours_1[i] - 1]['y'] + jo['joints'][COCO_to_ours_2[i] - 1]['y']) * 0.5} 135 | newjo['joints'].append(currentdict) 136 | 137 | if (jo['isVisible'][COCO_to_ours_1[i] - 1] == 2 or jo['isVisible'][COCO_to_ours_2[i] - 1] == 2): 138 | newjo['isVisible'].append(2) 139 | elif (jo['isVisible'][COCO_to_ours_1[i] - 1] == 3 or jo['isVisible'][COCO_to_ours_2[i] - 1] == 3): 140 | newjo['isVisible'].append(3) 141 | else: 142 | isVisible = jo['isVisible'][COCO_to_ours_1[i] - 1] and jo['isVisible'][COCO_to_ours_2[i] - 1] 143 | newjo['isVisible'].append(isVisible) 144 | 145 | meta['joint_others'][key] = newjo 146 | 147 | 148 | def TransformMetaJoints(meta): 149 | TransformJointsSelf(meta) 150 | TransformJointsOther(meta) 151 | 152 | def augmentation_scale(meta, oriImg, maskmiss): 153 | newmeta = copy.deepcopy(meta) 154 | ''' 155 | dice2 = np.random.uniform() 156 | scale_multiplier = (scale_max - scale_min) * dice2 + scale_min 157 | if newmeta['scale_provided']>0: 158 | scale_abs = target_dist / newmeta['scale_provided'] 159 | scale = scale_abs * scale_multiplier 160 | else: 161 | scale = 1 162 | ''' 163 | if config.TRAIN.scale_set == False: 164 | scale = 1 165 | else: 166 | dice2 = np.random.uniform() 167 | scale_multiplier = (config.TRAIN.scale_max - config.TRAIN.scale_min) * dice2 + config.TRAIN.scale_min 168 | scale = 368.0/oriImg.shape[0]*scale_multiplier 169 | 170 | resizeImage = cv.resize(oriImg, (0, 0), fx=scale, fy=scale) 171 | maskmiss_scale = cv.resize(maskmiss, (0,0), fx=scale, fy=scale) 172 | 173 | newmeta['objpos'][0] *= scale 174 | newmeta['objpos'][1] *= scale 175 | 176 | for i in range(len(meta['joint_self']['joints'])): 177 | newmeta['joint_self']['joints'][i]['x'] *= scale 178 | newmeta['joint_self']['joints'][i]['y'] *= scale 179 | 180 | for i in meta['joint_others']: 181 | for j in range(len(meta['joint_others'][i]['joints'])): 182 | newmeta['joint_others'][i]['joints'][j]['x'] *= scale 183 | newmeta['joint_others'][i]['joints'][j]['y'] *= scale 184 | 185 | # newmeta4['img_width'], newmeta4['img_height'] 186 | newmeta['img_height'] = resizeImage.shape[0] 187 | newmeta['img_width'] = resizeImage.shape[1] 188 | return (newmeta, resizeImage, maskmiss_scale) 189 | 190 | 191 | def onPlane(p, img_size): 192 | if (p[0] < 0 or p[1] < 0): 193 | return False 194 | if (p[0] >= img_size[0] - 1 or p[1] >= img_size[1]): 195 | return False 196 | return True 197 | 198 | def augmentation_flip(meta, croppedImage, maskmiss): 199 | dice = np.random.uniform() 200 | newmeta = copy.deepcopy(meta) 201 | 202 | if config.TRAIN.flip and dice > 0.5: 203 | flipImage = cv.flip(croppedImage, 1) 204 | maskmiss_flip = cv.flip(maskmiss, 1) 205 | 206 | newmeta['objpos'][0] = newmeta['img_width'] - 1 - newmeta['objpos'][0] 207 | 208 | for i in range(len(meta['joint_self']['joints'])): 209 | newmeta['joint_self']['joints'][i]['x'] = newmeta['img_width'] - 1- newmeta['joint_self']['joints'][i]['x'] 210 | 211 | for i in meta['joint_others']: 212 | for j in range(len(meta['joint_others'][i]['joints'])): 213 | newmeta['joint_others'][i]['joints'][j]['x'] = newmeta['img_width'] - 1 - newmeta['joint_others'][i]['joints'][j]['x'] 214 | else: 215 | flipImage = croppedImage.copy() 216 | maskmiss_flip = maskmiss.copy() 217 | 218 | return (newmeta, flipImage, maskmiss_flip) 219 | 220 | def augmentation_rotate(meta, flipimage, maskmiss): 221 | newmeta = copy.deepcopy(meta) 222 | dice2 = np.random.uniform() 223 | degree = (dice2 - 0.5)*2*config.TRAIN.max_rotate_degree 224 | 225 | #print degree 226 | center = (368/2, 368/2) 227 | 228 | R = cv.getRotationMatrix2D(center, degree, 1.0) 229 | 230 | rotatedImage = cv.warpAffine(flipimage, R, (368,368)) 231 | maskmiss_rotated = cv.warpAffine(maskmiss, R, (368,368)) 232 | 233 | for i in range(len(meta['joint_self']['joints'])): 234 | newmeta['joint_self']['joints'][i] = rotatePoint(R, newmeta['joint_self']['joints'][i]) 235 | 236 | for i in meta['joint_others']: 237 | for j in range(len(meta['joint_others'][i]['joints'])): 238 | newmeta['joint_others'][i]['joints'][j] = rotatePoint(R, newmeta['joint_others'][i]['joints'][j]) 239 | 240 | return (newmeta, rotatedImage, maskmiss_rotated) 241 | 242 | def augmentation_croppad(meta, oriImg, maskmiss): 243 | dice_x = 0.5 244 | dice_y = 0.5 245 | # float dice_x = static_cast (rand()) / static_cast (RAND_MAX); //[0,1] 246 | # float dice_y = static_cast (rand()) / static_cast (RAND_MAX); //[0,1] 247 | crop_x = config.TRAIN.crop_size_x 248 | crop_y = config.TRAIN.crop_size_y 249 | 250 | x_offset = int((dice_x - 0.5) * 2 * config.TRAIN.center_perterb_max) 251 | y_offset = int((dice_y - 0.5) * 2 * config.TRAIN.center_perterb_max) 252 | 253 | #print x_offset, y_offset 254 | newmeta2 = copy.deepcopy(meta) 255 | 256 | center = [x_offset + meta['objpos'][0], y_offset + meta['objpos'][1]] 257 | 258 | offset_left = -int(center[0] - crop_x / 2) 259 | offset_up = -int(center[1] - crop_y / 2) 260 | 261 | img_dst = np.full((crop_y, crop_x, 3), 128, dtype=np.uint8) 262 | maskmiss_cropped = np.full((crop_y, crop_x, 3), False, dtype=np.uint8) 263 | for i in range(crop_y): 264 | for j in range(crop_x): 265 | coord_x_on_img = int(center[0] - crop_x / 2 + j) 266 | coord_y_on_img = int(center[1] - crop_y / 2 + i) 267 | # print coord_x_on_img, coord_y_on_img 268 | if (onPlane([coord_x_on_img, coord_y_on_img], [oriImg.shape[1], oriImg.shape[0]])): 269 | img_dst[i, j, :] = oriImg[coord_y_on_img, coord_x_on_img, :] 270 | maskmiss_cropped[i, j] = maskmiss[coord_y_on_img, coord_x_on_img] 271 | 272 | newmeta2['objpos'][0] += offset_left 273 | newmeta2['objpos'][1] += offset_up 274 | 275 | for i in range(len(meta['joint_self']['joints'])): 276 | newmeta2['joint_self']['joints'][i]['x'] += offset_left 277 | newmeta2['joint_self']['joints'][i]['y'] += offset_up 278 | 279 | for i in meta['joint_others']: 280 | for j in range(len(meta['joint_others'][i]['joints'])): 281 | newmeta2['joint_others'][i]['joints'][j]['x'] += offset_left 282 | newmeta2['joint_others'][i]['joints'][j]['y'] += offset_up 283 | 284 | newmeta2['img_height'] = 368 285 | newmeta2['img_width'] = 368 286 | return (newmeta2, img_dst, maskmiss_cropped) 287 | 288 | def generateLabelMap(img_aug, meta): 289 | thre = 0.5 290 | crop_size_width = 368 291 | crop_size_height = 368 292 | 293 | augmentcols = 368 294 | augmentrows = 368 295 | stride = 8 296 | grid_x = augmentcols / stride 297 | grid_y = augmentrows / stride 298 | #sigma = 4.0 299 | #sigma = 10.0 300 | sigma = 7 301 | 302 | heat_map = list() 303 | for i in range(numofparts+1): 304 | heat_map.append(np.zeros((crop_size_width / stride, crop_size_height / stride))) 305 | 306 | for i in range(numofparts): 307 | if (meta['joint_self']['isVisible'][i] <= 1): 308 | putGaussianMaps(heat_map[i], 368, 368, 309 | meta['joint_self']['joints'][i]['x'], meta['joint_self']['joints'][i]['y'], 310 | stride, grid_x, grid_y, sigma) 311 | 312 | for j in meta['joint_others']: 313 | if (meta['joint_others'][j]['isVisible'][i] <= 1): 314 | putGaussianMaps(heat_map[i], 368, 368, 315 | meta['joint_others'][j]['joints'][i]['x'], 316 | meta['joint_others'][j]['joints'][i]['y'], 317 | stride, grid_x, grid_y, sigma) 318 | 319 | ### put background channel 320 | #heat_map[numofparts] = heat_map[0] 321 | 322 | for g_y in range(grid_y): 323 | for g_x in range(grid_x): 324 | maximum=0 325 | for i in range(numofparts): 326 | if maximum 0): 369 | for i in range(len(meta['segmentations'])): 370 | seg = meta['segmentations'][i] 371 | if len(seg) > 0: 372 | nlen = len(seg[0]) 373 | if nlen > 5: 374 | poly = zip(seg[0][0:nlen+2:2], seg[0][1:nlen+1:2]) 375 | img = Image.new("L", [nx, ny], 0) 376 | ImageDraw.Draw(img).polygon(poly, outline=1, fill=1) 377 | mask = np.array(img) 378 | maskall = np.logical_or(mask, maskall) 379 | except: 380 | print 'full mask' 381 | 382 | return np.logical_not(maskall) 383 | 384 | def getImageandLabel(iterjson): 385 | 386 | meta = readmeta(iterjson) 387 | 388 | TransformMetaJoints(meta) 389 | 390 | oriImg = cv.imread(meta['img_paths']) 391 | maskmiss = getMask(meta) 392 | maskmiss = maskmiss.astype(np.uint8) 393 | 394 | newmeta, resizeImage, maskmiss_scale = augmentation_scale(meta, oriImg, maskmiss) 395 | 396 | newmeta2, croppedImage, maskmiss_cropped = augmentation_croppad(newmeta, resizeImage, 397 | maskmiss_scale) 398 | 399 | newmeta3, rotatedImage, maskmiss_rotate= augmentation_rotate(newmeta2, croppedImage, 400 | maskmiss_cropped) 401 | 402 | newmeta4, flipImage, maskmiss_flip = augmentation_flip(newmeta3, rotatedImage, maskmiss_rotate) 403 | 404 | heatmap, pagmap = generateLabelMap(flipImage, newmeta4) 405 | 406 | return (flipImage, maskmiss_flip, heatmap, pagmap) 407 | -------------------------------------------------------------------------------- /modelCPMWeight.py: -------------------------------------------------------------------------------- 1 | #import caffe 2 | #from caffe.proto import caffe_pb2 3 | #use_caffe = True 4 | import re 5 | from google.protobuf import text_format 6 | import mxnet as mx 7 | from generateLabelCPMWeight import * 8 | 9 | numofparts = 15 10 | numoflinks = 13 11 | 12 | def CPMModel(): 13 | 14 | data = mx.symbol.Variable(name='data') 15 | ## heat map of human parts 16 | heatmaplabel = mx.sym.Variable("heatmaplabel") 17 | ## part affinity graph 18 | partaffinityglabel = mx.sym.Variable('partaffinityglabel') 19 | 20 | heatweight = mx.sym.Variable('heatweight') 21 | 22 | vecweight = mx.sym.Variable('vecweight') 23 | 24 | conv1_1 = mx.symbol.Convolution(name='conv1_1', data=data , num_filter=64, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 25 | relu1_1 = mx.symbol.Activation(name='relu1_1', data=conv1_1 , act_type='relu') 26 | conv1_2 = mx.symbol.Convolution(name='conv1_2', data=relu1_1 , num_filter=64, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 27 | relu1_2 = mx.symbol.Activation(name='relu1_2', data=conv1_2 , act_type='relu') 28 | pool1_stage1 = mx.symbol.Pooling(name='pool1_stage1', data=relu1_2 , pooling_convention='full', pad=(0,0), kernel=(2,2), stride=(2,2), pool_type='max') 29 | conv2_1 = mx.symbol.Convolution(name='conv2_1', data=pool1_stage1 , num_filter=128, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 30 | relu2_1 = mx.symbol.Activation(name='relu2_1', data=conv2_1 , act_type='relu') 31 | conv2_2 = mx.symbol.Convolution(name='conv2_2', data=relu2_1 , num_filter=128, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 32 | relu2_2 = mx.symbol.Activation(name='relu2_2', data=conv2_2 , act_type='relu') 33 | pool2_stage1 = mx.symbol.Pooling(name='pool2_stage1', data=relu2_2 , pooling_convention='full', pad=(0,0), kernel=(2,2), stride=(2,2), pool_type='max') 34 | conv3_1 = mx.symbol.Convolution(name='conv3_1', data=pool2_stage1 , num_filter=256, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 35 | relu3_1 = mx.symbol.Activation(name='relu3_1', data=conv3_1 , act_type='relu') 36 | conv3_2 = mx.symbol.Convolution(name='conv3_2', data=relu3_1 , num_filter=256, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 37 | relu3_2 = mx.symbol.Activation(name='relu3_2', data=conv3_2 , act_type='relu') 38 | conv3_3 = mx.symbol.Convolution(name='conv3_3', data=relu3_2 , num_filter=256, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 39 | relu3_3 = mx.symbol.Activation(name='relu3_3', data=conv3_3 , act_type='relu') 40 | conv3_4 = mx.symbol.Convolution(name='conv3_4', data=relu3_3 , num_filter=256, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 41 | relu3_4 = mx.symbol.Activation(name='relu3_4', data=conv3_4 , act_type='relu') 42 | pool3_stage1 = mx.symbol.Pooling(name='pool3_stage1', data=relu3_4 , pooling_convention='full', pad=(0,0), kernel=(2,2), stride=(2,2), pool_type='max') 43 | conv4_1 = mx.symbol.Convolution(name='conv4_1', data=pool3_stage1 , num_filter=512, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 44 | relu4_1 = mx.symbol.Activation(name='relu4_1', data=conv4_1 , act_type='relu') 45 | conv4_2 = mx.symbol.Convolution(name='conv4_2', data=relu4_1 , num_filter=512, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 46 | relu4_2 = mx.symbol.Activation(name='relu4_2', data=conv4_2 , act_type='relu') 47 | conv4_3_CPM = mx.symbol.Convolution(name='conv4_3_CPM', data=relu4_2 , num_filter=256, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 48 | relu4_3_CPM = mx.symbol.Activation(name='relu4_3_CPM', data=conv4_3_CPM , act_type='relu') 49 | conv4_4_CPM = mx.symbol.Convolution(name='conv4_4_CPM', data=relu4_3_CPM , num_filter=128, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 50 | relu4_4_CPM = mx.symbol.Activation(name='relu4_4_CPM', data=conv4_4_CPM , act_type='relu') 51 | conv5_1_CPM_L1 = mx.symbol.Convolution(name='conv5_1_CPM_L1', data=relu4_4_CPM , num_filter=128, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 52 | relu5_1_CPM_L1 = mx.symbol.Activation(name='relu5_1_CPM_L1', data=conv5_1_CPM_L1 , act_type='relu') 53 | conv5_1_CPM_L2 = mx.symbol.Convolution(name='conv5_1_CPM_L2', data=relu4_4_CPM , num_filter=128, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 54 | relu5_1_CPM_L2 = mx.symbol.Activation(name='relu5_1_CPM_L2', data=conv5_1_CPM_L2 , act_type='relu') 55 | conv5_2_CPM_L1 = mx.symbol.Convolution(name='conv5_2_CPM_L1', data=relu5_1_CPM_L1 , num_filter=128, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 56 | relu5_2_CPM_L1 = mx.symbol.Activation(name='relu5_2_CPM_L1', data=conv5_2_CPM_L1 , act_type='relu') 57 | conv5_2_CPM_L2 = mx.symbol.Convolution(name='conv5_2_CPM_L2', data=relu5_1_CPM_L2 , num_filter=128, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 58 | relu5_2_CPM_L2 = mx.symbol.Activation(name='relu5_2_CPM_L2', data=conv5_2_CPM_L2 , act_type='relu') 59 | conv5_3_CPM_L1 = mx.symbol.Convolution(name='conv5_3_CPM_L1', data=relu5_2_CPM_L1 , num_filter=128, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 60 | relu5_3_CPM_L1 = mx.symbol.Activation(name='relu5_3_CPM_L1', data=conv5_3_CPM_L1 , act_type='relu') 61 | conv5_3_CPM_L2 = mx.symbol.Convolution(name='conv5_3_CPM_L2', data=relu5_2_CPM_L2 , num_filter=128, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 62 | relu5_3_CPM_L2 = mx.symbol.Activation(name='relu5_3_CPM_L2', data=conv5_3_CPM_L2 , act_type='relu') 63 | conv5_4_CPM_L1 = mx.symbol.Convolution(name='conv5_4_CPM_L1', data=relu5_3_CPM_L1 , num_filter=512, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 64 | relu5_4_CPM_L1 = mx.symbol.Activation(name='relu5_4_CPM_L1', data=conv5_4_CPM_L1 , act_type='relu') 65 | conv5_4_CPM_L2 = mx.symbol.Convolution(name='conv5_4_CPM_L2', data=relu5_3_CPM_L2 , num_filter=512, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 66 | relu5_4_CPM_L2 = mx.symbol.Activation(name='relu5_4_CPM_L2', data=conv5_4_CPM_L2 , act_type='relu') 67 | conv5_5_CPM_L1 = mx.symbol.Convolution(name='conv5_5_CPM_L1', data=relu5_4_CPM_L1 , num_filter=numoflinks*2, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 68 | conv5_5_CPM_L2 = mx.symbol.Convolution(name='conv5_5_CPM_L2', data=relu5_4_CPM_L2 , num_filter=numofparts, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 69 | concat_stage2 = mx.symbol.Concat(name='concat_stage2', *[conv5_5_CPM_L1,conv5_5_CPM_L2,relu4_4_CPM] ) 70 | Mconv1_stage2_L1 = mx.symbol.Convolution(name='Mconv1_stage2_L1', data=concat_stage2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 71 | Mrelu1_stage2_L1 = mx.symbol.Activation(name='Mrelu1_stage2_L1', data=Mconv1_stage2_L1 , act_type='relu') 72 | Mconv1_stage2_L2 = mx.symbol.Convolution(name='Mconv1_stage2_L2', data=concat_stage2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 73 | Mrelu1_stage2_L2 = mx.symbol.Activation(name='Mrelu1_stage2_L2', data=Mconv1_stage2_L2 , act_type='relu') 74 | Mconv2_stage2_L1 = mx.symbol.Convolution(name='Mconv2_stage2_L1', data=Mrelu1_stage2_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 75 | Mrelu2_stage2_L1 = mx.symbol.Activation(name='Mrelu2_stage2_L1', data=Mconv2_stage2_L1 , act_type='relu') 76 | Mconv2_stage2_L2 = mx.symbol.Convolution(name='Mconv2_stage2_L2', data=Mrelu1_stage2_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 77 | Mrelu2_stage2_L2 = mx.symbol.Activation(name='Mrelu2_stage2_L2', data=Mconv2_stage2_L2 , act_type='relu') 78 | Mconv3_stage2_L1 = mx.symbol.Convolution(name='Mconv3_stage2_L1', data=Mrelu2_stage2_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 79 | Mrelu3_stage2_L1 = mx.symbol.Activation(name='Mrelu3_stage2_L1', data=Mconv3_stage2_L1 , act_type='relu') 80 | Mconv3_stage2_L2 = mx.symbol.Convolution(name='Mconv3_stage2_L2', data=Mrelu2_stage2_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 81 | Mrelu3_stage2_L2 = mx.symbol.Activation(name='Mrelu3_stage2_L2', data=Mconv3_stage2_L2 , act_type='relu') 82 | Mconv4_stage2_L1 = mx.symbol.Convolution(name='Mconv4_stage2_L1', data=Mrelu3_stage2_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 83 | Mrelu4_stage2_L1 = mx.symbol.Activation(name='Mrelu4_stage2_L1', data=Mconv4_stage2_L1 , act_type='relu') 84 | Mconv4_stage2_L2 = mx.symbol.Convolution(name='Mconv4_stage2_L2', data=Mrelu3_stage2_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 85 | Mrelu4_stage2_L2 = mx.symbol.Activation(name='Mrelu4_stage2_L2', data=Mconv4_stage2_L2 , act_type='relu') 86 | Mconv5_stage2_L1 = mx.symbol.Convolution(name='Mconv5_stage2_L1', data=Mrelu4_stage2_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 87 | Mrelu5_stage2_L1 = mx.symbol.Activation(name='Mrelu5_stage2_L1', data=Mconv5_stage2_L1 , act_type='relu') 88 | Mconv5_stage2_L2 = mx.symbol.Convolution(name='Mconv5_stage2_L2', data=Mrelu4_stage2_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 89 | Mrelu5_stage2_L2 = mx.symbol.Activation(name='Mrelu5_stage2_L2', data=Mconv5_stage2_L2 , act_type='relu') 90 | Mconv6_stage2_L1 = mx.symbol.Convolution(name='Mconv6_stage2_L1', data=Mrelu5_stage2_L1 , num_filter=128, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 91 | Mrelu6_stage2_L1 = mx.symbol.Activation(name='Mrelu6_stage2_L1', data=Mconv6_stage2_L1 , act_type='relu') 92 | Mconv6_stage2_L2 = mx.symbol.Convolution(name='Mconv6_stage2_L2', data=Mrelu5_stage2_L2 , num_filter=128, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 93 | Mrelu6_stage2_L2 = mx.symbol.Activation(name='Mrelu6_stage2_L2', data=Mconv6_stage2_L2 , act_type='relu') 94 | Mconv7_stage2_L1 = mx.symbol.Convolution(name='Mconv7_stage2_L1', data=Mrelu6_stage2_L1 , num_filter=numoflinks*2, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 95 | Mconv7_stage2_L2 = mx.symbol.Convolution(name='Mconv7_stage2_L2', data=Mrelu6_stage2_L2 , num_filter=numofparts, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 96 | concat_stage3 = mx.symbol.Concat(name='concat_stage3', *[Mconv7_stage2_L1,Mconv7_stage2_L2,relu4_4_CPM] ) 97 | Mconv1_stage3_L1 = mx.symbol.Convolution(name='Mconv1_stage3_L1', data=concat_stage3 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 98 | Mrelu1_stage3_L1 = mx.symbol.Activation(name='Mrelu1_stage3_L1', data=Mconv1_stage3_L1 , act_type='relu') 99 | Mconv1_stage3_L2 = mx.symbol.Convolution(name='Mconv1_stage3_L2', data=concat_stage3 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 100 | Mrelu1_stage3_L2 = mx.symbol.Activation(name='Mrelu1_stage3_L2', data=Mconv1_stage3_L2 , act_type='relu') 101 | Mconv2_stage3_L1 = mx.symbol.Convolution(name='Mconv2_stage3_L1', data=Mrelu1_stage3_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 102 | Mrelu2_stage3_L1 = mx.symbol.Activation(name='Mrelu2_stage3_L1', data=Mconv2_stage3_L1 , act_type='relu') 103 | Mconv2_stage3_L2 = mx.symbol.Convolution(name='Mconv2_stage3_L2', data=Mrelu1_stage3_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 104 | Mrelu2_stage3_L2 = mx.symbol.Activation(name='Mrelu2_stage3_L2', data=Mconv2_stage3_L2 , act_type='relu') 105 | Mconv3_stage3_L1 = mx.symbol.Convolution(name='Mconv3_stage3_L1', data=Mrelu2_stage3_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 106 | Mrelu3_stage3_L1 = mx.symbol.Activation(name='Mrelu3_stage3_L1', data=Mconv3_stage3_L1 , act_type='relu') 107 | Mconv3_stage3_L2 = mx.symbol.Convolution(name='Mconv3_stage3_L2', data=Mrelu2_stage3_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 108 | Mrelu3_stage3_L2 = mx.symbol.Activation(name='Mrelu3_stage3_L2', data=Mconv3_stage3_L2 , act_type='relu') 109 | Mconv4_stage3_L1 = mx.symbol.Convolution(name='Mconv4_stage3_L1', data=Mrelu3_stage3_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 110 | Mrelu4_stage3_L1 = mx.symbol.Activation(name='Mrelu4_stage3_L1', data=Mconv4_stage3_L1 , act_type='relu') 111 | Mconv4_stage3_L2 = mx.symbol.Convolution(name='Mconv4_stage3_L2', data=Mrelu3_stage3_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 112 | Mrelu4_stage3_L2 = mx.symbol.Activation(name='Mrelu4_stage3_L2', data=Mconv4_stage3_L2 , act_type='relu') 113 | Mconv5_stage3_L1 = mx.symbol.Convolution(name='Mconv5_stage3_L1', data=Mrelu4_stage3_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 114 | Mrelu5_stage3_L1 = mx.symbol.Activation(name='Mrelu5_stage3_L1', data=Mconv5_stage3_L1 , act_type='relu') 115 | Mconv5_stage3_L2 = mx.symbol.Convolution(name='Mconv5_stage3_L2', data=Mrelu4_stage3_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 116 | Mrelu5_stage3_L2 = mx.symbol.Activation(name='Mrelu5_stage3_L2', data=Mconv5_stage3_L2 , act_type='relu') 117 | Mconv6_stage3_L1 = mx.symbol.Convolution(name='Mconv6_stage3_L1', data=Mrelu5_stage3_L1 , num_filter=128, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 118 | Mrelu6_stage3_L1 = mx.symbol.Activation(name='Mrelu6_stage3_L1', data=Mconv6_stage3_L1 , act_type='relu') 119 | Mconv6_stage3_L2 = mx.symbol.Convolution(name='Mconv6_stage3_L2', data=Mrelu5_stage3_L2 , num_filter=128, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 120 | Mrelu6_stage3_L2 = mx.symbol.Activation(name='Mrelu6_stage3_L2', data=Mconv6_stage3_L2 , act_type='relu') 121 | Mconv7_stage3_L1 = mx.symbol.Convolution(name='Mconv7_stage3_L1', data=Mrelu6_stage3_L1 , num_filter=numoflinks*2, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 122 | Mconv7_stage3_L2 = mx.symbol.Convolution(name='Mconv7_stage3_L2', data=Mrelu6_stage3_L2 , num_filter=numofparts, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 123 | concat_stage4 = mx.symbol.Concat(name='concat_stage4', *[Mconv7_stage3_L1,Mconv7_stage3_L2,relu4_4_CPM] ) 124 | Mconv1_stage4_L1 = mx.symbol.Convolution(name='Mconv1_stage4_L1', data=concat_stage4 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 125 | Mrelu1_stage4_L1 = mx.symbol.Activation(name='Mrelu1_stage4_L1', data=Mconv1_stage4_L1 , act_type='relu') 126 | Mconv1_stage4_L2 = mx.symbol.Convolution(name='Mconv1_stage4_L2', data=concat_stage4 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 127 | Mrelu1_stage4_L2 = mx.symbol.Activation(name='Mrelu1_stage4_L2', data=Mconv1_stage4_L2 , act_type='relu') 128 | Mconv2_stage4_L1 = mx.symbol.Convolution(name='Mconv2_stage4_L1', data=Mrelu1_stage4_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 129 | Mrelu2_stage4_L1 = mx.symbol.Activation(name='Mrelu2_stage4_L1', data=Mconv2_stage4_L1 , act_type='relu') 130 | Mconv2_stage4_L2 = mx.symbol.Convolution(name='Mconv2_stage4_L2', data=Mrelu1_stage4_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 131 | Mrelu2_stage4_L2 = mx.symbol.Activation(name='Mrelu2_stage4_L2', data=Mconv2_stage4_L2 , act_type='relu') 132 | Mconv3_stage4_L1 = mx.symbol.Convolution(name='Mconv3_stage4_L1', data=Mrelu2_stage4_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 133 | Mrelu3_stage4_L1 = mx.symbol.Activation(name='Mrelu3_stage4_L1', data=Mconv3_stage4_L1 , act_type='relu') 134 | Mconv3_stage4_L2 = mx.symbol.Convolution(name='Mconv3_stage4_L2', data=Mrelu2_stage4_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 135 | Mrelu3_stage4_L2 = mx.symbol.Activation(name='Mrelu3_stage4_L2', data=Mconv3_stage4_L2 , act_type='relu') 136 | Mconv4_stage4_L1 = mx.symbol.Convolution(name='Mconv4_stage4_L1', data=Mrelu3_stage4_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 137 | Mrelu4_stage4_L1 = mx.symbol.Activation(name='Mrelu4_stage4_L1', data=Mconv4_stage4_L1 , act_type='relu') 138 | Mconv4_stage4_L2 = mx.symbol.Convolution(name='Mconv4_stage4_L2', data=Mrelu3_stage4_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 139 | Mrelu4_stage4_L2 = mx.symbol.Activation(name='Mrelu4_stage4_L2', data=Mconv4_stage4_L2 , act_type='relu') 140 | Mconv5_stage4_L1 = mx.symbol.Convolution(name='Mconv5_stage4_L1', data=Mrelu4_stage4_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 141 | Mrelu5_stage4_L1 = mx.symbol.Activation(name='Mrelu5_stage4_L1', data=Mconv5_stage4_L1 , act_type='relu') 142 | Mconv5_stage4_L2 = mx.symbol.Convolution(name='Mconv5_stage4_L2', data=Mrelu4_stage4_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 143 | Mrelu5_stage4_L2 = mx.symbol.Activation(name='Mrelu5_stage4_L2', data=Mconv5_stage4_L2 , act_type='relu') 144 | Mconv6_stage4_L1 = mx.symbol.Convolution(name='Mconv6_stage4_L1', data=Mrelu5_stage4_L1 , num_filter=128, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 145 | Mrelu6_stage4_L1 = mx.symbol.Activation(name='Mrelu6_stage4_L1', data=Mconv6_stage4_L1 , act_type='relu') 146 | Mconv6_stage4_L2 = mx.symbol.Convolution(name='Mconv6_stage4_L2', data=Mrelu5_stage4_L2 , num_filter=128, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 147 | Mrelu6_stage4_L2 = mx.symbol.Activation(name='Mrelu6_stage4_L2', data=Mconv6_stage4_L2 , act_type='relu') 148 | Mconv7_stage4_L1 = mx.symbol.Convolution(name='Mconv7_stage4_L1', data=Mrelu6_stage4_L1 , num_filter=numoflinks*2, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 149 | Mconv7_stage4_L2 = mx.symbol.Convolution(name='Mconv7_stage4_L2', data=Mrelu6_stage4_L2 , num_filter=numofparts, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 150 | concat_stage5 = mx.symbol.Concat(name='concat_stage5', *[Mconv7_stage4_L1,Mconv7_stage4_L2,relu4_4_CPM] ) 151 | Mconv1_stage5_L1 = mx.symbol.Convolution(name='Mconv1_stage5_L1', data=concat_stage5 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 152 | Mrelu1_stage5_L1 = mx.symbol.Activation(name='Mrelu1_stage5_L1', data=Mconv1_stage5_L1 , act_type='relu') 153 | Mconv1_stage5_L2 = mx.symbol.Convolution(name='Mconv1_stage5_L2', data=concat_stage5 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 154 | Mrelu1_stage5_L2 = mx.symbol.Activation(name='Mrelu1_stage5_L2', data=Mconv1_stage5_L2 , act_type='relu') 155 | Mconv2_stage5_L1 = mx.symbol.Convolution(name='Mconv2_stage5_L1', data=Mrelu1_stage5_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 156 | Mrelu2_stage5_L1 = mx.symbol.Activation(name='Mrelu2_stage5_L1', data=Mconv2_stage5_L1 , act_type='relu') 157 | Mconv2_stage5_L2 = mx.symbol.Convolution(name='Mconv2_stage5_L2', data=Mrelu1_stage5_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 158 | Mrelu2_stage5_L2 = mx.symbol.Activation(name='Mrelu2_stage5_L2', data=Mconv2_stage5_L2 , act_type='relu') 159 | Mconv3_stage5_L1 = mx.symbol.Convolution(name='Mconv3_stage5_L1', data=Mrelu2_stage5_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 160 | Mrelu3_stage5_L1 = mx.symbol.Activation(name='Mrelu3_stage5_L1', data=Mconv3_stage5_L1 , act_type='relu') 161 | Mconv3_stage5_L2 = mx.symbol.Convolution(name='Mconv3_stage5_L2', data=Mrelu2_stage5_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 162 | Mrelu3_stage5_L2 = mx.symbol.Activation(name='Mrelu3_stage5_L2', data=Mconv3_stage5_L2 , act_type='relu') 163 | Mconv4_stage5_L1 = mx.symbol.Convolution(name='Mconv4_stage5_L1', data=Mrelu3_stage5_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 164 | Mrelu4_stage5_L1 = mx.symbol.Activation(name='Mrelu4_stage5_L1', data=Mconv4_stage5_L1 , act_type='relu') 165 | Mconv4_stage5_L2 = mx.symbol.Convolution(name='Mconv4_stage5_L2', data=Mrelu3_stage5_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 166 | Mrelu4_stage5_L2 = mx.symbol.Activation(name='Mrelu4_stage5_L2', data=Mconv4_stage5_L2 , act_type='relu') 167 | Mconv5_stage5_L1 = mx.symbol.Convolution(name='Mconv5_stage5_L1', data=Mrelu4_stage5_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 168 | Mrelu5_stage5_L1 = mx.symbol.Activation(name='Mrelu5_stage5_L1', data=Mconv5_stage5_L1 , act_type='relu') 169 | Mconv5_stage5_L2 = mx.symbol.Convolution(name='Mconv5_stage5_L2', data=Mrelu4_stage5_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 170 | Mrelu5_stage5_L2 = mx.symbol.Activation(name='Mrelu5_stage5_L2', data=Mconv5_stage5_L2 , act_type='relu') 171 | Mconv6_stage5_L1 = mx.symbol.Convolution(name='Mconv6_stage5_L1', data=Mrelu5_stage5_L1 , num_filter=128, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 172 | Mrelu6_stage5_L1 = mx.symbol.Activation(name='Mrelu6_stage5_L1', data=Mconv6_stage5_L1 , act_type='relu') 173 | Mconv6_stage5_L2 = mx.symbol.Convolution(name='Mconv6_stage5_L2', data=Mrelu5_stage5_L2 , num_filter=128, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 174 | Mrelu6_stage5_L2 = mx.symbol.Activation(name='Mrelu6_stage5_L2', data=Mconv6_stage5_L2 , act_type='relu') 175 | Mconv7_stage5_L1 = mx.symbol.Convolution(name='Mconv7_stage5_L1', data=Mrelu6_stage5_L1 , num_filter=numoflinks*2, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 176 | Mconv7_stage5_L2 = mx.symbol.Convolution(name='Mconv7_stage5_L2', data=Mrelu6_stage5_L2 , num_filter=numofparts, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 177 | concat_stage6 = mx.symbol.Concat(name='concat_stage6', *[Mconv7_stage5_L1,Mconv7_stage5_L2,relu4_4_CPM] ) 178 | Mconv1_stage6_L1 = mx.symbol.Convolution(name='Mconv1_stage6_L1', data=concat_stage6 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 179 | Mrelu1_stage6_L1 = mx.symbol.Activation(name='Mrelu1_stage6_L1', data=Mconv1_stage6_L1 , act_type='relu') 180 | Mconv1_stage6_L2 = mx.symbol.Convolution(name='Mconv1_stage6_L2', data=concat_stage6 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 181 | Mrelu1_stage6_L2 = mx.symbol.Activation(name='Mrelu1_stage6_L2', data=Mconv1_stage6_L2 , act_type='relu') 182 | Mconv2_stage6_L1 = mx.symbol.Convolution(name='Mconv2_stage6_L1', data=Mrelu1_stage6_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 183 | Mrelu2_stage6_L1 = mx.symbol.Activation(name='Mrelu2_stage6_L1', data=Mconv2_stage6_L1 , act_type='relu') 184 | Mconv2_stage6_L2 = mx.symbol.Convolution(name='Mconv2_stage6_L2', data=Mrelu1_stage6_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 185 | Mrelu2_stage6_L2 = mx.symbol.Activation(name='Mrelu2_stage6_L2', data=Mconv2_stage6_L2 , act_type='relu') 186 | Mconv3_stage6_L1 = mx.symbol.Convolution(name='Mconv3_stage6_L1', data=Mrelu2_stage6_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 187 | Mrelu3_stage6_L1 = mx.symbol.Activation(name='Mrelu3_stage6_L1', data=Mconv3_stage6_L1 , act_type='relu') 188 | Mconv3_stage6_L2 = mx.symbol.Convolution(name='Mconv3_stage6_L2', data=Mrelu2_stage6_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 189 | Mrelu3_stage6_L2 = mx.symbol.Activation(name='Mrelu3_stage6_L2', data=Mconv3_stage6_L2 , act_type='relu') 190 | Mconv4_stage6_L1 = mx.symbol.Convolution(name='Mconv4_stage6_L1', data=Mrelu3_stage6_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 191 | Mrelu4_stage6_L1 = mx.symbol.Activation(name='Mrelu4_stage6_L1', data=Mconv4_stage6_L1 , act_type='relu') 192 | Mconv4_stage6_L2 = mx.symbol.Convolution(name='Mconv4_stage6_L2', data=Mrelu3_stage6_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 193 | Mrelu4_stage6_L2 = mx.symbol.Activation(name='Mrelu4_stage6_L2', data=Mconv4_stage6_L2 , act_type='relu') 194 | Mconv5_stage6_L1 = mx.symbol.Convolution(name='Mconv5_stage6_L1', data=Mrelu4_stage6_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 195 | Mrelu5_stage6_L1 = mx.symbol.Activation(name='Mrelu5_stage6_L1', data=Mconv5_stage6_L1 , act_type='relu') 196 | Mconv5_stage6_L2 = mx.symbol.Convolution(name='Mconv5_stage6_L2', data=Mrelu4_stage6_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 197 | Mrelu5_stage6_L2 = mx.symbol.Activation(name='Mrelu5_stage6_L2', data=Mconv5_stage6_L2 , act_type='relu') 198 | Mconv6_stage6_L1 = mx.symbol.Convolution(name='Mconv6_stage6_L1', data=Mrelu5_stage6_L1 , num_filter=128, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 199 | Mrelu6_stage6_L1 = mx.symbol.Activation(name='Mrelu6_stage6_L1', data=Mconv6_stage6_L1 , act_type='relu') 200 | Mconv6_stage6_L2 = mx.symbol.Convolution(name='Mconv6_stage6_L2', data=Mrelu5_stage6_L2 , num_filter=128, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 201 | Mrelu6_stage6_L2 = mx.symbol.Activation(name='Mrelu6_stage6_L2', data=Mconv6_stage6_L2 , act_type='relu') 202 | Mconv7_stage6_L1 = mx.symbol.Convolution(name='Mconv7_stage6_L1', data=Mrelu6_stage6_L1 , num_filter=numoflinks*2, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 203 | Mconv7_stage6_L2 = mx.symbol.Convolution(name='Mconv7_stage6_L2', data=Mrelu6_stage6_L2 , num_filter=numofparts, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 204 | 205 | conv5_5_CPM_L1r = mx.symbol.Reshape(data=conv5_5_CPM_L1, shape=(-1,), name='conv5_5_CPM_L1r') 206 | partaffinityglabelr = mx.symbol.Reshape(data=partaffinityglabel, shape=(-1, ), name='partaffinityglabelr') 207 | stage1_loss_L1s = mx.symbol.square(conv5_5_CPM_L1r-partaffinityglabelr) 208 | vecweightw = mx.symbol.Reshape(data=vecweight, shape=(-1,), name='conv5_5_CPM_L1w') 209 | stage1_loss_L1w = stage1_loss_L1s*vecweightw 210 | stage1_loss_L1 = mx.symbol.MakeLoss(stage1_loss_L1w) 211 | 212 | conv5_5_CPM_L2r = mx.symbol.Reshape(data=conv5_5_CPM_L2, shape=(-1,), name='conv5_5_CPM_L2r') 213 | heatmaplabelr = mx.symbol.Reshape(data=heatmaplabel, shape=(-1, ), name='heatmaplabelr') 214 | stage1_loss_L2s = mx.symbol.square(conv5_5_CPM_L2r-heatmaplabelr) 215 | heatweightw = mx.symbol.Reshape(data=heatweight, shape=(-1,), name='conv5_5_CPM_L2w') 216 | stage1_loss_L2w = stage1_loss_L2s*heatweightw 217 | stage1_loss_L2 = mx.symbol.MakeLoss(stage1_loss_L2w) 218 | 219 | Mconv7_stage2_L1r = mx.symbol.Reshape(data=Mconv7_stage2_L1, shape=(-1,), name='Mconv7_stage2_L1') 220 | #partaffinityglabelr = mx.symbol.Reshape(data=partaffinityglabel, shape=(-1, ), name='partaffinityglabelr') 221 | stage2_loss_L1s = mx.symbol.square(Mconv7_stage2_L1r - partaffinityglabelr) 222 | #vecweightw = mx.symbol.Reshape(data=vecweight, shape=(-1,), name='Mconv7_stage2_L1r') 223 | stage2_loss_L1w = stage2_loss_L1s*vecweightw 224 | stage2_loss_L1 = mx.symbol.MakeLoss(stage2_loss_L1w) 225 | 226 | Mconv7_stage2_L2r = mx.symbol.Reshape(data=Mconv7_stage2_L2, shape=(-1,), name='Mconv7_stage2_L2') 227 | #heatmaplabelr = mx.symbol.Reshape(data=heatmaplabel, shape=(-1, ), name='heatmaplabelr') 228 | stage2_loss_L2s = mx.symbol.square(Mconv7_stage2_L2r-heatmaplabelr) 229 | #heatweightw = mx.symbol.Reshape(data=heatweight, shape=(-1,), name='conv5_5_CPM_L1r') 230 | stage2_loss_L2w = stage2_loss_L2s*heatweightw 231 | stage2_loss_L2 = mx.symbol.MakeLoss(stage2_loss_L2w) 232 | 233 | 234 | Mconv7_stage3_L1r = mx.symbol.Reshape(data=Mconv7_stage3_L1, shape=(-1,), name='Mconv7_stage3_L1') 235 | #partaffinityglabelr = mx.symbol.Reshape(data=partaffinityglabel, shape=(-1, ), name='partaffinityglabelr') 236 | stage3_loss_L1s = mx.symbol.square(Mconv7_stage3_L1r - partaffinityglabelr) 237 | #vecweightw = mx.symbol.Reshape(data=vecweight, shape=(-1,), name='Mconv7_stage2_L1r') 238 | stage3_loss_L1w = stage3_loss_L1s*vecweightw 239 | stage3_loss_L1 = mx.symbol.MakeLoss(stage3_loss_L1w) 240 | 241 | Mconv7_stage3_L2r = mx.symbol.Reshape(data=Mconv7_stage3_L2, shape=(-1,), name='Mconv7_stage3_L2') 242 | #heatmaplabelr = mx.symbol.Reshape(data=heatmaplabel, shape=(-1, ), name='heatmaplabelr') 243 | stage3_loss_L2s = mx.symbol.square(Mconv7_stage3_L2r-heatmaplabelr) 244 | #heatweightw = mx.symbol.Reshape(data=heatweight, shape=(-1,), name='conv5_5_CPM_L1r') 245 | stage3_loss_L2w = stage3_loss_L2s*heatweightw 246 | stage3_loss_L2 = mx.symbol.MakeLoss(stage3_loss_L2w) 247 | 248 | Mconv7_stage4_L1r = mx.symbol.Reshape(data=Mconv7_stage4_L1, shape=(-1,), name='Mconv7_stage4_L1') 249 | #partaffinityglabelr = mx.symbol.Reshape(data=partaffinityglabel, shape=(-1, ), name='partaffinityglabelr') 250 | stage4_loss_L1s = mx.symbol.square(Mconv7_stage4_L1r - partaffinityglabelr) 251 | #vecweightw = mx.symbol.Reshape(data=vecweight, shape=(-1,), name='Mconv7_stage2_L1r') 252 | stage4_loss_L1w = stage4_loss_L1s*vecweightw 253 | stage4_loss_L1 = mx.symbol.MakeLoss(stage4_loss_L1w) 254 | 255 | Mconv7_stage4_L2r = mx.symbol.Reshape(data=Mconv7_stage4_L2, shape=(-1,), name='Mconv7_stage4_L2') 256 | #heatmaplabelr = mx.symbol.Reshape(data=heatmaplabel, shape=(-1, ), name='heatmaplabelr') 257 | stage4_loss_L2s = mx.symbol.square(Mconv7_stage4_L2r-heatmaplabelr) 258 | #heatweightw = mx.symbol.Reshape(data=heatweight, shape=(-1,), name='conv5_5_CPM_L1r') 259 | stage4_loss_L2w = stage4_loss_L2s*heatweightw 260 | stage4_loss_L2 = mx.symbol.MakeLoss(stage4_loss_L2w) 261 | 262 | Mconv7_stage5_L1r = mx.symbol.Reshape(data=Mconv7_stage5_L1, shape=(-1,), name='Mconv7_stage5_L1') 263 | #partaffinityglabelr = mx.symbol.Reshape(data=partaffinityglabel, shape=(-1, ), name='partaffinityglabelr') 264 | stage5_loss_L1s = mx.symbol.square(Mconv7_stage5_L1r - partaffinityglabelr) 265 | #vecweightw = mx.symbol.Reshape(data=vecweight, shape=(-1,), name='Mconv7_stage2_L1r') 266 | stage5_loss_L1w = stage5_loss_L1s*vecweightw 267 | stage5_loss_L1 = mx.symbol.MakeLoss(stage5_loss_L1w) 268 | 269 | Mconv7_stage5_L2r = mx.symbol.Reshape(data=Mconv7_stage5_L2, shape=(-1,), name='Mconv7_stage5_L2') 270 | #heatmaplabelr = mx.symbol.Reshape(data=heatmaplabel, shape=(-1, ), name='heatmaplabelr') 271 | stage5_loss_L2s = mx.symbol.square(Mconv7_stage5_L2r-heatmaplabelr) 272 | #heatweightw = mx.symbol.Reshape(data=heatweight, shape=(-1,), name='conv5_5_CPM_L1r') 273 | stage5_loss_L2w = stage5_loss_L2s*heatweightw 274 | stage5_loss_L2 = mx.symbol.MakeLoss(stage5_loss_L2w) 275 | 276 | 277 | Mconv7_stage6_L1r = mx.symbol.Reshape(data=Mconv7_stage6_L1, shape=(-1,), name='Mconv7_stage3_L1') 278 | #partaffinityglabelr = mx.symbol.Reshape(data=partaffinityglabel, shape=(-1, ), name='partaffinityglabelr') 279 | stage6_loss_L1s = mx.symbol.square(Mconv7_stage6_L1r - partaffinityglabelr) 280 | #vecweightw = mx.symbol.Reshape(data=vecweight, shape=(-1,), name='Mconv7_stage2_L1r') 281 | stage6_loss_L1w = stage6_loss_L1s*vecweightw 282 | stage6_loss_L1 = mx.symbol.MakeLoss(stage6_loss_L1w) 283 | 284 | Mconv7_stage6_L2r = mx.symbol.Reshape(data=Mconv7_stage6_L2, shape=(-1,), name='Mconv7_stage3_L2') 285 | #heatmaplabelr = mx.symbol.Reshape(data=heatmaplabel, shape=(-1, ), name='heatmaplabelr') 286 | stage6_loss_L2s = mx.symbol.square(Mconv7_stage6_L2r-heatmaplabelr) 287 | #heatweightw = mx.symbol.Reshape(data=heatweight, shape=(-1,), name='conv5_5_CPM_L1r') 288 | stage6_loss_L2w = stage6_loss_L2s*heatweightw 289 | stage6_loss_L2 = mx.symbol.MakeLoss(stage6_loss_L2w) 290 | 291 | group = mx.symbol.Group([stage1_loss_L1, stage1_loss_L2, 292 | stage2_loss_L1, stage2_loss_L2, 293 | stage3_loss_L1, stage3_loss_L2, 294 | stage4_loss_L1, stage4_loss_L2, 295 | stage5_loss_L1, stage5_loss_L2, 296 | stage6_loss_L1, stage6_loss_L2]) 297 | return group 298 | 299 | def CPMModel_test(): 300 | 301 | data = mx.symbol.Variable(name='data') 302 | ## heat map of human parts 303 | heatmaplabel = mx.sym.Variable("heatmaplabel") 304 | ## part affinity graph 305 | partaffinityglabel = mx.sym.Variable('partaffinityglabel') 306 | 307 | heatweight = mx.sym.Variable('heatweight') 308 | 309 | vecweight = mx.sym.Variable('vecweight') 310 | 311 | conv1_1 = mx.symbol.Convolution(name='conv1_1', data=data , num_filter=64, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 312 | relu1_1 = mx.symbol.Activation(name='relu1_1', data=conv1_1 , act_type='relu') 313 | conv1_2 = mx.symbol.Convolution(name='conv1_2', data=relu1_1 , num_filter=64, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 314 | relu1_2 = mx.symbol.Activation(name='relu1_2', data=conv1_2 , act_type='relu') 315 | pool1_stage1 = mx.symbol.Pooling(name='pool1_stage1', data=relu1_2 , pooling_convention='full', pad=(0,0), kernel=(2,2), stride=(2,2), pool_type='max') 316 | conv2_1 = mx.symbol.Convolution(name='conv2_1', data=pool1_stage1 , num_filter=128, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 317 | relu2_1 = mx.symbol.Activation(name='relu2_1', data=conv2_1 , act_type='relu') 318 | conv2_2 = mx.symbol.Convolution(name='conv2_2', data=relu2_1 , num_filter=128, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 319 | relu2_2 = mx.symbol.Activation(name='relu2_2', data=conv2_2 , act_type='relu') 320 | pool2_stage1 = mx.symbol.Pooling(name='pool2_stage1', data=relu2_2 , pooling_convention='full', pad=(0,0), kernel=(2,2), stride=(2,2), pool_type='max') 321 | conv3_1 = mx.symbol.Convolution(name='conv3_1', data=pool2_stage1 , num_filter=256, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 322 | relu3_1 = mx.symbol.Activation(name='relu3_1', data=conv3_1 , act_type='relu') 323 | conv3_2 = mx.symbol.Convolution(name='conv3_2', data=relu3_1 , num_filter=256, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 324 | relu3_2 = mx.symbol.Activation(name='relu3_2', data=conv3_2 , act_type='relu') 325 | conv3_3 = mx.symbol.Convolution(name='conv3_3', data=relu3_2 , num_filter=256, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 326 | relu3_3 = mx.symbol.Activation(name='relu3_3', data=conv3_3 , act_type='relu') 327 | conv3_4 = mx.symbol.Convolution(name='conv3_4', data=relu3_3 , num_filter=256, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 328 | relu3_4 = mx.symbol.Activation(name='relu3_4', data=conv3_4 , act_type='relu') 329 | pool3_stage1 = mx.symbol.Pooling(name='pool3_stage1', data=relu3_4 , pooling_convention='full', pad=(0,0), kernel=(2,2), stride=(2,2), pool_type='max') 330 | conv4_1 = mx.symbol.Convolution(name='conv4_1', data=pool3_stage1 , num_filter=512, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 331 | relu4_1 = mx.symbol.Activation(name='relu4_1', data=conv4_1 , act_type='relu') 332 | conv4_2 = mx.symbol.Convolution(name='conv4_2', data=relu4_1 , num_filter=512, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 333 | relu4_2 = mx.symbol.Activation(name='relu4_2', data=conv4_2 , act_type='relu') 334 | conv4_3_CPM = mx.symbol.Convolution(name='conv4_3_CPM', data=relu4_2 , num_filter=256, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 335 | relu4_3_CPM = mx.symbol.Activation(name='relu4_3_CPM', data=conv4_3_CPM , act_type='relu') 336 | conv4_4_CPM = mx.symbol.Convolution(name='conv4_4_CPM', data=relu4_3_CPM , num_filter=128, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 337 | relu4_4_CPM = mx.symbol.Activation(name='relu4_4_CPM', data=conv4_4_CPM , act_type='relu') 338 | conv5_1_CPM_L1 = mx.symbol.Convolution(name='conv5_1_CPM_L1', data=relu4_4_CPM , num_filter=128, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 339 | relu5_1_CPM_L1 = mx.symbol.Activation(name='relu5_1_CPM_L1', data=conv5_1_CPM_L1 , act_type='relu') 340 | conv5_1_CPM_L2 = mx.symbol.Convolution(name='conv5_1_CPM_L2', data=relu4_4_CPM , num_filter=128, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 341 | relu5_1_CPM_L2 = mx.symbol.Activation(name='relu5_1_CPM_L2', data=conv5_1_CPM_L2 , act_type='relu') 342 | conv5_2_CPM_L1 = mx.symbol.Convolution(name='conv5_2_CPM_L1', data=relu5_1_CPM_L1 , num_filter=128, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 343 | relu5_2_CPM_L1 = mx.symbol.Activation(name='relu5_2_CPM_L1', data=conv5_2_CPM_L1 , act_type='relu') 344 | conv5_2_CPM_L2 = mx.symbol.Convolution(name='conv5_2_CPM_L2', data=relu5_1_CPM_L2 , num_filter=128, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 345 | relu5_2_CPM_L2 = mx.symbol.Activation(name='relu5_2_CPM_L2', data=conv5_2_CPM_L2 , act_type='relu') 346 | conv5_3_CPM_L1 = mx.symbol.Convolution(name='conv5_3_CPM_L1', data=relu5_2_CPM_L1 , num_filter=128, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 347 | relu5_3_CPM_L1 = mx.symbol.Activation(name='relu5_3_CPM_L1', data=conv5_3_CPM_L1 , act_type='relu') 348 | conv5_3_CPM_L2 = mx.symbol.Convolution(name='conv5_3_CPM_L2', data=relu5_2_CPM_L2 , num_filter=128, pad=(1,1), kernel=(3,3), stride=(1,1), no_bias=False) 349 | relu5_3_CPM_L2 = mx.symbol.Activation(name='relu5_3_CPM_L2', data=conv5_3_CPM_L2 , act_type='relu') 350 | conv5_4_CPM_L1 = mx.symbol.Convolution(name='conv5_4_CPM_L1', data=relu5_3_CPM_L1 , num_filter=512, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 351 | relu5_4_CPM_L1 = mx.symbol.Activation(name='relu5_4_CPM_L1', data=conv5_4_CPM_L1 , act_type='relu') 352 | conv5_4_CPM_L2 = mx.symbol.Convolution(name='conv5_4_CPM_L2', data=relu5_3_CPM_L2 , num_filter=512, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 353 | relu5_4_CPM_L2 = mx.symbol.Activation(name='relu5_4_CPM_L2', data=conv5_4_CPM_L2 , act_type='relu') 354 | conv5_5_CPM_L1 = mx.symbol.Convolution(name='conv5_5_CPM_L1', data=relu5_4_CPM_L1 , num_filter=numoflinks*2, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 355 | conv5_5_CPM_L2 = mx.symbol.Convolution(name='conv5_5_CPM_L2', data=relu5_4_CPM_L2 , num_filter=numofparts, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 356 | concat_stage2 = mx.symbol.Concat(name='concat_stage2', *[conv5_5_CPM_L1,conv5_5_CPM_L2,relu4_4_CPM] ) 357 | Mconv1_stage2_L1 = mx.symbol.Convolution(name='Mconv1_stage2_L1', data=concat_stage2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 358 | Mrelu1_stage2_L1 = mx.symbol.Activation(name='Mrelu1_stage2_L1', data=Mconv1_stage2_L1 , act_type='relu') 359 | Mconv1_stage2_L2 = mx.symbol.Convolution(name='Mconv1_stage2_L2', data=concat_stage2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 360 | Mrelu1_stage2_L2 = mx.symbol.Activation(name='Mrelu1_stage2_L2', data=Mconv1_stage2_L2 , act_type='relu') 361 | Mconv2_stage2_L1 = mx.symbol.Convolution(name='Mconv2_stage2_L1', data=Mrelu1_stage2_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 362 | Mrelu2_stage2_L1 = mx.symbol.Activation(name='Mrelu2_stage2_L1', data=Mconv2_stage2_L1 , act_type='relu') 363 | Mconv2_stage2_L2 = mx.symbol.Convolution(name='Mconv2_stage2_L2', data=Mrelu1_stage2_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 364 | Mrelu2_stage2_L2 = mx.symbol.Activation(name='Mrelu2_stage2_L2', data=Mconv2_stage2_L2 , act_type='relu') 365 | Mconv3_stage2_L1 = mx.symbol.Convolution(name='Mconv3_stage2_L1', data=Mrelu2_stage2_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 366 | Mrelu3_stage2_L1 = mx.symbol.Activation(name='Mrelu3_stage2_L1', data=Mconv3_stage2_L1 , act_type='relu') 367 | Mconv3_stage2_L2 = mx.symbol.Convolution(name='Mconv3_stage2_L2', data=Mrelu2_stage2_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 368 | Mrelu3_stage2_L2 = mx.symbol.Activation(name='Mrelu3_stage2_L2', data=Mconv3_stage2_L2 , act_type='relu') 369 | Mconv4_stage2_L1 = mx.symbol.Convolution(name='Mconv4_stage2_L1', data=Mrelu3_stage2_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 370 | Mrelu4_stage2_L1 = mx.symbol.Activation(name='Mrelu4_stage2_L1', data=Mconv4_stage2_L1 , act_type='relu') 371 | Mconv4_stage2_L2 = mx.symbol.Convolution(name='Mconv4_stage2_L2', data=Mrelu3_stage2_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 372 | Mrelu4_stage2_L2 = mx.symbol.Activation(name='Mrelu4_stage2_L2', data=Mconv4_stage2_L2 , act_type='relu') 373 | Mconv5_stage2_L1 = mx.symbol.Convolution(name='Mconv5_stage2_L1', data=Mrelu4_stage2_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 374 | Mrelu5_stage2_L1 = mx.symbol.Activation(name='Mrelu5_stage2_L1', data=Mconv5_stage2_L1 , act_type='relu') 375 | Mconv5_stage2_L2 = mx.symbol.Convolution(name='Mconv5_stage2_L2', data=Mrelu4_stage2_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 376 | Mrelu5_stage2_L2 = mx.symbol.Activation(name='Mrelu5_stage2_L2', data=Mconv5_stage2_L2 , act_type='relu') 377 | Mconv6_stage2_L1 = mx.symbol.Convolution(name='Mconv6_stage2_L1', data=Mrelu5_stage2_L1 , num_filter=128, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 378 | Mrelu6_stage2_L1 = mx.symbol.Activation(name='Mrelu6_stage2_L1', data=Mconv6_stage2_L1 , act_type='relu') 379 | Mconv6_stage2_L2 = mx.symbol.Convolution(name='Mconv6_stage2_L2', data=Mrelu5_stage2_L2 , num_filter=128, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 380 | Mrelu6_stage2_L2 = mx.symbol.Activation(name='Mrelu6_stage2_L2', data=Mconv6_stage2_L2 , act_type='relu') 381 | Mconv7_stage2_L1 = mx.symbol.Convolution(name='Mconv7_stage2_L1', data=Mrelu6_stage2_L1 , num_filter=numoflinks*2, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 382 | Mconv7_stage2_L2 = mx.symbol.Convolution(name='Mconv7_stage2_L2', data=Mrelu6_stage2_L2 , num_filter=numofparts, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 383 | concat_stage3 = mx.symbol.Concat(name='concat_stage3', *[Mconv7_stage2_L1,Mconv7_stage2_L2,relu4_4_CPM] ) 384 | Mconv1_stage3_L1 = mx.symbol.Convolution(name='Mconv1_stage3_L1', data=concat_stage3 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 385 | Mrelu1_stage3_L1 = mx.symbol.Activation(name='Mrelu1_stage3_L1', data=Mconv1_stage3_L1 , act_type='relu') 386 | Mconv1_stage3_L2 = mx.symbol.Convolution(name='Mconv1_stage3_L2', data=concat_stage3 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 387 | Mrelu1_stage3_L2 = mx.symbol.Activation(name='Mrelu1_stage3_L2', data=Mconv1_stage3_L2 , act_type='relu') 388 | Mconv2_stage3_L1 = mx.symbol.Convolution(name='Mconv2_stage3_L1', data=Mrelu1_stage3_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 389 | Mrelu2_stage3_L1 = mx.symbol.Activation(name='Mrelu2_stage3_L1', data=Mconv2_stage3_L1 , act_type='relu') 390 | Mconv2_stage3_L2 = mx.symbol.Convolution(name='Mconv2_stage3_L2', data=Mrelu1_stage3_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 391 | Mrelu2_stage3_L2 = mx.symbol.Activation(name='Mrelu2_stage3_L2', data=Mconv2_stage3_L2 , act_type='relu') 392 | Mconv3_stage3_L1 = mx.symbol.Convolution(name='Mconv3_stage3_L1', data=Mrelu2_stage3_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 393 | Mrelu3_stage3_L1 = mx.symbol.Activation(name='Mrelu3_stage3_L1', data=Mconv3_stage3_L1 , act_type='relu') 394 | Mconv3_stage3_L2 = mx.symbol.Convolution(name='Mconv3_stage3_L2', data=Mrelu2_stage3_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 395 | Mrelu3_stage3_L2 = mx.symbol.Activation(name='Mrelu3_stage3_L2', data=Mconv3_stage3_L2 , act_type='relu') 396 | Mconv4_stage3_L1 = mx.symbol.Convolution(name='Mconv4_stage3_L1', data=Mrelu3_stage3_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 397 | Mrelu4_stage3_L1 = mx.symbol.Activation(name='Mrelu4_stage3_L1', data=Mconv4_stage3_L1 , act_type='relu') 398 | Mconv4_stage3_L2 = mx.symbol.Convolution(name='Mconv4_stage3_L2', data=Mrelu3_stage3_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 399 | Mrelu4_stage3_L2 = mx.symbol.Activation(name='Mrelu4_stage3_L2', data=Mconv4_stage3_L2 , act_type='relu') 400 | Mconv5_stage3_L1 = mx.symbol.Convolution(name='Mconv5_stage3_L1', data=Mrelu4_stage3_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 401 | Mrelu5_stage3_L1 = mx.symbol.Activation(name='Mrelu5_stage3_L1', data=Mconv5_stage3_L1 , act_type='relu') 402 | Mconv5_stage3_L2 = mx.symbol.Convolution(name='Mconv5_stage3_L2', data=Mrelu4_stage3_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 403 | Mrelu5_stage3_L2 = mx.symbol.Activation(name='Mrelu5_stage3_L2', data=Mconv5_stage3_L2 , act_type='relu') 404 | Mconv6_stage3_L1 = mx.symbol.Convolution(name='Mconv6_stage3_L1', data=Mrelu5_stage3_L1 , num_filter=128, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 405 | Mrelu6_stage3_L1 = mx.symbol.Activation(name='Mrelu6_stage3_L1', data=Mconv6_stage3_L1 , act_type='relu') 406 | Mconv6_stage3_L2 = mx.symbol.Convolution(name='Mconv6_stage3_L2', data=Mrelu5_stage3_L2 , num_filter=128, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 407 | Mrelu6_stage3_L2 = mx.symbol.Activation(name='Mrelu6_stage3_L2', data=Mconv6_stage3_L2 , act_type='relu') 408 | Mconv7_stage3_L1 = mx.symbol.Convolution(name='Mconv7_stage3_L1', data=Mrelu6_stage3_L1 , num_filter=numoflinks*2, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 409 | Mconv7_stage3_L2 = mx.symbol.Convolution(name='Mconv7_stage3_L2', data=Mrelu6_stage3_L2 , num_filter=numofparts, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 410 | concat_stage4 = mx.symbol.Concat(name='concat_stage4', *[Mconv7_stage3_L1,Mconv7_stage3_L2,relu4_4_CPM] ) 411 | Mconv1_stage4_L1 = mx.symbol.Convolution(name='Mconv1_stage4_L1', data=concat_stage4 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 412 | Mrelu1_stage4_L1 = mx.symbol.Activation(name='Mrelu1_stage4_L1', data=Mconv1_stage4_L1 , act_type='relu') 413 | Mconv1_stage4_L2 = mx.symbol.Convolution(name='Mconv1_stage4_L2', data=concat_stage4 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 414 | Mrelu1_stage4_L2 = mx.symbol.Activation(name='Mrelu1_stage4_L2', data=Mconv1_stage4_L2 , act_type='relu') 415 | Mconv2_stage4_L1 = mx.symbol.Convolution(name='Mconv2_stage4_L1', data=Mrelu1_stage4_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 416 | Mrelu2_stage4_L1 = mx.symbol.Activation(name='Mrelu2_stage4_L1', data=Mconv2_stage4_L1 , act_type='relu') 417 | Mconv2_stage4_L2 = mx.symbol.Convolution(name='Mconv2_stage4_L2', data=Mrelu1_stage4_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 418 | Mrelu2_stage4_L2 = mx.symbol.Activation(name='Mrelu2_stage4_L2', data=Mconv2_stage4_L2 , act_type='relu') 419 | Mconv3_stage4_L1 = mx.symbol.Convolution(name='Mconv3_stage4_L1', data=Mrelu2_stage4_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 420 | Mrelu3_stage4_L1 = mx.symbol.Activation(name='Mrelu3_stage4_L1', data=Mconv3_stage4_L1 , act_type='relu') 421 | Mconv3_stage4_L2 = mx.symbol.Convolution(name='Mconv3_stage4_L2', data=Mrelu2_stage4_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 422 | Mrelu3_stage4_L2 = mx.symbol.Activation(name='Mrelu3_stage4_L2', data=Mconv3_stage4_L2 , act_type='relu') 423 | Mconv4_stage4_L1 = mx.symbol.Convolution(name='Mconv4_stage4_L1', data=Mrelu3_stage4_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 424 | Mrelu4_stage4_L1 = mx.symbol.Activation(name='Mrelu4_stage4_L1', data=Mconv4_stage4_L1 , act_type='relu') 425 | Mconv4_stage4_L2 = mx.symbol.Convolution(name='Mconv4_stage4_L2', data=Mrelu3_stage4_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 426 | Mrelu4_stage4_L2 = mx.symbol.Activation(name='Mrelu4_stage4_L2', data=Mconv4_stage4_L2 , act_type='relu') 427 | Mconv5_stage4_L1 = mx.symbol.Convolution(name='Mconv5_stage4_L1', data=Mrelu4_stage4_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 428 | Mrelu5_stage4_L1 = mx.symbol.Activation(name='Mrelu5_stage4_L1', data=Mconv5_stage4_L1 , act_type='relu') 429 | Mconv5_stage4_L2 = mx.symbol.Convolution(name='Mconv5_stage4_L2', data=Mrelu4_stage4_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 430 | Mrelu5_stage4_L2 = mx.symbol.Activation(name='Mrelu5_stage4_L2', data=Mconv5_stage4_L2 , act_type='relu') 431 | Mconv6_stage4_L1 = mx.symbol.Convolution(name='Mconv6_stage4_L1', data=Mrelu5_stage4_L1 , num_filter=128, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 432 | Mrelu6_stage4_L1 = mx.symbol.Activation(name='Mrelu6_stage4_L1', data=Mconv6_stage4_L1 , act_type='relu') 433 | Mconv6_stage4_L2 = mx.symbol.Convolution(name='Mconv6_stage4_L2', data=Mrelu5_stage4_L2 , num_filter=128, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 434 | Mrelu6_stage4_L2 = mx.symbol.Activation(name='Mrelu6_stage4_L2', data=Mconv6_stage4_L2 , act_type='relu') 435 | Mconv7_stage4_L1 = mx.symbol.Convolution(name='Mconv7_stage4_L1', data=Mrelu6_stage4_L1 , num_filter=numoflinks*2, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 436 | Mconv7_stage4_L2 = mx.symbol.Convolution(name='Mconv7_stage4_L2', data=Mrelu6_stage4_L2 , num_filter=numofparts, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 437 | concat_stage5 = mx.symbol.Concat(name='concat_stage5', *[Mconv7_stage4_L1,Mconv7_stage4_L2,relu4_4_CPM] ) 438 | Mconv1_stage5_L1 = mx.symbol.Convolution(name='Mconv1_stage5_L1', data=concat_stage5 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 439 | Mrelu1_stage5_L1 = mx.symbol.Activation(name='Mrelu1_stage5_L1', data=Mconv1_stage5_L1 , act_type='relu') 440 | Mconv1_stage5_L2 = mx.symbol.Convolution(name='Mconv1_stage5_L2', data=concat_stage5 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 441 | Mrelu1_stage5_L2 = mx.symbol.Activation(name='Mrelu1_stage5_L2', data=Mconv1_stage5_L2 , act_type='relu') 442 | Mconv2_stage5_L1 = mx.symbol.Convolution(name='Mconv2_stage5_L1', data=Mrelu1_stage5_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 443 | Mrelu2_stage5_L1 = mx.symbol.Activation(name='Mrelu2_stage5_L1', data=Mconv2_stage5_L1 , act_type='relu') 444 | Mconv2_stage5_L2 = mx.symbol.Convolution(name='Mconv2_stage5_L2', data=Mrelu1_stage5_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 445 | Mrelu2_stage5_L2 = mx.symbol.Activation(name='Mrelu2_stage5_L2', data=Mconv2_stage5_L2 , act_type='relu') 446 | Mconv3_stage5_L1 = mx.symbol.Convolution(name='Mconv3_stage5_L1', data=Mrelu2_stage5_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 447 | Mrelu3_stage5_L1 = mx.symbol.Activation(name='Mrelu3_stage5_L1', data=Mconv3_stage5_L1 , act_type='relu') 448 | Mconv3_stage5_L2 = mx.symbol.Convolution(name='Mconv3_stage5_L2', data=Mrelu2_stage5_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 449 | Mrelu3_stage5_L2 = mx.symbol.Activation(name='Mrelu3_stage5_L2', data=Mconv3_stage5_L2 , act_type='relu') 450 | Mconv4_stage5_L1 = mx.symbol.Convolution(name='Mconv4_stage5_L1', data=Mrelu3_stage5_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 451 | Mrelu4_stage5_L1 = mx.symbol.Activation(name='Mrelu4_stage5_L1', data=Mconv4_stage5_L1 , act_type='relu') 452 | Mconv4_stage5_L2 = mx.symbol.Convolution(name='Mconv4_stage5_L2', data=Mrelu3_stage5_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 453 | Mrelu4_stage5_L2 = mx.symbol.Activation(name='Mrelu4_stage5_L2', data=Mconv4_stage5_L2 , act_type='relu') 454 | Mconv5_stage5_L1 = mx.symbol.Convolution(name='Mconv5_stage5_L1', data=Mrelu4_stage5_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 455 | Mrelu5_stage5_L1 = mx.symbol.Activation(name='Mrelu5_stage5_L1', data=Mconv5_stage5_L1 , act_type='relu') 456 | Mconv5_stage5_L2 = mx.symbol.Convolution(name='Mconv5_stage5_L2', data=Mrelu4_stage5_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 457 | Mrelu5_stage5_L2 = mx.symbol.Activation(name='Mrelu5_stage5_L2', data=Mconv5_stage5_L2 , act_type='relu') 458 | Mconv6_stage5_L1 = mx.symbol.Convolution(name='Mconv6_stage5_L1', data=Mrelu5_stage5_L1 , num_filter=128, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 459 | Mrelu6_stage5_L1 = mx.symbol.Activation(name='Mrelu6_stage5_L1', data=Mconv6_stage5_L1 , act_type='relu') 460 | Mconv6_stage5_L2 = mx.symbol.Convolution(name='Mconv6_stage5_L2', data=Mrelu5_stage5_L2 , num_filter=128, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 461 | Mrelu6_stage5_L2 = mx.symbol.Activation(name='Mrelu6_stage5_L2', data=Mconv6_stage5_L2 , act_type='relu') 462 | Mconv7_stage5_L1 = mx.symbol.Convolution(name='Mconv7_stage5_L1', data=Mrelu6_stage5_L1 , num_filter=numoflinks*2, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 463 | Mconv7_stage5_L2 = mx.symbol.Convolution(name='Mconv7_stage5_L2', data=Mrelu6_stage5_L2 , num_filter=numofparts, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 464 | concat_stage6 = mx.symbol.Concat(name='concat_stage6', *[Mconv7_stage5_L1,Mconv7_stage5_L2,relu4_4_CPM] ) 465 | Mconv1_stage6_L1 = mx.symbol.Convolution(name='Mconv1_stage6_L1', data=concat_stage6 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 466 | Mrelu1_stage6_L1 = mx.symbol.Activation(name='Mrelu1_stage6_L1', data=Mconv1_stage6_L1 , act_type='relu') 467 | Mconv1_stage6_L2 = mx.symbol.Convolution(name='Mconv1_stage6_L2', data=concat_stage6 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 468 | Mrelu1_stage6_L2 = mx.symbol.Activation(name='Mrelu1_stage6_L2', data=Mconv1_stage6_L2 , act_type='relu') 469 | Mconv2_stage6_L1 = mx.symbol.Convolution(name='Mconv2_stage6_L1', data=Mrelu1_stage6_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 470 | Mrelu2_stage6_L1 = mx.symbol.Activation(name='Mrelu2_stage6_L1', data=Mconv2_stage6_L1 , act_type='relu') 471 | Mconv2_stage6_L2 = mx.symbol.Convolution(name='Mconv2_stage6_L2', data=Mrelu1_stage6_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 472 | Mrelu2_stage6_L2 = mx.symbol.Activation(name='Mrelu2_stage6_L2', data=Mconv2_stage6_L2 , act_type='relu') 473 | Mconv3_stage6_L1 = mx.symbol.Convolution(name='Mconv3_stage6_L1', data=Mrelu2_stage6_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 474 | Mrelu3_stage6_L1 = mx.symbol.Activation(name='Mrelu3_stage6_L1', data=Mconv3_stage6_L1 , act_type='relu') 475 | Mconv3_stage6_L2 = mx.symbol.Convolution(name='Mconv3_stage6_L2', data=Mrelu2_stage6_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 476 | Mrelu3_stage6_L2 = mx.symbol.Activation(name='Mrelu3_stage6_L2', data=Mconv3_stage6_L2 , act_type='relu') 477 | Mconv4_stage6_L1 = mx.symbol.Convolution(name='Mconv4_stage6_L1', data=Mrelu3_stage6_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 478 | Mrelu4_stage6_L1 = mx.symbol.Activation(name='Mrelu4_stage6_L1', data=Mconv4_stage6_L1 , act_type='relu') 479 | Mconv4_stage6_L2 = mx.symbol.Convolution(name='Mconv4_stage6_L2', data=Mrelu3_stage6_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 480 | Mrelu4_stage6_L2 = mx.symbol.Activation(name='Mrelu4_stage6_L2', data=Mconv4_stage6_L2 , act_type='relu') 481 | Mconv5_stage6_L1 = mx.symbol.Convolution(name='Mconv5_stage6_L1', data=Mrelu4_stage6_L1 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 482 | Mrelu5_stage6_L1 = mx.symbol.Activation(name='Mrelu5_stage6_L1', data=Mconv5_stage6_L1 , act_type='relu') 483 | Mconv5_stage6_L2 = mx.symbol.Convolution(name='Mconv5_stage6_L2', data=Mrelu4_stage6_L2 , num_filter=128, pad=(3,3), kernel=(7,7), stride=(1,1), no_bias=False) 484 | Mrelu5_stage6_L2 = mx.symbol.Activation(name='Mrelu5_stage6_L2', data=Mconv5_stage6_L2 , act_type='relu') 485 | Mconv6_stage6_L1 = mx.symbol.Convolution(name='Mconv6_stage6_L1', data=Mrelu5_stage6_L1 , num_filter=128, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 486 | Mrelu6_stage6_L1 = mx.symbol.Activation(name='Mrelu6_stage6_L1', data=Mconv6_stage6_L1 , act_type='relu') 487 | Mconv6_stage6_L2 = mx.symbol.Convolution(name='Mconv6_stage6_L2', data=Mrelu5_stage6_L2 , num_filter=128, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 488 | Mrelu6_stage6_L2 = mx.symbol.Activation(name='Mrelu6_stage6_L2', data=Mconv6_stage6_L2 , act_type='relu') 489 | Mconv7_stage6_L1 = mx.symbol.Convolution(name='Mconv7_stage6_L1', data=Mrelu6_stage6_L1 , num_filter=numoflinks*2, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 490 | Mconv7_stage6_L2 = mx.symbol.Convolution(name='Mconv7_stage6_L2', data=Mrelu6_stage6_L2 , num_filter=numofparts, pad=(0,0), kernel=(1,1), stride=(1,1), no_bias=False) 491 | 492 | 493 | group = mx.symbol.Group([conv5_5_CPM_L1, conv5_5_CPM_L2, 494 | Mconv7_stage2_L1, Mconv7_stage2_L2, 495 | Mconv7_stage3_L1, Mconv7_stage3_L2, 496 | Mconv7_stage4_L1, Mconv7_stage4_L2, 497 | Mconv7_stage5_L1, Mconv7_stage5_L2, 498 | Mconv7_stage6_L1, Mconv7_stage6_L2]) 499 | return group 500 | 501 | class DataBatchweight(object): 502 | def __init__(self, data, heatmaplabel, partaffinityglabel, heatweight, vecweight, pad=0): 503 | self.data = [data] 504 | self.label = [heatmaplabel, partaffinityglabel, heatweight, vecweight] 505 | self.pad = pad 506 | 507 | -------------------------------------------------------------------------------- /pose_io/.ipynb_checkpoints/annotation-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false, 8 | "deletable": true, 9 | "editable": true 10 | }, 11 | "outputs": [ 12 | { 13 | "name": "stdout", 14 | "output_type": "stream", 15 | "text": [ 16 | "/data/guest_users/liangdong/liangdong/practice_demo/mxnet_CPM/pose_io\r\n" 17 | ] 18 | } 19 | ], 20 | "source": [ 21 | "!pwd" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 2, 27 | "metadata": { 28 | "collapsed": false, 29 | "deletable": true, 30 | "editable": true, 31 | "scrolled": true 32 | }, 33 | "outputs": [ 34 | { 35 | "name": "stdout", 36 | "output_type": "stream", 37 | "text": [ 38 | "loading annotations into memory...\n", 39 | "Done (t=5.81s)\n", 40 | "creating index...\n", 41 | "index created!\n" 42 | ] 43 | }, 44 | { 45 | "data": { 46 | "text/plain": [ 47 | "45174" 48 | ] 49 | }, 50 | "execution_count": 2, 51 | "metadata": {}, 52 | "output_type": "execute_result" 53 | } 54 | ], 55 | "source": [ 56 | "import sys\n", 57 | "sys.path.append(\"/data/guest_users/liangdong/liangdong/practice_demo\")\n", 58 | "\n", 59 | "from pycocotools.coco import COCO\n", 60 | "from collections import namedtuple\n", 61 | "import numpy as np\n", 62 | "from numpy import ma\n", 63 | "import math\n", 64 | "import skimage.io as io\n", 65 | "import matplotlib.pyplot as plt\n", 66 | "import matplotlib\n", 67 | "import pylab\n", 68 | "import mxnet as mx\n", 69 | "pylab.rcParams['figure.figsize'] = (10.0, 8.0)\n", 70 | "\n", 71 | "\n", 72 | "path1 = '/data/datasets/COCO/person_keypoints_trainval2014/person_keypoints_train2014.json'\n", 73 | "\n", 74 | "annFile = path1\n", 75 | "coco = COCO(annFile)\n", 76 | "cats = coco.loadCats(coco.getCatIds())\n", 77 | "catIds = coco.getCatIds(catNms=['person'])\n", 78 | "imgIds = coco.getImgIds(catIds=catIds )\n", 79 | "len(imgIds)" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 3, 85 | "metadata": { 86 | "collapsed": false, 87 | "deletable": true, 88 | "editable": true 89 | }, 90 | "outputs": [ 91 | { 92 | "data": { 93 | "text/plain": [ 94 | "{u'coco_url': u'http://mscoco.org/images/262145',\n", 95 | " u'date_captured': u'2013-11-20 02:07:55',\n", 96 | " u'file_name': u'COCO_train2014_000000262145.jpg',\n", 97 | " u'flickr_url': u'http://farm8.staticflickr.com/7187/6967031859_5f08387bde_z.jpg',\n", 98 | " u'height': 427,\n", 99 | " u'id': 262145,\n", 100 | " u'license': 2,\n", 101 | " u'width': 640}" 102 | ] 103 | }, 104 | "execution_count": 3, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "coco.loadImgs(imgIds[0])[0]" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 4, 116 | "metadata": { 117 | "collapsed": true, 118 | "deletable": true, 119 | "editable": true 120 | }, 121 | "outputs": [], 122 | "source": [ 123 | "trainimagepath = '/data/datasets/COCO/train2014/' " 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 5, 129 | "metadata": { 130 | "collapsed": false, 131 | "deletable": true, 132 | "editable": true 133 | }, 134 | "outputs": [ 135 | { 136 | "name": "stdout", 137 | "output_type": "stream", 138 | "text": [ 139 | "loading annotations into memory...\n", 140 | "Done (t=6.31s)\n", 141 | "creating index...\n", 142 | "index created!\n" 143 | ] 144 | }, 145 | { 146 | "data": { 147 | "text/plain": [ 148 | "45174" 149 | ] 150 | }, 151 | "execution_count": 5, 152 | "metadata": {}, 153 | "output_type": "execute_result" 154 | } 155 | ], 156 | "source": [ 157 | "import sys\n", 158 | "sys.path.append(\"/data/guest_users/liangdong/liangdong/practice_demo\")\n", 159 | "\n", 160 | "from pycocotools.coco import COCO\n", 161 | "from collections import namedtuple\n", 162 | "import numpy as np\n", 163 | "from numpy import ma\n", 164 | "import math\n", 165 | "import skimage.io as io\n", 166 | "import matplotlib.pyplot as plt\n", 167 | "import matplotlib\n", 168 | "import pylab\n", 169 | "import mxnet as mx\n", 170 | "pylab.rcParams['figure.figsize'] = (10.0, 8.0)\n", 171 | "\n", 172 | "\n", 173 | "path1 = '/data/datasets/COCO/person_keypoints_trainval2014/person_keypoints_train2014.json'\n", 174 | "\n", 175 | "annFile = path1\n", 176 | "coco = COCO(annFile)\n", 177 | "cats = coco.loadCats(coco.getCatIds())\n", 178 | "catIds = coco.getCatIds(catNms=['person'])\n", 179 | "imgIds = coco.getImgIds(catIds=catIds )\n", 180 | "len(imgIds)" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": 6, 186 | "metadata": { 187 | "collapsed": true, 188 | "deletable": true, 189 | "editable": true 190 | }, 191 | "outputs": [], 192 | "source": [ 193 | "jointall=[]" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 7, 199 | "metadata": { 200 | "collapsed": true, 201 | "deletable": true, 202 | "editable": true 203 | }, 204 | "outputs": [], 205 | "source": [ 206 | "from numpy import linalg as LA" 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": 8, 212 | "metadata": { 213 | "collapsed": true, 214 | "deletable": true, 215 | "editable": true 216 | }, 217 | "outputs": [], 218 | "source": [ 219 | "trainimagepath = '/data/datasets/COCO/train2014/' " 220 | ] 221 | }, 222 | { 223 | "cell_type": "raw", 224 | "metadata": { 225 | "deletable": true, 226 | "editable": true 227 | }, 228 | "source": [ 229 | "2 means cropped, 0 means occluded by still on image" 230 | ] 231 | }, 232 | { 233 | "cell_type": "markdown", 234 | "metadata": { 235 | "deletable": true, 236 | "editable": true 237 | }, 238 | "source": [ 239 | "### python read and write json" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": 10, 245 | "metadata": { 246 | "collapsed": false, 247 | "deletable": true, 248 | "editable": true 249 | }, 250 | "outputs": [], 251 | "source": [ 252 | "import json\n", 253 | "\n" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": 10, 259 | "metadata": { 260 | "collapsed": false, 261 | "deletable": true, 262 | "editable": true 263 | }, 264 | "outputs": [], 265 | "source": [ 266 | "#import time\n", 267 | "\n", 268 | "#startt = time.time()\n", 269 | "#count = 1\n", 270 | "\n", 271 | "#keysl = list(range(4,5))\n", 272 | "joint_all = dict()\n", 273 | "#joint_all[0] = [5]\n", 274 | "#joint_all" 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": 12, 280 | "metadata": { 281 | "collapsed": false, 282 | "deletable": true, 283 | "editable": true 284 | }, 285 | "outputs": [ 286 | { 287 | "name": "stdout", 288 | "output_type": "stream", 289 | "text": [ 290 | "160.55 217.12\n", 291 | "-112.42 -72.17\n", 292 | "-272.97 -289.29\n", 293 | "-52.74 13.82\n", 294 | "-213.29 -203.3\n", 295 | "59.68 85.99\n", 296 | "125.075 164.655\n", 297 | "-35.475 -52.465\n", 298 | "237.495 236.825\n", 299 | "177.815 150.835\n", 300 | "357.285 403.585\n", 301 | "196.735 186.465\n", 302 | "469.705 475.755\n", 303 | "410.025 389.765\n", 304 | "232.21 238.93\n", 305 | "414.08 482.44\n", 306 | "253.53 265.32\n", 307 | "526.5 554.61\n", 308 | "466.82 468.62\n", 309 | "289.005 317.785\n", 310 | "56.795 78.855\n", 311 | "252.43 335.91\n", 312 | "91.88 118.79\n", 313 | "364.85 408.08\n", 314 | "305.17 322.09\n", 315 | "127.355 171.255\n", 316 | "-104.855 -67.675\n", 317 | "-161.65 -146.53\n", 318 | "----image: 262145 ---------------\n", 319 | "----image: 262146 ---------------\n", 320 | "----image: 524291 ---------------\n", 321 | "----image: 393223 ---------------\n", 322 | "110.305 182.29\n", 323 | "164.59 244.56\n", 324 | "54.285 62.27\n", 325 | "-175.41 -171.705\n", 326 | "-285.715 -353.995\n", 327 | "-340.0 -416.265\n", 328 | "----image: 393224 ---------------\n", 329 | "191.905 204.435\n", 330 | "----image: 524297 ---------------\n", 331 | "----image: 393227 ---------------\n", 332 | "----image: 131084 ---------------\n", 333 | "----image: 393230 ---------------\n", 334 | "----image: 524311 ---------------\n", 335 | "-367.925 -291.11\n", 336 | "-394.285 -331.845\n", 337 | "-26.36 -40.735\n", 338 | "-310.53 -243.425\n", 339 | "57.395 47.685\n", 340 | "83.755 88.42\n", 341 | "-424.705 -379.61\n", 342 | "-56.78 -88.5\n", 343 | "-30.42 -47.765\n", 344 | "-114.175 -136.185\n", 345 | "-467.545 -420.565\n", 346 | "-99.62 -129.455\n", 347 | "-73.26 -88.72\n", 348 | "-157.015 -177.14\n", 349 | "-42.84 -40.955\n", 350 | "-432.375 -356.555\n", 351 | "-64.45 -65.445\n", 352 | "-38.09 -24.71\n", 353 | "-121.845 -113.13\n", 354 | "-7.67 23.055\n", 355 | "35.17 64.01\n", 356 | "-41.03 32.595\n", 357 | "326.895 323.705\n", 358 | "353.255 364.44\n", 359 | "269.5 276.02\n", 360 | "383.675 412.205\n", 361 | "426.515 453.16\n", 362 | "391.345 389.15\n", 363 | "-322.475 -268.47\n", 364 | "45.45 22.64\n", 365 | "71.81 63.375\n", 366 | "-11.945 -25.045\n", 367 | "102.23 111.14\n", 368 | "145.07 152.095\n", 369 | "109.9 88.085\n", 370 | "-281.445 -301.065\n", 371 | "-241.485 -195.635\n", 372 | "126.44 95.475\n", 373 | "152.8 136.21\n", 374 | "69.045 47.79\n", 375 | "183.22 183.975\n", 376 | "226.06 224.93\n", 377 | "190.89 160.92\n", 378 | "-200.455 -228.23\n", 379 | "80.99 72.835\n", 380 | "45.82 140.68\n", 381 | "413.745 431.79\n", 382 | "440.105 472.525\n", 383 | "356.35 384.105\n", 384 | "470.525 520.29\n", 385 | "513.365 561.245\n", 386 | "478.195 497.235\n", 387 | "86.85 108.085\n", 388 | "368.295 409.15\n", 389 | "287.305 336.315\n", 390 | "-43.16 19.385\n", 391 | "324.765 310.495\n", 392 | "351.125 351.23\n", 393 | "267.37 262.81\n", 394 | "381.545 398.995\n", 395 | "424.385 439.95\n", 396 | "389.215 375.94\n", 397 | "-2.13 -13.21\n", 398 | "279.315 287.855\n", 399 | "198.325 215.02\n", 400 | "-88.98 -121.295\n", 401 | "----image: 393241 ---------------\n", 402 | "224.73 118.47\n", 403 | "----image: 524314 ---------------\n", 404 | "-183.985 -209.345\n", 405 | "-240.89 -264.485\n", 406 | "-56.905 -55.14\n", 407 | "-157.98 -195.99\n", 408 | "26.005 13.355\n", 409 | "82.91 68.495\n", 410 | "----image: 262171 ---------------\n", 411 | "----image: 131101 ---------------\n", 412 | "-185.3 -131.03\n", 413 | "-241.56 -183.045\n", 414 | "-56.26 -52.015\n", 415 | "----image: 524320 ---------------\n", 416 | "-193.135 -46.91\n", 417 | "-270.84 -173.54\n", 418 | "-77.705 -126.63\n", 419 | "-180.435 -79.17\n", 420 | "12.7 -32.26\n", 421 | "90.405 94.37\n", 422 | "----image: 393251 ---------------\n", 423 | "----image: 36 ---------------\n", 424 | "----image: 524325 ---------------\n", 425 | "----image: 262191 ---------------\n", 426 | "89.35 90.015\n", 427 | "----image: 49 ---------------\n", 428 | "----image: 524338 ---------------\n", 429 | "-253.43 -213.685\n", 430 | "-197.395 -91.69\n", 431 | "56.035 121.995\n", 432 | "-103.665 37.99\n", 433 | "149.765 251.675\n", 434 | "93.73 129.68\n", 435 | "-315.535 -173.11\n", 436 | "-62.105 40.575\n", 437 | "-118.14 -81.42\n", 438 | "-211.87 -211.1\n", 439 | "----image: 393268 ---------------\n", 440 | "-167.18 -325.85\n", 441 | "----image: 131127 ---------------\n", 442 | "----image: 61 ---------------\n", 443 | "-121.145 -99.26\n", 444 | "183.785 223.245\n", 445 | "304.93 322.505\n", 446 | "347.96 355.845\n", 447 | "469.105 455.105\n", 448 | "164.175 132.6\n", 449 | "314.76 343.07\n", 450 | "435.905 442.33\n", 451 | "130.975 119.825\n", 452 | "-33.2 -12.775\n", 453 | "381.655 432.835\n", 454 | "502.8 532.095\n", 455 | "197.87 209.59\n", 456 | "33.695 76.99\n", 457 | "66.895 89.765\n", 458 | "----image: 174773 ---------------\n", 459 | "----image: 393290 ---------------\n", 460 | "-167.78 -99.305\n", 461 | "----image: 262221 ---------------\n", 462 | "----image: 524366 ---------------\n", 463 | "----image: 86 ---------------\n", 464 | "----image: 524375 ---------------\n", 465 | "----image: 393306 ---------------\n", 466 | "----image: 327695 ---------------\n", 467 | "----image: 262238 ---------------\n", 468 | "-278.605 -286.555\n", 469 | "----image: 262239 ---------------\n", 470 | "-171.635 -151.37\n", 471 | "-53.115 -29.095\n", 472 | "118.52 122.275\n", 473 | "78.06 126.57\n", 474 | "249.695 277.94\n", 475 | "131.175 155.665\n", 476 | "103.09 132.115\n", 477 | "274.725 283.485\n", 478 | "156.205 161.21\n", 479 | "25.03 5.545\n", 480 | "228.24 262.08\n", 481 | "399.875 413.45\n", 482 | "281.355 291.175\n", 483 | "150.18 135.51\n", 484 | "125.15 129.965\n", 485 | "-108.715 -107.41\n", 486 | "62.92 43.96\n", 487 | "-55.6 -78.315\n", 488 | "-186.775 -233.98\n", 489 | "-211.805 -239.525\n", 490 | "-336.955 -369.49\n", 491 | "121.395 105.155\n", 492 | "293.03 256.525\n", 493 | "174.51 134.25\n", 494 | "43.335 -21.415\n", 495 | "18.305 -26.96\n", 496 | "-106.845 -156.925\n", 497 | "230.11 212.565\n", 498 | "-202.86 -131.555\n", 499 | "-31.225 19.815\n", 500 | "-149.745 -102.46\n", 501 | "-280.92 -258.125\n", 502 | "-305.95 -263.67\n", 503 | "-431.1 -393.635\n", 504 | "-94.145 -24.145\n", 505 | "-324.255 -236.71\n", 506 | "274.4 312.185\n", 507 | "446.035 463.555\n", 508 | "327.515 341.28\n", 509 | "196.34 185.615\n", 510 | "171.31 180.07\n", 511 | "46.16 50.105\n", 512 | "383.115 419.595\n", 513 | "153.005 207.03\n", 514 | "477.26 443.74\n", 515 | "210.765 249.715\n", 516 | "382.4 401.085\n", 517 | "263.88 278.81\n", 518 | "132.705 123.145\n", 519 | "107.675 117.6\n", 520 | "-17.475 -12.365\n", 521 | "319.48 357.125\n", 522 | "89.37 144.56\n", 523 | "413.625 381.27\n", 524 | "-63.635 -62.47\n", 525 | "325.8 347.615\n", 526 | "497.435 498.985\n", 527 | "378.915 376.71\n", 528 | "247.74 221.045\n", 529 | "222.71 215.5\n", 530 | "97.56 85.535\n", 531 | "434.515 455.025\n", 532 | "204.405 242.46\n", 533 | "528.66 479.17\n", 534 | "51.4 35.43\n", 535 | "115.035 97.9\n", 536 | "----image: 131172 ---------------\n", 537 | "----image: 393317 ---------------\n", 538 | "----image: 131174 ---------------\n", 539 | "----image: 109 ---------------\n", 540 | "297.35 303.64\n", 541 | "111.82 128.9\n", 542 | "-185.53 -174.74\n", 543 | "193.115 201.375\n", 544 | "-104.235 -102.265\n", 545 | "81.295 72.475\n", 546 | "242.445 261.375\n", 547 | "-54.905 -42.265\n", 548 | "130.625 132.475\n", 549 | "49.33 60.0\n", 550 | "358.95 358.27\n", 551 | "61.6 54.63\n", 552 | "247.13 229.37\n", 553 | "165.835 156.895\n", 554 | "116.505 96.895\n", 555 | "-0.04 -3.34\n", 556 | "-297.39 -306.98\n", 557 | "-111.86 -132.24\n", 558 | "-193.155 -204.715\n", 559 | "-242.485 -264.715\n", 560 | "-358.99 -361.61\n", 561 | "48.62 63.05\n", 562 | "-248.73 -240.59\n", 563 | "-63.2 -65.85\n", 564 | "-144.495 -138.325\n", 565 | "-193.825 -198.325\n", 566 | "-310.33 -295.22\n", 567 | "48.66 66.39\n", 568 | "----image: 110 ---------------\n", 569 | "178.955 221.655\n", 570 | "380.045 419.33\n", 571 | "201.09 197.675\n", 572 | "-125.86 -106.43\n", 573 | "-304.815 -328.085\n", 574 | "-505.905 -525.76\n", 575 | "89.06 119.17\n", 576 | "-89.895 -102.485\n", 577 | "-290.985 -300.16\n", 578 | "214.92 225.6\n", 579 | "----image: 524401 ---------------\n", 580 | "----image: 262260 ---------------\n", 581 | "----image: 393333 ---------------\n", 582 | "-179.875 -172.155\n", 583 | "----image: 270604 ---------------\n", 584 | "----image: 131197 ---------------\n", 585 | "----image: 127 ---------------\n", 586 | "----image: 262273 ---------------\n", 587 | "73.045 104.775\n", 588 | "311.635 230.47\n", 589 | "238.59 125.695\n", 590 | "386.27 346.06\n", 591 | "313.225 241.285\n", 592 | "74.635 115.59\n", 593 | "170.705 209.11\n", 594 | "97.66 104.335\n", 595 | "-140.93 -21.36\n", 596 | "-215.565 -136.95\n", 597 | "----image: 524420 ---------------\n", 598 | "----image: 131208 ---------------\n", 599 | "----image: 131211 ---------------\n", 600 | "145.05 111.605\n", 601 | "----image: 524428 ---------------\n", 602 | "951681.267945\n" 603 | ] 604 | } 605 | ], 606 | "source": [ 607 | "for i in range(0,50):\n", 608 | " #print('----image: '+str(imgIds[i])+' ---------------')\n", 609 | " \n", 610 | " img = coco.loadImgs(imgIds[i])[0]\n", 611 | " annIds = coco.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)\n", 612 | " anns = coco.loadAnns(annIds)\n", 613 | " \n", 614 | " height = img['height']\n", 615 | " width = img['width']\n", 616 | " #print (height, width)\n", 617 | " prev_center = list(list())\n", 618 | " \n", 619 | " for j in range(len(anns)):\n", 620 | " \n", 621 | " if anns[j]['num_keypoints']<5 or anns[j]['area']<32*32:\n", 622 | " continue\n", 623 | " \n", 624 | " person_center = [anns[j]['bbox'][0] + anns[j]['bbox'][2]/2,\n", 625 | " anns[j]['bbox'][0] + anns[j]['bbox'][3]/2]\n", 626 | " flag = 0\n", 627 | " isValidation = 0 \n", 628 | " \n", 629 | " for k in range(len(prev_center)):\n", 630 | " dist1 = prev_center[k][0] - person_center[0]\n", 631 | " dist2 = prev_center[k][1] - person_center[1]\n", 632 | " print dist1, dist2\n", 633 | " if dist1*dist1+dist2*dist2 < prev_center[k][2]*0.3:\n", 634 | " flag = 1\n", 635 | " continue\n", 636 | " \n", 637 | " currentjoin={'isValidation': isValidation,\n", 638 | " 'img_paths': trainimagepath + img['file_name'],\n", 639 | " 'objpos': person_center,\n", 640 | " 'image_id': img['id'],\n", 641 | " 'bbox': anns[j]['bbox'],\n", 642 | " 'img_width': width,\n", 643 | " 'img_height': height,\n", 644 | " 'segment_area': anns[j]['area'],\n", 645 | " 'num_keypoints': anns[j]['num_keypoints'],\n", 646 | " 'joint_self': np.zeros((17,3)).tolist(),\n", 647 | " 'scale_provided': anns[j]['bbox'][3]/368,\n", 648 | " 'joint_others': {},\n", 649 | " 'annolist_index': i ,\n", 650 | " 'people_index': j,\n", 651 | " 'numOtherPeople':0,\n", 652 | " 'scale_provided_other':{},\n", 653 | " 'objpos_other':{},\n", 654 | " 'bbox_other':{},\n", 655 | " 'segment_area_other':{},\n", 656 | " 'num_keypoints_other':{}\n", 657 | " } \n", 658 | " \n", 659 | " '''\n", 660 | " currentjoin['isValidation'] = isValidation \n", 661 | " currentjoin['img_paths'] = trainimagepath + img['file_name']\n", 662 | " currentjoin['objpos'] = person_center \n", 663 | " currentjoin['image_id'] = img['id']\n", 664 | " currentjoin['bbox'] = anns[j]['bbox']\n", 665 | " currentjoin['segment_area'] = anns[j]['area'] \n", 666 | " currentjoin['num_keypoints'] = anns[j]['num_keypoints'] \n", 667 | " anno = anns[j]['keypoints']\n", 668 | " \n", 669 | " currentjoin['joint_self'] = np.zeros((17,3))\n", 670 | " '''\n", 671 | " #print currentjoin['joint_self']\n", 672 | " \n", 673 | " #currentjoin['joint_self'][0,0]\n", 674 | " #print anns[j]\n", 675 | " for part in range(17):\n", 676 | " currentjoin['joint_self'][part][0] = anns[j]['keypoints'][part*3]\n", 677 | " currentjoin['joint_self'][part][1] = anns[j]['keypoints'][part*3+1]\n", 678 | " \n", 679 | " if(anns[j]['keypoints'][part*3+2] == 2):\n", 680 | " currentjoin['joint_self'][part][2] = 1\n", 681 | " elif(anns[j]['keypoints'][part*3+2] == 1):\n", 682 | " currentjoin['joint_self'][part][2] = 0 \n", 683 | " else:\n", 684 | " currentjoin['joint_self'][part][2] = 2\n", 685 | " \n", 686 | " # currentjoin['scale_provided'] = ann['bbox'][3]/368\n", 687 | " \n", 688 | " count_other = 1\n", 689 | " \n", 690 | " currentjoin['joint_others'] ={}\n", 691 | " \n", 692 | " for k in range(len(anns)):\n", 693 | " \n", 694 | " if k==j or anns[k]['num_keypoints']==0:\n", 695 | " continue\n", 696 | " \n", 697 | " annop = anns[k]\n", 698 | " \n", 699 | " currentjoin['scale_provided_other'][count_other] = annop['bbox'][3]/368\n", 700 | " currentjoin['objpos_other'][count_other] = [annop['bbox'][0]+annop['bbox'][2]/2, \n", 701 | " annop['bbox'][1]+annop['bbox'][3]/2]\n", 702 | " currentjoin['bbox_other'][count_other] = annop['bbox']\n", 703 | " currentjoin['segment_area_other'][count_other] = annop['area']\n", 704 | " currentjoin['num_keypoints_other'][count_other] = annop['num_keypoints']\n", 705 | " \n", 706 | " currentjoin['joint_others'][count_other] = np.zeros((17,3)).tolist()\n", 707 | " \n", 708 | " for part in range(17):\n", 709 | " currentjoin['joint_others'][count_other][part][0] = annop['keypoints'][part*3]\n", 710 | " currentjoin['joint_others'][count_other][part][1] = annop['keypoints'][part*3+1]\n", 711 | " \n", 712 | " if(annop['keypoints'][part*3+2] == 2):\n", 713 | " currentjoin['joint_others'][count_other][part][2] = 1\n", 714 | " elif(annop['keypoints'][part*3+2] == 1):\n", 715 | " currentjoin['joint_others'][count_other][part][2] = 0\n", 716 | " else:\n", 717 | " currentjoin['joint_others'][count_other][part][2] = 2\n", 718 | " \n", 719 | " \n", 720 | " currentjoin['numOtherPeople'] = len(currentjoin['joint_others'])\n", 721 | " # joint_all(count).annolist_index = i;\n", 722 | " # joint_all(count).people_index = p;\n", 723 | " # joint_all(count).numOtherPeople = length(joint_all(count).joint_others);\n", 724 | " \n", 725 | " count_other = count_other + 1\n", 726 | " \n", 727 | " prev_center.append([person_center[0], person_center[1],\n", 728 | " max(anns[j]['bbox'][2], anns[j]['bbox'][3])])\n", 729 | " \n", 730 | " joint_all[i] = currentjoin\n", 731 | " #joint_all.append(currentjoin)\n", 732 | " \n", 733 | " print('----image: '+str(imgIds[i])+' ---------------')\n", 734 | " \n", 735 | " count = count + 1\n", 736 | "endt = time.time()\n", 737 | "print (endt-startt)*45174/4" 738 | ] 739 | }, 740 | { 741 | "cell_type": "code", 742 | "execution_count": 13, 743 | "metadata": { 744 | "collapsed": false, 745 | "deletable": true, 746 | "editable": true, 747 | "scrolled": false 748 | }, 749 | "outputs": [ 750 | { 751 | "data": { 752 | "text/plain": [ 753 | "50" 754 | ] 755 | }, 756 | "execution_count": 13, 757 | "metadata": {}, 758 | "output_type": "execute_result" 759 | } 760 | ], 761 | "source": [ 762 | "len(joint_all)" 763 | ] 764 | }, 765 | { 766 | "cell_type": "code", 767 | "execution_count": 15, 768 | "metadata": { 769 | "collapsed": false, 770 | "deletable": true, 771 | "editable": true 772 | }, 773 | "outputs": [], 774 | "source": [ 775 | "import json\n", 776 | "with open('data.json', 'w') as f:\n", 777 | " json.dump(joint_all, f)" 778 | ] 779 | }, 780 | { 781 | "cell_type": "code", 782 | "execution_count": 16, 783 | "metadata": { 784 | "collapsed": true, 785 | "deletable": true, 786 | "editable": true 787 | }, 788 | "outputs": [], 789 | "source": [ 790 | "# Reading data back\n", 791 | "with open('data.json', 'r') as f:\n", 792 | " data = json.load(f)" 793 | ] 794 | }, 795 | { 796 | "cell_type": "code", 797 | "execution_count": 19, 798 | "metadata": { 799 | "collapsed": false, 800 | "deletable": true, 801 | "editable": true 802 | }, 803 | "outputs": [ 804 | { 805 | "data": { 806 | "text/plain": [ 807 | "{u'annolist_index': 0,\n", 808 | " u'bbox': [153.18, 61.92, 106.78, 117.82],\n", 809 | " u'bbox_other': {u'1': [387, 71.1, 145, 322],\n", 810 | " u'2': [282.37, 86.79, 32.16, 97.02],\n", 811 | " u'3': [526.17, 44.14, 90.5, 188],\n", 812 | " u'4': [491.51, 81.19, 40.46, 85.34],\n", 813 | " u'5': [308.98, 76.76, 49.89, 148.73],\n", 814 | " u'6': [71.97, 104.59, 59.49, 144.89],\n", 815 | " u'7': [3.3, 121.27, 83.24, 124.52]},\n", 816 | " u'image_id': 262145,\n", 817 | " u'img_height': 427,\n", 818 | " u'img_paths': u'/data/datasets/COCO/train2014/COCO_train2014_000000262145.jpg',\n", 819 | " u'img_width': 640,\n", 820 | " u'isValidation': 0,\n", 821 | " u'joint_others': {u'1': [[407, 115, 0],\n", 822 | " [407, 105, 1],\n", 823 | " [0, 0, 2],\n", 824 | " [425, 95, 1],\n", 825 | " [0, 0, 2],\n", 826 | " [435, 124, 1],\n", 827 | " [457, 105, 1],\n", 828 | " [428, 187, 1],\n", 829 | " [447, 182, 1],\n", 830 | " [404, 210, 1],\n", 831 | " [419, 213, 1],\n", 832 | " [488, 222, 1],\n", 833 | " [515, 213, 1],\n", 834 | " [471, 293, 1],\n", 835 | " [487, 297, 1],\n", 836 | " [462, 372, 0],\n", 837 | " [486, 374, 1]],\n", 838 | " u'2': [[287, 99, 1],\n", 839 | " [289, 97, 1],\n", 840 | " [0, 0, 2],\n", 841 | " [294, 95, 1],\n", 842 | " [0, 0, 2],\n", 843 | " [302, 105, 1],\n", 844 | " [286, 106, 1],\n", 845 | " [313, 116, 0],\n", 846 | " [0, 0, 2],\n", 847 | " [0, 0, 2],\n", 848 | " [0, 0, 2],\n", 849 | " [301, 146, 1],\n", 850 | " [287, 146, 1],\n", 851 | " [303, 169, 1],\n", 852 | " [286, 171, 1],\n", 853 | " [305, 187, 0],\n", 854 | " [0, 0, 2]],\n", 855 | " u'3': [[0, 0, 2],\n", 856 | " [0, 0, 2],\n", 857 | " [0, 0, 2],\n", 858 | " [568, 67, 1],\n", 859 | " [590, 65, 1],\n", 860 | " [550, 92, 1],\n", 861 | " [608, 98, 1],\n", 862 | " [534, 132, 1],\n", 863 | " [607, 149, 1],\n", 864 | " [0, 0, 2],\n", 865 | " [0, 0, 2],\n", 866 | " [554, 174, 1],\n", 867 | " [594, 177, 1],\n", 868 | " [0, 0, 2],\n", 869 | " [0, 0, 2],\n", 870 | " [0, 0, 2],\n", 871 | " [0, 0, 2]],\n", 872 | " u'4': [[0, 0, 2],\n", 873 | " [0, 0, 2],\n", 874 | " [0, 0, 2],\n", 875 | " [508, 89, 1],\n", 876 | " [520, 91, 1],\n", 877 | " [499, 106, 1],\n", 878 | " [527, 106, 1],\n", 879 | " [493, 122, 1],\n", 880 | " [525, 119, 1],\n", 881 | " [0, 0, 2],\n", 882 | " [0, 0, 2],\n", 883 | " [503, 149, 1],\n", 884 | " [518, 148, 1],\n", 885 | " [0, 0, 2],\n", 886 | " [0, 0, 2],\n", 887 | " [0, 0, 2],\n", 888 | " [0, 0, 2]],\n", 889 | " u'5': [[320, 97, 1],\n", 890 | " [324, 93, 1],\n", 891 | " [0, 0, 2],\n", 892 | " [335, 93, 1],\n", 893 | " [0, 0, 2],\n", 894 | " [337, 112, 1],\n", 895 | " [0, 0, 2],\n", 896 | " [348, 137, 1],\n", 897 | " [0, 0, 2],\n", 898 | " [333, 161, 1],\n", 899 | " [0, 0, 2],\n", 900 | " [327, 170, 1],\n", 901 | " [0, 0, 2],\n", 902 | " [0, 0, 2],\n", 903 | " [0, 0, 2],\n", 904 | " [0, 0, 2],\n", 905 | " [0, 0, 2]],\n", 906 | " u'6': [[115, 121, 1],\n", 907 | " [116, 118, 1],\n", 908 | " [111, 118, 1],\n", 909 | " [0, 0, 2],\n", 910 | " [101, 119, 1],\n", 911 | " [120, 139, 1],\n", 912 | " [94, 140, 1],\n", 913 | " [0, 0, 2],\n", 914 | " [80, 161, 1],\n", 915 | " [0, 0, 2],\n", 916 | " [112, 177, 1],\n", 917 | " [125, 184, 0],\n", 918 | " [102, 188, 1],\n", 919 | " [0, 0, 2],\n", 920 | " [118, 215, 0],\n", 921 | " [0, 0, 2],\n", 922 | " [0, 0, 2]],\n", 923 | " u'7': [[19, 144, 1],\n", 924 | " [19, 141, 1],\n", 925 | " [0, 0, 2],\n", 926 | " [28, 138, 1],\n", 927 | " [0, 0, 2],\n", 928 | " [38, 154, 1],\n", 929 | " [41, 153, 1],\n", 930 | " [36, 177, 1],\n", 931 | " [39, 174, 1],\n", 932 | " [9, 177, 0],\n", 933 | " [14, 174, 0],\n", 934 | " [50, 194, 1],\n", 935 | " [0, 0, 2],\n", 936 | " [8, 189, 1],\n", 937 | " [0, 0, 2],\n", 938 | " [14, 231, 1],\n", 939 | " [0, 0, 2]]},\n", 940 | " u'joint_self': [[0, 0, 2],\n", 941 | " [0, 0, 2],\n", 942 | " [0, 0, 2],\n", 943 | " [191, 78, 1],\n", 944 | " [211, 76, 1],\n", 945 | " [183, 102, 1],\n", 946 | " [222, 102, 1],\n", 947 | " [172, 131, 1],\n", 948 | " [229, 139, 1],\n", 949 | " [164, 154, 1],\n", 950 | " [247, 137, 1],\n", 951 | " [191, 178, 0],\n", 952 | " [217, 177, 1],\n", 953 | " [0, 0, 2],\n", 954 | " [0, 0, 2],\n", 955 | " [0, 0, 2],\n", 956 | " [0, 0, 2]],\n", 957 | " u'numOtherPeople': 7,\n", 958 | " u'num_keypoints': 10,\n", 959 | " u'num_keypoints_other': {u'1': 15,\n", 960 | " u'2': 11,\n", 961 | " u'3': 8,\n", 962 | " u'4': 8,\n", 963 | " u'5': 7,\n", 964 | " u'6': 11,\n", 965 | " u'7': 12},\n", 966 | " u'objpos': [206.57, 212.09],\n", 967 | " u'objpos_other': {u'1': [459, 232.1],\n", 968 | " u'2': [298.45, 135.3],\n", 969 | " u'3': [571.42, 138.14],\n", 970 | " u'4': [511.74, 123.86],\n", 971 | " u'5': [333.925, 151.125],\n", 972 | " u'6': [101.715, 177.035],\n", 973 | " u'7': [44.919999999999995, 183.53]},\n", 974 | " u'people_index': 12,\n", 975 | " u'scale_provided': 0.32016304347826086,\n", 976 | " u'scale_provided_other': {u'1': 0,\n", 977 | " u'2': 0.26364130434782607,\n", 978 | " u'3': 0,\n", 979 | " u'4': 0.2319021739130435,\n", 980 | " u'5': 0.40415760869565215,\n", 981 | " u'6': 0.39372282608695647,\n", 982 | " u'7': 0.3383695652173913},\n", 983 | " u'segment_area': 6513.38965,\n", 984 | " u'segment_area_other': {u'1': 21258,\n", 985 | " u'2': 2053.16515,\n", 986 | " u'3': 9110.87005,\n", 987 | " u'4': 1962.25295,\n", 988 | " u'5': 5700.342,\n", 989 | " u'6': 4953.06355,\n", 990 | " u'7': 5266.81615}}" 991 | ] 992 | }, 993 | "execution_count": 19, 994 | "metadata": {}, 995 | "output_type": "execute_result" 996 | } 997 | ], 998 | "source": [ 999 | "data['0']" 1000 | ] 1001 | }, 1002 | { 1003 | "cell_type": "code", 1004 | "execution_count": null, 1005 | "metadata": { 1006 | "collapsed": true, 1007 | "deletable": true, 1008 | "editable": true 1009 | }, 1010 | "outputs": [], 1011 | "source": [] 1012 | }, 1013 | { 1014 | "cell_type": "code", 1015 | "execution_count": null, 1016 | "metadata": { 1017 | "collapsed": true, 1018 | "deletable": true, 1019 | "editable": true 1020 | }, 1021 | "outputs": [], 1022 | "source": [] 1023 | }, 1024 | { 1025 | "cell_type": "code", 1026 | "execution_count": null, 1027 | "metadata": { 1028 | "collapsed": true, 1029 | "deletable": true, 1030 | "editable": true 1031 | }, 1032 | "outputs": [], 1033 | "source": [] 1034 | } 1035 | ], 1036 | "metadata": { 1037 | "kernelspec": { 1038 | "display_name": "Python 2", 1039 | "language": "python", 1040 | "name": "python2" 1041 | }, 1042 | "language_info": { 1043 | "codemirror_mode": { 1044 | "name": "ipython", 1045 | "version": 2 1046 | }, 1047 | "file_extension": ".py", 1048 | "mimetype": "text/x-python", 1049 | "name": "python", 1050 | "nbconvert_exporter": "python", 1051 | "pygments_lexer": "ipython2", 1052 | "version": "2.7.12" 1053 | } 1054 | }, 1055 | "nbformat": 4, 1056 | "nbformat_minor": 2 1057 | } 1058 | -------------------------------------------------------------------------------- /pose_io/parse_label.py: -------------------------------------------------------------------------------- 1 | #from pycocotools.coco import COCO 2 | import sys 3 | sys.path.append('/data/guest_users/liangdong/liangdong/practice_demo') 4 | from collections import namedtuple 5 | import numpy as np 6 | from numpy import ma 7 | import math 8 | import skimage.io as io 9 | import matplotlib.pyplot as plt 10 | import matplotlib 11 | import pylab 12 | import json 13 | import time 14 | import mxnet as mx 15 | from numpy import linalg as LA 16 | import cv2 17 | pylab.rcParams['figure.figsize'] = (10.0, 8.0) 18 | 19 | path1 = '/data/guest_users/liangdong/liangdong/practice_demo/AIchallenger/keypoint_validation_annotations_20170911.json' #'/home/zhangchenghao/mxnet_cpm/pose_io/AI_label.json' 20 | trainimagepath = '/data/guest_users/liangdong/liangdong/practice_demo/AIchallenger/validation_image/keypoint_validation_images_20170911/' 21 | 22 | jointall = [] 23 | count = 1 24 | joint_all = dict() 25 | 26 | 27 | f = open(path1) 28 | label = json.load(f) 29 | i = 0 30 | numimgs = 0 31 | for img in label: 32 | numimgs += 1 33 | 34 | 35 | img_id = img['image_id'] 36 | box = img['human_annotations'] 37 | keypoint = img['keypoint_annotations'] 38 | 39 | if i % 1000 == 0: 40 | print('processing image:'+img_id) 41 | 42 | temp = cv2.imread(trainimagepath+img_id+'.jpg') 43 | sp = temp.shape 44 | height = sp[0] 45 | width = sp[1] 46 | 47 | prev_center = list(list()) 48 | 49 | 50 | j = 0 51 | for human in box: 52 | j += 1 # people index 53 | person_center = [box[str(human)][0] + box[str(human)][2]/2, box[str(human)][1] + box[str(human)][3]/2 ] 54 | 55 | flag = 0 56 | isValidation = 0 57 | 58 | for k in range(len(prev_center)): 59 | dist1 = prev_center[k][0] - person_center[0] 60 | dist2 = prev_center[k][1] - person_center[1] 61 | #print dist1, dist2 62 | if dist1*dist1+dist2*dist2 < prev_center[k][2]*0.3: 63 | flag = 1 64 | continue 65 | 66 | currentjoin={'isValidation': isValidation, 67 | 'img_paths': trainimagepath + img_id + '.jpg', 68 | 'objpos': person_center, 69 | 'image_id': numimgs, 70 | 'bbox': box[str(human)], 71 | 'img_width': width, 72 | 'img_height': height, 73 | 'segment_area': {}, 74 | 'num_keypoints': 14, 75 | 'joint_self': np.zeros((14,3)).tolist(), 76 | 'scale_provided': box[str(human)][3]/368.0, 77 | 'segmentations': {}, 78 | 'joint_others': {}, 79 | 'annolist_index': numimgs , 80 | 'people_index': j, 81 | 'numOtherPeople':0, 82 | 'scale_provided_other':{}, 83 | 'objpos_other':{}, 84 | 'bbox_other':{}, 85 | 'segment_area_other':{}, 86 | 'num_keypoints_other':{} 87 | } 88 | 89 | 90 | for part in range(14): 91 | currentjoin['joint_self'][part][0] = keypoint[str(human)][part*3] 92 | currentjoin['joint_self'][part][1] = keypoint[str(human)][part*3+1] 93 | # COCO dataset: 1 visible, 2 non-visible 94 | # 2 means cropped, 0 means occluded by still on image 95 | if(keypoint[str(human)][part*3+2] == 2): 96 | currentjoin['joint_self'][part][2] = 1 97 | elif(keypoint[str(human)][part*3+2] == 1): 98 | currentjoin['joint_self'][part][2] = 0 99 | else: 100 | currentjoin['joint_self'][part][2] = 2 101 | 102 | 103 | 104 | count_other = 1 105 | 106 | currentjoin['joint_others'] ={} 107 | 108 | for other_human in box: 109 | 110 | if other_human == human: 111 | continue 112 | 113 | currentjoin['scale_provided_other'][count_other] = box[str(other_human)][3]/368 114 | currentjoin['objpos_other'][count_other] = [box[str(other_human)][0]+box[str(other_human)][2]/2, 115 | box[str(other_human)][1]+box[str(other_human)][3]/2] 116 | 117 | 118 | currentjoin['bbox_other'][count_other] = box[str(other_human)] 119 | 120 | currentjoin['num_keypoints_other'][count_other] = 14 121 | 122 | currentjoin['joint_others'][count_other] = np.zeros((14,3)).tolist() 123 | 124 | for part in range(14): 125 | currentjoin['joint_others'][count_other][part][0] = keypoint[str(other_human)][part*3] 126 | currentjoin['joint_others'][count_other][part][1] = keypoint[str(other_human)][part*3+1] 127 | 128 | if(keypoint[str(other_human)][part*3+2] == 2): 129 | currentjoin['joint_others'][count_other][part][2] = 1 130 | elif(keypoint[str(other_human)][part*3+2] == 1): 131 | currentjoin['joint_others'][count_other][part][2] = 0 132 | else: 133 | currentjoin['joint_others'][count_other][part][2] = 2 134 | 135 | 136 | 137 | currentjoin['numOtherPeople'] = len(currentjoin['joint_others']) 138 | 139 | count_other = count_other + 1 140 | 141 | 142 | prev_center.append([person_center[0], person_center[1], 143 | max(box[str(human)][2], box[str(human)][3])]) 144 | 145 | 146 | joint_all[i] = currentjoin 147 | 148 | count = count + 1 149 | 150 | i += 1 151 | 152 | 153 | print len(joint_all) 154 | 155 | 156 | with open('AI_data_val.json', 'w') as f1: 157 | json.dump(joint_all, f1) 158 | 159 | 160 | print('-----finish------') 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | -------------------------------------------------------------------------------- /sample_image/multiperson.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PoseAIChallenger/mxnet_pose_for_AI_challenger/d81c7a2aa83768134e53101e451c25e122ed799f/sample_image/multiperson.jpg -------------------------------------------------------------------------------- /sample_image/ski.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PoseAIChallenger/mxnet_pose_for_AI_challenger/d81c7a2aa83768134e53101e451c25e122ed799f/sample_image/ski.jpg --------------------------------------------------------------------------------