Mac下安装Caffe

2016-11-14 更新:

一些读者反映了新版 Caffe 在 Mac 下编译时找不到 cblas.h 这个问题,始吾弗信,今亲测也发现了这个问题,特别记录下解决方法。

罪魁祸首就是 Makefile Line 361~399:

# BLAS configuration (default = ATLAS)
BLAS ?= atlas
ifeq ($(BLAS), mkl)
	# MKL
	LIBRARIES += mkl_rt
	COMMON_FLAGS += -DUSE_MKL
	MKLROOT ?= /opt/intel/mkl
	BLAS_INCLUDE ?= $(MKLROOT)/include
	BLAS_LIB ?= $(MKLROOT)/lib $(MKLROOT)/lib/intel64
else ifeq ($(BLAS), open)
	# OpenBLAS
	LIBRARIES += openblas
else
	# ATLAS
	ifeq ($(LINUX), 1)
		ifeq ($(BLAS), atlas)
			# Linux simply has cblas and atlas
			LIBRARIES += cblas atlas
		endif
	else ifeq ($(OSX), 1)
		# OS X packages atlas as the vecLib framework
		LIBRARIES += cblas
		# 10.10 has accelerate while 10.9 has veclib
		XCODE_CLT_VER := $(shell pkgutil --pkg-info=com.apple.pkg.CLTools_Executables | grep 'version' | sed 's/[^0-9]*\([0-9]\).*/\1/')
		XCODE_CLT_GEQ_7 := $(shell [ $(XCODE_CLT_VER) -gt 6 ] && echo 1)
		XCODE_CLT_GEQ_6 := $(shell [ $(XCODE_CLT_VER) -gt 5 ] && echo 1)
		ifeq ($(XCODE_CLT_GEQ_7), 1)
			BLAS_INCLUDE ?= /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/Headers
		else ifeq ($(XCODE_CLT_GEQ_6), 1)
			BLAS_INCLUDE ?= /System/Library/Frameworks/Accelerate.framework/Versions/Current/Frameworks/vecLib.framework/Headers/
			LDFLAGS += -framework Accelerate
		else
			BLAS_INCLUDE ?= /System/Library/Frameworks/vecLib.framework/Versions/Current/Headers/
			LDFLAGS += -framework vecLib
		endif
	endif
endif
INCLUDE_DIRS += $(BLAS_INCLUDE)
LIBRARY_DIRS += $(BLAS_LIB)

这里可以发现,只有 atlas 这个分支考虑了 OSX 系统,其他选项需要在 Makefile.config 里面进一步指定路径。

然而问题也就出在这里。我旧版本 Caffe 好好的,换新的就出问题了。于是找到旧版本,Makefile 相同位置内容为:

# BLAS configuration (default = ATLAS)
BLAS ?= atlas
ifeq ($(BLAS), mkl)
	# MKL
	LIBRARIES += mkl_rt
	COMMON_FLAGS += -DUSE_MKL
	MKLROOT ?= /opt/intel/mkl
	BLAS_INCLUDE ?= $(MKLROOT)/include
	BLAS_LIB ?= $(MKLROOT)/lib $(MKLROOT)/lib/intel64
else ifeq ($(BLAS), open)
	# OpenBLAS
	LIBRARIES += openblas
else
	# ATLAS
	ifeq ($(LINUX), 1)
		ifeq ($(BLAS), atlas)
			# Linux simply has cblas and atlas
			LIBRARIES += cblas atlas
		endif
	else ifeq ($(OSX), 1)
		# OS X packages atlas as the vecLib framework
		LIBRARIES += cblas
		# 10.10 has accelerate while 10.9 has veclib
		XCODE_CLT_VER := $(shell pkgutil --pkg-info=com.apple.pkg.CLTools_Executables | grep 'version' | sed 's/[^0-9]*\([0-9]\).*/\1/')
		XCODE_CLT_GEQ_6 := $(shell [ $(XCODE_CLT_VER) -gt 5 ] && echo 1)
		ifeq ($(XCODE_CLT_GEQ_6), 1)
			BLAS_INCLUDE ?= /System/Library/Frameworks/Accelerate.framework/Versions/Current/Frameworks/vecLib.framework/Headers/
			LDFLAGS += -framework Accelerate
		else
			BLAS_INCLUDE ?= /System/Library/Frameworks/vecLib.framework/Versions/Current/Headers/
			LDFLAGS += -framework vecLib
		endif
	endif
