Skip to content

Instantly share code, notes, and snippets.

@pyetras
Last active March 23, 2018 21:21
Show Gist options
  • Save pyetras/e7ef4bacc349d1b2e41f to your computer and use it in GitHub Desktop.
Save pyetras/e7ef4bacc349d1b2e41f to your computer and use it in GitHub Desktop.
Installing caffe (digits version) on OS X
  • Virtualenv

Create a new virtualenv with conda. Install digits' requirements:

conda create -n digits --file requirements.txt -c https://conda.binstar.org/slobodan -c https://conda.binstar.org/dougal -c https://conda.binstar.org/pyetras
  • Dependencies

First apply brew-boost.patch to brew repository (patches boost to version 1.5.5)

brew install --fresh snappy leveldb gflags glog szip lmdb opencv3 openblas
brew install --build-from-source --with-python --fresh protobuf
brew install --build-from-source --fresh boost boost-python

touch .../anaconda/envs/digits/lib/python2.7/site-packages/opencv3.pth
echo /usr/local/opt/opencv3/lib/python2.7/site-packages >> .../anaconda/envs/digits/lib/python2.7/site-packages/opencv3.pth
touch .../anaconda/envs/digits/lib/python2.7/site-packages/homebrew.pth
echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> .../anaconda/envs/digits/lib/python2.7/site-packages/homebrew.pth

Install CUDA-7.0.

  • Building caffe
git clone --branch caffe-0.12 https://github.com/NVIDIA/caffe.git

Apply Makefile.patch. Copy Makefile.config and replace ANACONDA_HOME with correct path to your virtualenv. Compile with

DYLD_FALLBACK_LIBRARY_PATH="/usr/local/lib:/Developer/NVIDIA/CUDA-7.0/lib:.../anaconda/envs/digits/lib/" LD_LIBRARY_PATH= CMAKE_CPLUS_INCLUDE_PATH="$CPLUS_INCLUDE_PATH;.../anaconda/envs/digits/include/python2.7" make all

substituting ... with correct anaconda path. Also run

make test
make runtest
make py
make distribute

with the same env. Now the $DYLD_FALLBACK_LIBRARY_PATH as above adding caffe-path/build/lib as well has to be included in your .bashrc or equivalent, as make doesn't link with absolute paths (probably due to OS X not being supported in Nvidia's caffe port).

