Ubuntu上编译Caffe和拓展应用(faster-rcnn, pvanet)的错误及解决方案

前端之家收集整理的这篇文章主要介绍了Ubuntu上编译Caffe和拓展应用(faster-rcnn, pvanet)的错误及解决方案前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Caffe

错误: 采用make方式编译时遇到如下错误

  1. In file included from /usr/include/boost/python/detail/prefix.hpp:13:0,from /usr/include/boost/python/args.hpp:8,from /usr/include/boost/python.hpp:11,from tools/caffe.cpp:2:
  2. /usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: No such file or directory
  3. compilation terminated.
  4. Makefile:575: recipe for target '.build_release/tools/caffe.o' Failed
  5. make: *** [.build_release/tools/caffe.o] Error 1

解决方修改Makefile.config,将

  1. PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
  2. # $(ANACONDA_HOME)/include/python2.7 \
  3. # $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \

取消以下2行注释

  1. PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
  2. $(ANACONDA_HOME)/include/python2.7 \
  3. $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \
  1. Note:$(ANACONDA_HOME) #虚拟环境Anaconda2的根目录

Faster-RCNN

问题: 如何编译只采用cpu版本的Faster-RCNN?

解决方
在./lib/setup.py中注释以下部分

  1. ...
  2. #CUDA = locate_cuda()
  3. ...
  4. ...
  5. #self.set_executable('compiler_so',CUDA['nvcc'])
  6. ...
  7. ...
  8. #Extension('nms.gpu_nms',
  9. #[‘nms/nms_kernel.cu','nms/gpu_nms.pyx'],
  10. #library_dirs=[CUDA['lib64']],
  11. #libraries=['cudart'],
  12. #language='c++',
  13. #runtime_library_dirs=[CUDA['lib64']],
  14. ## this Syntax is specific to this build system
  15. ## we're only going to use certain compiler args with nvcc and not with
  16. ## gcc the implementation of this trick is in customize_compiler() below
  17. #extra_compile_args={'gcc': ["-Wno-unused-function"],
  18. #’nvcc': ['-arch=sm_35',
  19. #’—ptxas-options=-v',
  20. #’-c’,
  21. #’—compiler-options',
  22. #”’-fPIC'"]},
  23. #include_dirs = [numpy_include,CUDA['include']]
  24. #)

问题:运行时,遇到错误ImportError: No module named cv2

  1. File "./tools/test_net.py",line 13,in <module>
  2. from fast_rcnn.test import test_net
  3. File "/home/rtc5/JpHu/pva-faster-rcnn-master/tools/../lib/fast_rcnn/test.py",line 15,in <module>
  4. import cv2
  5. ImportError: No module named cv2

解决方
(1)检查cv2是否存在:
${HOME}目录下运行

  1. $find -name cv2

进行查找
(2)如果不存在cv2,安装python-opencv

  1. sudo apt-get install python-opencv

(3)如果存在cv2,将文件夹cv2所在目录添加到.bashrc最后一行(如我将cv2安装在/home/rtc5/anaconda2/envs/tensorflow/lib/python2.7/site-packages/cv2下)

  1. export PATHONPATH=$PYTHONPATH:/home/rtc5/anaconda2/envs/tensorflow/lib/python2.7/site-packages/cv2

运行命令

  1. source ./bashrc #激活

激活./bashrc

问题:编译cpu版本成功后,faster-rcnn运行时,遇到错误ImportError: No module named gpu_nms

  1. File "./demo.py",line 18,in
  2. from fast_rcnn.test import im_detect
  3. File ".../py-faster-rcnn-master/tools/../lib/fast_rcnn/test.py",line 17,in
  4. from fast_rcnn.nms_wrapper import nms
  5. File ".../py-faster-rcnn-master/tools/../lib/fast_rcnn/nms_wrapper.py",line 11,in
  6. from nms.gpu_nms import gpu_nms
  7. ImportError: No module named gpu_nms

