From 5de7764e6811113bfb6294867c97d592d27682ad Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 17 Mar 2025 12:57:19 +0100 Subject: [PATCH 1/2] Initial steps. --- extras/mmpose/README.md | 25 +++++++++++++++++++++++++ extras/mmpose/run_container.sh | 11 +++++++++++ 2 files changed, 36 insertions(+) create mode 100644 extras/mmpose/README.md create mode 100755 extras/mmpose/run_container.sh diff --git a/extras/mmpose/README.md b/extras/mmpose/README.md new file mode 100644 index 0000000..13b2a49 --- /dev/null +++ b/extras/mmpose/README.md @@ -0,0 +1,25 @@ +# Finetuning MMPose models + +See: + +
+ +```bash +docker build --progress=plain -f extras/mmdeploy/dockerfile -t rpt_mmdeploy . + +./extras/mmpose/run_container.sh +``` + +```bash +cd /mmpose/ + +pip3 install "albumentations<1.4" + +python3 ./tools/train.py \ + /mmpose/projects/rtmpose/rtmpose/body_2d_keypoint/rtmpose-m_8xb256-420e_coco-384x288.py \ + --amp \ + --cfg-options \ + load_from=https://download.openmmlab.com/mmpose/v1/projects/rtmposev1/rtmpose-m_simcc-body7_pt-body7_420e-384x288-65e718c4_20230504.pth \ + train_batch_size=16 val_batch_size=16 + +``` diff --git a/extras/mmpose/run_container.sh b/extras/mmpose/run_container.sh new file mode 100755 index 0000000..2b138c8 --- /dev/null +++ b/extras/mmpose/run_container.sh @@ -0,0 +1,11 @@ +#! /bin/bash + +xhost + +docker run --privileged --rm --network host -it \ + --gpus all --shm-size=16g --ulimit memlock=-1 --ulimit stack=67108864 \ + --volume "$(pwd)"/:/RapidPoseTriangulation/ \ + --volume "$(pwd)"/../datasets/coco2017/annotations/:/mmpose/data/coco/annotations/ \ + --volume "$(pwd)"/../datasets/coco2017/images/:/mmpose/data/coco/ \ + --volume /tmp/.X11-unix:/tmp/.X11-unix \ + --env DISPLAY --env QT_X11_NO_MITSHM=1 \ + rpt_mmdeploy From dff0f7130975a18252edb801b8668d5ff696ce7a Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 17 Mar 2025 16:05:26 +0100 Subject: [PATCH 2/2] Create custom dockerfile as well. --- extras/mmpose/README.md | 12 +- ...ose-l_8xb32-270e_coco-wholebody-384x288.py | 235 ++++++++++++++++++ extras/mmpose/dockerfile | 9 + extras/mmpose/run_container.sh | 2 +- 4 files changed, 250 insertions(+), 8 deletions(-) create mode 100644 extras/mmpose/configs/rtmpose-l_8xb32-270e_coco-wholebody-384x288.py create mode 100644 extras/mmpose/dockerfile diff --git a/extras/mmpose/README.md b/extras/mmpose/README.md index 13b2a49..c731136 100644 --- a/extras/mmpose/README.md +++ b/extras/mmpose/README.md @@ -5,21 +5,19 @@ See:
```bash -docker build --progress=plain -f extras/mmdeploy/dockerfile -t rpt_mmdeploy . +docker build --progress=plain -f extras/mmpose/dockerfile -t rpt_mmpose . ./extras/mmpose/run_container.sh ``` ```bash cd /mmpose/ - -pip3 install "albumentations<1.4" +export CUDA_VISIBLE_DEVICES=0 python3 ./tools/train.py \ - /mmpose/projects/rtmpose/rtmpose/body_2d_keypoint/rtmpose-m_8xb256-420e_coco-384x288.py \ + /RapidPoseTriangulation/extras/mmpose/configs/rtmpose-l_8xb32-270e_coco-wholebody-384x288.py \ --amp \ --cfg-options \ - load_from=https://download.openmmlab.com/mmpose/v1/projects/rtmposev1/rtmpose-m_simcc-body7_pt-body7_420e-384x288-65e718c4_20230504.pth \ - train_batch_size=16 val_batch_size=16 - + load_from=https://download.openmmlab.com/mmpose/v1/projects/rtmposev1/rtmpose-l_simcc-coco-wholebody_pt-aic-coco_270e-384x288-eaeb96c8_20230125.pth \ + base_lr=0.00004 ``` diff --git a/extras/mmpose/configs/rtmpose-l_8xb32-270e_coco-wholebody-384x288.py b/extras/mmpose/configs/rtmpose-l_8xb32-270e_coco-wholebody-384x288.py new file mode 100644 index 0000000..9af4ae2 --- /dev/null +++ b/extras/mmpose/configs/rtmpose-l_8xb32-270e_coco-wholebody-384x288.py @@ -0,0 +1,235 @@ +_base_ = ['mmpose::_base_/default_runtime.py'] + +val_interval=1 +max_epochs = 3 + +# common setting +num_keypoints = 133 +input_size = (288, 384) + +# runtime +stage2_num_epochs = 30 +base_lr = 4e-3 +train_batch_size = 32 +val_batch_size = 32 + +train_cfg = dict(max_epochs=max_epochs, val_interval=val_interval) +randomness = dict(seed=21) + +# optimizer +optim_wrapper = dict( + type='OptimWrapper', + optimizer=dict(type='AdamW', lr=base_lr, weight_decay=0.05), + clip_grad=dict(max_norm=35, norm_type=2), + paramwise_cfg=dict( + norm_decay_mult=0, bias_decay_mult=0, bypass_duplicate=True)) + +# learning rate +param_scheduler = [ + dict( + type='LinearLR', + start_factor=1.0e-5, + by_epoch=False, + begin=0, + end=1000), + dict( + type='CosineAnnealingLR', + eta_min=base_lr * 0.05, + begin=max_epochs // 2, + end=max_epochs, + T_max=max_epochs // 2, + by_epoch=True, + convert_to_iter_based=True), +] + +# automatically scaling LR based on the actual training batch size +auto_scale_lr = dict(base_batch_size=512) + +# codec settings +codec = dict( + type='SimCCLabel', + input_size=input_size, + sigma=(6., 6.93), + simcc_split_ratio=2.0, + normalize=False, + use_dark=False) + +# model settings +model = dict( + type='TopdownPoseEstimator', + data_preprocessor=dict( + type='PoseDataPreprocessor', + mean=[123.675, 116.28, 103.53], + std=[58.395, 57.12, 57.375], + bgr_to_rgb=True), + backbone=dict( + _scope_='mmdet', + type='CSPNeXt', + arch='P5', + expand_ratio=0.5, + deepen_factor=1., + widen_factor=1., + out_indices=(4, ), + channel_attention=True, + norm_cfg=dict(type='SyncBN'), + act_cfg=dict(type='SiLU'), + init_cfg=dict( + type='Pretrained', + prefix='backbone.', + checkpoint='https://download.openmmlab.com/mmpose/v1/projects/' + 'rtmposev1/cspnext-l_udp-aic-coco_210e-256x192-273b7631_20230130.pth' # noqa + )), + head=dict( + type='RTMCCHead', + in_channels=1024, + out_channels=num_keypoints, + input_size=codec['input_size'], + in_featuremap_size=tuple([s // 32 for s in codec['input_size']]), + simcc_split_ratio=codec['simcc_split_ratio'], + final_layer_kernel_size=7, + gau_cfg=dict( + hidden_dims=256, + s=128, + expansion_factor=2, + dropout_rate=0., + drop_path=0., + act_fn='SiLU', + use_rel_bias=False, + pos_enc=False), + loss=dict( + type='KLDiscretLoss', + use_target_weight=True, + beta=10., + label_softmax=True), + decoder=codec), + test_cfg=dict(flip_test=True, )) + +# base dataset settings +dataset_type = 'CocoWholeBodyDataset' +data_mode = 'topdown' +data_root = 'data/coco/' + +backend_args = dict(backend='local') + +# pipelines +train_pipeline = [ + dict(type='LoadImage', backend_args=backend_args), + dict(type='GetBBoxCenterScale'), + dict(type='RandomFlip', direction='horizontal'), + dict(type='RandomHalfBody'), + dict( + type='RandomBBoxTransform', scale_factor=[0.6, 1.4], rotate_factor=80), + dict(type='TopdownAffine', input_size=codec['input_size']), + dict(type='mmdet.YOLOXHSVRandomAug'), + dict( + type='Albumentation', + transforms=[ + dict(type='Blur', p=0.1), + dict(type='MedianBlur', p=0.1), + dict( + type='CoarseDropout', + max_holes=1, + max_height=0.4, + max_width=0.4, + min_holes=1, + min_height=0.2, + min_width=0.2, + p=1.0), + ]), + dict(type='GenerateTarget', encoder=codec), + dict(type='PackPoseInputs') +] +val_pipeline = [ + dict(type='LoadImage', backend_args=backend_args), + dict(type='GetBBoxCenterScale'), + dict(type='TopdownAffine', input_size=codec['input_size']), + dict(type='PackPoseInputs') +] + +train_pipeline_stage2 = [ + dict(type='LoadImage', backend_args=backend_args), + dict(type='GetBBoxCenterScale'), + dict(type='RandomFlip', direction='horizontal'), + dict(type='RandomHalfBody'), + dict( + type='RandomBBoxTransform', + shift_factor=0., + scale_factor=[0.75, 1.25], + rotate_factor=60), + dict(type='TopdownAffine', input_size=codec['input_size']), + dict(type='mmdet.YOLOXHSVRandomAug'), + dict( + type='Albumentation', + transforms=[ + dict(type='Blur', p=0.1), + dict(type='MedianBlur', p=0.1), + dict( + type='CoarseDropout', + max_holes=1, + max_height=0.4, + max_width=0.4, + min_holes=1, + min_height=0.2, + min_width=0.2, + p=0.5), + ]), + dict(type='GenerateTarget', encoder=codec), + dict(type='PackPoseInputs') +] + +# data loaders +train_dataloader = dict( + batch_size=train_batch_size, + num_workers=10, + persistent_workers=True, + sampler=dict(type='DefaultSampler', shuffle=True), + dataset=dict( + type=dataset_type, + data_root=data_root, + data_mode=data_mode, + ann_file='annotations/coco_wholebody_train_v1.0.json', + data_prefix=dict(img='train2017/'), + pipeline=train_pipeline, + )) +val_dataloader = dict( + batch_size=val_batch_size, + num_workers=10, + persistent_workers=True, + drop_last=False, + sampler=dict(type='DefaultSampler', shuffle=False, round_up=False), + dataset=dict( + type=dataset_type, + data_root=data_root, + data_mode=data_mode, + ann_file='annotations/coco_wholebody_val_v1.0.json', + data_prefix=dict(img='val2017/'), + test_mode=True, + # bbox_file='data/coco/person_detection_results/' + # 'COCO_val2017_detections_AP_H_56_person.json', + pipeline=val_pipeline, + )) +test_dataloader = val_dataloader + +# hooks +default_hooks = dict( + checkpoint=dict( + save_best='coco-wholebody/AP', rule='greater', max_keep_ckpts=1)) + +custom_hooks = [ + dict( + type='EMAHook', + ema_type='ExpMomentumEMA', + momentum=0.0002, + update_buffers=True, + priority=49), + dict( + type='mmdet.PipelineSwitchHook', + switch_epoch=max_epochs - stage2_num_epochs, + switch_pipeline=train_pipeline_stage2) +] + +# evaluators +val_evaluator = dict( + type='CocoWholeBodyMetric', + ann_file=data_root + 'annotations/coco_wholebody_val_v1.0.json') +test_evaluator = val_evaluator diff --git a/extras/mmpose/dockerfile b/extras/mmpose/dockerfile new file mode 100644 index 0000000..455ba87 --- /dev/null +++ b/extras/mmpose/dockerfile @@ -0,0 +1,9 @@ +FROM rpt_mmdeploy + +RUN apt-get update && apt-get install -y --no-install-recommends nano +RUN pip3 install --upgrade --no-cache-dir "albumentations<1.4" + +RUN sed -i '94i\ self.runner.val_loop.run()' /usr/local/lib/python3.8/dist-packages/mmengine/runner/loops.py + +WORKDIR /mmpose/ +CMD ["/bin/bash"] diff --git a/extras/mmpose/run_container.sh b/extras/mmpose/run_container.sh index 2b138c8..2b0ed92 100755 --- a/extras/mmpose/run_container.sh +++ b/extras/mmpose/run_container.sh @@ -8,4 +8,4 @@ docker run --privileged --rm --network host -it \ --volume "$(pwd)"/../datasets/coco2017/images/:/mmpose/data/coco/ \ --volume /tmp/.X11-unix:/tmp/.X11-unix \ --env DISPLAY --env QT_X11_NO_MITSHM=1 \ - rpt_mmdeploy + rpt_mmpose