From db0014657a52a44d5eef8ac983694483e7d3b990 Mon Sep 17 00:00:00 2001
From: Pietras <p@pyetras.com>
Date: Tue, 11 Aug 2015 15:06:29 +0200
Subject: [PATCH 1/2] oldbp
---
Library/Formula/boost-python.rb | 59 ++++++++++++++++++++++++-----------------
1 file changed, 34 insertions(+), 25 deletions(-)
diff --git a/Library/Formula/boost-python.rb b/Library/Formula/boost-python.rb
index d3610e6..b95ddc0 100644
--- a/Library/Formula/boost-python.rb
+++ b/Library/Formula/boost-python.rb
@@ -1,31 +1,40 @@
+class UniversalPython < Requirement
+ satisfy(:build_env => false) { archs_for_command("python").universal? }
+
+ def message; <<-EOS.undent
+ A universal build was requested, but Python is not a universal build
+
+ Boost compiles against the Python it finds in the path; if this Python
+ is not a universal build then linking will likely fail.
+ EOS
+ end
+end
+
+class UniversalPython3 < Requirement
+ satisfy(:build_env => false) { archs_for_command("python3").universal? }
+
+ def message; <<-EOS.undent
+ A universal build was requested, but Python 3 is not a universal build
+
+ Boost compiles against the Python 3 it finds in the path; if this Python
+ is not a universal build then linking will likely fail.
+ EOS
+ end
+end
+
class BoostPython < Formula
- desc "C++ library for C++/Python interoperability"
homepage "http://www.boost.org"
- url "https://downloads.sourceforge.net/project/boost/boost/1.58.0/boost_1_58_0.tar.bz2"
- sha256 "fdfc204fc33ec79c99b9a74944c3e54bd78be4f7f15e260c0e2700a36dc7d3e5"
+ url "https://downloads.sourceforge.net/project/boost/boost/1.57.0/boost_1_57_0.tar.bz2"
+ sha1 "e151557ae47afd1b43dc3fac46f8b04a8fe51c12"
head "https://github.com/boostorg/boost.git"
- stable do
- # don't explicitly link a Python framework
- # https://github.com/boostorg/build/pull/78
- patch do
- url "https://gist.githubusercontent.com/tdsmith/9026da299ac1bfd3f419/raw/b73a919c38af08941487ca37d46e711864104c4d/boost-python.diff"
- sha256 "9f374761ada11eecd082e7f9d5b80efeb387039d3a290f45b61f0730bce3801a"
- end
- end
-
- bottle do
- cellar :any
- sha256 "7f627fb1887ecaaea4b6b363d300a21c5274a1607c7dc64f2114d3794b5fec11" => :yosemite
- sha256 "6239719b00615abb9ce2bd40c680b14182325c2e1844c1bea410c002b42ce1db" => :mavericks
- sha256 "24acf2ddde1edfabe04239856dec6ce85e8652f3c0d5d8cf357b219c2bf3272a" => :mountain_lion
- end
-
option :universal
option :cxx11
- option "without-python", "Build without python 2 support"
+ depends_on :python => :recommended
depends_on :python3 => :optional
+ depends_on UniversalPython if build.universal? and build.with? "python"
+ depends_on UniversalPython3 if build.universal? and build.with? "python3"
if build.cxx11?
depends_on "boost" => "c++11"
@@ -105,12 +114,12 @@ class BoostPython < Formula
boost::python::def("greet", greet);
}
EOS
- Language::Python.each_python(build) do |python, _|
- pyflags = (`#{python}-config --includes`.strip +
- `#{python}-config --ldflags`.strip).split(" ")
- system ENV.cxx, "-shared", "hello.cpp", "-lboost_#{python}", "-o", "hello.so", *pyflags
+ Language::Python.each_python(build) do |python, version|
+ pycflags = `#{python}-config --includes`.strip
+ pyldflags = `#{python}-config --ldflags`.strip
+ system "#{ENV.cxx} -shared hello.cpp #{pycflags} #{pyldflags} -lboost_#{python} -o hello.so"
output = `#{python} -c "from __future__ import print_function; import hello; print(hello.greet())"`
- assert_match "Hello, world!", output
+ assert output.include?("Hello, world!")
end
end
end
--
2.1.0
From e91af7212a3330b34ae33b4367385d8c10cd42b3 Mon Sep 17 00:00:00 2001
From: Pietras <p@pyetras.com>
Date: Tue, 11 Aug 2015 18:00:44 +0200
Subject: [PATCH 2/2] asdf
---
Library/Formula/boost-python.rb | 5 +-
Library/Formula/boost.rb | 197 +++++++++++++++++++++++++++-------------
2 files changed, 137 insertions(+), 65 deletions(-)
diff --git a/Library/Formula/boost-python.rb b/Library/Formula/boost-python.rb
index b95ddc0..76941c4 100644
--- a/Library/Formula/boost-python.rb
+++ b/Library/Formula/boost-python.rb
@@ -24,8 +24,9 @@ end
class BoostPython < Formula
homepage "http://www.boost.org"
- url "https://downloads.sourceforge.net/project/boost/boost/1.57.0/boost_1_57_0.tar.bz2"
- sha1 "e151557ae47afd1b43dc3fac46f8b04a8fe51c12"
+ url "https://downloads.sourceforge.net/project/boost/boost/1.55.0/boost_1_55_0.tar.bz2"
+ sha1 "cef9a0cc7084b1d639e06cd3bc34e4251524c840"
+ revision 2
head "https://github.com/boostorg/boost.git"
option :universal
diff --git a/Library/Formula/boost.rb b/Library/Formula/boost.rb
index 83ff291..8f48924 100644
--- a/Library/Formula/boost.rb
+++ b/Library/Formula/boost.rb
@@ -1,35 +1,108 @@
+require 'formula'
+
+class UniversalPython < Requirement
+ satisfy(:build_env => false) { archs_for_command("python").universal? }
+
+ def message; <<-EOS.undent
+ A universal build was requested, but Python is not a universal build
+
+ Boost compiles against the Python it finds in the path; if this Python
+ is not a universal build then linking will likely fail.
+ EOS
+ end
+end
+
+class UniversalPython3 < Requirement
+ satisfy(:build_env => false) { archs_for_command("python3").universal? }
+
+ def message; <<-EOS.undent
+ A universal build was requested, but Python 3 is not a universal build
+
+ Boost compiles against the Python 3 it finds in the path; if this Python
+ is not a universal build then linking will likely fail.
+ EOS
+ end
+end
+
class Boost < Formula
- desc "Collection of portable C++ source libraries"
- homepage "http://www.boost.org"
- url "https://downloads.sourceforge.net/project/boost/boost/1.58.0/boost_1_58_0.tar.bz2"
- sha256 "fdfc204fc33ec79c99b9a74944c3e54bd78be4f7f15e260c0e2700a36dc7d3e5"
+ homepage 'http://www.boost.org'
+ url 'https://downloads.sourceforge.net/project/boost/boost/1.55.0/boost_1_55_0.tar.bz2'
+ sha1 'cef9a0cc7084b1d639e06cd3bc34e4251524c840'
+ revision 2
- head "https://github.com/boostorg/boost.git"
+ head 'https://github.com/boostorg/boost.git'
bottle do
cellar :any
- sha256 "9a3929bec0e9e9db36e005f57193433ac6b5ff9ff86b2ed3262b975d58488c19" => :yosemite
- sha256 "c5ad1f8591ed91c551658fd198ce31cf9f6a8026fcbfd5970a39cb479e64faa2" => :mavericks
- sha256 "7b154bf42d72bbb90c3017bb94b24fdd2e0605ceab8717283f5c9f456ac5c03d" => :mountain_lion
+ revision 4
+ sha1 "81b8843487a6f0017fac77b4bf58bdc20f3298fa" => :mavericks
+ sha1 "40089f76eddb25ac418032fa0055b6f0b6d76847" => :mountain_lion
+ sha1 "da4fb2a221fd83f50741f757eefe4bc38b5e910c" => :lion
end
env :userpaths
option :universal
- option "with-icu4c", "Build regexp engine with icu support"
- option "without-single", "Disable building single-threading variant"
- option "without-static", "Disable building static library variant"
- option "with-mpi", "Build with MPI support"
+ option 'with-icu', 'Build regexp engine with icu support'
+ option 'without-single', 'Disable building single-threading variant'
+ option 'without-static', 'Disable building static library variant'
+ option 'with-mpi', 'Build with MPI support'
option :cxx11
- deprecated_option "with-icu" => "with-icu4c"
-
- if build.cxx11?
- depends_on "icu4c" => [:optional, "c++11"]
- depends_on "open-mpi" => "c++11" if build.with? "mpi"
- else
- depends_on "icu4c" => :optional
- depends_on :mpi => [:cc, :cxx, :optional]
+ depends_on :python => :optional
+ depends_on :python3 => :optional
+ depends_on UniversalPython if build.universal? and build.with? "python"
+ depends_on UniversalPython3 if build.universal? and build.with? "python3"
+
+ if build.with?("python3") && build.with?("python")
+ odie "boost: --with-python3 cannot be specified when using --with-python"
+ end
+
+ if build.with? 'icu'
+ if build.cxx11?
+ depends_on 'icu4c' => 'c++11'
+ else
+ depends_on 'icu4c'
+ end
+ end
+
+ if build.with? 'mpi'
+ if build.cxx11?
+ depends_on 'open-mpi' => 'c++11'
+ else
+ depends_on :mpi => [:cc, :cxx, :optional]
+ end
+ end
+
+ stable do
+ # Patches boost::atomic for LLVM 3.4 as it is used on OS X 10.9 with Xcode 5.1
+ # https://github.com/Homebrew/homebrew/issues/27396
+ # https://github.com/Homebrew/homebrew/pull/27436
+ patch :p2 do
+ url "https://github.com/boostorg/atomic/commit/6bb71fdd.diff"
+ sha1 "ca8679011d5293a7fd02cb3b97dde3515b8b2b03"
+ end
+
+ patch :p2 do
+ url "https://github.com/boostorg/atomic/commit/e4bde20f.diff"
+ sha1 "b68f5536474c9f543879698299bd4975538a89eb"
+ end
+
+ # Patch fixes upstream issue reported here (https://svn.boost.org/trac/boost/ticket/9698).
+ # Will be fixed in Boost 1.56 and can be removed once that release is available.
+ # See this issue (https://github.com/Homebrew/homebrew/issues/30592) for more details.
+
+ patch :p2 do
+ url "https://github.com/boostorg/chrono/commit/143260d.diff"
+ sha1 "2600214608e7706116831d6ffc302d099ba09950"
+ end
+
+ # Patch boost::serialization for Clang
+ # https://svn.boost.org/trac/boost/ticket/8757
+ patch :p1 do
+ url "https://gist.githubusercontent.com/philacs/375303205d5f8918e700/raw/d6ded52c3a927b6558984d22efe0a5cf9e59cd8c/0005-Boost.S11n-include-missing-algorithm.patch"
+ sha1 "a37552d48e5c1c0507ee9d48fb82a3fa5e3bc9fa"
+ end
end
fails_with :llvm do
@@ -37,38 +110,59 @@ class Boost < Formula
cause "Dropped arguments to functions when linking with boost"
end
- needs :cxx11 if build.cxx11?
-
def install
# https://svn.boost.org/trac/boost/ticket/8841
- if build.with?("mpi") && build.with?("single")
+ if build.with? 'mpi' and build.with? 'single'
raise <<-EOS.undent
Building MPI support for both single and multi-threaded flavors
- is not supported. Please use "--with-mpi" together with
- "--without-single".
+ is not supported. Please use '--with-mpi' together with
+ '--without-single'.
+ EOS
+ end
+
+ if build.cxx11? and build.with? 'mpi' and (build.with? 'python' \
+ or build.with? 'python3')
+ raise <<-EOS.undent
+ Building MPI support for Python using C++11 mode results in
+ failure and hence disabled. Please don't use this combination
+ of options.
EOS
end
ENV.universal_binary if build.universal?
- # Force boost to compile with the desired compiler
+ # Force boost to compile using the appropriate GCC version.
open("user-config.jam", "a") do |file|
file.write "using darwin : : #{ENV.cxx} ;\n"
- file.write "using mpi ;\n" if build.with? "mpi"
+ file.write "using mpi ;\n" if build.with? 'mpi'
+
+ # Link against correct version of Python if python3 build was requested
+ if build.with? 'python3'
+ py3executable = `which python3`.strip
+ py3version = `python3 -c "import sys; print(sys.version[:3])"`.strip
+ py3prefix = `python3 -c "import sys; print(sys.prefix)"`.strip
+
+ file.write <<-EOS.undent
+ using python : #{py3version}
+ : #{py3executable}
+ : #{py3prefix}/include/python#{py3version}m
+ : #{py3prefix}/lib ;
+ EOS
+ end
end
- # libdir should be set by --prefix but isn't
- bootstrap_args = ["--prefix=#{prefix}", "--libdir=#{lib}"]
+ # we specify libdir too because the script is apparently broken
+ bargs = ["--prefix=#{prefix}", "--libdir=#{lib}"]
- if build.with? "icu4c"
- icu4c_prefix = Formula["icu4c"].opt_prefix
- bootstrap_args << "--with-icu=#{icu4c_prefix}"
+ if build.with? 'icu'
+ icu4c_prefix = Formula['icu4c'].opt_prefix
+ bargs << "--with-icu=#{icu4c_prefix}"
else
- bootstrap_args << "--without-icu"
+ bargs << '--without-icu'
end
# Handle libraries that will not be built.
- without_libraries = ["python"]
+ without_libraries = []
# The context library is implemented as x86_64 ASM, so it
# won't build on PPC or 32-bit builds
@@ -82,11 +176,12 @@ class Boost < Formula
# Boost.Log cannot be built using Apple GCC at the moment. Disabled
# on such systems.
without_libraries << "log" if ENV.compiler == :gcc || ENV.compiler == :llvm
- without_libraries << "mpi" if build.without? "mpi"
+ without_libraries << "python" if (build.without? 'python' \
+ and build.without? 'python3')
+ without_libraries << "mpi" if build.without? 'mpi'
- bootstrap_args << "--without-libraries=#{without_libraries.join(",")}"
+ bargs << "--without-libraries=#{without_libraries.join(',')}"
- # layout should be synchronized with boost-python
args = ["--prefix=#{prefix}",
"--libdir=#{lib}",
"-d2",
@@ -118,12 +213,12 @@ class Boost < Formula
end
end
- system "./bootstrap.sh", *bootstrap_args
+ system "./bootstrap.sh", *bargs
system "./b2", *args
end
def caveats
- s = ""
+ s = ''
# ENV.compiler doesn't exist in caveats. Check library availability
# instead.
if Dir["#{lib}/libboost_log*"].empty?
@@ -143,28 +238,4 @@ class Boost < Formula
s
end
-
- test do
- (testpath/"test.cpp").write <<-EOS.undent
- #include <boost/algorithm/string.hpp>
- #include <string>
- #include <vector>
- #include <assert.h>
- using namespace boost::algorithm;
- using namespace std;
-
- int main()
- {
- string str("a,b");
- vector<string> strVec;
- split(strVec, str, is_any_of(","));
- assert(strVec.size()==2);
- assert(strVec[0]=="a");
- assert(strVec[1]=="b");
- return 0;
- }
- EOS
- system ENV.cxx, "test.cpp", "-std=c++1y", "-lboost_system", "-o", "test"
- system "./test"
- end
end
--
2.1.0
## Refer to http://caffe.berkeleyvision.org/installation.html
# Contributions simplifying and improving our build system are welcome!
# cuDNN acceleration switch (comment to build without cuDNN).
USE_CUDNN := 0
# CPU-only switch (uncomment to build without GPU support).
# CPU_ONLY := 1
# To customize your choice of compiler, uncomment and set the following.
# N.B. the default for Linux is g++ and the default for OSX is clang++
# CUSTOM_CXX := g++
# CUDA directory contains bin/ and lib/ directories that we need.
CUDA_DIR := /Developer/NVIDIA/CUDA-7.0
# On Ubuntu 14.04, if cuda tools are installed via
# "sudo apt-get install nvidia-cuda-toolkit" then use this instead:
# CUDA_DIR := /usr
# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the *_50 lines for compatibility.
CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
-gencode arch=compute_20,code=sm_21 \
-gencode arch=compute_30,code=sm_30 \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_50,code=sm_50 \
-gencode arch=compute_50,code=compute_50
# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := open
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)!
BLAS_INCLUDE := /usr/local/opt/openblas/include
BLAS_LIB := /usr/local/opt/openblas/lib
# This is required only if you will compile the matlab interface.
# MATLAB directory should contain the mex binary in /bin.
# MATLAB_DIR := /usr/local
# MATLAB_DIR := /Applications/MATLAB_R2012b.app
# NOTE: this is required only if you will compile the python interface.
# We need to be able to find Python.h and numpy/arrayobject.h.
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
ANACONDA_HOME := $(HOME)/.env/anaconda/envs/digits
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python2.7 \
$(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \
# We need to be able to find libpythonX.X.so or .dylib.
# PYTHON_LIB := /usr/lib
PYTHON_LIB := $(ANACONDA_HOME)/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 /usr/local/opt/opencv3/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/local/opt/opencv3/lib
# Uncomment to use `pkg-config` to specify OpenCV library paths.
# (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)
# USE_PKG_CONFIG := 1
BUILD_DIR := build
DISTRIBUTE_DIR := distribute
# Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171
# DEBUG := 1
# 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 ?= @
# Adds timing info in logs
# BENCHMARK_DATA := 1
# BENCHMARK_SOLVER := 1
# shared object suffix name to differentiate branches
LIBRARY_NAME_SUFFIX := -nv
From 7cf273b7cea65058f1a05e314224105e4cbca582 Mon Sep 17 00:00:00 2001
From: Pietras <p@pyetras.com>
Date: Tue, 11 Aug 2015 19:33:13 +0200
Subject: [PATCH] makefile
---
Makefile | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/Makefile b/Makefile
index 3946b03..54c602c 100644
--- a/Makefile
+++ b/Makefile
@@ -178,7 +178,7 @@ ifneq ($(CPU_ONLY), 1)
endif
LIBRARIES += glog gflags protobuf leveldb snappy \
lmdb boost_system hdf5_hl hdf5 m \
- opencv_core opencv_highgui opencv_imgproc
+ opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs
PYTHON_LIBRARIES := boost_python python2.7
WARNINGS := -Wall -Wno-sign-compare
--
2.1.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment