1 安装依赖包
brew install -vd snappy leveldb gflags glog szip lmdb
brew install openblas
brew install hdf5 opencv
# boost 需要在后续配置中注意
brew install boost boost-python3
# 编译protobuf3.7.0
cd protobuf-3.7.0
./autogen.sh
./configure
sudo make -j8
sudo make install -j8
2 下载caffe
git clone https://github.com/BVLC/caffe.git
cd caffe/
cp Makefile.config.example Makefile.config
3 配置文件
-
配置使用opencv4
./include/caffe/common.hpp第70行后新增如下内容:
// Supporting OpenCV4 #if (CV_MAJOR_VERSION == 4) #define CV_LOAD_IMAGE_COLOR cv::IMREAD_COLOR #define CV_LOAD_IMAGE_GRAYSCALE cv::IMREAD_GRAYSCALE #endif
-
配置Makefile.config
# CPU-only switch (uncomment to build without GPU support). CPU_ONLY := 1 # uncomment to disable IO dependencies and corresponding data layers USE_OPENCV := 0 # USE_LEVELDB := 0 # USE_LMDB := 0 # This code is taken from https://github.com/sh1r0/caffe-android-lib USE_HDF5 := 1 # Uncomment if you're using OpenCV 3 OPENCV_VERSION := 4 # open for OpenBlas BLAS := open # Homebrew puts openblas in a directory that is not on the standard search path BLAS_INCLUDE := $(shell brew --prefix openblas)/include BLAS_LIB := $(shell brew --prefix openblas)/lib # PYTHON_INCLUDE := # /usr/local/lib/python2.7/site-packages/numpy/core/include # Uncomment to use Python 3 (default is Python 2) PYTHON_LIBRARIES := boost_python3 python3.7m PYTHON_INCLUDE := /usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/include/python3.7m \ /usr/local/lib/python3.7/site-packages/numpy/core/include/numpy # We need to be able to find libpythonX.X.so or .dylib. PYTHON_LIB := /usr/local/lib # Uncomment to support layers written in Python (will link against Python libs) WITH_PYTHON_LAYER := 1 # Whatever else you find you need goes here. INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib # N.B. both build and distribute dirs are cleared on `make clean` BUILD_DIR := build DISTRIBUTE_DIR := distribute # The ID of the GPU that 'make runtest' will use to run unit tests. TEST_GPUID := 0 # enable pretty build (comment to see full commands) Q ?= @
4 预编译
mkdir build
cd build
cmake ..
5 修改编译后的文件配置
-
修改CaffeConfig.cmake第53行
set(Caffe_CPU_ONLY ON)
-
修改CMakeCache.txt
//Path to a program. PYTHON_EXECUTABLE:FILEPATH=/usr/local/bin/python3 //Path to a file. PYTHON_INCLUDE_DIR:PATH=/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/include/python3.7m //Path to a library. PYTHON_LIBRARY:FILEPATH=/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/libpython3.7m.dylib //Flags used by the CXX compiler during all build types. CMAKE_CXX_FLAGS:STRING=-std=c++11 //Boost python library (debug) Boost_PYTHON_LIBRARY_DEBUG:FILEPATH=/usr/local/Cellar/boost-python3/1.68.0/lib/libboost_python37-mt.dylib //Boost python library (release) Boost_PYTHON_LIBRARY_RELEASE:FILEPATH=/usr/local/Cellar/boost-python3/1.68.0/lib/libboost_python37-mt.dylib //Build Caffe without CUDA support CPU_ONLY:BOOL=ON
6 再编译
cmake ..
sudo make all -j8
sudo make pycaffe -j8
7 问题
-
Could NOT find vecLib (missing: vecLib_INCLUDE_DIR)
# 修改caffe下文件: cmake/Modules/FindvecLib.cmake 的Line 19修改为 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/Accelerate.framework/Versions/Current/Frameworks/vecLib.framework/Headers/
-
ld: symbol(s) not found for architecture x86_64
cv::imread(cv::String const&, int)”, referenced from: caffe::WindowDataLayer::InternalThreadEntry() in window_data_layer.o caffe::WindowDataLayer::InternalThreadEntry() in window_data_layer.o caffe::ReadImageToCVMat(std::string const&, int, int, bool) in io.o “cv::imdecode(cv::_InputArray const&, int)”, referenced from: caffe::DecodeDatumToCVMat(caffe::Datum const&, int, int, bool) in io.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)[/plain]
出现上述信息的原因是cv::imread是在opencv_imgcodecs中定义的,将-lopencv_imgcodecs在Linker Flags也加上即可,即在Makefile里搜索pthread 把这个选项紧跟在后面。
-
No module named caffe
vim ~/.bash_profile # 文件中添加 export PYTHONPATH=~/caffe/python:$PYTHONPATH # 重启 source ~/.bash_profile
-
ModuleNotFoundError: No module named 'google'
pip3 install protobuf
网友评论