提示:该文章仅仅记录自己的学习过程,如有问题,请大家指教。
项目下载地址:GitHub - tensorboy/pytorch_Realtime_Multi-Person_Pose_Estimation
一、环境配置requirements.txt ——该文件提供了需要的库
1.torch——进入官网 :Start Locally | PyTorchAn open source machine learning framework that accelerates the path from research prototyping to production deployment.https://pytorch.org/get-started/locally/根据自己电脑配置下载符合自己的pytorch
conda install pytorch==1.8.0 torchvision==0.9.0 torchaudio==0.8.0 cudatoolkit=11.1 -c pytorch -c conda-forge
会遇到下载不了的情况——————换源
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- defaults
show_channel_urls: true
2.pycocotools的安装(仅适用于windows)
通过在清华镜像源中找到适合自己的版本下载下来
镜像地址:links for pycocotools-windows
在虚拟环境下安装:pip install 文件名
二、demo文件夹picture_demo.py 输入图片关键点就会标注出来 web_demo.py 目前未学习三、train文件夹 1._init_paths.py 用于实现路径的添加 2.train_Vgg19.py的学习(训练VGG19模型)
2.1从lib包中引用相关的模块
from lib.network.rtpose_vgg import get_model, use_vgg from lib.datasets import coco, transforms, datasets from lib.config import update_config
2.2Python中的argparse用法总结
Python中的argparse用法总结_Code小学僧的课后笔记-CSDN博客
def cli():
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
train_cli(parser)
parser.add_argument('-o', '--output', default=None,
help='output file')
parser.add_argument('--stride-apply', default=1, type=int,
help='apply and reset gradients every n batches')
parser.add_argument('--epochs', default=75, type=int,
help='number of epochs to train')
parser.add_argument('--freeze-base', default=0, type=int,
help='number of epochs to train with frozen base')
parser.add_argument('--pre-lr', type=float, default=1e-4,
help='pre learning rate')
parser.add_argument('--update-batchnorm-runningstatistics',
default=False, action='store_true',
help='update batch norm running statistics')
parser.add_argument('--square-edge', default=368, type=int,
help='square edge of input images')
parser.add_argument('--ema', default=1e-3, type=float,
help='ema decay constant')
parser.add_argument('--debug-without-plots', default=False, action='store_true',
help='enable debug but dont plot')
parser.add_argument('--disable-cuda', action='store_true',
help='disable CUDA')
parser.add_argument('--model_path', default='./network/weight/', type=str, metavar='DIR',help='path to where the model saved')
args = parser.parse_args()
# add args.device
args.device = torch.device('cpu')
args.pin_memory = False
if not args.disable_cuda and torch.cuda.is_available():
args.device = torch.device('cuda')
args.pin_memory = True
return args
获取模型VGG19
model = get_model('vgg19')
def get_model(trunk='vgg19'):
"""Creates the whole CPM model
Args:
trunk: string, 'vgg19' or 'mobilenet'
Returns: Module, the defined model
"""
blocks = {}
# block0 is the preprocessing stage
if trunk == 'vgg19':
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]}]
elif trunk == 'mobilenet':
block0 = [{'conv_bn': [3, 32, 2]}, # out: 3, 32, 184, 184
{'conv_dw1': [32, 64, 1]}, # out: 32, 64, 184, 184
{'conv_dw2': [64, 128, 2]}, # out: 64, 128, 92, 92
{'conv_dw3': [128, 128, 1]}, # out: 128, 256, 92, 92
{'conv_dw4': [128, 256, 2]}, # out: 256, 256, 46, 46
{'conv4_3_CPM': [256, 256, 1, 3, 1]},
{'conv4_4_CPM': [256, 128, 1, 3, 1]}]
# Stage 1
blocks['block1_1'] = [{'conv5_1_CPM_L1': [128, 128, 3, 1, 1]},
{'conv5_2_CPM_L1': [128, 128, 3, 1, 1]},
{'conv5_3_CPM_L1': [128, 128, 3, 1, 1]},
{'conv5_4_CPM_L1': [128, 512, 1, 1, 0]},
{'conv5_5_CPM_L1': [512, 38, 1, 1, 0]}]
blocks['block1_2'] = [{'conv5_1_CPM_L2': [128, 128, 3, 1, 1]},
{'conv5_2_CPM_L2': [128, 128, 3, 1, 1]},
{'conv5_3_CPM_L2': [128, 128, 3, 1, 1]},
{'conv5_4_CPM_L2': [128, 512, 1, 1, 0]},
{'conv5_5_CPM_L2': [512, 19, 1, 1, 0]}]
# Stages 2 - 6
for i in range(2, 7):
blocks['block%d_1' % i] = [
{'Mconv1_stage%d_L1' % i: [185, 128, 7, 1, 3]},
{'Mconv2_stage%d_L1' % i: [128, 128, 7, 1, 3]},
{'Mconv3_stage%d_L1' % i: [128, 128, 7, 1, 3]},
{'Mconv4_stage%d_L1' % i: [128, 128, 7, 1, 3]},
{'Mconv5_stage%d_L1' % i: [128, 128, 7, 1, 3]},
{'Mconv6_stage%d_L1' % i: [128, 128, 1, 1, 0]},
{'Mconv7_stage%d_L1' % i: [128, 38, 1, 1, 0]}
]
blocks['block%d_2' % i] = [
{'Mconv1_stage%d_L2' % i: [185, 128, 7, 1, 3]},
{'Mconv2_stage%d_L2' % i: [128, 128, 7, 1, 3]},
{'Mconv3_stage%d_L2' % i: [128, 128, 7, 1, 3]},
{'Mconv4_stage%d_L2' % i: [128, 128, 7, 1, 3]},
{'Mconv5_stage%d_L2' % i: [128, 128, 7, 1, 3]},
{'Mconv6_stage%d_L2' % i: [128, 128, 1, 1, 0]},
{'Mconv7_stage%d_L2' % i: [128, 19, 1, 1, 0]}
]
models = {}
if trunk == 'vgg19':
print("Bulding VGG19")
models['block0'] = make_vgg19_block(block0)
for k, v in blocks.items():
models[k] = make_stages(list(v))
class rtpose_model(nn.Module):
def __init__(self, model_dict):
super(rtpose_model, self).__init__()
self.model0 = model_dict['block0']
self.model1_1 = model_dict['block1_1']
self.model2_1 = model_dict['block2_1']
self.model3_1 = model_dict['block3_1']
self.model4_1 = model_dict['block4_1']
self.model5_1 = model_dict['block5_1']
self.model6_1 = model_dict['block6_1']
self.model1_2 = model_dict['block1_2']
self.model2_2 = model_dict['block2_2']
self.model3_2 = model_dict['block3_2']
self.model4_2 = model_dict['block4_2']
self.model5_2 = model_dict['block5_2']
self.model6_2 = model_dict['block6_2']
self._initialize_weights_norm()
def forward(self, x):
saved_for_loss = []
out1 = self.model0(x)
out1_1 = self.model1_1(out1)
out1_2 = self.model1_2(out1)
out2 = torch.cat([out1_1, out1_2, out1], 1)
saved_for_loss.append(out1_1)
saved_for_loss.append(out1_2)
out2_1 = self.model2_1(out2)
out2_2 = self.model2_2(out2)
out3 = torch.cat([out2_1, out2_2, out1], 1)
saved_for_loss.append(out2_1)
saved_for_loss.append(out2_2)
out3_1 = self.model3_1(out3)
out3_2 = self.model3_2(out3)
out4 = torch.cat([out3_1, out3_2, out1], 1)
saved_for_loss.append(out3_1)
saved_for_loss.append(out3_2)
out4_1 = self.model4_1(out4)
out4_2 = self.model4_2(out4)
out5 = torch.cat([out4_1, out4_2, out1], 1)
saved_for_loss.append(out4_1)
saved_for_loss.append(out4_2)
out5_1 = self.model5_1(out5)
out5_2 = self.model5_2(out5)
out6 = torch.cat([out5_1, out5_2, out1], 1)
saved_for_loss.append(out5_1)
saved_for_loss.append(out5_2)
out6_1 = self.model6_1(out6)
out6_2 = self.model6_2(out6)
saved_for_loss.append(out6_1)
saved_for_loss.append(out6_2)
return (out6_1, out6_2), saved_for_loss
def _initialize_weights_norm(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
init.normal_(m.weight, std=0.01)
if m.bias is not None: # mobilenet conv2d doesn't add bias
init.constant_(m.bias, 0.0)
# last layer of these block don't have Relu
init.normal_(self.model1_1[8].weight, std=0.01)
init.normal_(self.model1_2[8].weight, std=0.01)
init.normal_(self.model2_1[12].weight, std=0.01)
init.normal_(self.model3_1[12].weight, std=0.01)
init.normal_(self.model4_1[12].weight, std=0.01)
init.normal_(self.model5_1[12].weight, std=0.01)
init.normal_(self.model6_1[12].weight, std=0.01)
init.normal_(self.model2_2[12].weight, std=0.01)
init.normal_(self.model3_2[12].weight, std=0.01)
init.normal_(self.model4_2[12].weight, std=0.01)
init.normal_(self.model5_2[12].weight, std=0.01)
init.normal_(self.model6_2[12].weight, std=0.01)
model = rtpose_model(models)
return model