解决方
注释${FCNN}/py-faster-rcnn/lib/fast_rcnn/nms_wrapper.py 中有关gpu的代码

  1. from fast_rcnn.config import cfg
  2. #from nms.gpu_nms import gpu_nms
  3. from nms.cpu_nms import cpu_nms
  4.  
  5. def nms(dets,thresh,force_cpu=False):
  6. """Dispatch to either cpu or GPU NMS implementations."""
  7.  
  8. if dets.shape[0] == 0:
  9. return []
  10. #if cfg.USE_GPU_NMS and not force_cpu:
  11. # return gpu_nms(dets,device_id=cfg.GPU_ID)
  12. else:
  13. return cpu_nms(dets,thresh)

问题:(1)运行vgg16版本的faster-rcnn的./tools/demo.py遇到如下问题

  1. WARNING: Logging before InitGoogleLogging() is written to STDERR
  2. F1207 00:08:31.251930 20944 common.cpp:66] Cannot use GPU in cpu-only Caffe: check mode.
  3. @H_712_404@*** Check failure stack trace: ***
  4. Aborted (core dumped)

解决方
采用命令:

  1. $./tools/demo.py --cpu

Note:运行pvanet示例时,遇到类似问题,则需要将测试文件*.py中set_gpu的相关代码注释

问题:如何编译cpu版本的pvanet

编译caffe,遇到问题:

  1. src/caffe/layers/proposal_layer.cpp:321:10: error: redefinition of void caffe::ProposalLayer<Dtype>::Backward_gpu(const std::vector<caffe::Blob<Dtype>*>&,const std::vector<bool>&,const std::vector<caffe::Blob<Dtype>*>&)’
  2. STUB_GPU(ProposalLayer);
  3. ^
  4. ./include/caffe/util/device_alternate.hpp:17:6: note: in definition of macro STUB_GPU
  5. void classname<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,\
  6. ^
  7. In file included from src/caffe/layers/proposal_layer.cpp:1:0:
  8. ./include/caffe/fast_rcnn_layers.hpp:122:16: note: virtual void caffe::ProposalLayer<Dtype>::Backward_gpu(const std::vector<caffe::Blob<Dtype>*>&,const std::vector<caffe::Blob<Dtype>*>&)’ prevIoUsly declared here
  9. virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,^
  10. Makefile:575: recipe for target '.build_release/src/caffe/layers/proposal_layer.o' Failed
  11. make: *** [.build_release/src/caffe/layers/proposal_layer.o] Error 1
  12. make: *** Waiting for unfinished jobs....

解决方
由于caffe::ProposalLayer<Dtype>::Backward_gpu./include/caffe/fast_rcnn_layers.hpp./include/caffe/util/device_alternate.hpp(后者为模板形式)中定义了两次,被系统认为重定义。
解决方法如下:
./include/caffe/fast_rcnn_layers.hppBackward_gpu代码

  1. virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,const vector<bool>& propagate_down,const vector<Blob<Dtype>*>& bottom){}

修改如下

  1. virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,const vector<Blob<Dtype>*>& bottom);

由于Backward_cpu只在./include/caffe/fast_rcnn_layers.hpp中定义过一次,所以一定避免对它做如上gpu的修改

问题:如何只用cpu训练caffe,py-faster-rcnn,pvanet?

*报错:

  1. smooth_L1_loss_layer Not Implemented Yet