endif
INCLUDE_DIRS += $(BLAS_INCLUDE)
LIBRARY_DIRS += $(BLAS_LIB)

直接将这部分替换掉新版的,再编译就不会报找不到 cblas.h 问题了。

我采取了更激进的措施。

# BLAS configuration (default = ATLAS)
BLAS ?= atlas
ifeq ($(BLAS), mkl)
        # MKL
        LIBRARIES += mkl_rt
        COMMON_FLAGS += -DUSE_MKL
        MKLROOT ?= /opt/intel/mkl
        BLAS_INCLUDE ?= $(MKLROOT)/include
        BLAS_LIB ?= $(MKLROOT)/lib $(MKLROOT)/lib/intel64
else ifeq ($(BLAS), open)
        # OpenBLAS
        LIBRARIES += openblas
else
        # ATLAS
        ifeq ($(LINUX), 1)
                ifeq ($(BLAS), atlas)
                        # Linux simply has cblas and atlas
                        LIBRARIES += cblas atlas
                endif
        else ifeq ($(OSX), 1)
                # OS X packages atlas as the vecLib framework
                LIBRARIES += cblas
                # 10.10 has accelerate while 10.9 has veclib
                XCODE_CLT_VER := $(shell pkgutil --pkg-info=com.apple.pkg.CLTools_Executables | grep 'version' | sed 's/[^0-9]*\([0-9]\).*/\1/')
                BLAS_INCLUDE ?= /System/Library/Frameworks/Accelerate.framework/Versions/Current/Frameworks/vecLib.framework/Headers/
                LDFLAGS += -framework Accelerate
        endif
endif
INCLUDE_DIRS += $(BLAS_INCLUDE)
LIBRARY_DIRS += $(BLAS_LIB)

干脆去掉了判断。因为我相信自己判断是对的。



——【深度学习:21 天实战 Caffe】——



2015-11-26 原文:

最近将工作笔记本从X1换成了MBA,真是不习惯,连复制粘贴都不那么顺手了。


好在命令行和Linux差不多,于是想搞个Caffe在Mac上跑跑,哪怕CPU版本的也行。


参考:http://caffe.berkeleyvision.org/install_osx.html

首先安装homebrew工具,相当于Mac下的yum或apt

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"


等待片刻,安装成功,然后安装Caffe依赖:

$ brew install -vd snappy leveldb gflags glog szip lmdb

$ brew tap homebrew/science

$ brew install hdf5 opencv

$ brew install protobuf boost

下载Caffe源码:

$ git clone https://github.com/bvlc/caffe.git

$ cd caffe/

$ mv Makefile.config.example Makefile.config

修改Makefile.config,打开CPU_ONLY选项,保存。

$ make -j

编译成功。

运行一个例子,我们需要下载几个额外文件。

$ cd data/ilsvrc12/

$ vi get_ilsvrc_aux.sh

将这行:

wget http://dl.caffe.berkeleyvision.org/caffe_ilsvrc12.tar.gz

改为:

curl http://dl.caffe.berkeleyvision.org/caffe_ilsvrc12.tar.gz > caffe_ilsvrc12.tar.gz

然后保存,执行。下载数据到当前目录。

下载模型:

$ cd ../../models/bvlc_reference_caffenet/

$ curl http://dl.caffe.berkeleyvision.org/bvlc_reference_caffenet.caffemodel > bvlc_reference_caffenet.caffemodel

回到根目录:

$ cd ../..

执行:

$./build/examples/cpp_classification/classification.bin models/bvlc_reference_caffenet/deploy.prototxt models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel data/ilsvrc12/imagenet_mean.binaryproto data/ilsvrc12/synset_words.txt examples/images/cat.jpg
---------- Prediction for examples/images/cat.jpg ----------
0.3132 - "n02123045 tabby, tabby cat"
0.2380 - "n02123159 tiger cat"
0.1235 - "n02124075 Egyptian cat"
0.1005 - "n02119022 red fox, Vulpes vulpes"
0.0716 - "n02127052 lynx, catamount"


评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值