mac下编译cpu only caffe并用xCode建caffe工程

mac编译caffe

好像又变容易了,直接git clone下载blvc源码,make.config里去掉了CPU_ONLY前面的注释,并没有安装任何依赖,也可能是自己mac上本来有,

 

xCode里调用caffe:

会报两次错误:

1. include <cblas.h>找不到:

locate cblas.h

自己mac上是在这里: /usr/local/Cellar/openblas/0.2.20_1/include/cblas.h

所以在Header Search Paths里增加/usr/local/Cellar/openblas/0.2.20_1/include

在Library Search Paths 里增加/usr/local/Cellar/openblas/0.2.20_1/lib

2. cv::imread 链接不到:

修改Other Link Flags:

-lopencv_calib3d -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_video -lopencv_videoio -lopencv_videostab -lglog -lhdf5 -lgflags -lprotobuf -lboost_system -lopenblas -lhdf5_hl -lleveldb -llmdb -lboost_filesystem -lm -lsnappy -lboost_thread-mt

即可。输出:

caffeTest: command line brew
usage: caffe <command> <args>

commands:
  train           train or finetune a model
  test            score a model
  device_query    show GPU diagnostic information
  time            benchmark model execution time

  No modules matched: use -help
Program ended with exit code: 0

-------------------2018.05.15---------------------------------------

以上,是基本的caffe,编译py-faster-rcnn时又出现几个坑:

lib目录下make找不到cython,pip install Cython安装,还有其他几个依赖项,编译能通过运行时需要用到

make caffe时clang找不到链接文件,具体是cv::imread()找不到,打开Make.config去掉OPENCV_VERSION := 3前面的注释就行了;

make pycaffe时找不到<numpy/arrayobject.h>,解决方案:手动复制numpy的include目录到系统目录

最后一个坑:

ImportError: dlopen(/Users/momo/wkspace/caffe_space/detection/py-faster-rcnn/tools/../caffe-fast-rcnn/python/caffe/_caffe.so, 2): Library not loaded: ../../build/lib/libcaffe.so.1.0.0-rc3
  Referenced from: /Users/momo/wkspace/caffe_space/detection/py-faster-rcnn/caffe-fast-rcnn/python/caffe/_caffe.so
  Reason: unsafe use of relative rpath ../../build/lib/libcaffe.so.1.0.0-rc3 in /Users/momo/wkspace/caffe_space/detection/py-faster-rcnn/tools/../caffe-fast-rcnn/python/caffe/_caffe.so with restricted binary

各种折腾PYTHONPATH和DYLD_PATH未果

看到make caffe时直接在caffe_ 目录下make -j8编译出来的是一个.a 一个.so 还一个.so软连接,在mac上。于是尝试用cmakeLists.txt编译, mkdir build + cd build + cmake _DCPU_ONLY=ON .. + make all -j8

然后又报了两个错:

ld: framework not found vecLib

blas版本和路径设置  这个要在CMakeLists.txt里添加include_directories("blas路径")

然后遇到hdf5:

Undefined symbols for architecture x86_64:
  "_H5LTfind_dataset", referenced from:
      caffe::SGDSolver<float>::RestoreSolverStateFromHDF5(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in sgd_solver.cpp.o

待解决,怎么也搞不定,还把之前装好的caffe玩坏了...

------------------2018.05.21-----------------------------------------

ssd caffe搞定以后发现也是调用.so库,名字都一样,但是ssd就没问题

otool -L _caffe.so:

ssd python目录下的.so多了一个@rpath,不是特别懂,但是找到了手动改链接路径的方法:

install_name_tool -change libcaffe.so.1.0.0-rc3 /Users/xxx/wkspace/caffe_space/detecton/py-faster-rcnn/caffe_fast_rcnn/build/lib/libcaffe.so.1.0.0-rc3 _caffe.so

再次otool -L查看,链接到了绝对路径:

_caffe.so:
    python/caffe/_caffe.so (compatibility version 0.0.0, current version 0.0.0)
    /Users/xxx/wkspace/caffe_space/detection/py-faster-rcnn/caffe-fast-rcnn/build/lib/libcaffe.so.1.0.0-rc3 (compatibility version 0.0.0, current version 0.0.0)

就可以用了。运行tools/demo.py时遇到新错误:

1. Unknown layer type: Python

Makefile.config里打开WITH_PYTHON_LAYER = 1,重新make就行了。

2. 'NoneType' object has no attribute '__getitem__' , 具体打印信息如下:

use cpu
Traceback (most recent call last):
  File "tools/demo.py", line 153, in <module>
    _, _= im_detect(net, im)
  File "/Users/momo/wkspace/caffe_space/detection/py-faster-rcnn/tools/../lib/fast_rcnn/test.py", line 154, in im_detect
    blobs_out = net.forward(**forward_kwargs)
  File "/Users/momo/wkspace/caffe_space/detection/py-faster-rcnn/tools/../caffe-fast-rcnn/python/caffe/pycaffe.py", line 105, in _Net_forward
    self._forward(start_ind, end_ind)
  File "/Users/momo/wkspace/caffe_space/detection/py-faster-rcnn/tools/../lib/rpn/proposal_layer.py", line 146, in forward
    keep = keep[:post_nms_topN]
TypeError: 'NoneType' object has no attribute '__getitem__'

看到是nms的问题,找到lib/nms_wrapper.py,定位到config的问题,修改lib/fast_rcnn/config.py:

# Use GPU implementation of non-maximum suppression
__C.USE_GPU_NMS = False

注释掉lib/setup.py第58行:

CUDA = locate_cuda()

在lib目录下重新make一下就可以了。

测试时用到了lib/fast_rcnn/test.py

会调用lib/nms_wrapper.py,注释掉其中与gpu_nms相关的东西即可:

from fast_rcnn.config import cfg
# from nms.gpu_nms import gpu_nms
from nms.cpu_nms import cpu_nms

def nms(dets, thresh, force_cpu=False):
    """Dispatch to either CPU or GPU NMS implementations."""

    if dets.shape[0] == 0:
        return []
    return cpu_nms(dets, thresh)

 

-------------------2018.05.17---------------------------------------

玩坏的原因是caffe依赖的openblas依赖的库文件找不到,可能是在家里brew update openblas时突然断网之类的吧,反正重新update一下又ok了。

BLVC版和liu Wei的ssd都可以,faster rcnn还是不行

ssd xCode工程遇两个坑:

1. 编译报错: data_transformer.hpp AnnotatedDatum未定义: 需要重新编译ssd版的proto.cc proto.h并复制到工程目录下, blvc版本caffe里编译出来的没有

2. boost找不到链接文件,大神的git issue#839

-------------------2018.09.17---------------------------------------

上述装好的caffe在被同事忽悠用homebrew更新opencv以后坏掉了,症状是sh调用ok,python无法调用,报错:segment fault

参考这里解决

要修改的PYTHON_LIB路径查找方法:

进入python,

import sys
print(sys.path)

从打印出来的一大堆信息种拎出如下字样即可:

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/

 

-------------------2018.11.27----------------------------------------

服务器上拉回来的工程首先照上面改gpu_nms问题,然后参考这里解决'ProposalLayer' object has no attribute 'param_str'的问题,yaml.load(self.param_str) 加上下划线param_str_

其实不是很懂为什么,服务器上没有下划线,test.prototxt里面也没有下划线

posted @ 2018-05-11 14:39 tszs_song 阅读(...) 评论(...) 编辑 收藏