错误TypeError: object of type <class ‘numpy.float64‘> cannot be safely interpreted as an integer.解决方案
程序员文章站
2022-04-25 21:25:07
...
在调用linespace函数时,也是在COCO数据集的调用时出现了问题
可以看到出现了如下的错误提示信息
Traceback (most recent call last):
File "/home/zmd/software/anaconda3/lib/python3.7/site-packages/numpy/core/function_base.py", line 117, in linspace
num = operator.index(num)
TypeError: 'numpy.float64' object cannot be interpreted as an integer
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "coco_validation.py", line 49, in <module>
main()
File "coco_validation.py", line 45, in main
coco_eval.evaluate_coco(dataset_val, retinanet)
File "/home/zmd/project/pytorch-retinanet/retinanet/coco_eval.py", line 76, in evaluate_coco
coco_eval = COCOeval(coco_true, coco_pred, 'bbox')
File "/home/zmd/.local/lib/python3.7/site-packages/pycocotools/cocoeval.py", line 76, in __init__
self.params = Params(iouType=iouType) # parameters
File "/home/zmd/.local/lib/python3.7/site-packages/pycocotools/cocoeval.py", line 527, in __init__
self.setDetParams()
File "/home/zmd/.local/lib/python3.7/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 6, in linspace
File "/home/zmd/software/anaconda3/lib/python3.7/site-packages/numpy/core/function_base.py", line 121, in linspace
.format(type(num)))
TypeError: object of type <class 'numpy.float64'> cannot be safely interpreted as an integer.
其实出现了这种错误信息一般是相通的,都是调用函数时出现了问题,而问题在于数据类型的错误使用。
- 解决方案一:降低numpy版本
但是在这里是因为numpy的版本太高了,因此调用函数np.linespace时会出现以上错误,numpy1.17.0版本以下是支持float型作为步长的。
因此只需要将numpy版本降低就可以了
pip install -U numpy==1.17.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
- 解决方案二:修改源代码
可以看到的是np.linespace需要的是整型变量作为步长,因此我们只需要在前面加上np.int()函数即可将float型变为了int型。