完美解决错误:in bbox2out catid = (clsid2catid[int(clsid)]) KeyError:
Posted 这是一个很随便的名字
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了完美解决错误:in bbox2out catid = (clsid2catid[int(clsid)]) KeyError:相关的知识,希望对你有一定的参考价值。
最近用了一下paddlepaddle深度学习框架训练了一下目标检测模型,然后在训练一段时候后在验证的时候出现了错误:
File "...\\PaddleDetection-master\\ppdet\\utils\\coco_eval.py", line 318, in bbox2out
catid = (clsid2catid[int(clsid)])
KeyError: 6
小编是训练一个目标检测的模型, KeyError: 6 中的6是要检测的类别的数量(6个类别)。
遇到这个错误的时候,很多人可能要放弃了(这框架我不玩了)
其实产生这个错误的原因就是paddle在验证的时候没有可虑到不同数据的问题,也就是说这个代码在coco数据集上训练可能是完全没有问题,那么怎样才能训练自己的数据呢?
首先找到报错的文件 File "...\\PaddleDetection-master\\ppdet\\utils\\coco_eval.py",这个文件是目标检测文件的位置,每个人可能放置的位置不一样,因人而异,找到你的文件夹的对应的文件,即可。
然后小编找到了报错的位置,对报错的内容进行了输出,发现是代码的bug(不兼容),
报错的函数:
def bbox2out(results, clsid2catid, is_bbox_normalized=False):
"""
Args:
results: request a dict, should include: `bbox`, `im_id`,
if is_bbox_normalized=True, also need `im_shape`.
clsid2catid: class id to category id map of COCO2017 dataset.
is_bbox_normalized: whether or not bbox is normalized.
"""
xywh_res = []
for t in results:
bboxes = t['bbox'][0]
if len(t['bbox'][1]) == 0: continue
lengths = t['bbox'][1][0]
im_ids = np.array(t['im_id'][0]).flatten()
if bboxes.shape == (1, 1) or bboxes is None or len(bboxes) == 0:
continue
k = 0
for i in range(len(lengths)):
num = lengths[i]
im_id = int(im_ids[i])
for j in range(num):
dt = bboxes[k]
clsid, score, xmin, ymin, xmax, ymax = dt.tolist()
if clsid < 0: continue
catid = (clsid2catid[int(clsid)])
if is_bbox_normalized:
xmin, ymin, xmax, ymax = \\
clip_bbox([xmin, ymin, xmax, ymax])
w = xmax - xmin
h = ymax - ymin
im_shape = t['im_shape'][0][i].tolist()
im_height, im_width = int(im_shape[0]), int(im_shape[1])
xmin *= im_width
ymin *= im_height
w *= im_width
h *= im_height
else:
# for yolov4
# w = xmax - xmin
# h = ymax - ymin
w = xmax - xmin + 1
h = ymax - ymin + 1
bbox = [xmin, ymin, w, h]
coco_res = {
'image_id': im_id,
'category_id': catid,
'bbox': bbox,
'score': score
}
xywh_res.append(coco_res)
k += 1
return xywh_res
我们看下函数的注释,说明了是用于coco数据集,那么我们的假的coco数据集可能就不兼容
后来小编通过自己编写代码完美修改了数据集标注的格式,模型训练就不会再产生问题了。
获取完整解决方案和解决代码:https://ai.52learn.online/60061
以上是关于完美解决错误:in bbox2out catid = (clsid2catid[int(clsid)]) KeyError:的主要内容,如果未能解决你的问题,请参考以下文章
OpenAI‘s services are not available in your country 完美解决方案
OpenAI‘s services are not available in your country 完美解决方案
完美解决 No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android(示例
完美解决一切python报错Cannot find reference ‘xxx‘ in ‘xxxxx‘,详细步骤
完美解决一切python报错Cannot find reference ‘xxx‘ in ‘xxxxx‘,详细步骤
完美解决error: command ‘C:Program FilesNVIDIA GPU Computing ToolkitCUDAv11.0in vcc.exe‘ failed