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

can't train mmpose with error 'dict' object has no attribute 'max_epochs' #2496

Closed SunWeimiao closed 1 year ago

SunWeimiao commented 1 year ago

Prerequisite

Environment

mmaction 0.5.0 mmcv 2.0.0 mmdet 3.0.0 mmengine 0.7.3 mmpose 1.0.0

Reproduces the problem - code sample

I'm the beginner of mmpose. I follow the tutorial at https://github.com/open-mmlab/mmpose/blob/main/demo/MMPose_Tutorial.ipynb However, I encountered an error.I wonder where i made the mistake and how to solve it.

My steps are as follows:

1. create the dataset class like follow:

import json import os.path as osp from typing import Callable, List, Optional, Sequence, Union import numpy as np from mmengine.utils import check_file_exist from mmpose.registry import DATASETS from mmpose.datasets.datasets.base import BaseCocoStyleDataset

@DATASETS.register_module() class TinyCocoDataset(BaseCocoStyleDataset): METAINFO: dict = dict(from_file='configs/base/datasets/coco.py')

def _load_annotations(self) -> List[dict]:
    check_file_exist(self.ann_file)
    with open(self.ann_file) as anno_file:
        anns = json.load(anno_file)

    data_list = []
    ann_id = 0

    for idx, ann in enumerate(anns):

        img_h, img_w = ann['image_size']
        x, y, w, h = ann['bbox']

        x1 = np.clip(x, 0, img_w - 1)
        y1 = np.clip(y, 0, img_h - 1)
        x2 = np.clip(x + w, 0, img_w - 1)
        y2 = np.clip(y + h, 0, img_h - 1)

        bbox = np.array([x1, y1, x2, y2], dtype=np.float32).reshape(1, 4)

        joints_3d = np.array(ann['keypoints']).reshape(1, -1, 3)

        num_joints = joints_3d.shape[1]

        keypoints = np.zeros((1, num_joints, 2), dtype=np.float32)
        keypoints[:, :, :2] = joints_3d[:, :, :2]

        # keypoints_visible中所有标注了的点均为1,未标注的点为0
        keypoints_visible = np.minimum(1, joints_3d[:, :, 2:3])
        keypoints_visible = keypoints_visible.reshape(1, -1)

        data_info = {
            'id': ann_id,
            'img_id': int(ann['image_file'].split('.')[0]),
            'img_path': osp.join(self.data_prefix['img'], ann['image_file']),
            'bbox': bbox,
            'bbox_score': np.ones(1, dtype=np.float32),
            'keypoints': keypoints,
            'keypoints_visible': keypoints_visible,
        }

        data_list.append(data_info)
        ann_id = ann_id + 1

    return data_list, None

2.register this dataset class

from .aic_dataset import AicDataset from .coco_dataset import CocoDataset from .crowdpose_dataset import CrowdPoseDataset from .jhmdb_dataset import JhmdbDataset from .mhp_dataset import MhpDataset from .mpii_dataset import MpiiDataset from .mpii_trb_dataset import MpiiTrbDataset from .ochuman_dataset import OCHumanDataset from .posetrack18_dataset import PoseTrack18Dataset from .posetrack18_video_dataset import PoseTrack18VideoDataset from .tinycocodataset import TinyCocoDataset

all = [ 'CocoDataset', 'MpiiDataset', 'MpiiTrbDataset', 'AicDataset', 'CrowdPoseDataset', 'OCHumanDataset', 'MhpDataset', 'PoseTrack18Dataset', 'JhmdbDataset', 'PoseTrack18VideoDataset', 'TinyCocoDataset' ]

3.create config file(hrnet_w32_coco_tiny_256x192.py) like follow

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

runtime 

train_cfg = dict(max_epochs=40, val_interval=1)

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=train_cfg.max_epochs, milestones=[17, 35], gamma=0.1, by_epoch=True) ]

automatically scaling LR based on the actual training batch size

