Skip to content

How to Build on Mac

Karthik Ramamurthy edited this page Feb 22, 2016 · 3 revisions

Install and run Caffe on your MacBook Pro

This wiki catalogs steps I took to get Caffe building and running on my Mac. Make sure you have one that contains an NVIDIA graphics card (Mid-2014 or earlier with NVIDIA cards).

My system specifications as follows:

  1. MacBook Pro mid 2014 2.5Ghz Intel Core i7, 16GB, NVIDIA Geforce GT 750M
  2. OSX El Capitan Version 10.11.3
  3. CUDA 7.5 Driver/Runtime (Instructions below)
  4. cuDNN 4.0 (Instructions below)

Steps

  • Clone from this repo from branch caffe-0.14 - git clone https://github.com/kramamur/caffe/tree/caffe-0.14 ~/caffe

  • Make sure you have NVIDIA graphics driver and toolkit version 7.5 from CUDA Downloads

  • Alternatively you can install the toolkit separately (if you already have the driver installed). If you choose this separate path, make sure the Driver is newer than Toolkit (and uncheck the CUDA Driver option during toolkit install). We are going to use cuDNN 4.0 so you will need CUDA Toolkit version 7.0 or 7.5.

  • After CUDA Toolkit install, build the samples and run deviceQuery. You could also simply build the deviceQuery sample.

$ cd /usr/local/cuda/samples/1_Utilities/deviceQuery  
$ make  
$ ./deviceQuery
  • You should see an output like this
./deviceQuery Starting...

CUDA Device Query (Runtime API) version (CUDART static linking)

Detected 1 CUDA Capable device(s)

Device 0: "GeForce GT 750M"
CUDA Driver Version / Runtime Version          7.5 / 7.5
CUDA Capability Major/Minor version number:    3.0
Total amount of global memory:                 2048 MBytes (2147024896 bytes)
( 2) Multiprocessors, (192) CUDA Cores/MP:     384 CUDA Cores
GPU Max Clock rate:                            926 MHz (0.93 GHz)
...
  • Make sure this runs fine and does not give any errors. If no errors, your CUDA Toolkit install is fine.

  • Download cuDNN version 4 from NVIDIA's website here

    • The filename should look like this - cudnn-7.0-osx-x64-v4.0-prod.tgz
    • Uncompress the tarball into /usr/local
    • Make sure that cudnn.h goes into /usr/local/cuda/include and the library files into /usr/local/cuda/lib
  • Install the following libraries (abridged from Caffe OSX Install here). If you don't have Homebrew install it from here. Notice that we are not installing opencv (will explain why later). Also I am using openblas (as opposed to atlas) and it works fine.

$ brew install -vd snappy leveldb gflags glog szip lmdb
$ brew tap homebrew/science
$ brew install hdf5
$ brew install --build-from-source --with-python -vd protobuf
$ brew install --build-from-source -vd boost boost-python
$ brew install openblas python
  • Don't use Anaconda Python

    • At least on the first go this did not work for me
    • Brew python like specified above
    • Verify that which python shows /usr/local/bin/python
  • I did not install opencv via brew because it threw a version mismatch error with libpng during build (Issue here). Instead I used Macports.

    • Install Macports and OpenCV as documented here
    • I installed OpenCV latest version 3.1 (as of this writing). OpenCV 2.4.11 gave errors building some CUDA code for my card.
    • With OpenCV 3.1 I had to make a change to Makefile to add one OpenCV library that was not there. This change has been checked into this fork so if you cloned from here you don't need to do anything. But if you are curious, the change is to add opencv_imgcodecs to OpenCV LIBRARIES like below.
ifeq ($(USE_OPENCV), 1)
  LIBRARIES += opencv_imgcodecs opencv_core open cv_highgui opencv_imgproc
endif
  • The Makefile.config file in this fork should work with the above steps. It will use cuDNN. But if you deviated, feel free to copy Makefile.config.example into Makefile.config and make any changes necessary.

  • Makefile changes

    • The default Makefile assumes an Ubuntu installation for CUDA. I have made the Makefile changes though not ideal but work (CUDA libs go in /usr/local/cuda/lib as opposed to /usr/local/cuda/lib64 on Ubuntu). If you are cloning from this fork you should already have it. But if you are curious, the following change was made:
# cuDNN acceleration configuration.
ifeq ($(USE_CUDNN), 1)
        LIBRARIES += cudnn
        INCLUDE_DIRS += ${CUDA_INCLUDE_DIR}
        LIBRARY_DIRS += ${CUDA_LIB_DIR}
        COMMON_FLAGS += -DUSE_CUDNN
endif
  • Set some environment variables. You can add this to your ~/.profile or ~/.bashrc files and source them. My caffe was cloned into /usr/local/caffe. If you cloned into a different directory use appropriate DYLD_FALLBACK_LIBRARY_PATH.
export DYLD_FALLBACK_LIBRARY_PATH=/usr/local/cuda/lib:/usr/local/opt/openblas/lib:/usr/local/opt/opencv/lib:/usr/local/lib:/usr/lib:~/caffe/build/lib
export CUDA_HOME=/usr/local/cuda
export CUDA_TOOLKIT_INCLUDE=/usr/local/cuda/include
  • Build Caffe
$ make clean && make all -j8
$ make test -j8
$ make runtest

That's it! Have fun training and testing your favorite DNN's.


My initial trials were from Christopher's blog which is awesome - Christopher Bourez's Blog. You can always refer here if something in my list doesn't work for you.

Clone this wiki locally