open-mmlab / mmpose

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

When evaluating with CocoMetric, AP is all 0 #2248

Open hsp2454 opened 1 year ago

hsp2454 commented 1 year ago

When I train 'configs\animal_2d_keypoint\topdown_heatmap\animalpose\td-hm_hrnet-w48_8xb64-210e_animalpose-256x256.py' with my own data, when I use CocoMetric as evaluation, my AP is all 0, and when I use PCK, it is a value. What is the situation? Below is my config file:

Config:
default_scope = 'mmpose'
default_hooks = dict(
    timer=dict(type='IterTimerHook'),
    logger=dict(type='LoggerHook', interval=10),
    param_scheduler=dict(type='ParamSchedulerHook'),
    checkpoint=dict(
        type='CheckpointHook',
        interval=10,
        save_best='coco/AP',
        rule='greater'),
    sampler_seed=dict(type='DistSamplerSeedHook'),
    visualization=dict(type='PoseVisualizationHook', enable=False))
custom_hooks = [dict(type='SyncBuffersHook')]
env_cfg = dict(
    cudnn_benchmark=False,
    mp_cfg=dict(mp_start_method='fork', opencv_num_threads=0),
    dist_cfg=dict(backend='nccl'))
vis_backends = [
    dict(type='LocalVisBackend'),
    dict(type='TensorboardVisBackend')
]
visualizer = dict(
    type='PoseLocalVisualizer',
    vis_backends=[
        dict(type='LocalVisBackend'),
        dict(type='TensorboardVisBackend')
    ],
    name='visualizer')
log_processor = dict(
    type='LogProcessor', window_size=50, by_epoch=True, num_digits=6)
log_level = 'INFO'
load_from = None
resume = False
backend_args = dict(backend='local')
train_cfg = dict(by_epoch=True, max_epochs=210, val_interval=10)
val_cfg = dict()
test_cfg = dict()
optim_wrapper = dict(optimizer=dict(type='Adam', lr=0.0005))
param_scheduler = [
    dict(
        type='LinearLR', begin=0, end=210, start_factor=0.001, by_epoch=False),
    dict(
        type='MultiStepLR',
        begin=0,
        end=210,
        milestones=[170, 200],
        gamma=0.1,
        by_epoch=True)
]
auto_scale_lr = dict(base_batch_size=512)
codec = dict(
    type='MSRAHeatmap', input_size=(256, 256), heatmap_size=(64, 64), sigma=2)
channel_cfg = dict(
    num_output_channels=21,
    dataset_joints=21,
    dataset_channel=[[
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
        20
    ]],
    inference_channel=[
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
        20
    ])
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=(48, 96)),
            stage3=dict(
                num_modules=4,
                num_branches=3,
                block='BASIC',
                num_blocks=(4, 4, 4),
                num_channels=(48, 96, 192)),
            stage4=dict(
                num_modules=3,
                num_branches=4,
                block='BASIC',
                num_blocks=(4, 4, 4, 4),
                num_channels=(48, 96, 192, 384))),
        init_cfg=dict(
            type='Pretrained',
            checkpoint=
            'https://download.openmmlab.com/mmpose/pretrain_models/hrnet_w48-8ef0771d.pth'
        )),
    head=dict(
        type='HeatmapHead',
        in_channels=48,
        out_channels=21,
        deconv_out_channels=None,
        loss=dict(type='KeypointMSELoss', use_target_weight=True),
        decoder=dict(
            type='MSRAHeatmap',
            input_size=(256, 256),
            heatmap_size=(64, 64),
            sigma=2)),
    test_cfg=dict(flip_test=True, flip_mode='heatmap', shift_heatmap=True))
dataset_type = 'PigPoseDataset'
data_mode = 'topdown'
data_root = 'D:\\Code\\mmpose-1.x\\data\\coco/'
train_pipeline = [
    dict(type='LoadImage'),
    dict(type='GetBBoxCenterScale'),
    dict(type='RandomFlip', direction='horizontal'),
    dict(type='RandomHalfBody'),
    dict(type='RandomBBoxTransform'),
    dict(type='TopdownAffine', input_size=(256, 256)),
    dict(
        type='GenerateTarget',
        encoder=dict(
            type='MSRAHeatmap',
            input_size=(256, 256),
            heatmap_size=(64, 64),
            sigma=2)),
    dict(type='PackPoseInputs')
]
val_pipeline = [
    dict(type='LoadImage'),
    dict(type='GetBBoxCenterScale'),
    dict(type='TopdownAffine', input_size=(256, 256)),
    dict(type='PackPoseInputs')
]
train_dataloader = dict(
    batch_size=32,
    num_workers=2,
    persistent_workers=True,
    sampler=dict(type='DefaultSampler', shuffle=True),
    dataset=dict(
        type='PigPoseDataset',
        data_root='D:\\Code\\mmpose-1.x\\data\\coco/',
        data_mode='topdown',
        ann_file='annotations/keypoints_train.json',
        data_prefix=dict(img='D:\\Code\\mmpose-1.x\\data\\coco\\train'),
        metainfo=dict(
            from_file='D:\\Code\\mmpose-1.x\\configs\\_base_\\datasets\\pig.py'
        ),
        pipeline=[
            dict(type='LoadImage'),
            dict(type='GetBBoxCenterScale'),
            dict(type='RandomFlip', direction='horizontal'),
            dict(type='RandomHalfBody'),
            dict(type='RandomBBoxTransform'),
            dict(type='TopdownAffine', input_size=(256, 256)),
            dict(
                type='GenerateTarget',
                encoder=dict(
                    type='MSRAHeatmap',
                    input_size=(256, 256),
                    heatmap_size=(64, 64),
                    sigma=2)),
            dict(type='PackPoseInputs')
        ]))
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='PigPoseDataset',
        data_root='D:\\Code\\mmpose-1.x\\data\\coco/',
        data_mode='topdown',
        ann_file='annotations/keypoints_val.json',
        data_prefix=dict(img='D:\\Code\\mmpose-1.x\\data\\coco\\val'),
        metainfo=dict(
            from_file='D:\\Code\\mmpose-1.x\\configs\\_base_\\datasets\\pig.py'
        ),
        test_mode=True,
        pipeline=[
            dict(type='LoadImage'),
            dict(type='GetBBoxCenterScale'),
            dict(type='TopdownAffine', input_size=(256, 256)),
            dict(type='PackPoseInputs')
        ]))
test_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='PigPoseDataset',
        data_root='D:\\Code\\mmpose-1.x\\data\\coco/',
        data_mode='topdown',
        ann_file='annotations/keypoints_val.json',
        data_prefix=dict(img='D:\\Code\\mmpose-1.x\\data\\coco\\val'),
        metainfo=dict(
            from_file='D:\\Code\\mmpose-1.x\\configs\\_base_\\datasets\\pig.py'
        ),
        test_mode=True,
        pipeline=[
            dict(type='LoadImage'),
            dict(type='GetBBoxCenterScale'),
            dict(type='TopdownAffine', input_size=(256, 256)),
            dict(type='PackPoseInputs')
        ]))
val_evaluator = [
    dict(type='PCKAccuracy', thr=0.2),
    dict(type='AUC'),
    dict(type='EPE')
]
test_evaluator = [
    dict(type='PCKAccuracy', thr=0.2),
    dict(type='AUC'),
    dict(type='EPE')
]

Here is the result of my evaluation with CocoMetric: image

Below is my evaluation result using PCK: image

LareinaM commented 1 year ago

Hi, the COCO AP equals zero means that your prediction result is very inaccurate, but does not indicate an error in evaluation.

hsp2454 commented 1 year ago

Hello, what is the general reason for this situation? When I was training in mmpose0.x before, when I tested demo.py, I could completely identify the key points.

Tau-J commented 1 year ago

Could you try setting the batch_size to 2 in test_dataloader and test again?

hsp2454 commented 1 year ago

Thank you for your answer. When I changed the batch_size in val_dataloader from 32 to 2 and ran test.py, it reported the following error. image

But when I evaluate it with PCK, it works image

hsp2454 commented 1 year ago

Also when I run demo\image_demo.py, it only detects a few points. I think my data is fine, I have trained HRNet in mmpose0.x, it can identify all the points

Tau-J commented 1 year ago

Could you print the config of CocoMetric you use to us ?

Ben-Louis commented 1 year ago

Additionally, it would be helpful if you could share the contents of the dataset meta-information file located at "D:\Code\mmpose-1.x\configs\base\datasets\pig.py"

hsp2454 commented 1 year ago

This is the information in my D:\Code\mmpose-1.x\configs\ base \datasets\pig.py,And no changes have been made to the information in my mmpose\evaluation\metrics\coco_metric.py file

dataset_info = dict(
    dataset_name='PigCocoDataset',
    paper_info=dict(
        author='Lin, Tsung-Yi and Maire, Michael and '
               'Belongie, Serge and Hays, James and '
               'Perona, Pietro and Ramanan, Deva and '
               r'Doll{\'a}r, Piotr and Zitnick, C Lawrence',
        title='Microsoft coco: Common objects in context',
        container='European conference on computer vision',
        year='2014',
        homepage='http://cocodataset.org/',
    ),
    keypoint_info={
        0:
        dict(name='nose', id=0, color=[51, 153, 255], type='upper', swap=''),
        1:
        dict(
            name='righteye',
            id=1,
            color=[51, 153, 255],
            type='upper',
            swap='lefteye'),
        2:
        dict(
            name='rightear',
            id=2,
            color=[51, 153, 255],
            type='upper',
            swap='leftear'),
        3:
        dict(
            name='lefteye',
            id=3,
            color=[51, 153, 255],
            type='upper',
            swap='righteye'),
        4:
        dict(
            name='leftear',
            id=4,
            color=[51, 153, 255],
            type='upper',
            swap='rightear'),
        5:
        dict(
            name='rightshoulder',
            id=5,
            color=[0, 255, 0],
            type='upper',
            swap='leftshoulder'),
        6:
        dict(
            name='rightfrontknee',
            id=6,
            color=[255, 128, 0],
            type='upper',
            swap='leftfrontknee'),
        7:
        dict(
            name='rightfronthoof',
            id=7,
            color=[0, 255, 0],
            type='upper',
            swap='leftfronthoof'),
        8:
        dict(
            name='righthip',
            id=8,
            color=[255, 128, 0],
            type='upper',
            swap='lefthip'),
        9:
        dict(
            name='rightbackknee',
            id=9,
            color=[0, 255, 0],
            type='upper',
            swap='leftbackknee'),
        10:
        dict(
            name='rightbackhoof',
            id=10,
            color=[255, 128, 0],
            type='upper',
            swap='rightbackhoof'),
        11:
        dict(
            name='leftshoulder',
            id=11,
            color=[0, 255, 0],
            type='lower',
            swap='rightshoulder'),
        12:
        dict(
            name='leftfrontknee',
            id=12,
            color=[255, 128, 0],
            type='lower',
            swap='rightfrontknee'),
        13:
        dict(
            name='leftfronthoof',
            id=13,
            color=[0, 255, 0],
            type='lower',
            swap='rightfronthoof'),
        14:
        dict(
            name='lefthip',
            id=14,
            color=[255, 128, 0],
            type='lower',
            swap='righthip'),
        15:
        dict(
            name='leftbackknee',
            id=15,
            color=[0, 255, 0],
            type='lower',
            swap='rightbackknee'),
        16:
        dict(
            name='leftbackhoof',
            id=16,
            color=[255, 128, 0],
            type='lower',
            swap='rightbackhoof'),
        17:
        dict(
            name='pigback',
            id=17,
            color=[255, 128, 40],
            type='lower',
            swap=''),
        18:
        dict(
            name='pigbelly',
            id=18,
            color=[255, 128, 100],
            type='lower',
            swap=''),
        19:
        dict(
            name='pigtail',
            id=19,
            color=[255, 128, 200],
            type='lower',
            swap='')
    },
    skeleton_info={
        0:
        dict(link=('nose', 'righteye'), id=0, color=[0, 255, 0]),
        1:
        dict(link=('nose', 'lefteye'), id=1, color=[0, 255, 0]),
        2:
        dict(link=('righteye', 'rightear'), id=2, color=[255, 128, 0]),
        3:
        dict(link=('lefteye', 'leftear'), id=3, color=[255, 128, 0]),
        4:
        dict(link=('nose', 'rightshoulder'), id=4, color=[51, 153, 255]),
        5:
        dict(link=('nose', 'leftshoulder'), id=5, color=[51, 153, 255]),
        6:
        dict(link=('rightshoulder', 'rightfrontknee'), id=6, color=[51, 153, 255]),
        7:
        dict(
            link=('rightfrontknee', 'rightfronthoof'),
            id=7,
            color=[51, 153, 255]),
        8:
        dict(link=('leftshoulder', 'leftfrontknee'), id=8, color=[0, 255, 0]),
        9:
        dict(
            link=('leftfrontknee', 'leftfronthoof'), id=9, color=[255, 128, 0]),
        10:
        dict(link=('rightshoulder', 'pigback'), id=10, color=[0, 255, 0]),
        11:
        dict(link=('leftshoulder', 'pigback'), id=11, color=[255, 128, 0]),
        12:
        dict(link=('rightshoulder', 'righthip'), id=12, color=[51, 153, 255]),
        13:
        dict(link=('leftshoulder', 'lefthip'), id=13, color=[51, 153, 0]),
        14:
        dict(link=('rightshoulder', 'pigbelly'), id=14, color=[51, 153, 40]),
        15:
        dict(link=('leftshoulder', 'pigbelly'), id=15, color=[51, 153, 80]),
        16:
        dict(link=('pigback', 'righthip'), id=16, color=[51, 153, 140]),
        17:
        dict(link=('pigback', 'lefthip'), id=17, color=[51, 153, 200]),
        18:
        dict(
            link=('pigbelly', 'righthip'), id=18, color=[51, 153, 220]),
        19:
        dict(
            link=('pigbelly', 'lefthip'), id=19, color=[51, 120, 255]),
        20:
        dict(
            link=('righthip', 'rightbackknee'), id=20, color=[51, 80, 255]),
        21:
        dict(
            link=('rightbackhoof', 'rightbackknee'), id=21, color=[51, 40, 255]),
        22:
        dict(
            link=('lefthip', 'leftbackknee'), id=22, color=[51, 0, 255]),
        23:
        dict(
            link=('leftbackhoof', 'leftbackknee'), id=23, color=[51, 0, 80]),
        24:
        dict(
            link=('righthip', 'pigtail'), id=24, color=[40, 153, 40]),
        25:
        dict(
            link=('lefthip', 'pigtail'), id=25, color=[51, 40, 40])
    },
    joint_weights=[
        1., 1., 1., 1., 1., 1., 1., 1.2, 1.2, 1.5, 1.5, 1., 1., 1.2, 1.2, 1.5,
        1.5,1.5,1.5,1.5
    ],
    sigmas=[
        0.026, 0.025, 0.025, 0.035, 0.035, 0.079, 0.079, 0.072, 0.072, 0.062,
        0.062, 0.107, 0.107, 0.087, 0.087, 0.089, 0.089,0.072, 0.072, 0.062
    ])
Ben-Louis commented 1 year ago

This is the information in my D:\Code\mmpose-1.x\configs\ base \datasets\pig.py,And no changes have been made to the information in my mmpose\evaluation\metrics\coco_metric.py file

We are referring to the configuration of CocoMetric as shown below:

val_evaluator=...

There appears to be a discrepancy between the number of keypoints specified in the meta-information (20) and the configuration (21). This inconsistency may result in a mismatch in the number of channels between the sigmas and the predicted keypoints in testing.

hsp2454 commented 1 year ago

sorry i misunderstood

val_evaluator = [ 
     dict(type='PCKAccuracy', thr=0.2), 
     dict(type='AUC'), 
     dict(type='EPE'), 
] 

