HRNet 训练自定义数据集

在这里插入图片描述

基于 HRNet 训练人脸特征点数据集.


INSTALL

conda create -n openmmlab python=3.8
# torch gpu```
pip install torch==1.12.1+cu102 torchvision==0.13.1+cu102 torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/cu102
# 
pip install -U openmim
mim install mmcv-full

# install MMPose.
git clone https://github.com/open-mmlab/mmpose.git
cd mmpose
pip install -r requirements.txt
pip install -v -e .

Train custom dataset

  1. 添加 configs/__base__/dataset/custom.py , 配置特征点信息 (我的数据是 37个人脸特征点)
    dataset_info = dict(
        dataset_name='landmark',
        paper_info=dict(
            author='Wu, Wayne and Qian, Chen and Yang, Shuo and Wang, '
                   'Quan and Cai, Yici and Zhou, Qiang',
            title='Look at boundary: A boundary-aware face alignment algorithm',
            container='Proceedings of the IEEE conference on computer '
                      'vision and pattern recognition',
            year='2018',
            homepage='https://wywu.github.io/projects/LAB/WFLW.html',
        ),
        keypoint_info={
          
          
            0:
                dict(
                    name='left_eye_left_corner', id=0, color=[255, 255, 255], type='', swap='right_eye_right_corner'),
            1:
                dict(
                    name='left_eye_top_1', id=1, color=[255, 255, 255], type='', swap='right_eye_top_4'),
            2:
                dict(
                    name='left_eye_top_2', id=2, color=[255, 255, 255], type='', swap='right_eye_top_3'),
            3:
                dict(
                    name='left_eye_top_3', id=3, color=[255, 255, 255], type='', swap='right_eye_top_2'),
            4:
                dict(
                    name='left_eye_top_4', id=4, color=[255, 255, 255], type='', swap='right_eye_top_1'),
            5:
                dict(
                    name='left_eye_right_corner', id=5, color=[255, 255, 255], type='', swap='right_eye_left_corner'),
            6:
                dict(
                    name='left_eye_bottom_1', id=6, color=[255, 255, 255], type='', swap='right_eye_bottom_4'),
            7:
                dict(
                    name='left_eye_bottom_2', id=7, color=[255, 255, 255], type='', swap='right_eye_bottom_3'),
            8:
                dict(
                    name='left_eye_bottom_3', id=8, color=[255, 255, 255], type='', swap='right_eye_bottom_2'),
            9:
                dict(
                    name='left_eye_bottom_4', id=9, color=[255, 255, 255], type='', swap='right_eye_bottom_1'),
            10:
                dict(
                    name='right_eye_left_corner',
                    id=10,
                    color=[255, 255, 255],
                    type='',
                    swap='left_eye_right_corner'),
            11:
                dict(
                    name='right_eye_top_1',
                    id=11,
                    color=[255, 255, 255],
                    type='',
                    swap='left_eye_top_4'),
            12:
                dict(
                    name='right_eye_top_2',
                    id=12,
                    color=[255, 255, 255],
                    type='',
                    swap='left_eye_top_3'),
            13:
                dict(
                    name='right_eye_top_3',
                    id=13,
                    color=[255, 255, 255],
                    type='',
                    swap='left_eye_top_2'),
            14:
                dict(
                    name='right_eye_top_4',
                    id=14,
                    color=[255, 255, 255],
                    type='',
                    swap='left_eye_top_1'),
            15:
                dict(
                    name='right_eye_right_corner',
                    id=15,
                    color=[255, 255, 255],
                    type='',
                    swap='left_eye_left_corner'),
            16:
                dict(name='right_eye_bottom_1', id=16, color=[255, 255, 255], type='', swap='left_eye_bottom_4'),
            17:
                dict(
                    name='right_eye_bottom_2',
                    id=17,
                    color=[255, 255, 255],
                    type='',
                    swap='left_eye_bottom_3'),
            18:
                dict(
                    name='right_eye_bottom_3',
                    id=18,
                    color=[255, 255, 255],
                    type='',
                    swap='left_eye_bottom_2'),
            19:
                dict(
                    name='right_eye_bottom_4',
                    id=19,
                    color=[255, 255, 255],
                    type='',
                    swap='left_eye_bottom_1'),
            20:
                dict(
                    name='left_eyebrow_left_corner',
                    id=20,
                    color=[255, 255, 255],
                    type='',
                    swap='right_eyebrow_right_corner'),
            21:
                dict(
                    name='left_eyebrow_right_corner',
                    id=21,
                    color=[255, 255, 255],
                    type='',
                    swap='right_eyebrow_left_corner'),
            22:
                dict(
                    name='right_eyebrow_left_corner',
                    id=22,
                    color=[255, 255, 255],
                    type='',
                    swap='left_eyebrow_right_corner'),
            23:
                dict(
                    name='right_eyebrow_right_corner',
                    id=23,
                    color=[255, 255, 255],
                    type='',
                    swap='left_eyebrow_left_corner'),
            24:
                dict(
                    name='nose_left_corner',
                    id=24,
                    color=[255, 255, 255],
                    type='',
                    swap='nose_right_corner'),
            25:
                dict(
                    name='nose_right_corner',
                    id=25,
                    color=[255, 255, 255],
                    type='',
                    swap='nose_left_corner'),
            26:
                dict(
                    name='mouth_left_corner',
                    id=26,
                    color=[255, 255, 255],
                    type='',
                    swap='mouth_right_corner'),
            27:
                dict(
                    name='mouth_top_1', id=27, color=[255, 255, 255], type='',
                    swap='mouth_top_3'),
            28:
                dict(
                    name='mouth_top_2', id=28, color=[255, 255, 255], type='',
                    swap='mouth_top_2'),
            29:
                dict(
                    name='mouth_top_3', id=29, color=[255, 255, 255], type='',
                    swap='mouth_top_1'),
            30:
                dict(
                    name='mouth_right_corner', id=30, color=[255, 255, 255], type='',
                    swap='mouth_left_corner'),
            31:
                dict(
                    name='mouth_bottom_1', id=31, color=[255, 255, 255], type='',
                    swap='mouth_bottom_3'),
            32:
                dict(
                    name='mouth_bottom_2', id=32, color=[255, 255, 255], type='',
                    swap='mouth_bottom_2'),
            33:
                dict(
                    name='mouth_bottom_3', id=33, color=[255, 255, 255], type='',
                    swap='mouth_bottom_1'),
            34:
                dict(
                    name='chin_left', id=34, color=[255, 255, 255], type='',
                    swap='chin_right'),
            35:
                dict(
                    name='chin_mid', id=35, color=[255, 255, 255], type='',
                    swap='chin_mid'),
            36:
                dict(
                    name='chin_right', id=36, color=[255, 255, 255], type='',
                    swap='chin_left')
        },
        skeleton_info={
          
          },
        joint_weights=[1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5,
                       1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
        sigmas=[])
    
  2. 创建 mmpose/datasets/datasets/face/face_custom_dataset.py, 这里起名叫 FaceCustomDataset ,参考其他数据集,然后根据自己数据集修改就行.
  3. init.py 增加 FaceCustomDataset
    from .face_custom_dataset import FaceCustomDataset  
    __all__ = [  
        'Face300WDataset', 'FaceAFLWDataset', 'FaceWFLWDataset', 'FaceCOFWDataset',  
        'FaceCocoWholeBodyDataset', 'FaceCustomDataset'  
    ]
    
  4. 创建 configs/custom/litehrnet_w18_face_256x192.py
    # 修改 _base_ 改成 custom.py
    _base_ = [  
        '../_base_/default_runtime.py',  
        '../_base_/datasets/custom.py'  
    ]
    	...
    	# 数据集配置
    	data = dict(  
    	    samples_per_gpu=64,  
    	    workers_per_gpu=4,  
    	    val_dataloader=dict(samples_per_gpu=32),  
    	    test_dataloader=dict(samples_per_gpu=32),  
    	    train=dict(  
    	       # 设置我们自定义数据集
    	        type='FaceCustomDataset',  
    	        ann_file=f'{
            
            data_root}/annotations/face_landmark_train.json',  
    	        img_prefix=f'{
            
            data_root}/images/',  
    	        data_cfg=data_cfg,  
    	        pipeline=train_pipeline,  
    	        dataset_info={
          
          {
          
          _base_.dataset_info}}),  
    	    val=dict(  
    	        type='FaceCustomDataset',  
    	        ann_file=f'{
            
            data_root}/annotations/face_landmark_val.json',  
    	        img_prefix=f'{
            
            data_root}/images/',  
    	        data_cfg=data_cfg,  
    	        pipeline=val_pipeline,  
    	        dataset_info={
          
          {
          
          _base_.dataset_info}}),  
    	    test=dict(  
    	        type='FaceCustomDataset',  
    	        ann_file=f'{
            
            data_root}/annotations/face_landmarks_wflw_test.json',  
    	        img_prefix=f'{
            
            data_root}/images/',  
    	        data_cfg=data_cfg,  
    	        pipeline=test_pipeline,  
    	        dataset_info={
          
          {
          
          _base_.dataset_info}}),  
    	)
    	```
    
  5. Train
    # signle gpu
    python .tools/train.py "./configs/custom/litehrnet_w18_face_256x192.py" -work-dir "./tools/runs/models"
    # mul gpu
    ./tools/dist_train.sh "./configs/custom/litehrnet_w18_face_256x192.py" 4 --work-dir "./tools/runs/ld_37"
    

在这里插入图片描述
6. 验证
python demo/face_img_demo.py ./configs/custom/litehrnet_w18_face_256x192.py tools/runs/ld_37/best_NME_epoch_1660.pth --img-root tests/data/ld_37/ --img 1.jpg --out-img-root vis_results
在这里插入图片描述


参考

  1. https://github.com/open-mmlab/mmpose
  2. https://github.com/HRNet/Lite-HRNet

猜你喜欢

转载自blog.csdn.net/haiyangyunbao813/article/details/129919257