解决方案:*
补充./src/caffe/layers/smooth_L1_loss_layer.cpp函数实体SmoothL1LossLayer::Forward_cpu和SmoothL1LossLayer::Backward_cpu

  1. `// ------------------------------------------------------------------
  2. // Fast R-CNN
  3. // Copyright (c) 2015 Microsoft
  4. // Licensed under The MIT License [see fast-rcnn/LICENSE for details]
  5. // Written by Ross Girshick
  6. // ------------------------------------------------------------------
  7.  
  8. #include "caffe/fast_rcnn_layers.hpp"
  9.  
  10. namespace caffe {
  11.  
  12. template
  13. void SmoothL1LossLayer::LayerSetUp(
  14. const vector<Blob>& bottom,const vector<Blob>& top) {
  15. SmoothL1LossParameter loss_param = this->layer_param_.smooth_l1_loss_param();
  16. sigma2_ = loss_param.sigma() * loss_param.sigma();
  17. has_weights_ = (bottom.size() >= 3);
  18. if (has_weights_) {
  19. CHECK_EQ(bottom.size(),4) << "If weights are used,must specify both "
  20. "inside and outside weights";
  21. }
  22. }
  23.  
  24. template
  25. void SmoothL1LossLayer::Reshape(
  26. const vector<Blob>& bottom,const vector<Blob>& top) {
  27. LossLayer::Reshape(bottom,top);
  28. CHECK_EQ(bottom[0]->channels(),bottom[1]->channels());
  29. CHECK_EQ(bottom[0]->height(),bottom[1]->height());
  30. CHECK_EQ(bottom[0]->width(),bottom[1]->width());
  31. if (has_weights_) {
  32. CHECK_EQ(bottom[0]->channels(),bottom[2]->channels());
  33. CHECK_EQ(bottom[0]->height(),bottom[2]->height());
  34. CHECK_EQ(bottom[0]->width(),bottom[2]->width());
  35. CHECK_EQ(bottom[0]->channels(),bottom[3]->channels());
  36. CHECK_EQ(bottom[0]->height(),bottom[3]->height());
  37. CHECK_EQ(bottom[0]->width(),bottom[3]->width());
  38. }
  39. diff_.Reshape(bottom[0]->num(),bottom[0]->channels(),bottom[0]->height(),bottom[0]->width());
  40. errors_.Reshape(bottom[0]->num(),bottom[0]->width());
  41. // vector of ones used to sum
  42. ones_.Reshape(bottom[0]->num(),bottom[0]->width());
  43. for (int i = 0; i < bottom[0]->count(); ++i) {
  44. ones_.mutable_cpu_data()[i] = Dtype(1);
  45. }
  46. }
  47.  
  48. template
  49. void SmoothL1LossLayer::Forward_cpu(const vector<Blob>& bottom,const vector<Blob>& top) {
  50. // NOT_IMPLEMENTED;
  51. int count = bottom[0]->count();
  52. //int num = bottom[0]->num();
  53. const Dtype* in = diff_.cpu_data();
  54. Dtype* out = errors_.mutable_cpu_data();
  55. caffe_set(errors_.count(),Dtype(0),out);
  56.  
  57. caffe_sub(
  58. count,bottom[0]->cpu_data(),bottom[1]->cpu_data(),diff_.mutable_cpu_data()); // d := b0 - b1
  59. if (has_weights_) {
  60. // apply "inside" weights
  61. caffe_mul(
  62. count,bottom[2]->cpu_data(),diff_.cpu_data(),diff_.mutable_cpu_data()); // d := w_in * (b0 - b1)
  63. }
  64.  
  65. for (int index = 0;index < count; ++index){
  66. Dtype val = in[index];
  67. Dtype abs_val = abs(val);
  68. if (abs_val < 1.0 / sigma2_) {
  69. out[index] = 0.5 * val * val * sigma2_;
  70. } else {
  71. out[index] = abs_val - 0.5 / sigma2_;
  72. }
  73. }
  74.  
  75. if (has_weights_) {
  76. // apply "outside" weights
  77. caffe_mul(
  78. count,bottom[3]->cpu_data(),errors_.cpu_data(),errors_.mutable_cpu_data()); // d := w_out * SmoothL1(w_in * (b0 - b1))
  79. }
  80.  
  81. Dtype loss = caffe_cpu_dot(count,ones_.cpu_data(),errors_.cpu_data());
  82. top[0]->mutable_cpu_data()[0] = loss / bottom[0]->num();
  83. }
  84.  
  85. template
  86. void SmoothL1LossLayer::Backward_cpu(const vector<Blob>& top,const vector& propagate_down,const vector<Blob>& bottom) {
  87. // NOT_IMPLEMENTED;
  88. int count = diff_.count();
  89. //int num = diff_.num();
  90. const Dtype* in = diff_.cpu_data();
  91. Dtype* out = errors_.mutable_cpu_data();
  92. caffe_set(errors_.count(),out);
  93.  
  94. for (int index = 0;index < count; ++index){
  95. Dtype val = in[index];
  96. Dtype abs_val = abs(val);
  97. if (abs_val < 1.0 / sigma2_) {
  98. out[index] = sigma2_ * val;
  99. } else {
  100. out[index] = (Dtype(0) < val) - (val < Dtype(0));
  101. }
  102. }
  103.  
  104. for (int i = 0; i < 2; ++i) {
  105. if (propagate_down[i]) {
  106. const Dtype sign = (i == 0) ? 1 : -1;
  107. const Dtype alpha = sign * top[0]->cpu_diff()[0] / bottom[i]->num();
  108. caffe_cpu_axpby(
  109. count,// count
  110. alpha,// alpha
  111. diff_.cpu_data(),// x
  112. Dtype(0),// beta
  113. bottom[i]->mutable_cpu_diff()); // y
  114. if (has_weights_) {
  115. // Scale by "inside" weight
  116. caffe_mul(
  117. count,bottom[i]->cpu_diff(),bottom[i]->mutable_cpu_diff());
  118. // Scale by "outside" weight
  119. caffe_mul(
  120. count,bottom[i]->mutable_cpu_diff());
  121. }
  122. }
  123. }
  124. }
  125.  
  126. #ifdef cpu_ONLY
  127. STUB_GPU(SmoothL1LossLayer);
  128. #endif
  129.  
  130. INSTANTIATE_CLASS(SmoothL1LossLayer);
  131. REGISTER_LAYER_CLASS(SmoothL1Loss);
  132.  
  133. } // namespace caffe

转自: zhouphd 的解答,已验证有效,caffe能够通过编译,并进行训练

问题:运行pvanet时,报错

  1. Intel MKL FATAL ERROR: Cannot load libmkl_avx2.so or libmkl_def.so.

原因:由于之前安装tensorflow时,采用的是anaconda,它独自创建了一个虚拟环境(自行另安装依赖库),但由于anaconda会在~/.bashrc中添加PATH路径。所以执行caffe程序时(在虚拟环境之外),其依赖库也会受到anaconda安装软件的影响。
解决方案:屏蔽anaconda设置的PATH,在~/.bashrc中注释

  1. #export PATH="/home/cvrsg/anaconda2/bin:$PATH"
  2. $source ~/.bashrc #激活.bashrc

注意:重开一个终端,在当前终端,source命令是没有生效的。
如何验证?

  1. 如果在当前终端下输入
  2. sudo echo $PATH
  3. 你会发现anaconda2/bin还在PATH中,source未生效
  4. 重开终端之后,
  5. anaconda2/bin已消失

同样由此可知,当我们需要anaconda2时,我们可以将

  1. #export PATH="/home/cvrsg/anaconda2/bin:$PATH"

解注释,并source ~/.bashrc激活
不需要时,注释即可。
在上述命令被注释的情况下,运行source activate tensorflow会出现以下错误

  1. bash: activate: No such file or directory

别着急,解注释,激活就好。

Note-切记:::
另外,如果我们要用conda安装软件时,一定要切换到相应的虚拟环境下,否则安装的软件很容易和系统软件发生版本冲突,导致程序出错。

在安装pycaffe依赖库时,遇到的问题

利用命令for req in $(cat requirements.txt); do pip install $req; done安装pycaffe相关依赖库遇到问题:ImportError: No module named packaging.version
描述:这是因为采用 sudo apt-get install python-pip安装的pip有问题

  1. sudo apt-get remove python-pip #删除原有pip
  2. wget https://bootstrap.pypa.io/get-pip.py //获取特定pip,并进行安装
  3. sudo python get-pip.py

错误

  1. F0608 15:36:07.750129 6353 concat_layer.cpp:42] Check Failed: top_shape[j] == bottom[i]->shape(j) (63 vs. 62) All inputs must have the same shape,except at concat_axis.
  2. *** Check failure stack trace: ***
  3. Aborted (core dumped)

这个似乎是新版本的PVANET的问题,旧版本的PVANET没有该问题。问题出在lib文件的改变。

其他

问题: wget如何避免防火墙的影响?

解决方
在命令

  1. wget xxx
  2. #如wget https://www.dropBox.com/s/87zu4y6cvgeu8vs/test.model?dl=0 -O models/pvanet/full/test.model

之后加

  1. no-check-certificate

猜你在找的Ubuntu相关文章