Yes, my custom data set is indeed 21 points

Tau-J commented 1 year ago

sorry i misunderstood

val_evaluator = [ 
     dict(type='PCKAccuracy', thr=0.2), 
     dict(type='AUC'), 
     dict(type='EPE'), 
] 

Yes, my custom data set is indeed 21 points

Please show the configuration of CocoMetric you use.

hsp2454 commented 1 year ago

This is my dataset format, I copied other datasets directly, only changed the data path, and the detected APs are all 0

# Copyright (c) OpenMMLab. All rights reserved.
import os.path as osp
import tempfile
import warnings
from collections import OrderedDict, defaultdict

import json_tricks as json
import numpy as np
from mmcv import Config, deprecated_api_warning
from xtcocotools.cocoeval import COCOeval

from ....core.post_processing import oks_nms, soft_oks_nms
from ...builder import DATASETS
from ..base import Kpt2dSviewRgbImgTopDownDataset

@DATASETS.register_module(name='PigAnimal')
class PigAnimal(Kpt2dSviewRgbImgTopDownDataset):
    """CocoDataset dataset for top-down pose estimation.

    "Microsoft COCO: Common Objects in Context", ECCV'2014.
    More details can be found in the `paper
    <https://arxiv.org/abs/1405.0312>`__ .

    The dataset loads raw features and apply specified transforms
    to return a dict containing the image tensors and other information.

    COCO keypoint indexes::

        0: 'nose',
        1: 'left_eye',
        2: 'right_eye',
        3: 'left_ear',
        4: 'right_ear',
        5: 'left_shoulder',
        6: 'right_shoulder',
        7: 'left_elbow',
        8: 'right_elbow',
        9: 'left_wrist',
        10: 'right_wrist',
        11: 'left_hip',
        12: 'right_hip',
        13: 'left_knee',
        14: 'right_knee',
        15: 'left_ankle',
        16: 'right_ankle'

    Args:
        ann_file (str): Path to the annotation file.
        img_prefix (str): Path to a directory where images are held.
            Default: None.
        data_cfg (dict): config
        pipeline (list[dict | callable]): A sequence of data transforms.
        dataset_info (DatasetInfo): A class containing all dataset info.
        test_mode (bool): Store True when building test or
            validation dataset. Default: False.
    """

    def __init__(self,
                 ann_file,
                 img_prefix,
                 data_cfg,
                 pipeline,
                 dataset_info=None,
                 test_mode=False):

        if dataset_info is None:
            warnings.warn(
                'dataset_info is missing. '
                'Check https://github.com/open-mmlab/mmpose/pull/663 '
                'for details.', DeprecationWarning)
            cfg = Config.fromfile('D:\Code\mmpose2\configs\_base_\datasets\pig_animal.py')
            dataset_info = cfg._cfg_dict['dataset_info']

        super().__init__(
            ann_file,
            img_prefix,
            data_cfg,
            pipeline,
            dataset_info=dataset_info,
            test_mode=test_mode)

        self.use_gt_bbox = data_cfg['use_gt_bbox']
        self.bbox_file = data_cfg['bbox_file']
        self.det_bbox_thr = data_cfg.get('det_bbox_thr', 0.0)
        self.use_nms = data_cfg.get('use_nms', True)
        self.soft_nms = data_cfg['soft_nms']
        self.nms_thr = data_cfg['nms_thr']
        self.oks_thr = data_cfg['oks_thr']
        self.vis_thr = data_cfg['vis_thr']

        self.db = self._get_db()

        print(f'=> num_images: {self.num_images}')
        print(f'=> load {len(self.db)} samples')

    def _get_db(self):
        """Load dataset."""
        if (not self.test_mode) or self.use_gt_bbox:
            # use ground truth bbox
            gt_db = self._load_coco_keypoint_annotations()
        else:
            # use bbox from detection
            gt_db = self._load_coco_person_detection_results()
        return gt_db

    def _load_coco_keypoint_annotations(self):
        """Ground truth bbox and keypoints."""
        gt_db = []
        for img_id in self.img_ids:
            gt_db.extend(self._load_coco_keypoint_annotation_kernel(img_id))
        return gt_db

    def _load_coco_keypoint_annotation_kernel(self, img_id):
        """load annotation from COCOAPI.

        Note:
            bbox:[x1, y1, w, h]

        Args:
            img_id: coco image id

        Returns:
            dict: db entry
        """
        img_ann = self.coco.loadImgs(img_id)[0]
        width = img_ann['width']
        height = img_ann['height']
        num_joints = self.ann_info['num_joints']

        ann_ids = self.coco.getAnnIds(imgIds=img_id, iscrowd=False)
        objs = self.coco.loadAnns(ann_ids)

        # sanitize bboxes
        valid_objs = []
        for obj in objs:
            if 'bbox' not in obj:
                continue
            x, y, w, h = obj['bbox']
            x1 = max(0, x)
            y1 = max(0, y)
            x2 = min(width - 1, x1 + max(0, w))
            y2 = min(height - 1, y1 + max(0, h))
            if ('area' not in obj or obj['area'] > 0) and x2 > x1 and y2 > y1:
                obj['clean_bbox'] = [x1, y1, x2 - x1, y2 - y1]
                valid_objs.append(obj)
        objs = valid_objs

        bbox_id = 0
        rec = []
        for obj in objs:
            if 'keypoints' not in obj:
                continue
            if max(obj['keypoints']) == 0:
                continue
            if 'num_keypoints' in obj and obj['num_keypoints'] == 0:
                continue
            joints_3d = np.zeros((num_joints, 3), dtype=np.float32)
            joints_3d_visible = np.zeros((num_joints, 3), dtype=np.float32)

            keypoints = np.array(obj['keypoints']).reshape(-1, 3)
            joints_3d[:, :2] = keypoints[:, :2]
            joints_3d_visible[:, :2] = np.minimum(1, keypoints[:, 2:3])

            image_file = osp.join(self.img_prefix, self.id2name[img_id])
            rec.append({
                'image_file': image_file,
                'bbox': obj['clean_bbox'][:4],
                'rotation': 0,
                'joints_3d': joints_3d,
                'joints_3d_visible': joints_3d_visible,
                'dataset': self.dataset_name,
                'bbox_score': 1,
                'bbox_id': bbox_id
            })
            bbox_id = bbox_id + 1

        return rec

    def _load_coco_person_detection_results(self):
        """Load coco person detection results."""
        num_joints = self.ann_info['num_joints']
        all_boxes = None
        with open(self.bbox_file, 'r') as f:
            all_boxes = json.load(f)

        if not all_boxes:
            raise ValueError('=> Load %s fail!' % self.bbox_file)

        print(f'=> Total boxes: {len(all_boxes)}')

        kpt_db = []
        bbox_id = 0
        for det_res in all_boxes:
            if det_res['category_id'] != 1:
                continue

            image_file = osp.join(self.img_prefix,
                                  self.id2name[det_res['image_id']])
            box = det_res['bbox']
            score = det_res['score']

            if score < self.det_bbox_thr:
                continue

            joints_3d = np.zeros((num_joints, 3), dtype=np.float32)
            joints_3d_visible = np.ones((num_joints, 3), dtype=np.float32)
            kpt_db.append({
                'image_file': image_file,
                'rotation': 0,
                'bbox': box[:4],
                'bbox_score': score,
                'dataset': self.dataset_name,
                'joints_3d': joints_3d,
                'joints_3d_visible': joints_3d_visible,
                'bbox_id': bbox_id
            })
            bbox_id = bbox_id + 1
        print(f'=> Total boxes after filter '
              f'low score@{self.det_bbox_thr}: {bbox_id}')
        return kpt_db

    @deprecated_api_warning(name_dict=dict(outputs='results'))
    def evaluate(self, results, res_folder=None, metric='mAP', **kwargs):
        """Evaluate coco keypoint results. The pose prediction results will be
        saved in ``${res_folder}/result_keypoints.json``.

        Note:
            - batch_size: N
            - num_keypoints: K
            - heatmap height: H
            - heatmap width: W

        Args:
            results (list[dict]): Testing results containing the following
                items:

                - preds (np.ndarray[N,K,3]): The first two dimensions are \
                    coordinates, score is the third dimension of the array.
                - boxes (np.ndarray[N,6]): [center[0], center[1], scale[0], \
                    scale[1],area, score]
                - image_paths (list[str]): For example, ['data/coco/val2017\
                    /000000393226.jpg']
                - heatmap (np.ndarray[N, K, H, W]): model output heatmap
                - bbox_id (list(int)).
            res_folder (str, optional): The folder to save the testing
                results. If not specified, a temp folder will be created.
                Default: None.
            metric (str | list[str]): Metric to be performed. Defaults: 'mAP'.

        Returns:
            dict: Evaluation results for evaluation metric.
        """
        metrics = metric if isinstance(metric, list) else [metric]
        allowed_metrics = ['mAP']
        for metric in metrics:
            if metric not in allowed_metrics:
                raise KeyError(f'metric {metric} is not supported')

        if res_folder is not None:
            tmp_folder = None
            res_file = osp.join(res_folder, 'result_keypoints.json')
        else:
            tmp_folder = tempfile.TemporaryDirectory()
            res_file = osp.join(tmp_folder.name, 'result_keypoints.json')

        kpts = defaultdict(list)

        for result in results:
            preds = result['preds']
            boxes = result['boxes']
            image_paths = result['image_paths']
            bbox_ids = result['bbox_ids']

            batch_size = len(image_paths)
            for i in range(batch_size):
                image_id = self.name2id[image_paths[i][len(self.img_prefix):]]
                kpts[image_id].append({
                    'keypoints': preds[i],
                    'center': boxes[i][0:2],
                    'scale': boxes[i][2:4],
                    'area': boxes[i][4],
                    'score': boxes[i][5],
                    'image_id': image_id,
                    'bbox_id': bbox_ids[i]
                })
        kpts = self._sort_and_unique_bboxes(kpts)

        # rescoring and oks nms
        num_joints = self.ann_info['num_joints']
        vis_thr = self.vis_thr
        oks_thr = self.oks_thr
        valid_kpts = []
        for image_id in kpts.keys():
            img_kpts = kpts[image_id]
            for n_p in img_kpts:
                box_score = n_p['score']
                if kwargs.get('rle_score', False):
                    pose_score = n_p['keypoints'][:, 2]
                    n_p['score'] = float(box_score + np.mean(pose_score) +
                                         np.max(pose_score))
                else:
                    kpt_score = 0
                    valid_num = 0
                    for n_jt in range(0, num_joints):
                        t_s = n_p['keypoints'][n_jt][2]
                        if t_s > vis_thr:
                            kpt_score = kpt_score + t_s
                            valid_num = valid_num + 1
                    if valid_num != 0:
                        kpt_score = kpt_score / valid_num
                    # rescoring
                    n_p['score'] = kpt_score * box_score

            if self.use_nms:
                nms = soft_oks_nms if self.soft_nms else oks_nms
                keep = nms(img_kpts, oks_thr, sigmas=self.sigmas)
                valid_kpts.append([img_kpts[_keep] for _keep in keep])
            else:
                valid_kpts.append(img_kpts)

        self._write_coco_keypoint_results(valid_kpts, res_file)

        # do evaluation only if the ground truth keypoint annotations exist
        if 'annotations' in self.coco.dataset:
            info_str = self._do_python_keypoint_eval(res_file)
            name_value = OrderedDict(info_str)

            if tmp_folder is not None:
                tmp_folder.cleanup()
        else:
            warnings.warn(f'Due to the absence of ground truth keypoint'
                          f'annotations, the quantitative evaluation can not'
                          f'be conducted. The prediction results have been'
                          f'saved at: {osp.abspath(res_file)}')
            name_value = {}

        return name_value

    def _write_coco_keypoint_results(self, keypoints, res_file):
        """Write results into a json file."""
        data_pack = [{
            'cat_id': self._class_to_coco_ind[cls],
            'cls_ind': cls_ind,
            'cls': cls,
            'ann_type': 'keypoints',
            'keypoints': keypoints
        } for cls_ind, cls in enumerate(self.classes)
                     if not cls == '__background__']

        results = self._coco_keypoint_results_one_category_kernel(data_pack[0])

        with open(res_file, 'w') as f:
            json.dump(results, f, sort_keys=True, indent=4)

    def _coco_keypoint_results_one_category_kernel(self, data_pack):
        """Get coco keypoint results."""
        cat_id = data_pack['cat_id']
        keypoints = data_pack['keypoints']
        cat_results = []

        for img_kpts in keypoints:
            if len(img_kpts) == 0:
                continue

            _key_points = np.array(
                [img_kpt['keypoints'] for img_kpt in img_kpts])
            key_points = _key_points.reshape(-1,
                                             self.ann_info['num_joints'] * 3)

            result = [{
                'image_id': img_kpt['image_id'],
                'category_id': cat_id,
                'keypoints': key_point.tolist(),
                'score': float(img_kpt['score']),
                'center': img_kpt['center'].tolist(),
                'scale': img_kpt['scale'].tolist()
            } for img_kpt, key_point in zip(img_kpts, key_points)]

            cat_results.extend(result)

        return cat_results

    def _do_python_keypoint_eval(self, res_file):
        """Keypoint evaluation using COCOAPI."""
        coco_det = self.coco.loadRes(res_file)
        coco_eval = COCOeval(self.coco, coco_det, 'keypoints', self.sigmas)
        coco_eval.params.useSegm = None
        coco_eval.evaluate()
        coco_eval.accumulate()
        coco_eval.summarize()

        stats_names = [
            'AP', 'AP .5', 'AP .75', 'AP (M)', 'AP (L)', 'AR', 'AR .5',
            'AR .75', 'AR (M)', 'AR (L)'
        ]

        info_str = list(zip(stats_names, coco_eval.stats))

        return info_str

    def _sort_and_unique_bboxes(self, kpts, key='bbox_id'):
        """sort kpts and remove the repeated ones."""
        for img_id, persons in kpts.items():
            num = len(persons)
            kpts[img_id] = sorted(kpts[img_id], key=lambda x: x[key])
            for i in range(num - 1, 0, -1):
                if kpts[img_id][i][key] == kpts[img_id][i - 1][key]:
                    del kpts[img_id][i]

        return kpts
