2016年Semantic Segmentation方向比较出色的一篇文章,DeepLab: Semantic Image Segmentation with Deep Convolutional Nets, Atrous Convolution, and Fully Connected CRFs
https://arxiv.org/abs/1606.00915
作者提供了开源的代码,shell版本:https://bitbucket.org/aquariusjay/deeplab-public-ver2
和python版本:https://github.com/TheLegendAli/DeepLab-Context2
以及相关Model下载:
http://liangchiehchen.com/projects/DeepLab_Models.html
作者所使用的Caffe版本比较陈旧,导致会出现很多和最新环境不兼容的情况。我使用的环境是Ubuntu 16.04, CUDA 8.0, cuDNN 5.1, Python 2.7.12,以下就以这个配置写个简单流程说明。
下载matio(https://sourceforge.net/projects/matio/files/matio/1.5.2/)
$ tar zxf matio-1.5.2.tar.gz
$ cd matio-1.5.2
$ ./configure
$ make
$ make check
$ make install
$ sudo ldconfig
执行脚本时如果提示 ImportError: No module named wget,说明还需要安装wget
sudo pip install wget
和BVLC版本一样,对DeepLab版本的caffe进行编译
make -j8
make test -j8
make pycaffe
make pytest
编译时会出现如下错误提示:
./include/caffe/common.cuh(9): error: function "atomicAdd(double *, double)" has already been defined
原因是CUDA 8.0 提供了对atomicAdd函数的定义,但atomicAdd在之前的CUDA toolkit中并未出现,因此一些程序自定义了atomicAdd函数。
解决方法:打开./include/caffe/common.cuh文件,在atomicAdd前添加宏判断即可。
如下:
#if !defined(__CUDA_ARCH__) || __CUDA_ARCH__ >= 600
#else
static __inline__ __device__ double atomicAdd(double* address, double val)
{
...
}
#endif
再次编译程序即可。
./include/caffe/util/cudnn.hpp: In function ‘void caffe::cudnn::createPoolingDesc(cudnnPoolingStruct**, caffe::PoolingParameter_PoolMethod, cudnnPoolingMode_t*, int, int, int, int, int, int)’:
./include/caffe/util/cudnn.hpp:127:41: error: too few arguments to function ‘cudnnStatus_t cudnnSetPooling2dDescriptor(cudnnPoolingDescriptor_t, cudnnPoolingMode_t, cudnnNanPropagation_t, int, int, int, int, int, int)’
pad_h, pad_w, stride_h, stride_w));
这是由于所使用的cuDNN版本不一致的导致的,作者配置环境是cuDNN 4.0,但是5.0版本后的cuDNN接口有所变化。
解决方法 :将以下几个文件用最新BVLC版本的caffe对应文件替换并重新编译
./include/caffe/util/cudnn.hpp
./include/caffe/layers/cudnn_conv_layer.hpp
./include/caffe/layers/cudnn_relu_layer.hpp
./include/caffe/layers/cudnn_sigmoid_layer.hpp
./include/caffe/layers/cudnn_tanh_layer.hpp
./src/caffe/layers/cudnn_conv_layer.cpp
./src/caffe/layers/cudnn_conv_layer.cu
./src/caffe/layers/cudnn_relu_layer.cpp
./src/caffe/layers/cudnn_relu_layer.cu
./src/caffe/layers/cudnn_sigmoid_layer.cpp
./src/caffe/layers/cudnn_sigmoid_layer.cu
./src/caffe/layers/cudnn_tanh_layer.cpp
./src/caffe/layers/cudnn_tanh_layer.cu
../lib/libcaffe.so.1.0.0-rc3: undefined reference to `Mat_VarFree'
../lib/libcaffe.so.1.0.0-rc3: undefined reference to `Mat_VarReadDataLinear'
../lib/libcaffe.so.1.0.0-rc3: undefined reference to `Mat_Open'
../lib/libcaffe.so.1.0.0-rc3: undefined reference to `Mat_VarCreate'
../lib/libcaffe.so.1.0.0-rc3: undefined reference to `Mat_CreateVer'
../lib/libcaffe.so.1.0.0-rc3: undefined reference to `Mat_VarWrite'
../lib/libcaffe.so.1.0.0-rc3: undefined reference to `Mat_VarReadInfo'
../lib/libcaffe.so.1.0.0-rc3: undefined reference to `Mat_Close'
解决方法:
下载FindMATIO.cmake.zip文件,解压缩后拷贝到./cmake/Modules目录中。
文件下载路径:https://github.com/TheLegendAli/DeepLab-Context/files/453735/FindMATIO.cmake.zip
并添加以下代码至./cmake/Dependencies.cmake文件中
find_package(MATIO REQUIRED)
include_directories(${MATIO_INCLUDE_DIR})
list(APPEND Caffe_LINKER_LIBS ${MATIO_LIBRARIES})
python run.py
Error parsing text-format caffe.NetParameter: 316:9: Message type "caffe.ConvolutionParameter" has no field named "hole".
v1版本中将Atrous Convolution的参数称为”hole”,而v2为了和BVLC版本一致而改称为”dilation”。因此将train.prototxt中的hole全部替换为dilation即可。
为了增加对v1版本prototxt的兼容性,还需要对源代码进行多项改动,需要增加对ImageSegDataLayer, InterpLayer, SegAccuracyLayer, MatWriteLayer这几层对prototxt文件信息的支持。首先在./src/caffe/proto/caffe.proto文件中添加唯一ID
message V1LayerParameter {
enum LayerType {
IMAGE_SEG_DATA = 40;
INTERP = 41;
SEG_ACCURACY = 42;
MAT_READ = 44;
MAT_WRITE = 43;
}
optional InterpParameter interp_param = 43;
optional SegAccuracyParameter seg_accuracy_param = 44;
optional MatReadParameter mat_read_param = 51;
optional MatWriteParameter mat_write_param = 45;
}
./src/caffe/util/upgrade_proto.cpp文件的UpgradeV1LayerType(const V1LayerParameter_LayerType type)函数中添加
const char* UpgradeV1LayerType(const V1LayerParameter_LayerType type) {
switch (type) {
case V1LayerParameter_LayerType_IMAGE_SEG_DATA:
return "ImageSegData";
case V1LayerParameter_LayerType_INTERP:
return "Interp";
case V1LayerParameter_LayerType_SEG_ACCURACY:
return "SegAccuracy";
case V1LayerParameter_LayerType_MAT_WRITE:
return "MatWrite";
}
还需要补充对InterpLayer参数interp_param的支持,
bool UpgradeV1LayerParameter(const V1LayerParameter& v1_layer_param,
LayerParameter* layer_param) {
if (v1_layer_param.has_interp_param()) {
layer_param->mutable_interp_param()->CopyFrom(
v1_layer_param.interp_param());
}
}