- 使用pip install mmcls安装mmcls之后可以通过pretrained='mmcls://shufflenet_v2’直接加载预训练模型
- 报错:
urllib.error.URLError: <urlopen error TopDown: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certi…
解决方案:
在train.py文件中添加:
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
Downloading: “https://download.openmmlab.com/mmclassification/v0/shufflenet_v2/shufflenet_v2_batch1024_imagenet_20200812-5bf4721e.pth” to /home/liuman/.cache/torch/hub/checkpoints/shufflenet_v2_batch1024_imagenet_20200812-5bf4721e.pth
继续报错:
_pickle.UnpicklingError: invalid load key, ‘<’.
解决方案:
把checkpoint中下载的损坏文件删除重新下载即可
去mmpretrain里面把shufflenetv2的权重文件下载到本地
https://mmpretrain.readthedocs.io/en/latest/papers/shufflenet_v2.html
- 使用的mmcv版本为1.4.0
conda create -n mmpose27 python=3.7
pip install torch1.8.1+cu111 torchvision0.9.1+cu111 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html
pip3 install openmim
mim install mmcv==1.4.0
cd poseval
pip install -e .
cd mmpose0.27.0
pip install -r requirements.txt
pip install -e .
pip install dsntnn
pip install thop
模型训练流程:
- 初始化配置文件中的参数,得到cfg
- build_posenet 构建姿态估计模型
2.1 构建backbone
2.2 构建neck
2.3 构建head
2.4 构建loss
2.5 初始化backbone、neck以及head的权重 - build_dataset构建数据集
3.1 读取标注文件
3.2 生成target(top_down_transform.py) - 开始训练:
tools/train.py:
train_model()
apis/train.py:
def train_model():
runner.run(data_loaders, cfg.workflow, cfg.total_epochs)
mmpose/datasets/datasets/base/kpt_2d_sview_rgb_img_top_down_dataset.py:
def __len__(self):
"""Get the size of the dataset."""
return len(self.db)
mmpose/models/backbones/shufflenet_v2.py:
def train():
super().train(mode)
self._freeze_stages()
if mode and self.norm_eval: # 运行这一行之后又跳转到 return len(self.db)
for m in self.modules():
if isinstance(m, nn.BatchNorm2d):
m.eval()
mmpose/datasets/datasets/base/kpt_2d_sview_rgb_img_top_down_dataset.py:
def __len__(self):
"""Get the size of the dataset."""
return len(self.db) (num_workers=4 会return 4次)
mmpose/models/detectors/base.py:
def train_step(self, data_batch, ...):
losses = self.forward(**data_batch) #执行前向传播计算损失
mmpose/models/detectors/top_down.py:
class TopDown(BasePose):
def forward():
if return_loss:
return self.forward_train(img, target, target_weight, img_metas,**kwargs)
def forward_train(self, img, target, target_weight, img_metas, **kwargs):
"""Defines the computation performed at every call when training."""
output = self.backbone(img) # torch.Size([64, 1024, 8, 8])
if self.with_neck:
output = self.neck(output) # torch.Size([64, 1024])
if self.with_keypoint:
if self.with_simcc:
output,heatmap,pred_x,pred_y = self.keypoint_head(output)
target_x = kwargs['target_x']
target_y = kwargs['target_y']
elif self.is_dsnt:
output,heatmap = self.keypoint_head(output) # 关键点和热图,(64,16,2),(64,16,8,8)
else:
output = self.keypoint_head(output) # torch.Size([64, 16, 2])
# 计算损失
# if return loss
losses = dict()
if self.with_keypoint:
if self.with_simcc:
keypoint_losses = self.keypoint_head.get_loss(
output, target, heatmap,target_weight,pred_x,pred_y,target_x,target_y)
losses.update(keypoint_losses)
keypoint_accuracy = self.keypoint_head.get_accuracy(
output, target, target_weight)
losses.update(keypoint_accuracy)
elif self.is_dsnt:
keypoint_losses = self.keypoint_head.get_loss(
output, target,heatmap,target_weight) # {'reg_loss': tensor(1.2045, device='cuda:0', grad_fn=<DivBackward0>)}
losses.update(keypoint_losses)
keypoint_accuracy = self.keypoint_head.get_accuracy(
output, target, target_weight)
losses.update(keypoint_accuracy)
else:
keypoint_losses = self.keypoint_head.get_loss(
output, target, target_weight)
losses.update(keypoint_losses) # 计算的损失应该是64张图片的所有关键点的损失的平均值
keypoint_accuracy = self.keypoint_head.get_accuracy(
output, target, target_weight) # {'acc_pose': 0.0}
losses.update(keypoint_accuracy)
return losses
mmpose/models/heads/deeppose_regression_head.py:
def get_loss(self, output, target, target_weight):
"""Calculate top-down keypoint loss.
Note:
- batch_size: N
- num_keypoints: K
Args:
output (torch.Tensor[N, K, 2 or 4]): Output keypoints.
target (torch.Tensor[N, K, 2]): Target keypoints.
target_weight (torch.Tensor[N, K, 2]):
Weights across different joint types.
"""
losses = dict()
assert not isinstance(self.loss, nn.Sequential)
assert target.dim() == 3 and target_weight.dim() == 3
losses['reg_loss'] = self.loss(output, target, target_weight)
self.iter_num += 1
print(self.iter_num)
return losses
迭代一次之后重新回到base.py:train_step()方法再次迭代。
RLELoss计算流程:
1. mmpose/models/heads/deeppose_regression_head.py:
def get_loss():
losses['reg_loss'] = self.loss(output, target, target_weight)
2. mmpose/models/losses/regression_loss.py:
Class RLELoss():
def forward(self, output, target, target_weight=None):