Tau-J commented 1 year ago

Kpt2dSviewRgbImgTopDownDataset is of 0.x, and you are using mmpose v1.0.0, they are imcompatible.

hsp2454 commented 1 year ago

I switched to 0.x to use it later, but it still doesn't work, because when I first trained hrnet with 0.x, I used PCK for evaluation, but now I want to use mAP but can't use it. I don't know if it is related to The data configuration in mmpose\datasets\datasets is related. That is, I am using 0.x now, but I don't know how to use mAP evaluation for custom data

Tau-J commented 1 year ago

Again, please show the configuration of CocoMetric you use, just like:

val_evaluator = [ 
     dict(type='CocoMetric` ....
hsp2454 commented 1 year ago

Sorry, 0.x does not have this configuration, all my configurations are on it, and the configuration file only has this evaluation = dict(interval=1, metric='mAP', save_best='AP')

hsp2454 commented 1 year ago

Here is my config file in 0.x

Config:
checkpoint_config = dict(interval=2)
log_config = dict(
    interval=1,
    hooks=[dict(type='TextLoggerHook'),
           dict(type='TensorboardLoggerHook')])
log_level = 'INFO'
load_from = None
resume_from = None
dist_params = dict(backend='nccl')
workflow = [('train', 1), ('val', 1)]
opencv_num_threads = 0
mp_start_method = 'fork'
dataset_info = dict(
    dataset_name='PigAnimal',
    paper_info=dict(
        author=
        'Lin, Tsung-Yi and Maire, Michael and Belongie, Serge and Hays, James and Perona, Pietro and Ramanan, Deva and Doll{\'a}r, Piotr and Zitnick, C Lawrence',
        title='Microsoft coco: Common objects in context',
        container='European conference on computer vision',
        year='2014',
        homepage='http://cocodataset.org/'),
    keypoint_info=dict({
        0:
        dict(name='1', id=0, color=[51, 153, 255], type='upper', swap=''),
        1:
        dict(name='2', id=1, color=[51, 153, 255], type='upper', swap=''),
        2:
        dict(name='3', id=2, color=[51, 153, 255], type='upper', swap=''),
        3:
        dict(name='4', id=3, color=[51, 153, 255], type='upper', swap=''),
        4:
        dict(name='5', id=4, color=[51, 153, 255], type='upper', swap=''),
        5:
        dict(name='6', id=5, color=[0, 255, 0], type='upper', swap=''),
        6:
        dict(name='7', id=6, color=[255, 128, 0], type='upper', swap=''),
        7:
        dict(name='8', id=7, color=[0, 255, 0], type='upper', swap='9'),
        8:
        dict(name='9', id=8, color=[255, 128, 0], type='upper', swap=''),
        9:
        dict(name='10', id=9, color=[0, 255, 0], type='upper', swap='16'),
        10:
        dict(name='11', id=10, color=[255, 128, 0], type='upper', swap='17'),
        11:
        dict(name='12', id=11, color=[0, 255, 0], type='lower', swap='18'),
        12:
        dict(name='13', id=12, color=[255, 128, 0], type='lower', swap='19'),
        13:
        dict(name='14', id=13, color=[0, 255, 0], type='lower', swap='20'),
        14:
        dict(name='15', id=14, color=[255, 128, 0], type='lower', swap='21'),
        15:
        dict(name='16', id=15, color=[0, 255, 0], type='lower', swap=''),
        16:
        dict(name='17', id=16, color=[255, 128, 0], type='lower', swap=''),
        17:
        dict(name='18', id=17, color=[0, 255, 0], type='lower', swap=''),
        18:
        dict(name='19', id=18, color=[0, 255, 0], type='lower', swap=''),
        19:
        dict(name='20', id=19, color=[0, 255, 0], type='lower', swap=''),
        20:
        dict(name='21', id=20, color=[0, 255, 0], type='lower', swap='')
    }),
    skeleton_info=dict({
        0: dict(link=('1', '2'), id=0, color=[0, 255, 0]),
        1: dict(link=('2', '3'), id=1, color=[0, 255, 0]),
        2: dict(link=('3', '4'), id=2, color=[255, 128, 0]),
        3: dict(link=('4', '5'), id=3, color=[255, 128, 0]),
        4: dict(link=('5', '6'), id=4, color=[51, 153, 255]),
        5: dict(link=('6', '7'), id=5, color=[51, 153, 255]),
        6: dict(link=('7', '8'), id=6, color=[51, 153, 255]),
        7: dict(link=('8', '13'), id=7, color=[51, 153, 255]),
        8: dict(link=('9', '10'), id=8, color=[0, 255, 0]),
        9: dict(link=('9', '16'), id=9, color=[255, 128, 0]),
        10: dict(link=('9', '13'), id=10, color=[0, 255, 0]),
        11: dict(link=('9', '19'), id=11, color=[255, 128, 0]),
        12: dict(link=('10', '11'), id=12, color=[51, 153, 255]),
        13: dict(link=('11', '12'), id=13, color=[51, 153, 255]),
        14: dict(link=('13', '14'), id=14, color=[51, 153, 255]),
        15: dict(link=('14', '15'), id=15, color=[51, 153, 255]),
        16: dict(link=('16', '17'), id=16, color=[51, 153, 255]),
        17: dict(link=('17', '18'), id=17, color=[51, 153, 255]),
        18: dict(link=('19', '20'), id=18, color=[51, 153, 255]),
        19: dict(link=('20', '21'), id=19, color=[51, 123, 255]),
        20: dict(link=('8', '19'), id=20, color=[51, 123, 255]),
        21: dict(link=('2', '10'), id=21, color=[51, 123, 255]),
        22: dict(link=('2', '16'), id=22, color=[51, 123, 255])
    }),
    joint_weights=[
        1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.2, 1.2, 1.5, 1.5, 1.0, 1.0, 1.2,
        1.2, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5
    ],
    sigmas=[
        0.26, 0.25, 0.25, 0.35, 0.35, 0.79, 0.79, 0.72, 0.72, 0.9, 0.9, 0.9,
        1.07, 0.87, 0.87, 0.89, 0.89, 0.9, 0.9, 0.9, 0.9
    ])
evaluation = dict(interval=1, metric='mAP', save_best='AP')
optimizer = dict(type='Adam', lr=0.0005)
optimizer_config = dict(grad_clip=None)
lr_config = dict(
    policy='step',
    warmup='linear',
    warmup_iters=100,
    warmup_ratio=0.001,
    step=[50, 90])
total_epochs = 210
channel_cfg = dict(
    num_output_channels=21,
    dataset_joints=21,
    dataset_channel=[[
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
        20
    ]],
    inference_channel=[
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
        20
    ])
model = dict(
    type='TopDown',
    pretrained=
    'https://download.openmmlab.com/mmpose/pretrain_models/hrnet_w48-8ef0771d.pth',
    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=(48, 96)),
            stage3=dict(
                num_modules=4,
                num_branches=3,
                block='BASIC',
                num_blocks=(4, 4, 4),
                num_channels=(48, 96, 192)),
            stage4=dict(
                num_modules=3,
                num_branches=4,
                block='BASIC',
                num_blocks=(4, 4, 4, 4),
                num_channels=(48, 96, 192, 384)))),
    keypoint_head=dict(
        type='TopdownHeatmapSimpleHead',
        in_channels=48,
        out_channels=21,
        num_deconv_layers=0,
        extra=dict(final_conv_kernel=1),
        loss_keypoint=dict(type='JointsMSELoss', use_target_weight=True)),
    train_cfg=dict(),
    test_cfg=dict(
        flip_test=True,
        post_process='default',
        shift_heatmap=True,
        modulate_kernel=11))
