open-mmlab / mmpose

OpenMMLab Pose Estimation Toolbox and Benchmark.
https://mmpose.readthedocs.io/en/latest/
Apache License 2.0
5.68k stars 1.23k forks source link

Could you find the problem in my custom train process? #1888

Closed shinysoo closed 1 year ago

shinysoo commented 1 year ago

Hello, I'm trying to train model with my custom datasets. so I prepared train images and annotation json files as coco style.

After that, I changed cofig file(td-hm_hrnet-w32_8xb64-210e_coco-256x192_custom.py) is as follows:

data_root = 'data_custom/coco/' 

# pipelines
train_pipeline = [
    dict(type='LoadImage', file_client_args={{_base_.file_client_args}}),
    dict(type='GetBBoxCenterScale'),
    dict(type='RandomFlip', direction='horizontal'),
    dict(type='RandomHalfBody'),
    dict(type='RandomBBoxTransform'),
    dict(type='TopdownAffine', input_size=codec['input_size']),
    dict(type='GenerateTarget', encoder=codec),
    dict(type='PackPoseInputs')
]
val_pipeline = [
    dict(type='LoadImage', file_client_args={{_base_.file_client_args}}),
    dict(type='GetBBoxCenterScale'),
    dict(type='TopdownAffine', input_size=codec['input_size']),
    dict(type='PackPoseInputs')
]

# data loaders
train_dataloader = dict(
    batch_size=64,
    num_workers=2,
    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/person_keypoints_train_custom.json', 
        data_prefix=dict(img='train_custom/'),  
        pipeline=train_pipeline,
    ))
val_dataloader = dict(
    batch_size=32,
    num_workers=2,
    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/person_keypoints_val_custom.json', 
        bbox_file='data/coco/person_detection_results/' 
        'COCO_val2017_detections_AP_H_56_person.json',  
        data_prefix=dict(img='val_custom/'),
        test_mode=True,
        pipeline=val_pipeline,
    ))
test_dataloader = val_dataloader

# evaluators
val_evaluator = dict(
    type='CocoMetric',
    ann_file=data_root + 'annotations/person_keypoints_val_custom.json')
test_evaluator = val_evaluator

And the demo script is as follows:

!python demo/topdown_demo_with_mmdet.py \
    demo/mmdetection_cfg/faster_rcnn_r50_fpn_coco.py \
    https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth \
    configs/body_2d_keypoint/topdown_heatmap/coco/td-hm_hrnet-w32_8xb64-210e_coco-256x192_custom.py \
    https://download.openmmlab.com/mmpose/top_down/hrnet/hrnet_w32_coco_256x192-c78dce93_20200708.pth \
    --input /pose_estimation/_sample_data/20210719_160714_color.mp4 \
    --output-root=output/base \
    --device=cuda:0 \
    --show --save-predictions 

Right, I've only modified the paths of annotations and images in the config file. And I compared "origin predictions result" and "custom-train predictions result".

As expected, there was no difference between "origin predictions result" and "custom-train predictions result". I think the custom config didn't work and the origin config file worked twice.

What's the problem in the process? Should I change this code?:

bbox_file='data/coco/person_detection_results/' 
    'COCO_val2017_detections_AP_H_56_person.json'

or should I modify the {MMDET_CONFIG_FILE} and {~_CHECKPOINT_FILE} in the demo script?

Please find the poblems in my code. Thank you.

Tau-J commented 1 year ago

Thanks for using MMPose 1.0. For custom dataset, you should not use COCO_val2017_detections_AP_H_56_person.json, which contains bboxes detected by a detector on COCO dataset.

shinysoo commented 1 year ago

Thanks for using MMPose 1.0. For custom dataset, you should not use COCO_val2017_detections_AP_H_56_person.json, which contains bboxes detected by a detector on COCO dataset.

Can you share the format of COCO_val2017_detections_AP_H_56_person.json? and how to modify the json? Do you have any examples?

shinysoo commented 1 year ago

@Tau-J I found the json from this Google Drive. Thanks! But I have one question. How to calculate the score? I have only bbox, image_id, category_id without score.

[+] I tried with my custom COCO_val2017_detections_AP_H_56_person.json but noting changed. (I set all the scores to 0.9.)

I think I have to change somthing more. Could you give me some advice about it? this is my config code:

_base_ = ['../../../_base_/default_runtime.py']

# runtime
train_cfg = dict(max_epochs=210, val_interval=10)

# optimizer
optim_wrapper = dict(optimizer=dict(
    type='Adam',
    lr=5e-4,
))

# learning policy
param_scheduler = [
    dict(
        type='LinearLR', begin=0, end=500, start_factor=0.001,
        by_epoch=False),  # warm-up
    dict(
        type='MultiStepLR',
        begin=0,
        end=210,
        milestones=[170, 200],
        gamma=0.1,
        by_epoch=True)
]

# automatically scaling LR based on the actual training batch size
auto_scale_lr = dict(base_batch_size=512)

# hooks
default_hooks = dict(checkpoint=dict(save_best='coco/AP', rule='greater'))

# codec settings
codec = dict(
    type='MSRAHeatmap', input_size=(192, 256), heatmap_size=(48, 64), sigma=2)

# 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(
        type='HRNet',
        in_channels=3,
        extra=dict(
            stage1=dict(
                num_modules=1,
                num_branches=1,
                block='BOTTLENECK',
                num_blocks=(4, ),
                num_channels=(64, )),
            stage2=dict(
                num_modules=1,
                num_branches=2,
                block='BASIC',
                num_blocks=(4, 4),
                num_channels=(32, 64)),
            stage3=dict(
                num_modules=4,
                num_branches=3,
                block='BASIC',
                num_blocks=(4, 4, 4),
                num_channels=(32, 64, 128)),
            stage4=dict(
                num_modules=3,
                num_branches=4,
                block='BASIC',
                num_blocks=(4, 4, 4, 4),
                num_channels=(32, 64, 128, 256))),
        init_cfg=dict(
            type='Pretrained',
            checkpoint='https://download.openmmlab.com/mmpose/'
            'pretrain_models/hrnet_w32-36af842e.pth'),
    ),
    head=dict(
        type='HeatmapHead',
        in_channels=32,
        out_channels=17,
        deconv_out_channels=None,
        loss=dict(type='KeypointMSELoss', use_target_weight=True),
        decoder=codec),
    test_cfg=dict(
        flip_test=True,
        flip_mode='heatmap',
        shift_heatmap=True,
    ))

# base dataset settings
dataset_type = 'CocoDataset'
data_mode = 'topdown'
data_root = 'data_custom/coco/' ## I modified ##

# pipelines
train_pipeline = [
    dict(type='LoadImage', file_client_args={{_base_.file_client_args}}),
    dict(type='GetBBoxCenterScale'),
    dict(type='RandomFlip', direction='horizontal'),
    dict(type='RandomHalfBody'),
    dict(type='RandomBBoxTransform'),
    dict(type='TopdownAffine', input_size=codec['input_size']),
    dict(type='GenerateTarget', encoder=codec),
    dict(type='PackPoseInputs')
]
val_pipeline = [
    dict(type='LoadImage', file_client_args={{_base_.file_client_args}}),
    dict(type='GetBBoxCenterScale'),
    dict(type='TopdownAffine', input_size=codec['input_size']),
    dict(type='PackPoseInputs')
]

# data loaders
train_dataloader = dict(
    batch_size=64,
    num_workers=2,
    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/person_keypoints_train_custom.json', ## I modified ##
        data_prefix=dict(img='train_custom/'), ## I modified ##
        pipeline=train_pipeline,
    ))
val_dataloader = dict(
    batch_size=32,
    num_workers=2,
    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/person_keypoints_val_custom.json',
        bbox_file='data_custom/coco/person_detection_results/'
        'COCO_val_detections_custom.json',    ## I modified ##
        data_prefix=dict(img='val_custom/'),  ## I modified ##
        test_mode=True,
        pipeline=val_pipeline,
    ))
test_dataloader = val_dataloader

# evaluators
val_evaluator = dict(
    type='CocoMetric',
    ann_file=data_root + 'annotations/person_keypoints_val_custom.json') ## I modified ##
test_evaluator = val_evaluator

I didn't modified dataset_type = 'CocoDataset' because I've modified my annotation format to COCO format.

Tau-J commented 1 year ago

bbox_file is not necessary for custom dataset, you can just comment it out. Then the evaluation result will be based on your GT.

jin-s13 commented 1 year ago

It seems the problem is solved. I will close this issue for now. Feel free to reopen it if have further questions.