auto_scale_lr = dict(base_batch_size=512)

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='configs/body_2d_keypoint/topdown_heatmap/coco/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 = 'TinyCocoDataset' data_mode = 'topdown' data_root = 'data/coco_tiny' work_dir = 'work_dirs/hrnet_w32_tiny_coco_256x192' randomness = dict(seed=0)

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', target_type='heatmap', encoder=codec), dict(type='PackPoseInputs') ] test_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=16, 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='train.json', data_prefix=dict(img='images/'), pipeline=train_pipeline, )) val_dataloader = dict( batch_size=16, 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='val.json', data_prefix=dict(img='images/'), test_mode=True, pipeline=test_pipeline, )) test_dataloader = val_dataloader

evaluators

val_evaluator = dict( type='PCKAccuracy') test_evaluator = val_evaluator

hooks

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

Reproduces the problem - command or script

python tools/train.py configs/body_2d_keypoint/topdown_heatmap/coco/hrnet_w32_coco_tiny_256x192.py

Reproduces the problem - error message

Traceback (most recent call last): File "tools/train.py", line 160, in main() File "tools/train.py", line 142, in main cfg = Config.fromfile(args.config) File "D:\Anaconda3\envs\openmmlab\lib\site-packages\mmengine\config\config.py", line 178, in fromfile cfg_dict, cfg_text, env_variables = Config._file2dict( File "D:\Anaconda3\envs\openmmlab\lib\site-packages\mmengine\config\config.py", line 522, in _file2dict eval(codeobj, global_locals_var, global_locals_var) File "", line 20, in AttributeError: 'dict' object has no attribute 'max_epochs'

Additional information

No response

Tau-J commented 1 year ago

@SunWeimiao Could you provide the link of this config you use? train_cfg.max_epochs seems to be wrong, it should be train_cfg["max_epochs"]

SunWeimiao commented 1 year ago

@SunWeimiao Could you provide the link of this config you use? train_cfg.max_epochs seems to be wrong, it should be train_cfg["max_epochs"]

thanks for your help, the link is https://github.com/open-mmlab/mmpose/blob/main/demo/MMPose_Tutorial.ipynb

the train_cfg.max_epochs is in the section Create a config file 1

and i encountered another error, what's the correct key: Traceback (most recent call last): File "D:\Anaconda3\envs\openmmlab\lib\site-packages\mmengine\runner\runner.py", line 1783, in call_hook getattr(hook, fn_name)(self, **kwargs) File "D:\Anaconda3\envs\openmmlab\lib\site-packages\mmengine\hooks\checkpoint_hook.py", line 329, in after_val_epoch self._save_best_checkpoint(runner, metrics) File "D:\Anaconda3\envs\openmmlab\lib\site-packages\mmengine\hooks\checkpoint_hook.py", line 458, in _save_best_checkpoint key_score = metrics[key_indicator] KeyError: 'coco/AP'

SunWeimiao commented 1 year ago

@SunWeimiao Could you provide the link of this config you use? train_cfg.max_epochs seems to be wrong, it should be train_cfg["max_epochs"]

thanks for your help, the link is https://github.com/open-mmlab/mmpose/blob/main/demo/MMPose_Tutorial.ipynb

the train_cfg.max_epochs is in the section Create a config file 1

and i encountered another error, what's the correct key: Traceback (most recent call last): File "D:\Anaconda3\envs\openmmlab\lib\site-packages\mmengine\runner\runner.py", line 1783, in call_hook getattr(hook, fn_name)(self, **kwargs) File "D:\Anaconda3\envs\openmmlab\lib\site-packages\mmengine\hooks\checkpoint_hook.py", line 329, in after_val_epoch self._save_best_checkpoint(runner, metrics) File "D:\Anaconda3\envs\openmmlab\lib\site-packages\mmengine\hooks\checkpoint_hook.py", line 458, in _save_best_checkpoint key_score = metrics[key_indicator] KeyError: 'coco/AP'

After modifying coco/AP to PCK, I re executed the training command and the command was successfully executed. @Tau-J In the same section Create a config file, there is another error: coco/AP should be changed to PCK coco-ap