data_cfg = dict(
    image_size=[256, 256],
    heatmap_size=[64, 64],
    num_output_channels=21,
    num_joints=21,
    dataset_channel=[[
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
        20
    ]],
    inference_channel=[
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
        20
    ],
    soft_nms=False,
    nms_thr=0.9,
    oks_thr=0.9,
    vis_thr=0.4,
    use_gt_bbox=True,
    det_bbox_thr=0.0,
    bbox_file=
    'D:\Code\mmpose2\data\pigBehavior/NewData\coco\instances_val2017.json')
train_pipeline = [
    dict(type='LoadImageFromFile'),
    dict(type='TopDownGetBboxCenterScale', padding=1.25),
    dict(type='TopDownRandomShiftBboxCenter', shift_factor=0.16, prob=0.5),
    dict(type='TopDownRandomFlip', flip_prob=0.7),
    dict(
        type='TopDownHalfBodyTransform',
        num_joints_half_body=8,
        prob_half_body=0.7),
    dict(
        type='TopDownGetRandomScaleRotation', rot_factor=40, scale_factor=0.5),
    dict(type='TopDownAffine'),
    dict(type='ToTensor'),
    dict(
        type='NormalizeTensor',
        mean=[0.485, 0.456, 0.406],
        std=[0.229, 0.224, 0.225]),
    dict(type='TopDownGenerateTarget', sigma=2),
    dict(
        type='Collect',
        keys=['img', 'target', 'target_weight'],
        meta_keys=[
            'image_file', 'joints_3d', 'joints_3d_visible', 'center', 'scale',
            'rotation', 'bbox_score', 'flip_pairs'
        ])
]
val_pipeline = [
    dict(type='LoadImageFromFile'),
    dict(type='TopDownGetBboxCenterScale', padding=1.25),
    dict(type='TopDownAffine'),
    dict(type='ToTensor'),
    dict(
        type='NormalizeTensor',
        mean=[0.485, 0.456, 0.406],
        std=[0.229, 0.224, 0.225]),
    dict(
        type='Collect',
        keys=['img'],
        meta_keys=[
            'image_file', 'center', 'scale', 'rotation', 'bbox_score',
            'flip_pairs'
        ])
]
test_pipeline = [
    dict(type='LoadImageFromFile'),
    dict(type='TopDownGetBboxCenterScale', padding=1.25),
    dict(type='TopDownAffine'),
    dict(type='ToTensor'),
    dict(
        type='NormalizeTensor',
        mean=[0.485, 0.456, 0.406],
        std=[0.229, 0.224, 0.225]),
    dict(
        type='Collect',
        keys=['img'],
        meta_keys=[
            'image_file', 'center', 'scale', 'rotation', 'bbox_score',
            'flip_pairs'
        ])
]
data_root = 'D:\Code\mmpose2\data\pigBehavior\coco'
data = dict(
    samples_per_gpu=16,
    workers_per_gpu=4,
    val_dataloader=dict(samples_per_gpu=16),
    test_dataloader=dict(samples_per_gpu=16),
    train=dict(
        type='PigAnimal',
        ann_file=
        'D:\Code\mmpose2\data\pigBehavior\coco/annotations\keypoints_train.json',
        img_prefix='D:\Code\mmpose2\data\pigBehavior\coco/train/',
        data_cfg=dict(
            image_size=[256, 256],
            heatmap_size=[64, 64],
            num_output_channels=21,
            num_joints=21,
            dataset_channel=[[
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
                18, 19, 20
            ]],
            inference_channel=[
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
                18, 19, 20
            ],
            soft_nms=False,
            nms_thr=0.9,
            oks_thr=0.9,
            vis_thr=0.4,
            use_gt_bbox=True,
            det_bbox_thr=0.0,
            bbox_file=
            'D:\Code\mmpose2\data\pigBehavior/NewData\coco\instances_val2017.json'
        ),
        pipeline=[
            dict(type='LoadImageFromFile'),
            dict(type='TopDownGetBboxCenterScale', padding=1.25),
            dict(
                type='TopDownRandomShiftBboxCenter',
                shift_factor=0.16,
                prob=0.5),
            dict(type='TopDownRandomFlip', flip_prob=0.7),
            dict(
                type='TopDownHalfBodyTransform',
                num_joints_half_body=8,
                prob_half_body=0.7),
            dict(
                type='TopDownGetRandomScaleRotation',
                rot_factor=40,
                scale_factor=0.5),
            dict(type='TopDownAffine'),
            dict(type='ToTensor'),
            dict(
                type='NormalizeTensor',
                mean=[0.485, 0.456, 0.406],
                std=[0.229, 0.224, 0.225]),
            dict(type='TopDownGenerateTarget', sigma=2),
            dict(
                type='Collect',
                keys=['img', 'target', 'target_weight'],
                meta_keys=[
                    'image_file', 'joints_3d', 'joints_3d_visible', 'center',
                    'scale', 'rotation', 'bbox_score', 'flip_pairs'
                ])
        ],
        dataset_info=dict(
            dataset_name='PigAnimal',
            paper_info=dict(
                author=
                'Lin, Tsung-Yi and Maire, Michael and Belongie, Serge and Hays, James and Perona, Pietro and Ramanan, Deva and Doll{\'a}r, Piotr and Zitnick, C Lawrence',                title='Microsoft coco: Common objects in context',
                container='European conference on computer vision',
                year='2014',
                homepage='http://cocodataset.org/'),
            keypoint_info=dict({
                0:
                dict(
                    name='1',
                    id=0,
                    color=[51, 153, 255],
                    type='upper',
                    swap=''),
                1:
                dict(
                    name='2',
                    id=1,
                    color=[51, 153, 255],
                    type='upper',
                    swap=''),
                2:
                dict(
                    name='3',
                    id=2,
                    color=[51, 153, 255],
                    type='upper',
                    swap=''),
                3:
                dict(
                    name='4',
                    id=3,
                    color=[51, 153, 255],
                    type='upper',
                    swap=''),
                4:
                dict(
                    name='5',
                    id=4,
                    color=[51, 153, 255],
                    type='upper',
                    swap=''),
                5:
                dict(name='6', id=5, color=[0, 255, 0], type='upper', swap=''),
                6:
                dict(
                    name='7', id=6, color=[255, 128, 0], type='upper',
                    swap=''),
                7:
                dict(
                    name='8', id=7, color=[0, 255, 0], type='upper', swap='9'),
                8:
                dict(
                    name='9', id=8, color=[255, 128, 0], type='upper',
                    swap=''),
                9:
                dict(
                    name='10',
                    id=9,
                    color=[0, 255, 0],
                    type='upper',
                    swap='16'),
                10:
                dict(
                    name='11',
                    id=10,
                    color=[255, 128, 0],
                    type='upper',
                    swap='17'),
                11:
                dict(
                    name='12',
                    id=11,
                    color=[0, 255, 0],
                    type='lower',
                    swap='18'),
                12:
                dict(
                    name='13',
                    id=12,
                    color=[255, 128, 0],
                    type='lower',
                    swap='19'),
                13:
                dict(
                    name='14',
                    id=13,
                    color=[0, 255, 0],
                    type='lower',
                    swap='20'),
                14:
                dict(
                    name='15',
                    id=14,
                    color=[255, 128, 0],
                    type='lower',
                    swap='21'),
                15:
                dict(
                    name='16', id=15, color=[0, 255, 0], type='lower',
                    swap=''),
                16:
                dict(
                    name='17',
                    id=16,
                    color=[255, 128, 0],
                    type='lower',
                    swap=''),
                17:
                dict(
                    name='18', id=17, color=[0, 255, 0], type='lower',
                    swap=''),
                18:
                dict(
                    name='19', id=18, color=[0, 255, 0], type='lower',
                    swap=''),
                19:
                dict(
                    name='20', id=19, color=[0, 255, 0], type='lower',
                    swap=''),
                20:
                dict(
                    name='21', id=20, color=[0, 255, 0], type='lower', swap='')
            }),
            skeleton_info=dict({
                0:
                dict(link=('1', '2'), id=0, color=[0, 255, 0]),
                1:
                dict(link=('2', '3'), id=1, color=[0, 255, 0]),
                2:
                dict(link=('3', '4'), id=2, color=[255, 128, 0]),
                3:
                dict(link=('4', '5'), id=3, color=[255, 128, 0]),
                4:
                dict(link=('5', '6'), id=4, color=[51, 153, 255]),
                5:
                dict(link=('6', '7'), id=5, color=[51, 153, 255]),
                6:
                dict(link=('7', '8'), id=6, color=[51, 153, 255]),
                7:
                dict(link=('8', '13'), id=7, color=[51, 153, 255]),
                8:
                dict(link=('9', '10'), id=8, color=[0, 255, 0]),
                9:
                dict(link=('9', '16'), id=9, color=[255, 128, 0]),
                10:
                dict(link=('9', '13'), id=10, color=[0, 255, 0]),
                11:
                dict(link=('9', '19'), id=11, color=[255, 128, 0]),
                12:
                dict(link=('10', '11'), id=12, color=[51, 153, 255]),
                13:
                dict(link=('11', '12'), id=13, color=[51, 153, 255]),
                14:
                dict(link=('13', '14'), id=14, color=[51, 153, 255]),
                15:
                dict(link=('14', '15'), id=15, color=[51, 153, 255]),
                16:
                dict(link=('16', '17'), id=16, color=[51, 153, 255]),
                17:
                dict(link=('17', '18'), id=17, color=[51, 153, 255]),
                18:
                dict(link=('19', '20'), id=18, color=[51, 153, 255]),
                19:
                dict(link=('20', '21'), id=19, color=[51, 123, 255]),
                20:
                dict(link=('8', '19'), id=20, color=[51, 123, 255]),
                21:
                dict(link=('2', '10'), id=21, color=[51, 123, 255]),
                22:
                dict(link=('2', '16'), id=22, color=[51, 123, 255])
            }),
            joint_weights=[
                1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.2, 1.2, 1.5, 1.5, 1.0,
                1.0, 1.2, 1.2, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5
            ],
            sigmas=[
                0.26, 0.25, 0.25, 0.35, 0.35, 0.79, 0.79, 0.72, 0.72, 0.9, 0.9,
                0.9, 1.07, 0.87, 0.87, 0.89, 0.89, 0.9, 0.9, 0.9, 0.9
            ])),
    val=dict(
        type='PigAnimal',
        ann_file=
        'D:\Code\mmpose2\data\pigBehavior\coco/annotations\keypoints_val.json',
        img_prefix='D:\Code\mmpose2\data\pigBehavior\coco/val/',
        data_cfg=dict(
            image_size=[256, 256],
            heatmap_size=[64, 64],
            num_output_channels=21,
            num_joints=21,
            dataset_channel=[[
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
                18, 19, 20
            ]],
            inference_channel=[
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
                18, 19, 20
            ],
            soft_nms=False,
            nms_thr=0.9,
            oks_thr=0.9,
            vis_thr=0.4,
            use_gt_bbox=True,
            det_bbox_thr=0.0,
            bbox_file=
            'D:\Code\mmpose2\data\pigBehavior/NewData\coco\instances_val2017.json'
        ),
        pipeline=[
            dict(type='LoadImageFromFile'),
            dict(type='TopDownGetBboxCenterScale', padding=1.25),
            dict(type='TopDownAffine'),
            dict(type='ToTensor'),
            dict(
                type='NormalizeTensor',
                mean=[0.485, 0.456, 0.406],
                std=[0.229, 0.224, 0.225]),
            dict(
                type='Collect',
                keys=['img'],
                meta_keys=[
                    'image_file', 'center', 'scale', 'rotation', 'bbox_score',
                    'flip_pairs'
                ])
        ],
        dataset_info=dict(
            dataset_name='PigAnimal',
            paper_info=dict(
                author=
                'Lin, Tsung-Yi and Maire, Michael and Belongie, Serge and Hays, James and Perona, Pietro and Ramanan, Deva and Doll{\'a}r, Piotr and Zitnick, C Lawrence',                title='Microsoft coco: Common objects in context',
                container='European conference on computer vision',
                year='2014',
                homepage='http://cocodataset.org/'),
            keypoint_info=dict({
                0:
                dict(
                    name='1',
                    id=0,
                    color=[51, 153, 255],
                    type='upper',
                    swap=''),
                1:
                dict(
                    name='2',
                    id=1,
                    color=[51, 153, 255],
                    type='upper',
                    swap=''),
                2:
                dict(
                    name='3',
                    id=2,
                    color=[51, 153, 255],
                    type='upper',
                    swap=''),
                3:
                dict(
                    name='4',
                    id=3,
                    color=[51, 153, 255],
                    type='upper',
                    swap=''),
                4:
                dict(
                    name='5',
                    id=4,
                    color=[51, 153, 255],
                    type='upper',
                    swap=''),
                5:
                dict(name='6', id=5, color=[0, 255, 0], type='upper', swap=''),
                6:
                dict(
                    name='7', id=6, color=[255, 128, 0], type='upper',
                    swap=''),
                7:
                dict(
                    name='8', id=7, color=[0, 255, 0], type='upper', swap='9'),
                8:
                dict(
                    name='9', id=8, color=[255, 128, 0], type='upper',
                    swap=''),
                9:
                dict(
                    name='10',
                    id=9,
                    color=[0, 255, 0],
                    type='upper',
                    swap='16'),
                10:
                dict(
                    name='11',
                    id=10,
                    color=[255, 128, 0],
                    type='upper',
                    swap='17'),
                11:
                dict(
                    name='12',
                    id=11,
                    color=[0, 255, 0],
                    type='lower',
                    swap='18'),
                12:
                dict(
                    name='13',
                    id=12,
                    color=[255, 128, 0],
                    type='lower',
                    swap='19'),
                13:
                dict(
                    name='14',
                    id=13,
                    color=[0, 255, 0],
                    type='lower',
                    swap='20'),
                14:
                dict(
                    name='15',
                    id=14,
                    color=[255, 128, 0],
                    type='lower',
                    swap='21'),
                15:
                dict(
                    name='16', id=15, color=[0, 255, 0], type='lower',
                    swap=''),
                16:
                dict(
                    name='17',
                    id=16,
                    color=[255, 128, 0],
                    type='lower',
                    swap=''),
                17:
                dict(
                    name='18', id=17, color=[0, 255, 0], type='lower',
                    swap=''),
                18:
                dict(
                    name='19', id=18, color=[0, 255, 0], type='lower',
                    swap=''),
                19:
                dict(
                    name='20', id=19, color=[0, 255, 0], type='lower',
                    swap=''),
                20:
                dict(
                    name='21', id=20, color=[0, 255, 0], type='lower', swap='')
            }),
            skeleton_info=dict({
                0:
                dict(link=('1', '2'), id=0, color=[0, 255, 0]),
                1:
                dict(link=('2', '3'), id=1, color=[0, 255, 0]),
                2:
                dict(link=('3', '4'), id=2, color=[255, 128, 0]),
                3:
                dict(link=('4', '5'), id=3, color=[255, 128, 0]),
                4:
                dict(link=('5', '6'), id=4, color=[51, 153, 255]),
                5:
                dict(link=('6', '7'), id=5, color=[51, 153, 255]),
                6:
                dict(link=('7', '8'), id=6, color=[51, 153, 255]),
                7:
                dict(link=('8', '13'), id=7, color=[51, 153, 255]),
                8:
                dict(link=('9', '10'), id=8, color=[0, 255, 0]),
                9:
                dict(link=('9', '16'), id=9, color=[255, 128, 0]),
                10:
                dict(link=('9', '13'), id=10, color=[0, 255, 0]),
                11:
                dict(link=('9', '19'), id=11, color=[255, 128, 0]),
                12:
                dict(link=('10', '11'), id=12, color=[51, 153, 255]),
                13:
                dict(link=('11', '12'), id=13, color=[51, 153, 255]),
                14:
                dict(link=('13', '14'), id=14, color=[51, 153, 255]),
                15:
                dict(link=('14', '15'), id=15, color=[51, 153, 255]),
                16:
                dict(link=('16', '17'), id=16, color=[51, 153, 255]),
                17:
                dict(link=('17', '18'), id=17, color=[51, 153, 255]),
                18:
                dict(link=('19', '20'), id=18, color=[51, 153, 255]),
                19:
                dict(link=('20', '21'), id=19, color=[51, 123, 255]),
                20:
                dict(link=('8', '19'), id=20, color=[51, 123, 255]),
                21:
                dict(link=('2', '10'), id=21, color=[51, 123, 255]),
                22:
                dict(link=('2', '16'), id=22, color=[51, 123, 255])
            }),
            joint_weights=[
                1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.2, 1.2, 1.5, 1.5, 1.0,
                1.0, 1.2, 1.2, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5
            ],
            sigmas=[
                0.26, 0.25, 0.25, 0.35, 0.35, 0.79, 0.79, 0.72, 0.72, 0.9, 0.9,
                0.9, 1.07, 0.87, 0.87, 0.89, 0.89, 0.9, 0.9, 0.9, 0.9
            ])),
    test=dict(
        type='PigAnimal',
        ann_file=
        'D:\Code\mmpose2\data\pigBehavior\coco/annotations\keypoints_val.json',
        img_prefix='D:\Code\mmpose2\data\pigBehavior\coco/val/',
        data_cfg=dict(
            image_size=[256, 256],
            heatmap_size=[64, 64],
            num_output_channels=21,
            num_joints=21,
            dataset_channel=[[
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
                18, 19, 20
            ]],
            inference_channel=[
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
                18, 19, 20
            ],
            soft_nms=False,
            nms_thr=0.9,
            oks_thr=0.9,
            vis_thr=0.4,
            use_gt_bbox=True,
            det_bbox_thr=0.0,
            bbox_file=
            'D:\Code\mmpose2\data\pigBehavior/NewData\coco\instances_val2017.json'
        ),
        pipeline=[
            dict(type='LoadImageFromFile'),
            dict(type='TopDownGetBboxCenterScale', padding=1.25),
            dict(type='TopDownAffine'),
            dict(type='ToTensor'),
            dict(
                type='NormalizeTensor',
                mean=[0.485, 0.456, 0.406],
                std=[0.229, 0.224, 0.225]),
            dict(
                type='Collect',
                keys=['img'],
                meta_keys=[
                    'image_file', 'center', 'scale', 'rotation', 'bbox_score',
                    'flip_pairs'
                ])
        ],
        dataset_info=dict(
            dataset_name='PigAnimal',
            paper_info=dict(
                author=
                'Lin, Tsung-Yi and Maire, Michael and Belongie, Serge and Hays, James and Perona, Pietro and Ramanan, Deva and Doll{\'a}r, Piotr and Zitnick, C Lawrence',                title='Microsoft coco: Common objects in context',
                container='European conference on computer vision',
                year='2014',
                homepage='http://cocodataset.org/'),
            keypoint_info=dict({
                0:
                dict(
                    name='1',
                    id=0,
                    color=[51, 153, 255],
                    type='upper',
                    swap=''),
                1:
                dict(
                    name='2',
                    id=1,
                    color=[51, 153, 255],
                    type='upper',
                    swap=''),
                2:
                dict(
                    name='3',
                    id=2,
                    color=[51, 153, 255],
                    type='upper',
                    swap=''),
                3:
                dict(
                    name='4',
                    id=3,
                    color=[51, 153, 255],
                    type='upper',
                    swap=''),
                4:
                dict(
                    name='5',
                    id=4,
                    color=[51, 153, 255],
                    type='upper',
                    swap=''),
                5:
                dict(name='6', id=5, color=[0, 255, 0], type='upper', swap=''),
                6:
                dict(
                    name='7', id=6, color=[255, 128, 0], type='upper',
                    swap=''),
                7:
                dict(
                    name='8', id=7, color=[0, 255, 0], type='upper', swap='9'),
                8:
                dict(
                    name='9', id=8, color=[255, 128, 0], type='upper',
                    swap=''),
                9:
                dict(
                    name='10',
                    id=9,
                    color=[0, 255, 0],
                    type='upper',
                    swap='16'),
                10:
                dict(
                    name='11',
                    id=10,
                    color=[255, 128, 0],
                    type='upper',
                    swap='17'),
                11:
                dict(
                    name='12',
                    id=11,
                    color=[0, 255, 0],
                    type='lower',
                    swap='18'),
                12:
                dict(
                    name='13',
                    id=12,
                    color=[255, 128, 0],
                    type='lower',
                    swap='19'),
                13:
                dict(
                    name='14',
                    id=13,
                    color=[0, 255, 0],
                    type='lower',
                    swap='20'),
                14:
                dict(
                    name='15',
                    id=14,
                    color=[255, 128, 0],
                    type='lower',
                    swap='21'),
                15:
                dict(
                    name='16', id=15, color=[0, 255, 0], type='lower',
                    swap=''),
                16:
                dict(
                    name='17',
                    id=16,
                    color=[255, 128, 0],
                    type='lower',
                    swap=''),
                17:
                dict(
                    name='18', id=17, color=[0, 255, 0], type='lower',
                    swap=''),
                18:
                dict(
                    name='19', id=18, color=[0, 255, 0], type='lower',
                    swap=''),
                19:
                dict(
                    name='20', id=19, color=[0, 255, 0], type='lower',
                    swap=''),
                20:
                dict(
                    name='21', id=20, color=[0, 255, 0], type='lower', swap='')
            }),
            skeleton_info=dict({
                0:
                dict(link=('1', '2'), id=0, color=[0, 255, 0]),
                1:
                dict(link=('2', '3'), id=1, color=[0, 255, 0]),
                2:
                dict(link=('3', '4'), id=2, color=[255, 128, 0]),
                3:
                dict(link=('4', '5'), id=3, color=[255, 128, 0]),
                4:
                dict(link=('5', '6'), id=4, color=[51, 153, 255]),
                5:
                dict(link=('6', '7'), id=5, color=[51, 153, 255]),
                6:
                dict(link=('7', '8'), id=6, color=[51, 153, 255]),
                7:
                dict(link=('8', '13'), id=7, color=[51, 153, 255]),
                8:
                dict(link=('9', '10'), id=8, color=[0, 255, 0]),
                9:
                dict(link=('9', '16'), id=9, color=[255, 128, 0]),
                10:
                dict(link=('9', '13'), id=10, color=[0, 255, 0]),
                11:
                dict(link=('9', '19'), id=11, color=[255, 128, 0]),
                12:
                dict(link=('10', '11'), id=12, color=[51, 153, 255]),
                13:
                dict(link=('11', '12'), id=13, color=[51, 153, 255]),
                14:
                dict(link=('13', '14'), id=14, color=[51, 153, 255]),
                15:
                dict(link=('14', '15'), id=15, color=[51, 153, 255]),
                16:
                dict(link=('16', '17'), id=16, color=[51, 153, 255]),
                17:
                dict(link=('17', '18'), id=17, color=[51, 153, 255]),
                18:
                dict(link=('19', '20'), id=18, color=[51, 153, 255]),
                19:
                dict(link=('20', '21'), id=19, color=[51, 123, 255]),
                20:
                dict(link=('8', '19'), id=20, color=[51, 123, 255]),
                21:
                dict(link=('2', '10'), id=21, color=[51, 123, 255]),
                22:
                dict(link=('2', '16'), id=22, color=[51, 123, 255])
            }),
            joint_weights=[
                1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.2, 1.2, 1.5, 1.5, 1.0,
                1.0, 1.2, 1.2, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5
            ],
            sigmas=[
                0.26, 0.25, 0.25, 0.35, 0.35, 0.79, 0.79, 0.72, 0.72, 0.9, 0.9,
                0.9, 1.07, 0.87, 0.87, 0.89, 0.89, 0.9, 0.9, 0.9, 0.9
            ])))
