5.安装opencv3.4.1
安装之前需要安装cmake
安装cmake: sudo apt-get install cmake
1.下载opencv
下载地址:https://github.com/opencv/opencv
2.下载之后解压安装包
解压:unzip opencv-3.4.1.zip
3.在当前目录下创建一个新文件夹用于编译opencv
创建文件夹:mkdir opencv3.4_build
4.进入刚刚创建的文件夹里面
进入文件夹:cd opencv_build
5.编译opencv ,如果没有安装cuda,用命令1进行cmake,如果需要支持cuda加速,用命令2进行cmake
编译:
5.1 没有安装cuda可以不开cuda进行cmake
cmake ../opencv-3.4.1 -DWITH_GTK_2_X=ON -DCMAKE_INSTALL_PREFIX=/usr/local
5.2 安装了cuda可以使用支持cuda加速命令进行cmake(可以直接复制)
cmake ../opencv-3.4.1 -DWITH_GTK_2_X=ON -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local -DWITH_TBB=ON -DBUILD_NEW_PYTHON_SUPPORT=ON -DWITH_V4L=ON -DINSTALL_C_EXAMPLES=ON -DINSTALL_PYTHON_EXAMPLES=ON -DBUILD_EXAMPLES=ON -DWITH_OPENGL=ON -DENABLE_FAST_MATH=1 -DCUDA_FAST_MATH=1 -DWITH_CUBLAS=1 -DWITH_OPENMP=ON
编译opencv时碰到如下是错误:
driver_api_multi.cpp:(.text._ZNK6WorkerclEi+0x893): undefined reference to `cuCtxPopCurrent_v2'
CMakeFiles/example_gpu_driver_api_multi.dir/driver_api_multi.cpp.o: In function `main':
driver_api_multi.cpp:(.text.startup.main+0x11c): undefined reference to `cuInit'
driver_api_multi.cpp:(.text.startup.main+0x133): undefined reference to `cuDeviceGet'
driver_api_multi.cpp:(.text.startup.main+0x14d): undefined reference to `cuCtxCreate_v2'
driver_api_multi.cpp:(.text.startup.main+0x15d): undefined reference to `cuCtxPopCurrent_v2'
driver_api_multi.cpp:(.text.startup.main+0x172): undefined reference to `cuDeviceGet'
driver_api_multi.cpp:(.text.startup.main+0x18c): undefined reference to `cuCtxCreate_v2'
driver_api_multi.cpp:(.text.startup.main+0x19c): undefined reference to `cuCtxPopCurrent_v2'
collect2: error: ld returned 1 exit status
samples/gpu/CMakeFiles/example_gpu_driver_api_multi.dir/build.make:125: recipe for target 'bin/example_gpu_driver_api_multi' failed
make[2]: *** [bin/example_gpu_driver_api_multi] Error 1
CMakeFiles/Makefile2:39358: recipe for target 'samples/gpu/CMakeFiles/example_gpu_driver_api_multi.dir/all' failed
make[1]: *** [samples/gpu/CMakeFiles/example_gpu_driver_api_multi.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
原因是找不到cuda库,解决方式如下(红色为新添加)
/opencv3.41/samples/gpu/CMakeLists.txt
(大概54行左右)
foreach(sample_filename ${all_samples})
ocv_define_sample(tgt ${sample_filename} gpu)
ocv_target_link_libraries(${tgt} ${OPENCV_LINKER_LIBS} ${OPENCV_CUDA_SAMPLES_REQUIRED_DEPS})
if(HAVE_CUDA AND NOT ANDROID)
ocv_target_link_libraries(${tgt} ${CUDA_CUDA_LIBRARY})
endif()
if(HAVE_opencv_xfeatures2d)
ocv_target_link_libraries(${tgt} opencv_xfeatures2d)
endif()
6.cmake成功之后进行编译
编译opencv:make -j8 (j8表示使用8个线程进行编译)
6.安装opencv
安装:sudo make install
7.至此,opencv已经安装完成,可能用到其他的一些命令:
1.查看opencv版本:pkg-config --modversion opencv
2. 配置opencv环境(可选)(
1):echo '/usr/local/lib' | sudo tee -a /etc/ld.so.conf.d/opencv.conf
2): sudo ldconfig
(3):printf'#OpenCV\nPKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig\nexport PKG_CONFIG_PATH\n' >> ~/.bashrc
4) : source ~/.bashrc
6 配置FASTER-RCNN Caffe
(1) 安装相关依赖
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libopenblas-dev liblapack-dev libatlas-base-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
(2)Caffe 中常用的molu:
pip install cython
pip install easydict
pip install python-opencv
1)拉取faster rcnn代码
git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git
- 1
2)cd到lib目录,生成cython
-
cd py-faster-rcnn/lib
-
make -j8
3)修改Makefile.config文件
-
cp Makefile.config.example Makefile.config
-
sudo gedit Makefile.config
进行Makefile和Makefile.config的修改
内容附上:
Makefile.config::
## Refer to http://caffe.berkeleyvision.org/installation.html
# Contributions simplifying and improving our build system are welcome!
# cuDNN acceleration switch (uncomment to build with cuDNN).
USE_CUDNN:=1
# CPU-only switch (uncomment to build without GPU support).
# CPU_ONLY := 1
# uncomment to disable IO dependencies and corresponding data layers
# USE_OPENCV := 0
# USE_LEVELDB := 0
# USE_LMDB := 0
# uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
# You should not set this flag if you will be reading LMDBs with any
# possibility of simultaneous read and write
# ALLOW_LMDB_NOLOCK := 1
# Uncomment if you're using OpenCV 3
OPENCV_VERSION := 3
# To customize your choice of compiler, uncomment and set the following.
# N.B. the default for Linux is g++ and the default for OSX is clang++
# CUSTOM_CXX := g++
# CUDA directory contains bin/ and lib/ directories that we need.
CUDA_DIR := /usr/local/cuda
# On Ubuntu 14.04, if cuda tools are installed via
# "sudo apt-get install nvidia-cuda-toolkit" then use this instead:
# CUDA_DIR := /usr
# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
# For CUDA >= 9.0, comment the *_20 and *_21 lines for compatibility.
CUDA_ARCH := -gencode arch=compute_30,code=sm_30 \
-gencode arch=compute_30,code=sm_30 \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_50,code=sm_50 \
-gencode arch=compute_52,code=sm_52 \
-gencode arch=compute_60,code=sm_60 \
-gencode arch=compute_61,code=sm_61 \
-gencode arch=compute_61,code=compute_61
# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := atlas
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)!
# BLAS_INCLUDE := /path/to/your/blas
# BLAS_LIB := /path/to/your/blas
# Homebrew puts openblas in a directory that is not on the standard search path
# BLAS_INCLUDE := $(shell brew --prefix openblas)/include
# BLAS_LIB := $(shell brew --prefix openblas)/lib
# This is required only if you will compile the matlab interface.
# MATLAB directory should contain the mex binary in /bin.
# MATLAB_DIR := /usr/local
# MATLAB_DIR := /Applications/MATLAB_R2012b.app
# NOTE: this is required only if you will compile the python interface.
# We need to be able to find Python.h and numpy/arrayobject.h.
PYTHON_INCLUDE := /usr/include/python2.7 \
/usr/lib/python2.7/dist-packages/numpy/core/include
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
#ANACONDA_HOME := $(HOME)/anaconda2
#PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
# $(ANACONDA_HOME)/include/python2.7 \
# $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include\
# /usr/include/python2.7
LIBRARIES += glog gflags protobuf leveldb snappy \
lmdb boost_system hdf5_h1 hdf5 m\
opencv_core opencv_highhui opencv_imgproc opencv_imgcodes opencv_videoio
# Uncomment to use Python 3 (default is Python 2)
# PYTHON_LIBRARIES := boost_python3 python3.5m
# PYTHON_INCLUDE := /usr/include/python3.5m \
# /usr/lib/python3.5/dist-packages/numpy/core/include
# We need to be able to find libpythonX.X.so or .dylib.
PYTHON_LIB := /usr/lib
#PYTHON_LIB := $(ANACONDA_HOME)/lib
# Homebrew installs numpy in a non standard path (keg only)
# PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include
# PYTHON_LIB += $(shell brew --prefix numpy)/lib
# Uncomment to support layers written in Python (will link against Python libs)
WITH_PYTHON_LAYER := 1
# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial
# If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies
# INCLUDE_DIRS += $(shell brew --prefix)/include
# LIBRARY_DIRS += $(shell brew --prefix)/lib
# NCCL acceleration switch (uncomment to build with NCCL)
# https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)
# USE_NCCL := 1
# Uncomment to use `pkg-config` to specify OpenCV library paths.
# (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)
# USE_PKG_CONFIG := 1
# N.B. both build and distribute dirs are cleared on `make clean`
BUILD_DIR := build
DISTRIBUTE_DIR := distribute
# Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171
# DEBUG := 1
# The ID of the GPU that 'make runtest' will use to run unit tests.
TEST_GPUID := 0
# enable pretty build (comment to see full commands)
Q ?= @
Makefile:
修改makefile文件 打开makefile文件,做如下修改:
将: NVCCFLAGS +=-ccbin=$(CXX)-Xcompiler-fPIC $(COMMON_FLAGS)
替换为: NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
(5)编辑/usr/local/cuda/include/host_config.h
将其中的第115行注释掉: 将
# error-- unsupported GNU version! gcc versions later than 4.9 are not supported!
改为 //#error-- unsupported GNU version! gcc versions later than 4.9 are not supported!
4)替换cudnn文件
1).将/py-faster-rcnn/caffe-fast-rcnn/include/caffe/util/cudnn.hpp 换成最新版的caffe里的cudnn的实现,即相应的cudnn.hpp.
3).将/py-faster-rcnn/caffe-fast-rcnn/include/caffe/layer里的,所有以cudnn开头的文件都替换成最新版的caffe里的相应的同名文件
3).将/py-faster-rcnn/caffe-fast-rcnn/src/caffe/layer里的,所有以cudnn开头的文件都替换成最新版的caffe里的相应的同名文件
5)编译pycaffe
-
cd ~/caffe
-
make all -j8
-
make test -j8
-
make runtest -j8
make test -j8:
Error 2 :
src/caffe/test/test_smooth_L1_loss_layer.cpp:11:35: fatal error: caffe/vision_layers.hpp: No such file or directory
compilation terminated.
Makefile:563: recipe for target '.build_release/src/caffe/test/test_smooth_L1_loss_layer.o' failed
make: *** [.build_release/src/caffe/test/test_smooth_L1_loss_layer.o] Error 1
make: *** Waiting for unfinished jobs....
src/caffe/test/test_smooth_L1_loss_layer.cpp:11:35: fatal error: caffe/vision_layers.hpp: No such file or directory
解决方法:
找到文件 /home/xmart/py-faster-rcnn/caffe-fast-rcnn/src/caffe/test/test_smooth_L1_loss_layer.cpp
删除第十一行
11 #include "caffe/vision_layers.hpp"
(2)Caffe 中常用的molu:
pip install cython
pip install easydict
pip install python-opencv
pip install scikit-image
pip install protobuf
呃呃呃呃呃呃,这里需要什么安装什么模型就好
-
cd $FRCN_ROOT
-
./data/scripts/fetch_faster_rcnn_models.sh
2、从ImageNet训练来的Caffe models (ZF, VGG16) pre-trained 模型下载命令(在SCRIPTS文件下包含下载的脚本,如果遇到错误一定是服务器上翻墙的问题)
./data/scripts/fetch_imagenet_models.sh
3、 从VOC 2007训练来的Faster R-CNN models trained 模型下载命令(同上)
./data/scripts/fetch_faster_rcnn_models.sh
4、设置好以上下载之后,我们的./data目录下会出现需要的模型:
四、跑通demo.py文件:
上面已经编译好了caffe并且下载做好了训练好的模型何必要数据:我们现在可以开始跑demo了:
-
cd $FRCN_ROOT
-
./tools/demo.py
跑通之后我们可以看到自己预测的图片的目标框: