1.makeconfig file modify
1.应用 opencv 版本
将
#OPENCV_VERSION := 3
修改为:
OPENCV_VERSION := 3
- 1
- 2
2.使用 python 接口
将
#WITH_PYTHON_LAYER := 1
修改为
WITH_PYTHON_LAYER := 1
- 4
3.修改 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
4.修改 python version
all content is:
## 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 USE_OPENCV := 1 #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-8.0 # 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_20,code=sm_20 \ -gencode arch=compute_20,code=sm_21 \ -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)/anaconda # 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/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 /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 ?= @
2.modify makefile
然后修改 caffe 目录下的 Makefile 文件:
将:
NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)
替换为:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
- 1
- 2
- 3
- 4
将:
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
- 1
- 2
- 3
- 4
然后修改 /usr/local/cuda/include/host_config.h 文件 :
将
#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
编译成功后测试命令
sudo make runtest -j8
error:
1.caffe编译出现connot find -lopencv_imgcodecs的解决方式
先将Makefile.config文件中
OPENCV_VERSION :=3 注释掉,只修改USE_OPENCV := 1
修改后的结果:
USE_OPENCV := 1
#OPENCV_VERSION := 3
在caffe根目录下,找到Makefile文件,打开文件
查找“Derive include and lib directories”一节,修改“LIBRARIES +=”的最后一行(LIBRARIES +=opencv_imgcodecs ),增加opencv_imgcodecs
opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs
(此时应该变为LIBRARIES += opencv_imgcodecs opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs)
保存,退出。
注意:在做前边的基础工作是将Makefile.config文件中
2.[caffe]python3编译caffe错误:cannot find -lboost_python3
针对Python3编译caffe时make all
的时候出现了如下错误
cannot find -lboost_python3
- 1
首先去/usr/lib/x86_64-linux-gnu
目录下查看是否有python3版本的libboost,如果有类似libboost_python35.so
但是没有libboost_python3.so
则需要手动建立连接。
方法为:sudo ln -s libboost_python-py35.so libboost_python3.so
CTPN caffe 版本不同造成的问题:
[libprotobuf ERROR google/protobuf/text_format.cc:274] Error parsing text-format caffe.NetParameter: 387:19: Message type "caffe.LayerParameter" has no field named "transpose_param". F0519 15:36:28.264559 15846 upgrade_proto.cpp:90] Check failed: ReadProtoFromTextFile(param_file, param) Failed to parse NetParameter file: /media/yang/3ed2dc8e-c16d-4d0a-bf9d-d783aaa7c4f2/yang/textrecon/sceneReco-master/CTPN/models/deploy.prototxt *** Check failure stack trace: ***
原因:缺少相关的 transpose,reverse,lstm层。使用老版本却不支持cuda8.0
所以自定义向支持cuda8.0的caffe添加这三层,其实根据老版本向新的版本添加这几个就可以了,具体如下:
(1)在./src/caffe/proto/caffe.proto 中增加对应layer的paramter message;
(2)在./include/caffe/***layers.hpp中增加该layer的类的声明;
(3)在./src/caffe/layers/目录下新建.cpp和.cu(GPU)文件,进行类实现。
(4)在./src/caffe/gtest/中增加layer的测试代码,对所写的layer前传和反传进行测试,测试还包括速度。(可省略,但建议加上)
参考:https://www.cnblogs.com/573177885qq/p/6065625.html
https://blog.csdn.net/shuzfan/article/details/51322976
例子如下
reverse_layer.hpp
#ifndef CAFFE_REVERSE_LAYER_HPP_ #define CAFFE_REVERSE_LAYER_HPP_ #include <vector> #include "caffe/blob.hpp" #include "caffe/layer.hpp" #include "caffe/proto/caffe.pb.h" #include "caffe/layers/neuron_layer.hpp" namespace caffe { /** * @brief Computes the Reverse function. * * TODO(dox): thorough documentation for Forward, Backward, and proto params. */ template <typename Dtype> class ReverseLayer : public Layer<Dtype> { public: explicit ReverseLayer(const LayerParameter& param) : Layer<Dtype>(param) {} virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top); virtual void Reshape(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top); virtual inline const char* type() const { return "Reverse"; } virtual inline int ExactNumBottomBlobs() const { return 1; } virtual inline int ExactNumTopBlobs() const { return 1; } protected: virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top); virtual void Backward_cpu(const vector<Blob<Dtype>*>& top, const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom); virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top); virtual void Backward_gpu(const vector<Blob<Dtype>*>& top, const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom); private: ReverseParameter reverse_param_; Blob<int> bottom_counts_; int axis_; }; } #endif // CAFFE_REVERSE_LAYER_HPP_reverse_layer.cpp
#include <algorithm> #include <vector> #include "caffe/layers/reverse_layer.hpp" namespace caffe { template <typename Dtype> void reverse_cpu(const int count, const Dtype* from_data, Dtype* to_data, const int* counts, const int axis_count, const int axis) { for(int index=0; index<count; index++) { int ind=(index/counts[axis])%axis_count; int to_index=counts[axis]*(axis_count-2*ind-1)+index; *(to_data+to_index)=*(from_data+index); } } template <typename Dtype> void ReverseLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { CHECK_NE(bottom[0], top[0])<<this->type()<<" does not support in-place computation."; reverse_param_=this->layer_param_.reverse_param(); } template <typename Dtype> void ReverseLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { vector<int> shape=bottom[0]->shape(); axis_=reverse_param_.axis(); CHECK_GT(shape.size(), 0)<<this->type()<<" does not support 0 axes blob."; CHECK_GE(axis_, 0)<<"axis must be greater than or equal to 0."; CHECK_LT(axis_, shape.size())<<"axis must be less than bottom's dimension."; top[0]->ReshapeLike(*bottom[0]); const int dim=shape.size(); shape.clear(); shape.push_back(dim); bottom_counts_.Reshape(shape); int* p=bottom_counts_.mutable_cpu_data(); for (int i=1; i<dim; i++) { *p=bottom[0]->count(i); p++; } *p=1; } template <typename Dtype> void ReverseLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { reverse_cpu<Dtype>(bottom[0]->count(), bottom[0]->cpu_data(), top[0]->mutable_cpu_data(), bottom_counts_.cpu_data(), bottom[0]->shape(axis_), axis_); } template <typename Dtype> void ReverseLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top, const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) { if (!propagate_down[0]) { return; } reverse_cpu<Dtype>(bottom[0]->count(), top[0]->cpu_diff(), bottom[0]->mutable_cpu_diff(), bottom_counts_.cpu_data(), bottom[0]->shape(axis_), axis_); } #ifdef CPU_ONLY STUB_GPU(ReverseLayer); #endif INSTANTIATE_CLASS(ReverseLayer); REGISTER_LAYER_CLASS(Reverse); } // namespace caffe