hsp2454 commented 1 year ago

The following is my val_evaluator of 1.x. I have tried 0.x and 1.x at the same time, but it is useless, and the output AP is 0.

 val_evaluator = dict(
      type='CocoMetric', use_area=True,ann_file=data_root + 
      'annotations\keypoints_val.json',score_mode='bbox_keypoint',keypoint_score_thr=0.2,)
NormalGamer0701 commented 1 year ago

The following is my val_evaluator of 1.x. I have tried 0.x and 1.x at the same time, but it is useless, and the output AP is 0.

 val_evaluator = dict(
      type='CocoMetric', use_area=True,ann_file=data_root + 
      'annotations\keypoints_val.json',score_mode='bbox_keypoint',keypoint_score_thr=0.2,)

Hi, I have the same issue. Did you find the reason?

pesiRD2 commented 5 months ago

@hsp2454 I found the reason that "CocoMetric, AP is all 0" is caused by the annotation json file in json_data["annotations"][0]["area"], below is an example of Coco format:

"annotations": [
   {
       "segmentation": [
           [
               [x1, y1],
               [x2, y2],
               [x3, y3],
               ...
           ]
       ],
       "area": 1000,
       "iscrowd": 0,
       "image_id": 1,
       "bbox": [x, y, width, height],
       "category_id": 1,
       "id": 1
   },
   {
       "segmentation": [
           [
               [x1, y1],
               [x2, y2],
               [x3, y3],
               ...
           ]
       ],
       "area": 800,
       "iscrowd": 0,
       "image_id": 1,
       "bbox": [x, y, width, height],
       "category_id": 2,
       "id": 2
   }
]

the value of "area" key need to be set to greater than 0 (you can just calculate your bound box area and put the value into it). When your "area" value is greater than 0, the evaluation will start to calculate all the CocoMetric.