【Ultralytics】评估报错:解决 KeyError: ‘info‘ 错误
文章目录
- 一、示例错误
- 二、报错解决
一、示例错误
当运行代码 res.dataset['info'] = copy.deepcopy(self.dataset['info'])
时出现 KeyError: 'info'
,通常是因为 self.dataset 中缺少键 ‘info’。这是在处理 COCO 数据集或使用 pycocotools 时的常见问题,尤其是在较新版本的 pycocotools 中。
二、报错解决
在执行深拷贝之前,确保 self.dataset 包含键 ‘info’。可以通过以下代码检查:
if 'info' in self.dataset:res.dataset['info'] = copy.deepcopy(self.dataset['info'])
else:res.dataset['info'] = {}
此方法会在缺少 ‘info’ 键时为其赋予一个空字典,避免错误。
但是还是会存在报错:
但发现无法解决,研究后发现是某些版本的 pycocotools(如 2.0.9 或更高版本)移除了对 ‘info’ 键的支持。可以通过降级到兼容版本(如 2.0.7)解决:
pip install pycocotools==2.0.7
通过以上方法,可以有效避免 KeyError: ‘info’ 错误,并确保代码正常运行。