这是根据CVPR2017的论文代码改写的Pytorch版,也可以看做是OpenPose的代码。
今天对这个repo里的一个demo代码进行了阅读,把其中比较难懂的部分做了注释。
后续可能会根据论文做一个代码对比流程。
- Source Code:
https://github.com/tensorboy/pytorch_Realtime_Multi-Person_Pose_Estimation/blob/master/picture_demo.py - Require:
- Pytorch 0.3.1
- Python
参考文章:
https://www.cnblogs.com/demian/p/8988396.html
Code:
import os
import re
import sys
import cv2
import math
import time
import scipy
import argparse
import matplotlib
from torch import np # Pytorch里可以直接加载numpy,但是官网文档里查不到
import pylab as plt
from joblib import Parallel, delayed
import util
import torch
import torch as T
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from collections import OrderedDict
from config_reader import config_reader
from scipy.ndimage.filters import gaussian_filter
#parser = argparse.ArgumentParser()
#parser.add_argument('--t7_file', required=True)
#parser.add_argument('--pth_file', required=True)
#args = parser.parse_args()
torch.set_num_threads(torch.get_num_threads())
weight_name = './model/pose_model.pth'
blocks = {
}
'''
18个关节,pt19为背景
part_str = [nose, neck, Rsho, Relb, Rwri, Lsho, Lelb, Lwri, Rhip, Rkne, Rank, Lhip, Lkne, Lank, Leye, Reye, Lear, Rear, pt19]
'''
# find connection in the specified sequence, center 29 is in the position 15
# 18个关节对应19个关节链接(也就是肢体)
limbSeq = [[2,3], [2,6], [3,4], [4,5], [6,7], [7,8], [2,9], [9,10], \
[10,11], [2,12], [12,13], [13,14], [2,1], [1,15], [15,17], \
[1,16], [16,18], [3,17], [6,18]]
# the middle joints heatmap correpondence
# 肢体对应的PAF特征图(19-56共38张,每两张表示一个二维方向向量)
mapIdx = [[31,32], [39,40], [33,34], [35,36], [41,42], [43,44], [19,20], [21,22], \
[23,24], [25,26], [27,28], [29,30], [47,48], [49,50], [53,54], [51,52], \
[55,56], [37,38], [45,46]]
# visualize
colors = [[255, 0, 0], [255, 85, 0], [255, 170, 0], [255, 255, 0], [170, 255, 0], [85, 255, 0], [0, 255, 0], \
[0, 255, 85], [0, 255, 170], [0, 255, 255], [0, 170, 255], [0, 85, 255], [0, 0, 255], [85, 0, 255], \
[170, 0, 255], [255, 0, 255], [255, 0, 170], [255, 0, 85]]
# 以下为构造网络部分,很好理解
block0 = [{
'conv1_1':[3,64,3,1,1]},{
'conv1_2':[64,64,3,1,1]},{
'pool1_stage1':[2,2,0]},{
'conv2_1':[64,128,3,1,1]},{
'conv2_2':[128,128,3,1,1]},{
'pool2_stage1':[2,2,0]},{
'conv3_1':[128,256,3,1,1]},{
'conv3_2':[256,256,3,1,1]},{
'conv3_3':[256,256,3,1,1]},{
'conv3_4':[256,256,3,1,1]},{
'pool3_stage1':[2,2,0]},{
'conv4_1':[256,512,3,1,1]},{
'conv4_2':[512,512,3,1,1]},{
'conv4_3_CPM':[512,256,3,1,1]},{
'conv4_4_CPM':[256,128,3,1,1]}]
blocks['block1_1'] = [{
'conv5_1_CPM_L1':[128,128,3,1,1]},{
'conv5_2_CPM_L1'