文章目录
1. 安装opencv
opencv只是用于读取图片的(参考网上),所以不需要安装cuda版的,不然后有很多问题。
(1) 从官网(http://opencv.org/downloads.html)下载Opencv,并将其解压到你要安装的位置,假设解压到了/home/opencv。
-
unzip opencv-3.2.0.zip
-
将pencv-3.2.0改为opencv
-
(2) 安装前准备,创建编译文件夹
cd ~/opencv
mkdir build
cd build
(3) 配置
sudo apt install cmake
sudo cmake -D WITH_CUDA=OFF -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local …
WITH_CUDA=OFF
表示不安装gpu版
(4) 编译
sudo make -j8 (-j8表示并行计算,根据自己电脑的配置进行设置,配置比较低的电脑可以将数字改小或不使用,直接输make)
(5) 安装
sudo make install # 安装
(6) 看opencv的版本
pkg-config --modversion opencv
可能错误:
-
下载ippicv_linux_20151201.tgz的时候超时,请参考:ICV: Downloading ippicv_linux_20151201.tgz 超时
-
"libopencv_core.so.3.2: cannot open shared object file: No such file or direct,请参考
OpenCV runtime error: "libopencv_core.so.3.2: cannot open shared object file: No such file or direct
-
运行编译的文件时出现:
test.cpp:(.text+0x46):对‘cv::imread(cv::String const&, int)’未定义的引用
test.cpp:(.text+0xbe):对‘cv::imshow(cv::String const&, cv::_InputArray const&)’未定义的引用
test.cpp:(.text+0xe6):对‘cv::waitKey(int)’未定义的引用
/tmp/ccSDVP3R.o:在函数‘cv::String::String(char const*)’中:
test.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x4d):对‘cv::String::allocate(unsigned long)’未定义的引用
/tmp/ccSDVP3R.o:在函数‘cv::String::~String()’中:
test.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14):对‘cv::String::deallocate()’未定义的引用
/tmp/ccSDVP3R.o:在函数‘cv::String::operator=(cv::String const&)’中:
test.cpp:(.text.
ZN2cv6StringaSERKS0
[
ZN2cv6StringaSERKS0
]+0x28):对‘cv::String::deallocate()’未定义的引用
/tmp/ccSDVP3R.o:在函数‘cv::Mat::~Mat()’中:
test.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39):对‘cv::fastFree(void*)’未定义的引用
/tmp/ccSDVP3R.o:在函数‘cv::Mat::release()’中:
test.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x4b):对‘cv::Mat::deallocate()’未定义的引用
collect2: error: ld returned 1 exit status
-
编译的时候用下面的命令:
g++ opencvtest.cpp -o opencvtest `pkg-config --cflags --libs opencv`
可以正常显示图片则表面安装成功了
2. 测试opencv
// 文件名保存为opencvtest.cpp,采用上面的方式进行编译,读取一张图片,并进行显示,能正常显示图片就可以
using namespace cv;
using namespace std;
int main()
{
Mat img = imread("dazu.jpg");
if(img.empty())
{
cout<<"error";
return -1;
}
imshow("mypic",img);
waitKey();
return 0;
}
3. 安装caffe
-
安装相关依赖项
-
1 sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
2 sudo apt-get install --no-install-recommends libboost-all-dev
3 sudo apt-get install libopenblas-dev liblapack-dev libatlas-base-dev
4 sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
-
下载caffe
git clone https://github.com/BVLC/caffe.git
-
修改Makefile.config
-
将 Makefile.config.example 文件复制一份并更名为 Makefile.config ,也可以在 caffe 目录下直接调用以下命令完成复制操作 :
sudo cp Makefile.config.example Makefile.config
复制一份的原因是编译 caffe 时需要的是 Makefile.config 文件,而Makefile.config.example 只是caffe 给出的配置文件例子,不能用来编译 caffe。
然后修改 Makefile.config 文件,在 caffe 目录下打开该文件:
sudo gedit Makefile.config
修改 Makefile.config 文件内容:
1.应用 cudnn
将
#USE_CUDNN := 1
修改成:
USE_CUDNN := 1
2.应用 opencv 版本
将
#OPENCV_VERSION := 3
修改为:
OPENCV_VERSION := 3
3.使用 python 接口
将
#WITH_PYTHON_LAYER := 1
修改为
WITH_PYTHON_LAYER := 1
4.修改 python 路径
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
修改为:
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 /usr/lib/x86_64-linux-gnu/hdf5/serial
5.然后修改 caffe 目录下的 Makefile 文件:
将:
NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)
替换为:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
将:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
改为:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
6.然后修改 /usr/local/cuda/include/host_config.h 文件 :(cuda9.0没有)
将
#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!
OK ,可以开始编译了,在 caffe 目录下执行 :
make all -j8
这是如果之前的配置或安装出错,那么编译就会出现各种各样的问题,所以前面的步骤一定要细心。
4. 遇到的问题
-
libpng16.so.16:对‘inflateValidate@ZLIB_1.2.9’未定义的引用
1)GitHub上的解决方案,链接:https://github.com/BVLC/caffe/issues/6139
在Makefile.config文件中添加如下一行:
-
LINKFLAGS := -Wl,-rpath,$(HOME)/anaconda3/lib
-
编译通过
-
2)fatal error: numpy/arrayobject.h: 没有那个文件或目录
解决:
sudo apt-get install python-numpy
3)python中import caffe,报错:ModuleNotFoundError: No module named ‘caffe’
解决办法:
vi ~/.bashrc 添加
export PYTHONPATH=/xx/xx/caffe/python:$PYTHONPATH 然后source ~/.bashrc
4)ImportError: dynamic module does not define module export function (PyInit__caffe)
-
caffe支持python2.7,所以在终端中运行
python2.7
-
5)ImportError: /lib/x86_64-linux-gnu/libz.so.1: version `ZLIB_1.2.9’ not found (required by /home/deepblue/anaconda3/lib/libpng16.so.16)
zlib的版本不对,重装以下zlib
1 git clone --branch v1.2.9 https://github.com/madler/zlib
2 cd path/to/zlib
3 ./configure
4 make
5 make install
6)ImportError: No module named skimage.io
sudo apt-get install python-skimage
7)ImportError: No module named google.protobuf.internal
protobuf安装的问题
-
首先通过
which python
确定用的默认的python2.7还是conda中的python
-
caffe
好像
只支持python2.7,所以需要将默认的python设置成
usr/bin/python2.7
-
8) libopencv_imgcodecs.so: undefined reference to `inflateValidate@ZLIB_1.2.9’
collect2: error: ld returned 1 exit status
安装了
zlib1g_1.2.11就OK了。
具体问题:
/usr/local/lib/libopencv_imgcodecs.so: undefined reference to `inflateValidate@ZLIB_1.2.9'
collect2: error: ld returned 1 exit status
Makefile:619: recipe for target '.build_release/tools/upgrade_net_proto_binary.bin' failed
make: *** [.build_release/tools/upgrade_net_proto_binary.bin] Error 1
make: *** Waiting for unfinished jobs....
/usr/local/lib/libopencv_imgcodecs.so: undefined reference to `inflateValidate@ZLIB_1.2.9'
collect2: error: ld returned 1 exit status
Makefile:619: recipe for target '.build_release/tools/convert_annoset.bin' failed
make: *** [.build_release/tools/convert_annoset.bin] Error 1
/usr/local/lib/libopencv_imgcodecs.so: undefined reference to `inflateValidate@ZLIB_1.2.9'
collect2: error: ld returned 1 exit status
Makefile:619: recipe for target '.build_release/tools/create_label_map.bin' failed
make: *** [.build_release/tools/create_label_map.bin] Error 1
/usr/local/lib/libopencv_imgcodecs.so: undefined reference to `inflateValidate@ZLIB_1.2.9'
collect2: error: ld returned 1 exit status
Makefile:619: recipe for target '.build_release/tools/caffe.bin' failed
make: *** [.build_release/tools/caffe.bin] Error 1
/usr/local/lib/libopencv_imgcodecs.so: undefined reference to `inflateValidate@ZLIB_1.2.9'
collect2: error: ld returned 1 exit status
Makefile:619: recipe for target '.build_release/tools/get_image_size.bin' failed
make: *** [.build_release/tools/get_image_size.bin] Error 1
/usr/local/lib/libopencv_imgcodecs.so: undefined reference to `inflateValidate@ZLIB_1.2.9'
collect2: error: ld returned 1 exit status
Makefile:619: recipe for target '.build_release/tools/upgrade_solver_proto_text.bin' failed
make: *** [.build_release/tools/upgrade_solver_proto_text.bin] Error 1
/usr/local/lib/libopencv_imgcodecs.so: undefined reference to `inflateValidate@ZLIB_1.2.9'
collect2: error: ld returned 1 exit status
Makefile:619: recipe for target '.build_release/tools/convert_imageset.bin' failed
make: *** [.build_release/tools/convert_imageset.bin] Error 1
5. 测试
-
import caffe
-
reference
-
Ubuntu 16.04下安装Caffe(GPU版本 GTX970
-