深度学习部署:Windows安装pycocotools报错解决方法

本系列是作者在运行一些深度学习实例时遇到的一系列各种问题和解决方案,希望对大家有所帮助。

1.pycocotools库的简介

pycocotools即python api tools of COCO。COCO是一个大型的图像数据集,用于目标检测、分割、人的关键点检测、素材分割和标题生成。这个包提供了Matlab、Python和luaapi,这些api有助于在COCO中加载、解析和可视化注释。请访问COCO – Common Objects in Context,可以了解关于COCO的更多信息,包括数据、论文和教程。COCO网站上也描述了注释的确切格式。Matlab和PythonAPI是完整的,LuaAPI只提供基本功能。

2.pycocotools安装的坑

今天我在跑超轻量目标检测模型NanoDet时,遇到了各种各样的问题,其中卡住最久的就是pycocotools的安装,整整花费了我数小时。我出现了以下的报错
深度学习部署:Windows安装pycocotools报错解决方法

执行: python -m pip install pycocotools时

ERROR: Command errored out with exit status 1:
command: 'D:\Anaconda3\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\46653\\AppData\\Local\\Temp\\pip-install-j33342g5\\pycocotools\\setup.py'"'"'; __file__='"'"'C:\\Users\\46653\\AppData\\Local\\Temp\\pip-install-j33342g5\\pycocotools\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d 'C:\Users\46653\AppData\Local\Temp\pip-wheel-0xpeseyp' --python-tag cp37
cwd: C:\Users\46653\AppData\Local\Temp\pip-install-j33342g5\pycocotools\
Complete output (13 lines):
running bdist_wheel
running build
running build_py
creating build
creating build\lib.win-amd64-3.7
creating build\lib.win-amd64-3.7\pycocotools
copying pycocotools\coco.py -> build\lib.win-amd64-3.7\pycocotools
copying pycocotools\cocoeval.py -> build\lib.win-amd64-3.7\pycocotools
copying pycocotools\mask.py -> build\lib.win-amd64-3.7\pycocotools
copying pycocotools\__init__.py -> build\lib.win-amd64-3.7\pycocotools
running build_ext
building 'pycocotools._mask' extension
error: Microsoft Visual C++ 14.0 is required. Get it with "Build Tools for Visual Studio": https://visualstudio.microsoft.com/downloads/
----------------------------------------
ERROR: Failed building wheel for pycocotools
ERROR: Command errored out with exit status 1:
command: 'D:\Anaconda3\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\46653\\AppData\\Local\\Temp\\pip-install-j33342g5\\pycocotools\\setup.py'"'"'; __file__='"'"'C:\\Users\\46653\\AppData\\Local\\Temp\\pip-install-j33342g5\\pycocotools\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\46653\AppData\Local\Temp\pip-record-ucmhdqzv\install-record.txt' --single-version-externally-managed --compile
cwd: C:\Users\46653\AppData\Local\Temp\pip-install-j33342g5\pycocotools\
Complete output (13 lines):
running install
running build
running build_py
creating build
creating build\lib.win-amd64-3.7
creating build\lib.win-amd64-3.7\pycocotools
copying pycocotools\coco.py -> build\lib.win-amd64-3.7\pycocotools
copying pycocotools\cocoeval.py -> build\lib.win-amd64-3.7\pycocotools
copying pycocotools\mask.py -> build\lib.win-amd64-3.7\pycocotools
copying pycocotools\__init__.py -> build\lib.win-amd64-3.7\pycocotools
running build_ext
building 'pycocotools._mask' extension
error: Microsoft Visual C++ 14.0 is required. Get it with "Build Tools for Visual Studio": https://visualstudio.microsoft.com/downloads/
----------------------------------------
ERROR: Command errored out with exit status 1: 'D:\Anaconda3\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\46653\\AppData\\Local\\Temp\\pip-install-j33342g5\\pycocotools\\setup.py'"'"'; __file__='"'"'C:\\Users\\46653\\AppData\\Local\\Temp\\pip-install-j33342g5\\pycocotools\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\46653\AppData\Local\Temp\pip-record-ucmhdqzv\install-record.txt' --single-version-externally-managed --compile Check the logs for full command output.

深度学习部署:Windows安装pycocotools报错解决方法

3.解决办法

经过我大量的查找资料发现,这是因为你的Windows下没有c/c++代码编译环境,因为pycocotools的主站点源码https://github.com/cocodataset/cocoapi默认只提供了源码,没有针对各平台编译好了的Release,而且声称将来不支持Windows! 。

  • 首先我们需要安装MS VC,下载地址:
  https://visualstudio.microsoft.com/zh-hans/downloads/

但是当你安装完,MS VC后发现仍会报错。

  • 因为我们还需要下载安装visualcppbuildtools_full.exe,下载地址:
   https://go.microsoft.com/fwlink/?LinkId=691126
  • 最后因为pip安装默认下载的源码是不支持Windows上编译的,需要从一个让pycocotools PythonAPI支持使用Windows和Python 3来build的clone站点https://github.com/philferriere/cocoapi来安装。
    打开cmd执行下面的命令:
pip install git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI

注意:由于github连接非常的不稳定,若中途报错远程连接断开并退出,可以多尝试几次。

  • 也可以使用国内镜像源安装
pip install pycocotools-windows -i https://pypi.tuna.tsinghua.edu.cn/simple

上次安装成功
深度学习部署:Windows安装pycocotools报错解决方法
NanoDet成功的跑起来了
深度学习部署:Windows安装pycocotools报错解决方法

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
乘风的头像乘风管理团队
上一篇 2022年3月21日 下午1:36
下一篇 2022年3月21日 下午2:00

相关推荐