mmdetection测试报错,data[‘category_id‘] = self.cat_ids[label]IndexError: list index out of range

本文讲述了使用自定义的COCO格式数据集仅包含一类stone时,在MMDetection框架下训练无误,但在测试阶段遇到IndexError。通过修改mmdetection中CocoDataset类的CLASSES属性,解决了测试时的类别索引超出范围问题。解决方案已在GitHub issue中分享。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用自制数据集,格式为coco格式,数据集只有一类stone

训练的时候没问题,测试的时候有问题

报错提示如下

  File "tools/test.py", line 231, in main
    metric = dataset.evaluate(outputs, **eval_kwargs)
  File "/home/ubuntu/xy/mmdetection/mmdet/datasets/coco.py", line 414, in evaluate
    result_files, tmp_dir = self.format_results(results, jsonfile_prefix)
  File "/home/ubuntu/xy/mmdetection/mmdet/datasets/coco.py", line 359, in format_results
    result_files = self.results2json(results, jsonfile_prefix)
  File "/home/ubuntu/xy/mmdetection/mmdet/datasets/coco.py", line 291, in results2json
    json_results = self._det2json(results)
  File "/home/ubuntu/xy/mmdetection/mmdet/datasets/coco.py", line 228, in _det2json
    data['category_id'] = self.cat_ids[label]
IndexError: list index out of range

最主要的原因是coco中为80类,而数据集只有一类,修改mmdet/datasets/coco.py,将其中的80类改为自己的一类,我的修改示例如下

class CocoDataset(CustomDataset):
    CLASSES = ('stone')
    # CLASSES = ('person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
    #            'train', 'truck', 'boat', 'traffic light', 'fire hydrant',
    #            'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog',
    #            'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe',
    #            'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
    #            'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat',
    #            'baseball glove', 'skateboard', 'surfboard', 'tennis racket',
    #            'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl',
    #            'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot',
    #            'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
    #            'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop',
    #            'mouse', 'remote', 'keyboard', 'cell phone', 'microwave',
    #            'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock',
    #            'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush')

修改之后可以正常测试

如有其他问题可以参考github issue:

https://github.com/open-mmlab/mmdetection/issues/4243

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值