使用opencv dnn 模块调用darknet模型时候出错,不支持relu激活函数
问题:使用opencv dnn 模块调用darknet模型时候出错,报错信息为 不支持relu激活函数以下过程为笔者自己解决该问题的过程,供各位参考学些,因为中间又遇到新的坑,所以各位务必看完再决定如何操作执行。cv2.dnn.readNetFromDarknet error: (-212:Parsing error)Unsupported activation: relu in function
问题:
使用opencv dnn 模块调用darknet模型时候出错,报错信息为 不支持relu激活函数
以下过程为笔者自己解决该问题的过程,供各位参考学些,因为中间又遇到新的坑,所以各位务必看完再决定如何操作执行。
cv2.dnn.readNetFromDarknet error: (-212:Parsing error)
Unsupported activation: relu in function
'cv::dnn::darknet::ReadDarknetFromCfgStream'
解决办法:
优化opencv,使得opencv支持 CNN “relu” 和 maxpool “VALID”。
complie
get opencv
#wget https://codeload.github.com/opencv/opencv/zip/4.0.1
wget http://www.chineseocr.com:9990/static/models/opencv/opencv.4.0.1.zip
注意:使用wget下载不下来,最终使用迅雷可以有效下载,手动上传到服务器,执行后续命令。
而darknet_io.cpp、layer_common.cpp、all_layers.hpp需要从github下载,解压后处理
unzip opencv.4.0.1.zip &&
cp darknet_io.cpp opencv-4.0.1/modules/dnn/src/darknet &&
cp layers_common.cpp opencv-4.0.1/modules/dnn/src/layers &&
cp all_layers.hpp opencv-4.0.1/modules/dnn/include/opencv2/dnn/
------------------------------------------------------------------------------------
编译
mac下:
cd opencv-4.0.1 && mkdir build && cd build && cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D PYTHON3_EXCUTABLE=~/anaconda3/envs/chineseocr/bin/python3 \
-D PYTHON3_INCLUDE_DIR=~/anaconda3/envs/chineseocr/include/python3.6m \
-D PYTHON3_LIBRARY=~/anaconda3/envs/opencv/lib/libpython3.6m.dylib \
-D PYTHON_NUMPY_PATH=~/anaconda3/envs/opencv/lib/python3.6/site-packages/numpy/core/include .. \
&& make -j2 && make install
ubuntu下:
需要先cd到opencv中的cmake目录下再执行编译
cd opencv-4.0.1/cmake/
insightzen@master:/opt/OCR_darknet/opencv-4.0.1/cmake$ pwd
/opt/OCR_darknet/opencv-4.0.1/cmake
------------------------------------------------------------------------------------
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D PYTHON3_EXCUTABLE=/usr/bin/python \
-D PYTHON3_INCLUDE_DIR=/usr/include/python3.6m \
-D PYTHON3_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.so \
-D PYTHON_NUMPY_PATH=/usr/local/lib/python3.6/dist-packages/numpy/ .. \
&& make -j2 && make install
------------------------------------------------------------------------------------
编译遇到如下错误:
/opt/OCR_darknet/opencv-4.0.1/modules/python/src2/cv2.cpp:9:20: fatal error: Python.h: No such file or directory
compilation terminated.
modules/python3/CMakeFiles/opencv_python3.dir/build.make:81: recipe for target 'modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o' failed
make[2]: *** [modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o] Error 1
CMakeFiles/Makefile2:5466: recipe for target 'modules/python3/CMakeFiles/opencv_python3.dir/all' failed
make[1]: *** [modules/python3/CMakeFiles/opencv_python3.dir/all] Error 2
上stackoverflow、以及github对应包的issue部分查找得到:
I fixed the issue. The problem was that PYTHON3_INCLUDE_DIR was set to
~/anaconda3/include while Anaconda's Python.h is actually inside
~/anaconda3/include/python3.5m.
Setting PYTHON3_INCLUDE_DIR=anaconda3/include/python3.5m/
fixes this issue as it now sees Python.h
------------------------------------------------------------------------------------
错误已经解决, 已经重新从零开始编译, 另外, 用ubuntu的记得cmake之前加上
mkdir build && cd build
作者没写这个,加上我也没看mac的命令导致我被坑了好久
另外,贴上自己的cmake指令
注意:结合自己的环境的路径特点进行对应修改........
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/home/ubuntu/anaconda3/envs/sparkPy37 \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D PYTHON3_EXECUTABLE=/home/insightzen/anaconda3/envs/sparkPy37/bin/python3.7 \
-D PYTHON3_INCLUDE_PATH=/home/insightzen/anaconda3/include/python3.7m \
-D PYTHON3_LIBRARIES=/home/insightzen/anaconda3/envs/sparkPy37/lib/libpython3.7m.so \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/home/insightzen/anaconda3/envs/sparkPy37/lib/python3.7/site-packages/numpy/core/include .. \
&& make -j2 && sudo make install
------------------------------------------------------------------------------
编译中遇到权限问题:
insightzen is not in the sudoers file. This incident will be reported.
su root
chmod u+w /etc/sudoers
su insightzen
-----------------------------------------------------------------------
接着是这个问题:
-- Install configuration: "RELEASE"
CMake Error at cmake_install.cmake:46 (file):
file cannot create directory: /usr/local/share/licenses/opencv4. Maybe
need administrative privileges.
手动建目录:
su root
mkdir /usr/local/share/licenses/opencv4
-----------------------------------------------------------------------
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D PYTHON3_EXECUTABLE=/usr/bin/python \
-D PYTHON3_INCLUDE_PATH=/usr/include/python3.6m \
-D PYTHON3_LIBRARIES=/usr/lib/x86_64-linux-gnu/libpython3.6m.so \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.6/dist-packages/numpy/core/include .. \
&& make -j2 && sudo make install
-----------------------------------------------------------------------
XXX is not in the sudoers file. This incident will be reported.解决办法
在用sudo时提示"xxx is not in the sudoers file. This incident will be reported.其中XXX是你的用户名,也就是你的用户名没有权限使用sudo,我们只要修改一下/etc/sudoers文件就行了。
修改方法:
1)进入超级用户模式。也就是输入"su -",系统会让你输入超级用户密码,输入密码后就进入了超级用户模式。(当然,你也可以直接用root用)
2)添加文件的写权限。也就是输入命令"chmod u+w /etc/sudoers"。
3)编辑/etc/sudoers文件。也就是输入命令"vim /etc/sudoers",输入"i"进入编辑模式,找到这一 行:"root ALL=(ALL) ALL"在起下面添加"xxx ALL=(ALL) ALL"(这里的xxx是你的用户名),然后保存(就是先按一 下Esc键,然后输入":wq")退出。
4)撤销文件的写权限。也就是输入命令"chmod u-w /etc/sudoers"。(如果不撤销的话会提示sudo: /etc/sudoers is mode 0640, should be 0440)
-----------------------------------------------------------------------------------
虽然没有报错,但是没有对应激活函数的问题依旧存在,所以继续查看github issues
------------------------------------------------------------------------------------
前面老不成功,折腾了挺久
PYTHON3_EXCUTABLE 少了个E 应该是PYTHON3_EXECUTABLE
PYTHON3_INCLUDE_DIR 貌似应该是 PYTHON3_INCLUDE_PATH
PYTHON3_LIBRARY 改成了 PYTHON3_LIBRARIES
PYTHON_NUMPY_PATH 改成了 PYTHON3_NUMPY_INCLUDE_DIRS 然后要到 /numpy/core/include这一级
另外贴一下调整后的 conda虚拟环境的
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=~/miniconda3/envs/[虚拟环境名称] \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D PYTHON3_EXECUTABLE=~/miniconda3/envs/[虚拟环境名称]/bin/python3.6 \
-D PYTHON3_INCLUDE_PATH=~/miniconda3/envs/[虚拟环境名称]/include/python3.6m \
-D PYTHON3_LIBRARIES=~/miniconda3/envs/[虚拟环境名称]/lib/libpython3.6m.so \
-D PYTHON3_NUMPY_INCLUDE_DIRS=~/miniconda3/envs/[虚拟环境名称]/lib/python3.6/site-packages/numpy/core/include .. \
&& make -j2 && sudo make install
-----------------------------------------------------------------------------------
本地环境最终版:
cd opencv-4.0.1
mkdir build && cd build
然后编译:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/home/insightzen/anaconda3/envs/sparkPy37 \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D PYTHON3_EXECUTABLE=/home/insightzen/anaconda3/envs/sparkPy37/bin/python3.7 \
-D PYTHON3_INCLUDE_PATH=/home/insightzen/anaconda3/include/python3.7m \
-D PYTHON3_LIBRARIES=/home/insightzen/anaconda3/envs/sparkPy37/lib/libpython3.7m.so \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/home/insightzen/anaconda3/envs/sparkPy37/lib/python3.7/site-packages/numpy/core/include .. \
&& make -j2 && sudo make install
参考:chineseocr/opencv-for-darknet
参考:opencv+github issue list + stackoverflow
更多推荐
所有评论(0)