当前位置: 首页 > 工具软件 > Numpy.GPU > 使用案例 >

Yolov6解决常见报错(1)TypeError numpy.float64 object cannot be interpreted as an index

窦弘义
2023-12-01

这几天偶然看见Yolov6出来,迫不及待的试了一下,结果看见网上评论说bug太多了,我作为使用者,想着积极做出贡献,把一些我解决的bug分享一下,方便大家也能看到顺便解决。

先看报错

Training completed in 0.082 hours.
Traceback (most recent call last):
File "tools/train.py", line 92, in
main(args)
File "tools/train.py", line 82, in main
trainer.train()
File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/multi-gpu-4-tesla-m60/code/Users/zhanghe/cv-safety-detection/YOLOv6/yolov6/core/engine.py", line 70, in train
self.train_in_loop()
File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/multi-gpu-4-tesla-m60/code/Users/zhanghe/cv-safety-detection/YOLOv6/yolov6/core/engine.py", line 89, in train_in_loop
self.eval_and_save()
File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/multi-gpu-4-tesla-m60/code/Users/zhanghe/cv-safety-detection/YOLOv6/yolov6/core/engine.py", line 115, in eval_and_save
self.eval_model()
File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/multi-gpu-4-tesla-m60/code/Users/zhanghe/cv-safety-detection/YOLOv6/yolov6/core/engine.py", line 134, in eval_model
results = eval.run(self.data_dict,
File "/anaconda/envs/azureml_py38/lib/python3.8/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
return func(*args, **kwargs)
File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/multi-gpu-4-tesla-m60/code/Users/zhanghe/cv-safety-detection/YOLOv6/tools/eval.py", line 83, in run
eval_result = val.eval_model(pred_result, model, dataloader, task)
File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/multi-gpu-4-tesla-m60/code/Users/zhanghe/cv-safety-detection/YOLOv6/yolov6/core/evaler.py", line 128, in eval_model
cocoEval = COCOeval(anno, pred, 'bbox')
File "/anaconda/envs/azureml_py38/lib/python3.8/site-packages/pycocotools/cocoeval.py", line 76, in init
self.params = Params(iouType=iouType) # parameters
File "/anaconda/envs/azureml_py38/lib/python3.8/site-packages/pycocotools/cocoeval.py", line 527, in init
self.setDetParams()
File "/anaconda/envs/azureml_py38/lib/python3.8/site-packages/pycocotools/cocoeval.py", line 507, in setDetParams
self.iouThrs = np.linspace(.5, 0.95, np.round((0.95 - .5) / .05) + 1, endpoint=True)
File "<array_function internals>", line 5, in linspace
File "/anaconda/envs/azureml_py38/lib/python3.8/site-packages/numpy/core/function_base.py", line 113, in linspace
num = operator.index(num)
TypeError: 'numpy.float64' object cannot be interpreted as an integer

如果大家和我一样是这样的报错,在第一轮epoch结束的时候的报错,num = operator.index(num)报错。

刚开始去网上找原因,发现很多说因为numpy的版本太高了,需要转低版本,但是我转了低版本也并没有解决问题,没有办法,只好去看代码来找原因,看到pycocotools/cocoeval.py这个文件里的报错。

大家先别急,我又去网上找这个的报错,结果终于找到了,是因为numpy 1.11.0之后的版本只支持int类型的输入参数,而源码使用的浮点数。在代码的503行的setDetParams函数,

    def setDetParams(self):
        self.imgIds = []
        self.catIds = []
        np.arange causes trouble.  the data point on arange is slightly larger than the true value
        self.iouThrs = np.linspace(.5, 0.95, np.round((0.95 - .5) / .05) + 1, endpoint=True)
        self.recThrs = np.linspace(.0, 1.00, np.round((1.00 - .0) / .01) + 1, endpoint=True)
        self.maxDets = [1, 10, 100]
        self.areaRng = [[0 ** 2, 1e5 ** 2], [0 ** 2, 32 ** 2], [32 ** 2, 96 ** 2], [96 ** 2, 1e5 ** 2]]
        self.areaRngLbl = ['all', 'small', 'medium', 'large']
        self.useCats = 1

其中np.linspace使用的是浮点数,生成0.05步长,0.5~0.95的数组。

使用numpy.arange函数代替

    def setDetParams(self):
        self.imgIds = []
        self.catIds = []
        # np.arange causes trouble.  the data point on arange is slightly larger than the true value
        # self.iouThrs = np.linspace(.5, 0.95, np.round((0.95 - .5) / .05) + 1, endpoint=True)
        # self.recThrs = np.linspace(.0, 1.00, np.round((1.00 - .0) / .01) + 1, endpoint=True)
        self.iouThrs = np.arange(0.5, 0.97, 0.05)
        self.recThrs = np.arange(0., 1.0001, 0.01)
        self.maxDets = [1, 10, 100]
        self.areaRng = [[0 ** 2, 1e5 ** 2], [0 ** 2, 32 ** 2], [32 ** 2, 96 ** 2], [96 ** 2, 1e5 ** 2]]
        self.areaRngLbl = ['all', 'small', 'medium', 'large']
        self.useCats = 1

另外529行的setKpParams也要修改

    def setKpParams(self):
        self.imgIds = []
        self.catIds = []
        # np.arange causes trouble.  the data point on arange is slightly larger than the true value
        # self.iouThrs = np.linspace(.5, 0.95, np.round((0.95 - .5) / .05) + 1, endpoint=True)
        # self.recThrs = np.linspace(.0, 1.00, np.round((1.00 - .0) / .01) + 1, endpoint=True)
        self.iouThrs = np.arange(0.5, 0.97, 0.05)
        self.recThrs = np.arange(0., 1.0001, 0.01)
        self.maxDets = [20]
        self.areaRng = [[0 ** 2, 1e5 ** 2], [32 ** 2, 96 ** 2], [96 ** 2, 1e5 ** 2]]
        self.areaRngLbl = ['all', 'medium', 'large']
        self.useCats = 1

以上是由schmiloo的文章帮我解决了这个问题,非常感谢大神

文章连接:

TypeError numpy.float64 object cannot be interpreted as an index 完美解决方法_schmiloo的博客-CSDN博客

 类似资料: