电脑配置:ubuntu10+cuda 10+opencv3
声明:要先装单独opencv3,编译caffe时候 makeconfig里有这个选项
注1:工业界用C++版本的居多,也就是说pycaffe和matlab都只是可选项
注2:安装opencv3方法:https://blog.youkuaiyun.com/qq_40234695/article/details/88669618
注3:CUDA7.5+OpenCV3.1 ,其余版本尽量对应
Clion配置opencv3见最后
一、安装方法
0.参考caffe官网
1.不借助ancaconda:
https://blog.youkuaiyun.com/CAU_Ayao/article/details/84000151
2.借助anaconda:
https://blog.youkuaiyun.com/CAU_Ayao/article/details/80578600
ubuntu16的可以参考这两个:
https://blog.youkuaiyun.com/u010193446/article/details/53259294
https://blog.youkuaiyun.com/u011021773/article/details/81298666
上面几个帖子都写的很好,因为caffe的安装过程大体相同,就不再赘述,下面主要说一下容易遇到的问题。
注1:如果借助anaconda,就创建一个python2.7的环境,在caffe的makeconfig文件中导入这个2.7版本的caffe,然后安装编译caffe,再装一下spyder ,就可以在spyder里用caffe了。但还是建议不要使用anaconda
注2:如果是学习一些经典教程,如ssd模型,教程上是在终端运行的,每次运行终端命令前都要激活创建caffe的那个环境。
二、踩坑记录
1.修改config时候,可能要选择BLAS := atlas 根据你安装的情况选
2.安装pycaffe最好使用python2,在终端下打开python ,如果弹出来的是python2那就没毛病,如果弹出来的是python3,则需要把默认的python调整为python2。另外,安装了Anaconda后,默认会使用它自带的python3,把环境信息那里anaconda 那一栏(conda initialize )先注释。
接下来把系统的默认python设置为python2,参考这个(ubuntu自带的python最好别删,否则系统可能容易崩溃)
https://www.cnblogs.com/Yanfang20180701/p/10588087.html
3.fatal error: caffe/proto/caffe.pb.h: No such file or directory
进入到CAFFE_ROOT/src/caffe/proto/目录:
protoc --cpp_out=CAFFE_ROOT/include/caffe/ caffe.proto
4…build_release/src/caffe/proto/caffe.pb.h:23:35: fatal error: google/protobuf/arena.h: 没有那个文件
安装anaconda时候把proto版本更新了,所以还是避免安装anaconda
3.编译完pycaffe时候没有报错,但是在import caffe出现
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named caffe
是因为没有把caffe里面的python导入解释器中:
gedit ~/.bashrc
export PYTHONPATH=/***/***/caffe/python:$PYTHONPATH#写的是caffe主目录里python的位置
#可以自行进入自己的caffe文件夹里的python目录下,输入pwd查看路径。
source ~/.bashrc
4.make runtest如果报错一般是makefile.config和makefile文件有问题,重新检查一下在caffe目录下make clean重新编译
5.caffe安装完毕,想运行官网的mnist教程,下载数据集时候本应该在终端运行“./data/mnist/get_mnist.sh”,结果出现“bash: ./data/mnist/get_mnist.sh: 权限不够”的问题
是因为编译好的caffe没有写入权限,运行:sudo chmod -R 777 ~/caffe-master/即可
6.训练ssd的时候出现:Check failed: a <= b <0 vs -1.19209e-007>
网上办法是注释掉 CHECK_LE(a, b),但如果注释掉 CHECK_LE(a, b) 会出现Data layer prefetch queue empty
解决办法修改src/caffe/util/sampler.cpp,如下面修改代码所示//renew注释下,加入两个判断,使得bbox长宽不要越界。
void SampleBBox(const Sampler& sampler, NormalizedBBox* sampled_bbox) {
// Get random scale.
CHECK_GE(sampler.max_scale(), sampler.min_scale());
CHECK_GT(sampler.min_scale(), 0.);
CHECK_LE(sampler.max_scale(), 1.);
float scale;
caffe_rng_uniform(1, sampler.min_scale(), sampler.max_scale(), &scale);
// Get random aspect ratio.
CHECK_GE(sampler.max_aspect_ratio(), sampler.min_aspect_ratio());
CHECK_GT(sampler.min_aspect_ratio(), 0.);
CHECK_LT(sampler.max_aspect_ratio(), FLT_MAX);
float aspect_ratio;
caffe_rng_uniform(1, sampler.min_aspect_ratio(), sampler.max_aspect_ratio(),
&aspect_ratio);
aspect_ratio = std::max<float>(aspect_ratio, std::pow(scale, 2.));
aspect_ratio = std::min<float>(aspect_ratio, 1 / std::pow(scale, 2.));
// Figure out bbox dimension.
float bbox_width = scale * sqrt(aspect_ratio);
float bbox_height = scale / sqrt(aspect_ratio);
//renew
if(bbox_width>=1.0)
{
bbox_width=1.0;
}
if(bbox_height>=1.0)
{
bbox_height=1.0;
}
// Figure out top left coordinates.
float w_off, h_off;
caffe_rng_uniform(1, 0.f, 1 - bbox_width, &w_off);
caffe_rng_uniform(1, 0.f, 1 - bbox_height, &h_off);
sampled_bbox->set_xmin(w_off);
sampled_bbox->set_ymin(h_off);
sampled_bbox->set_xmax(w_off + bbox_width);
sampled_bbox->set_ymax(h_off + bbox_height);
}
注:如果只是安装C++的caffe,下载好官网的caffe-master后,删掉原来的build文件夹再新建一个build空文件夹进入,在终端上输入cmake…就行
三、caffe网络可视化
1.http://ethereon.github.io/netscope/#/editor
Netscope是个支持prototxt格式描述的神经网络结构的在线可视工具,将文件内容复制进来就可以了
2.https://blog.youkuaiyun.com/baidu_40840693/article/details/85228597
四、推荐caffe入门学习的小网站:
1.https://www.cnblogs.com/denny402/category/759199.html
2.学习SSD的:https://www.cnblogs.com/objectDetect/p/5780006.html#3764116
五、附件
1.如果要安装支持在线数据增强的caffe版本,可以安装下面的,两个都装也行,用的时候自由切换
https://github.com/twtygqyy/caffe-augmentation
2.我的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 lines after *_35 for compatibility.
CUDA_ARCH :=-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_61,code=sm_61
# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := atlas
#BLAS := open
# 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)/anaconda3/envs/py27
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python2.7 \
$(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include
# Uncomment to use Python 3 (default is Python 2)
#PYTHON_LIBRARIES := boost_python3 python3.5m
#PYTHON_INCLUDE := /usr/local/include/python3.5m \
# /usr/local/lib/python3.6/site-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
# 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 ?= @
LINKFLAGS := -Wl,-rpath,$(HOME)/anaconda3/lib
Clion 配置opencv3.4
目前配置:ubuntu18+cuda10+opencv3.4.6
注:cuda版本的配置一定要与opencv配套
cmakelist如下:
cmake_minimum_required(VERSION 3.13)
project(Learning)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# Where to find CMake modules and OpenCV
set(OpenCV_DIR "/home/em/software/opencv-3.4.6/build/CMakeFiles/install")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(Learning main.cpp)
# add libs you need
set(OpenCV_LIBS opencv_core opencv_imgproc opencv_highgui opencv_imgcodecs)
# linking
target_link_libraries(Learning ${OpenCV_LIBS})