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
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用HRNet训练自己的数据集,您可以按照以下步骤进行操作: 1. 准备数据集:收集和标注适用于您的任务的图像数据集。确保每个图像都具有正确的标注,以便能够训练HRNet模型。 2. 安装依赖项:确保您的开发环境中安装了必要的依赖项,例如Python、PyTorch和其他可能需要的库。 3. 下载HRNet代码:从HRNet官方仓库(https://github.com/HRNet/HRNet-Image-Classification)下载代码。 4. 配置训练参数:根据您的数据集训练需求,修改配置文件(如`experiments/cls_hrnet_w18_sgd_lr5e-2_wd1e-4_bs32_x100.yaml`)以设置正确的参数,包括数据路径、批量大小、学习率等。 5. 准备数据加载器:根据您的数据集格式,编写数据加载器代码,将图像和标注加载到模型中。这通常涉及编写一个数据集类来处理数据集的加载和预处理。 6. 开始训练:运行训练脚本,例如`tools/train.py`,并指定配置文件作为参数。这将启动训练过程,并在指定的迭代次数内逐步优化模型。 7. 评估和调整:在训练完成后,使用验证集或测试集评估模型的性能。根据评估结果,您可以进行超参数调整、数据增强等操作来进一步改进模型。 请注意,以上步骤只是一个大致的指导,具体的实施细节可能因您的数据集和任务而异。您可能还需要参考HRNet官方文档或搜索相关的参考资料来获取更详细和具体的指导。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值