Windows下安裝pycocotools


參考:
Windows下安裝 pycocotools

原版的pycocotools(不支持windows)的Github項目地址:https://github.com/cocodataset/cocoapi
支持windows的改寫版本的Github項目地址:https://github.com/philferriere/cocoapi

安裝步驟:

1.

安裝pycocotools首先需要安裝Cython,在相應的conda環境下輸入:

pip install cython

2.

由於支持windows的改寫pycocotools太久沒有更新,直接輸入

pip install git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI

進行安裝的話會在運行時報錯:

...
C:\D disk\Anaconda\Anaconda3\envs\pytorch\lib\site-packages\pycocotools\cocoeval.py in setDetParams(self)
    505         self.catIds = []
    506         # np.arange causes trouble.  the data point on arange is slightly larger than the true value
--> 507         self.iouThrs = np.linspace(.5, 0.95, np.round((0.95 - .5) / .05) + 1, endpoint=True)
    508         self.recThrs = np.linspace(.0, 1.00, np.round((1.00 - .0) / .01) + 1, endpoint=True)
    509         self.maxDets = [1, 10, 100]

<__array_function__ internals> in linspace(*args, **kwargs)

C:\D disk\Anaconda\Anaconda3\envs\pytorch\lib\site-packages\numpy\core\function_base.py in linspace(start, stop, num, endpoint, retstep, dtype, axis)
    119         raise TypeError(
    120             "object of type {} cannot be safely interpreted as an integer."
--> 121                 .format(type(num)))
    122 
    123     if num < 0:

TypeError: object of type <class 'numpy.float64'> cannot be safely interpreted as an integer.

因此我們需要將下載 https://github.com/philferriere/cocoapi 的源碼,並在修改后使用

python setup.py install

來進行安裝

3.

報錯指向pycocotools的cocoeval.py文件的507行,在對比了原版pycocotools的cocoeval.py文件改寫版本的cocoeval.py文件后我們可以發現:
原版的cocoeval.py文件的506、507行分別為:

self.iouThrs = np.linspace(.5, 0.95, int(np.round((0.95 - .5) / .05)) + 1, endpoint=True)
self.recThrs = np.linspace(.0, 1.00, int(np.round((1.00 - .0) / .01)) + 1, endpoint=True)

517、518行分別為:

self.iouThrs = np.linspace(.5, 0.95, int(np.round((0.95 - .5) / .05)) + 1, endpoint=True)
self.recThrs = np.linspace(.0, 1.00, int(np.round((1.00 - .0) / .01)) + 1, endpoint=True)

而改寫版本的cocoeval.py文件的507、508行分別為:

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)

518、519行分別為:

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)

即原版比改寫版本多了int()。

我們在看原版pycocotools的commits記錄的時候也能發現,原版pycocotools在2019年12月26號修復了這個問題:

因此我們只需要將Github上改寫版本的pycocotools的源碼下載下來,解壓后修改cocoeval.py文件的上述內容使之與原版一致(即添加四個int(...)),再使用python setup.py install安裝即可:

(base) PS C:\Users\username> conda activate pytorch
(pytorch) PS C:\Users\username> cd "改版pycocotools解壓路徑"
(pytorch) PS 改版pycocotools解壓路徑> python setup.py install

4.

改寫版本的pycocotools看起來是不會再更新了,因此能安裝原版pycocotools的話最好。我發現了一些windows系統安裝原版pycocotools的教程,但還沒有去試過:
成功解決ERROR: FAILED BUILDING WHEEL FOR PYCOCOTOOLS
Windows 10 編譯 Pycocotools 踩坑記


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM