机器学习备注:Windows上运行Mask_RCNN模型

准备工作

这些模型在Linux上运行比较方便,不过想起Windows便捷的各种工具,总是忍不住弄过来,

原模型的地址在这,
https://github.com/matterport/Mask_RCNN
当然github上还有很多其他版本的,tensorflow, pytorch是主流,大家可自行搜索,安装使用过程大同小异。下面的是一个tensorflow的版本。

注意:windows下的pycocotools版本和Linux不同

需要用到的工具pycocotools在这
不过要注意,这个pycocotools工具有Linux版和windows版本的,不要弄错了,官网链接给出的都是Linux版本的
windows版本( Windows下用这个 :-o )
https://github.com/philferriere/cocoapi
linux版本,
https://github.com/waleedka/coco

前提:已经安装好anaconda, 搭建好tensorflow开发环境
提示:
conda create -n tensorflow python=3.6
activate tensorflow
pip3 install (numpy, skimage, scipy, pillow, cython, etc......)

运行时中途碰到一个错误:
ImportError: No module named cv2
原因就是因为没有安装opencv-python,此时在tensorflow环境安装即可
(tensorflow) (tensorflow) E:\AtensorflowDev>pip3 install opencv-python

操作步骤如下

step1 clone the Mask_RCNN repository
git clone https://github.com/matterport/Mask_RCNN.git
此时会在你的目录下生成一个Mask_RCNN的文件夹,这个就是你项目的ROOT_DIR,后面要用到
我的目录是:E:\AtensorflowDev\Mask_RCNN
该目录下有一个requirements.txt文件,此时还需要运行一下
pip3 install -r requirements.txt
以确保所需要的包都安装好

