open-mmlab / mmpose

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

KeyError: 'inputs' How to solve the problem? #2546

Closed seon-creator closed 1 year ago

seon-creator commented 1 year ago

Prerequisite

Environment

mmcv 2.0.1 mmdet 3.1.0 mmengine 0.8.1 mmpose 1.1.0

Reproduces the problem - code sample

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

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

# optimizer
optim_wrapper = dict(optimizer=dict(
    type='Adam',
    lr=1.5e-3,
))

# 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=300,
        milestones=[200, 260],
        gamma=0.1,
        by_epoch=True)
]

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

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

# codec settings
codec = dict(
    type='AssociativeEmbedding',
    input_size=(512, 512),
    heatmap_size=(128, 128),
    sigma=2,
    decode_keypoint_order=[ 
        0, 1, 2, 3, 4, 5 # edit
    ],
    decode_max_instances=30)

# model settings
model = dict(
    type='BottomupPoseEstimator',
    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='AssociativeEmbeddingHead',
        in_channels=32,
        num_keypoints=5, # edit
        tag_dim=1,
        tag_per_keypoint=True,
        deconv_out_channels=None,
        keypoint_loss=dict(type='KeypointMSELoss', use_target_weight=True),
        tag_loss=dict(type='AssociativeEmbeddingLoss', loss_weight=0.001),
        # The heatmap will be resized to the input size before decoding
        # if ``restore_heatmap_size==True``
        decoder=dict(codec, heatmap_size=codec['input_size'])),
    test_cfg=dict(
        multiscale_test=False,
        flip_test=True,
        shift_heatmap=True,
        restore_heatmap_size=True,
        align_corners=False))

# base dataset settings
dataset_type = 'CocoArm'
data_mode = 'bottomup'
data_root = 'data/coco/'

# pipelines
train_pipeline = []
val_pipeline = [
    dict(type='LoadImage'),
    dict(
        type='BottomupResize',
        input_size=codec['input_size'],
        size_factor=32,
        resize_mode='expand'),
    dict(type='PackPoseInputs')
]

# data loaders
train_dataloader = dict(
    batch_size=24,
    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='/data/home/seondeok/mmpose/datasets/DS_11_89/DS_arm_1_80.json',    # edit
        data_prefix=dict(img='/data/home/seondeok/mmpose/datasets/DS_11_89/'),     # edit
        pipeline=train_pipeline,
    ))
val_dataloader = dict(
    batch_size=1,
    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='/data/home/seondeok/mmpose/datasets/DS_1_10_90_100/DS_arm_81_100.json',     # edit
        data_prefix=dict(img='/data/home/seondeok/mmpose/datasets/DS_1_10_90_100/'),      # edit
        test_mode=True,
        pipeline=val_pipeline,
    ))
test_dataloader = val_dataloader

# evaluators
val_evaluator = dict(
    type='CocoMetric',
    ann_file='/data/home/seondeok/mmpose/datasets/DS_1_10_90_100/DS_arm_81_100.json',   # edit
    nms_mode='none',
    score_mode='keypoint',
)
test_evaluator = val_evaluator

Reproduces the problem - command or script

python tools/train.py configs/body_2d_keypoint/associative_embedding/custom_model/ae_hrnet-w32_8xb24-300e_coco-512x512.py

Same file root configs/body_2d_keypoint/associative_embedding/coco/ae_hrnet-w32_8xb24-300e_coco-512x512.py

Reproduces the problem - error message

Traceback (most recent call last):
  File "/data/home/seondeok/mmpose/tools/train.py", line 161, in <module>
    main()
  File "/data/home/seondeok/mmpose/tools/train.py", line 157, in main
    runner.train()
  File "/data/home/seondeok/.conda/envs/mmp/lib/python3.9/site-packages/mmengine/runner/runner.py", line 1735, in train
    model = self.train_loop.run()  # type: ignore
  File "/data/home/seondeok/.conda/envs/mmp/lib/python3.9/site-packages/mmengine/runner/loops.py", line 96, in run
    self.run_epoch()
  File "/data/home/seondeok/.conda/envs/mmp/lib/python3.9/site-packages/mmengine/runner/loops.py", line 112, in run_epoch
    self.run_iter(idx, data_batch)
  File "/data/home/seondeok/.conda/envs/mmp/lib/python3.9/site-packages/mmengine/runner/loops.py", line 128, in run_iter
    outputs = self.runner.model.train_step(
  File "/data/home/seondeok/.conda/envs/mmp/lib/python3.9/site-packages/mmengine/model/base_model/base_model.py", line 113, in train_step
    data = self.data_preprocessor(data, True)
  File "/data/home/seondeok/.conda/envs/mmp/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "/data/home/seondeok/.conda/envs/mmp/lib/python3.9/site-packages/mmengine/model/base_model/data_preprocessor.py", line 247, in forward
    _batch_inputs = data['inputs']
KeyError: 'inputs'

Additional information

When I train topdown method models, it works well. I also try to train bottom up method model in the same environment. But error occur. How to solve the problem?

I also add code of topdown heatmap model This model train works well.

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

# runtime
train_cfg = dict(max_epochs=5000, val_interval=10)   # edit

# 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)

# hooksCocoDataset
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))),
        # if don't want to pretrain, just remove
        # 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=5, # edit : counts of keypoints are five
        deconv_out_channels=None,
        loss=dict(type='KeypointMSELoss', use_target_weight=True),
        decoder=codec),
    test_cfg=dict(
        flip_test=False,    # edit
        flip_mode='heatmap',
        shift_heatmap=True,
    ))

# base dataset settings
dataset_type = 'CocoArm'
data_mode = 'topdown'
data_root = 'datasets/'     # edit

# pipelines
train_pipeline = [
    dict(type='LoadImage'),
    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'),
    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='/data/home/seondeok/mmpose/datasets/arm/json_file/arm_Train.json',   # edit
        # data_prefix : image folder
        data_prefix=dict(img='/data/home/seondeok/mmpose/datasets/arm/Train/'),    # edit
        # metainfo=dict(from_file='configs/_base_/datasets/coco_armpoint.py'), # if module register do not work correctly, add your custom keypoint information
        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='/data/home/seondeok/mmpose/datasets/arm/json_file/arm_Test.json', # edit 
        # bbox_file='/data/home/seondeok/mmpose/datasets/DS_1_10_90_100/DS_arm_81_100.json', # edit
        data_prefix=dict(img='/data/home/seondeok/mmpose/datasets/arm/Test/'), # edit
        # metainfo=dict(from_file='configs/_base_/datasets/coco_armpoint.py'), # if module register do not work correctly
        test_mode=True,
        pipeline=val_pipeline,
    ))
test_dataloader = val_dataloader

# evaluators
val_evaluator = dict(
    type='CocoMetric',
    ann_file='/data/home/seondeok/mmpose/datasets/arm/json_file/arm_Test.json') # edit 
test_evaluator = val_evaluator
Tau-J commented 1 year ago

Thanks for using MMPose. Associative Embedding is still under migration, please refer to here to see the progress.