一、下载SegNet代码
1、segnet代码的github地址:https://github.com/alexgkendall/SegNet-Tutorial (不包括caffe代码)
2、在caffe-segnet的文件夹中,放置 下载到的caffe代码https://github.com/alexgkendall/caffe-segnet
二、编译caffe代码
1、因为cuda版本不一样,所以将caffe-segnet/src/caffe/layers/文件夹下的cudnn开头的文件全部用最新的替换掉。(我是用caffe官方提供的https://github.com/BVLC/caffe替换)
2、 /caffe-segnet/include/caffe/文件夹下没有layers文件夹,我通过最新的caffe版本添加了这个存放.hpp的文件夹layers。(跟cpp文件匹配最好,不要有没有用都添加)
3、修改Makefile.config文件
cp Makefile.config.example Make.config
修改的部分:1、USE_CUDNN := 1
2、OPENCV_VERSION := 3
3、CUSTOM_CXX := g++
4、CUDA_DIR := /usr/local/cuda
5、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_60,code=sm_60 \
-gencode arch=compute_61,code=sm_61 \
-gencode arch=compute_61,code=compute_61
6、BLAS := atlas
7、PYTHON_INCLUDE := /usr/include/python2.7 /usr/local/lib/python2.7/dist-packages/numpy/core/include
8、PYTHON_LIB := /usr/lib
9、WITH_PYTHON_LAYER := 1
10、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
11、BUILD_DIR := build
DISTRIBUTE_DIR := distribute
4、我的网络中需要添加一个crop层,Segnet网络中没有。所以从caffe官方提供的代码中来复制。在src和include的layers文件中添加crop_layers文件(共三个eg. .cpp, .cu, .hpp)以及修改src/caffe/proto/caffe.proto文件,参照:caffe官方提供的代码。
三、caffe编译中遇到的问题
1、Makefile:526: recipe for target '.build_release/src/caffe/layers/contrastive_loss_layer.o' failed
src/caffe/layers/contrastive_loss_layer.cpp:56:30: error: no matching function for call to ‘max(double, float)’
Dtype dist = std::max(margin - sqrt(dist_sq_.cpu_data()[i]), Dtype(0.0));
修改contrastive_loss_layer.cpp文件:Dtype dist = std::max(margin - (float)sqrt(dist_sq_.cpu_data()[i]), Dtype(0.0));
2、PoolingParameter_RoundMode does not name a type.
分析:现有运行平台使用的caffe版本较低,pooling层不支持round_mode
参数。
解决:用新版本caffe中的pooling_layer.hpp
和pooling_layer.cpp,pooling_layer.cu
文件替换旧版本caffe中的相应文件。在caffe.proto
文件的message PoolingParameter
中追加如下代码:
enum RoundMode {
CEIL = 0;
FLOOR = 1;
}
optional RoundMode round_mode = 13 [default = CEIL];
四、数据集的准备
五、Segnet网络结构修改
1、source: "../datasets/train_list.lst"