step2 下载已经训练好的COCO weights (mask_rcnn_coco.h5)
官网(https://github.com/matterport/Mask_RCNN/releases)给出的地址是
https://github.com/matterport/Mask_RCNN/releases/download/v2.1/mask_rcnn_balloon.h5
因为()的原因,下载起来好慢,我下载了10多次,差不多花了一天,如果你运气和我一样差,那就用这个包吧
链接:https://pan.baidu.com/s/12A0E6h-mP4zl9oqoTvj6Ig 密码:4eks

step3 把 mask_rcnn_coco.h5 放置到Mask_RCNN这个目录下面
E:\AtensorflowDev\Mask_RCNN\mask_rcnn_coco.h5

step4 安装coco工具
$ pip install git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI
我的输出界面是这样的

1.(执行命令)
(tensorflow) E:\AtensorflowDev>pip install git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI 
(然后回车)
2.(下面是运行后输出的结果,表示安装成功)
Collecting git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI
  Cloning https://github.com/philferriere/cocoapi.git to c:\users\admini~1\appdata\local\temp\pip-req-build-a4gugnlj
Building wheels for collected packages: pycocotools
  Running setup.py bdist_wheel for pycocotools ... done
  Stored in directory: C:\Users\ADMINI~1\AppData\Local\Temp\pip-ephem-wheel-cache-35xkjtfa\wheels\69\2b\12\2fa959e49f73d26cff202c2f4e5079096c9c57c8a8509fd75c
Successfully built pycocotools
Installing collected packages: pycocotools
Successfully installed pycocotools-2.0

如果你在安装pycocotools这一步碰到困难,请参考后面的pycocotools安装说明。

step5 启动anaconda中的tensor开发环境,选择jupyter notebook,然后在jupyter中upload这个demo,
E:\AtensorflowDev\MaskRCNN\samples\demo.ipynb

最后给出一张本人运行的结果全图,注意包括了一些调试信息,有点啰嗦,还有那个ROOT_DIR路径,因为老是提示ModuleNotFoundError,我干脆写成绝对路径,另外说一下的是,我没有GPU,程序中的那个GPU_COUNT=1仅仅是用来计算每个批次图片的张数(这里就一张)和真实的GPU数量无关

 

pycocotools安装说明

有的人可能会发现在线安装 比较费时间,有时我也是,一连试了好几次,安装不成功,不知道什么问题,止在这里,

(torch) E:\>pip install git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI
Collecting git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI
  Cloning https://github.com/philferriere/cocoapi.git to c:\users\admini~1\appdata\local\temp\pip-req-build-mff3lz_x
...
​
于是我的做法就是直接把包下载下来,本地编译安装,主要步骤就是切换到PythonAPI目录, 然后运行安装指令:
python setup.py install
编译运行之前请检查已经安装了VS2017工具(https://github.com/philferriere/cocoapi 上有说明)和cython。
全部运行过程及内容如下,其中包括VS2017community工具使用的详细信息。(torch) E:\>cd E:\AMaskRCNN\pf-cocoapi-master\PythonAPI
​(torch) E:\AMaskRCNN\pf-cocoapi-master\PythonAPI>dir
 驱动器 E 中的卷是 文档
 卷的序列号是 84xx-xxxx​
 E:\AMaskRCNN\pf-cocoapi-master\PythonAPI 的目录
2018/10/24 周三  下午 04:00    <DIR>          .
2018/10/24 周三  下午 04:00    <DIR>          ..
2018/10/24 周三  下午 04:00    <DIR>          demos
2018/10/24 周三  下午 04:00               199 Makefile
2018/10/24 周三  下午 04:00    <DIR>          pycocotools
2018/10/24 周三  下午 04:00               910 setup.py
               2 个文件          1,109 字节
               4 个目录 213,706,694,656 可用字节
​
(torch) E:\AMaskRCNN\pf-cocoapi-master\PythonAPI>python setup.py install
Compiling pycocotools/_mask.pyx because it changed.
[1/1] Cythonizing pycocotools/_mask.pyx
d:\Anaconda3\envs\torch\lib\site-packages\Cython\Compiler\Main.py:367: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: E:\AMaskRCNN\pf-cocoapi-master\PythonAPI\pycocotools\_mask.pyx
  tree = Parsing.p_module(s, pxd, full_module_name)
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
creating build\temp.win-amd64-3.7
creating build\temp.win-amd64-3.7\Release
creating build\temp.win-amd64-3.7\Release\pycocotools
creating build\temp.win-amd64-3.7\common
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Id:\Anaconda3\envs\torch\lib\site-packages\numpy\core\include -I../common -Id:\Anaconda3\envs\torch\include -Id:\Anaconda3\envs\torch\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\cppwinrt" /Tcpycocotools/_mask.c /Fobuild\temp.win-amd64-3.7\Release\pycocotools/_mask.obj
_mask.c
d:\anaconda3\envs\torch\lib\site-packages\numpy\core\include\numpy\npy_1_7_deprecated_api.h(12) : Warning Msg: Using deprecated NumPy API, disable it by #defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
pycocotools/_mask.c(3271): warning C4244: “=”: 从“Py_ssize_t”转换到“siz”,可能丢失数据
pycocotools/_mask.c(3675): warning C4244: “函数”: 从“npy_intp”转换到“siz”,可能丢失数据
pycocotools/_mask.c(6637): warning C4244: “=”: 从“npy_intp”转换到“siz”,可能丢失数据
pycocotools/_mask.c(7383): warning C4244: “=”: 从“Py_ssize_t”转换到“siz”,可能丢失数据
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Id:\Anaconda3\envs\torch\lib\site-packages\numpy\core\include -I../common -Id:\Anaconda3\envs\torch\include -Id:\Anaconda3\envs\torch\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\cppwinrt" /Tc../common/maskApi.c /Fobuild\temp.win-amd64-3.7\Release\../common/maskApi.obj
maskApi.c
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:d:\Anaconda3\envs\torch\libs /LIBPATH:d:\Anaconda3\envs\torch\PCbuild\amd64 "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\ATLMFC\lib\x64" "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\lib\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.17763.0\ucrt\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.17763.0\um\x64" /EXPORT:PyInit__mask build\temp.win-amd64-3.7\Release\pycocotools/_mask.obj build\temp.win-amd64-3.7\Release\../common/maskApi.obj /OUT:build\lib.win-amd64-3.7\pycocotools\_mask.cp37-win_amd64.pyd /IMPLIB:build\temp.win-amd64-3.7\Release\pycocotools\_mask.cp37-win_amd64.lib
  正在创建库 build\temp.win-amd64-3.7\Release\pycocotools\_mask.cp37-win_amd64.lib 和对象 build\temp.win-amd64-3.7\Release\pycocotools\_mask.cp37-win_amd64.exp
正在生成代码
已完成代码的生成
running install_lib
creating d:\Anaconda3\envs\torch\Lib\site-packages\pycocotools
copying build\lib.win-amd64-3.7\pycocotools\coco.py -> d:\Anaconda3\envs\torch\Lib\site-packages\pycocotools
copying build\lib.win-amd64-3.7\pycocotools\cocoeval.py -> d:\Anaconda3\envs\torch\Lib\site-packages\pycocotools
copying build\lib.win-amd64-3.7\pycocotools\mask.py -> d:\Anaconda3\envs\torch\Lib\site-packages\pycocotools
copying build\lib.win-amd64-3.7\pycocotools\_mask.cp37-win_amd64.pyd -> d:\Anaconda3\envs\torch\Lib\site-packages\pycocotools
copying build\lib.win-amd64-3.7\pycocotools\__init__.py -> d:\Anaconda3\envs\torch\Lib\site-packages\pycocotools
byte-compiling d:\Anaconda3\envs\torch\Lib\site-packages\pycocotools\coco.py to coco.cpython-37.pyc
byte-compiling d:\Anaconda3\envs\torch\Lib\site-packages\pycocotools\cocoeval.py to cocoeval.cpython-37.pyc
byte-compiling d:\Anaconda3\envs\torch\Lib\site-packages\pycocotools\mask.py to mask.cpython-37.pyc
byte-compiling d:\Anaconda3\envs\torch\Lib\site-packages\pycocotools\__init__.py to __init__.cpython-37.pyc
running install_egg_info
Writing d:\Anaconda3\envs\torch\Lib\site-packages\pycocotools-2.0-py3.7.egg-info
​
(torch) E:\AMaskRCNN\pf-cocoapi-master\PythonAPI>

参考资料

Object Detection and Segmentation in Python with Mask-RCNN
http://bradsliz.com/2017-11-06-object-segmentation/
https://github.com/philferriere/cocoapi
https://github.com/matterport/Mask_RCNN
https://github.com/waleedka/coco

AI视像算法学习群: 824991413


 

posted @ 2018-09-24 14:12  SpaceVision  阅读(60)  评论(0编辑  收藏  举报