重置Ubuntu
由于之前的caffe配置不成功,参照Windows10如何重置/卸载/修复内置Linux子系统
重新安装Ubuntu。
之后先安装对应python2.7的Anaconda2,在官网下载Anaconda(注意选linux版本)
下载完成后直接bash打开下载的Anaconda2-2018.12-Linux-x86_64.sh文件,按照提示安装,上次安装Anaconda3的时候,最后会提示:是否把Anaconda的python作为默认的python,Do you wish the installer to initialize Anaconda2
in your /home/kangm/.bashrc ? [yes|no]这里建议选择yes。但是Anaconda2安装的到最后,它默认选择了No,需要在根目录下$ export PATH=/home/kangm/anaconda2/bin:$PATH 手动添加环境变量。
crfasrnn
下载模型:git clone --recursive https://github.com/torrvision/crfasrnn.git
caffe 安装
首先需要使用pip安装依赖库。
下载pip:
$ wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate
执行安装:
$ python get-pip.py
然后在根目录下使用pip安装crfasrnn/caffe/python/requirements.txt中提到的Python包
$ pip install -r /home/kangm/crfasrnn/caffe/python/requirements.txt
安装过程中出错提示:找不到gcc,使用一下方法安装gcc:
$ sudo apt-get update //一定要记得先update,不然找不到gcc
$ sudo apt-get install gcc
然后又报错提示说:gcc: error trying to exec ‘cc1plus’: execvp: No such file or directory
原来还需要安装g++ (无奈)
$ sudo apt-get install build-essential
$ sudo apt-get install g++
安装过g++后重新安装python包,终于不再报错了,但是提示有下列警告:
anaconda-client 1.7.2 has requirement python-dateutil>=2.6.1, but you'll have python-dateutil 1.5 which is incompatible.
搜索解决方法,Python-dateutil已经是最新的版本了,所以暂时不管他。
接下来安装caffe所需要的依赖包:
# install General dependencies
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
# 安装BLAS库
sudo apt-get install libatlas-base-dev
安装过程中出错,随时$ sudo apt-get update
安装openCV
从github上下载安装脚本:https://github.com/jayrambhia/Install-OpenCV
进入Ubuntu/2.4目录,对所有脚本增加可执行权限
$ sudo chmod +x *.sh
cd …转到Ubuntu/下,安装依赖项 $ sudo ./dependencies.sh
进入Ubuntu/2.4目录,安装opencv 2.4.10: $ sudo sh ./opencv2_4_10.sh
配置CPU版本的caffe
参照caffe安装和 py-faster-rcnn的CPU安装
首先
cd caffe
cp Makefile.config.example Makefile.config //注意复制一份Makefile.config.example
修改Makefile.config相关配置:
去掉注释CPU_ONLY :=1
注释掉CUDA有关的行:
#CUDA_DIR := /usr/local/cuda
#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_50,code=compute_50
去掉注释WITH_PYTHON_LAYER := 1
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/i386-linux-gnu/hdf5/serial /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial
#TEST_GPUID := 0
修改完成后,$ make all ,不出所料报错:
解决方法:$ sudo apt-get install libgflags-dev
然后,接着报错:
解决方法:$ sudo apt-get install libgoogle-glog-dev
然后接着报错:
按照错误提示将caffe/src/caffe/layers/contrastive_loss_layer.cpp文件中第56行改为:
Dtype dist = std::max(margin - sqrt(dist_sq_.cpu_data()[i]), Dtype(0.0));
上面的错误解决了之后,开始报下面的错,跟之前遇到那个不能解决的错误一样,心好慌。。。
报错原因: caffe默认使用opencv2.X版本,但是我安装的是opencv 3,于是
Makefile.config中添加OPENCV_VERSION := 3。
之后重新编译不再报错了,但是有下面两个警告,不管了,就当做通过了吧。
#1、首先在src/caffe/layers/multi_stage_meanfield.cpp:
src/caffe/layers/multi_stage_meanfield.cpp: In instantiation of ‘void caffe::MultiStageMeanfieldLayer<Dtype>::LayerSetUp(const std::vector<caffe::Blob<Dtype>*>&, const std::vector<caffe::Blob<Dtype>*>&) [with Dtype = float]’:
src/caffe/layers/multi_stage_meanfield.cpp:258:1: required from here
src/caffe/layers/multi_stage_meanfield.cpp:72:13: warning: format ‘%lf’ expects argument of type ‘double*’, but argument 3 has type ‘float*’ [-Wformat=]
fscanf(pFile, "%lf", &this->blobs_[0]->mutable_cpu_data()[i * channels_ + i]);
src/caffe/layers/multi_stage_meanfield.cpp:72:13: warning: ignoring return value of ‘int fscanf(FILE*, const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
fscanf(pFile, "%lf", &this->blobs_[0]->mutable_cpu_data()[i * channels_ + i]);
#2、在CXX .build_release/src/caffe/proto/caffe.pb.cc
examples/mnist/convert_mnist_data.cpp:33:6: warning: ‘__builtin___snprintf_chk’ output may be truncated before the last format character [-Wformat-truncation=]
void convert_dataset(const char* image_filename, const char* label_filename,
In file included from /usr/include/stdio.h:862:0,
from /usr/include/c++/7/cstdio:42,
from /usr/include/c++/7/ext/string_conversions.h:43,
from /usr/include/c++/7/bits/basic_string.h:6352,
from /usr/include/c++/7/string:52,
from /usr/include/gflags/gflags.h:81,
from examples/mnist/convert_mnist_data.cpp:9:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:65:44: note: ‘__builtin___snprintf_chk’ output between 9 and 11 bytes into a destination of size 10
__bos (__s), __fmt, __va_arg_pack ());
接下来$ make test 失败了:
In file included from /usr/include/c++/7/algorithm:62:0,
from ./include/caffe/blob.hpp:4,
from src/caffe/test/test_concat_layer.cpp:6:
/usr/include/c++/7/bits/stl_algo.h:3468:5: note: candidate: template<class _Tp, class _Compare> constexpr _Tp std::max(std::initializer_list<_Tp>, _Compare)
max(initializer_list<_Tp> __l, _Compare __comp)
/usr/include/c++/7/bits/stl_algo.h:3468:5: note: template argument deduction/substitution failed:
In file included from src/caffe/test/test_concat_layer.cpp:12:0:
./include/caffe/test/test_gradient_check_util.hpp:172:31: note: mismatched types ‘std::initializer_list<_Tp>’ and ‘float’
Dtype scale = std::max(
std::max(fabs(computed_gradient), fabs(estimated_gradient)), 1.);
Makefile:533: recipe for target '.build_release/src/caffe/test/test_concat_layer.o' failed
make: *** [.build_release/src/caffe/test/test_concat_layer.o] Error 1
应该也是 Dtype scale = std::max(std::max(fabs(computed_gradient), fabs(estimated_gradient)), 1.);这个原因,但是不知道怎么改,放弃了。
接下来$ make pycaffe 没有报错。
打开~/.bashrc文件,$ vim ~/.bashrc 在这个文件中的最后一行添加
export PYTHONPATH=/home/kangm/crfasrnn/caffe/python:$PYTHONPATH。
试着在python中导入caffe没有成功,直接在caffe文件夹下添加上述路径,成功导入了caffe,可能是之前没有重启/.bashrc的原因。
CRF as RNN测试
执行crfasrnn/python-scripts目录下的download_trained_model.sh文件,下载crfasrnn model文件:TVG_CRFRNN_COCO_VOC.caffemodel
然后:python crfasrnn_demo.py 使用官方给出的图片分割效果倒也不错,但是我在VOC2012中随便找了一张图片输进去后的分割效果是这样的:
效果真的是一般般。。。