Last active
August 29, 2015 14:24
-
-
Save etrepum/5b79a1dce88c2d17b0c7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e | |
DEEPDREAM="${HOME}/Library/DeepDream" | |
BREW_PREFIX="/usr/local" | |
join () { | |
local IFS="$1" | |
shift | |
echo "$*" | |
} | |
osastring () { | |
local res=${1//\\/\\\\} | |
echo "\"${res//"/\\"}\"" | |
} | |
dialog () { | |
local title="$1" | |
local dialog="$2" | |
local buttons=( "${@:3}" ) | |
local -a buttonstr | |
for i in "${!buttons[@]}"; do | |
buttonstr[$i]=$(osastring "${buttons[$i]}") | |
done | |
local cancel=${buttonstr[${#buttonstr[@]} - 1]} | |
echo $(osascript -e "try | |
tell app \"SystemUIServer\" | |
set answer to button returned of (display dialog $(osastring "${dialog}") with title $(osastring "${title}") buttons {$(join ", " "${buttonstr[@]}")} default button ${buttonstr[0]} cancel button ${cancel}) | |
end | |
on error number -128 | |
set answer to ${cancel} | |
end | |
activate app (path to frontmost application as text) | |
answer") | |
} | |
xcode_path () { | |
xcode-select -p 2>/dev/null | |
} | |
install_xcode () { | |
while ! xcode_path >/dev/null; do | |
echo "Install Xcode or Command Line Tools. Press return to check again or Ctrl-C to abort." | |
xcode-select --install | |
read | |
done | |
echo "Developer tools at: $(xcode_path)" | |
} | |
homebrew_path () { | |
/usr/bin/which brew | |
} | |
install_homebrew () { | |
while ! homebrew_path >/dev/null; do | |
echo "Installing Homebrew." | |
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" | |
hash -r | |
done | |
echo "Homebrew detected at: $(homebrew_path)" | |
} | |
cuda7_path () { | |
local p | |
for p in "/Developer/NVIDIA/CUDA-7.5" "/Developer/NVIDIA/CUDA-7.0"; do | |
if [ -e "${p}" ]; then | |
echo "${p}" | |
return 0 | |
fi | |
done | |
return 1 | |
} | |
install_cuda7 () { | |
while ! cuda7_path >/dev/null; do | |
local r=$(dialog "CUDA 7.0 or 7.5 Not Detected" \ | |
"CUDA is required to build a fast version of Caffe (the deep learning framework)" \ | |
"Download" \ | |
"Abort") | |
case $r in | |
Download) | |
open "https://developer.nvidia.com/cuda-downloads" | |
echo "Press return after installing the PKG" | |
read | |
;; | |
*) | |
echo "ABORTING" | |
exit 1 | |
;; | |
esac | |
done | |
echo "CUDA detected at: $(cuda7_path)" | |
} | |
install_cudnn () { | |
local CUDNN_INSTALL_PATHS=( "${CUDA7_PATH}/lib/libcudnn.dylib" "${CUDA7_PATH}/include/cudnn.h" ) | |
local CUDNN_DOWNLOAD_PATH="${HOME}/Downloads/cudnn-6.5-osx-v2.tgz" | |
while [ "${#CUDNN_INSTALL_PATHS[@]}" -ne "$(ls -1 "${CUDNN_INSTALL_PATHS[@]}" 2>/dev/null| wc -l)" ]; do | |
if [ -e "${CUDNN_DOWNLOAD_PATH}" ]; then | |
echo "Extracting cuDNN" | |
local d=$(mktemp -dt "cuDNN") | |
tar -xf "${CUDNN_DOWNLOAD_PATH}" -C "${d}" | |
sudo sh -c "mv \"${d}\"/*/libcudnn* \"${CUDA7_PATH}/lib\" && mv \"${d}\"/*/*.h \"${CUDA7_PATH}/include\"" | |
rm -rf "${d}" | |
else | |
echo "cuDNN download not found at ${CUDNN_DOWNLOAD_PATH}" | |
local r=$(dialog "cuDNN download not found" \ | |
"cuDNN is also required to build a fast Caffe." \ | |
"Download" \ | |
"Abort") | |
case $r in | |
Download) | |
open "https://developer.nvidia.com/cudnn" | |
echo "Press return after downloading $(basename "${CUDNN_DOWNLOAD_PATH}")" | |
read | |
;; | |
*) | |
echo "ABORTING" | |
exit 1 | |
;; | |
esac | |
fi | |
done | |
echo "cuDNN detected at: ${CUDA7_PATH}" | |
} | |
brew_tap () { | |
local repo="$1" | |
if ! (brew tap | grep -Fx "${repo}" >/dev/null); then | |
brew tap "${repo}" | |
fi | |
} | |
install_packages () { | |
local IFS=$'\n' | |
local -a expect=($(echo "$*" | sort)) | |
local -a packages=($(brew ls -1 | grep -Fx "${expect[*]}" | sort)) | |
local -a diffs=($(comm -23 <(echo "${expect[*]}") <(echo "${packages[*]}"))) | |
if [ "${#diffs[@]}" -gt "0" ]; then | |
brew install "${diffs[@]}" | |
hash -r | |
fi | |
} | |
install_protobuf () { | |
if ! ( brew ls protobuf 2>/dev/null | grep -F python2.7/site-packages/homebrew-protobuf.pth >/dev/null ); then | |
brew install --build-from-source --with-python protobuf | |
fi | |
} | |
install_boost_1_57 () { | |
if ! ( brew ls boost 2>/dev/null | grep -F boost/1.57.0/lib/ >/dev/null ); then | |
brew install --build-from-source \ | |
https://raw.githubusercontent.com/Homebrew/homebrew/6fd6a9b6b2f56139a44dd689d30b7168ac13effb/Library/Formula/boost.rb | |
brew switch boost 1.57.0 | |
fi | |
if ! ( brew ls boost-python 2>/dev/null | grep -F boost-python/1.57.0/lib/ >/dev/null ); then | |
brew install --build-from-source \ | |
https://raw.githubusercontent.com/Homebrew/homebrew/3141234b3473717e87f3958d4916fe0ada0baba9/Library/Formula/boost-python.rb | |
brew switch boost-python 1.57.0 | |
fi | |
} | |
create_destination_dir () { | |
if [ ! -e "${DEEPDREAM}" ]; then | |
mkdir -p "${DEEPDREAM}" | |
fi | |
echo "Installing to: ${DEEPDREAM}" | |
} | |
install_virtualenv () { | |
if [ ! -e "${BREW_PREFIX}/bin/virtualenv" ]; then | |
"${BREW_PREFIX}/bin/pip2.7" install virtualenv | |
hash -r | |
fi | |
} | |
activate_virtualenv () { | |
if [ ! -e "${DEEPDREAM}/virtualenv/bin/activate" ]; then | |
"${BREW_PREFIX}/bin/virtualenv" -p "${BREW_PREFIX}/bin/python2.7" "${DEEPDREAM}/virtualenv" | |
fi | |
source "${DEEPDREAM}/virtualenv/bin/activate" | |
} | |
install_caffe () { | |
if ! python -c 'import caffe' 2>/dev/null; then | |
clone_caffe | |
( set -e | |
cd "${DEEPDREAM}/caffe" | |
pip install --requirement python/requirements.txt | |
patch_caffe | |
build_caffe | |
) | |
echo "${DEEPDREAM}/caffe/distribute/python" > "${DEEPDREAM}/virtualenv/lib/python2.7/site-packages/caffe.pth" | |
fi | |
} | |
clone_caffe () { | |
if [ ! -e "${DEEPDREAM}/caffe" ]; then | |
git clone https://github.com/BVLC/caffe.git "${DEEPDREAM}/caffe" | |
fi | |
} | |
framework_base () { | |
echo "/System/Library/Frameworks/$1.framework/Versions/Current" | |
} | |
sdk_path () { | |
local sdk | |
local p | |
for sdk in "MacOSX10.9.sdk" "MacOSX10.10.sdk"; do | |
p="${XCODE_PATH}/Platforms/MacOSX.platform/Developer/SDKs/${sdk}" | |
if [ -e "${p}" ]; then | |
echo "${p}" | |
return 0 | |
fi | |
done | |
} | |
patch_caffe () { | |
cat <<EOT > Makefile.config | |
USE_CUDNN := 1 | |
CUDA_DIR := ${CUDA7_PATH} | |
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 := atlas | |
BLAS_INCLUDE := $(sdk_path)$(framework_base Accelerate)/Frameworks/vecLib.framework/Versions/Current/Headers | |
PYTHON_INCLUDE := $(python -c 'import distutils; print(distutils.sysconfig_get_python_inc())') \\ | |
$(python -c 'import numpy.core, os; print(os.path.dirname(numpy.core.__file__))')/include | |
PYTHON_LIB := $(python -c 'import distutils; print(distutils.sysconfig_get_config_vars()["LIBDIR"])') \\ | |
$(python -c 'import numpy, os; print(os.path.dirname(numpy.__file__))')/lib | |
WITH_PYTHON_LAYER := 1 | |
INCLUDE_DIRS := \$(PYTHON_INCLUDE) ${BREW_PREFIX}/include | |
LIBRARY_DIRS := \$(PYTHON_LIB) ${BREW_PREFIX}/lib /usr/lib | |
BUILD_DIR := build | |
DISTRIBUTE_DIR := distribute | |
TEST_GPUID := 0 | |
Q ?= @ | |
EOT | |
if grep 'framework vecLib' Makefile >/dev/null; then | |
# The detection in caffe's Makefile is busted | |
sed -i .bak 's/framework vecLib/framework Accelerate/' Makefile | |
fi | |
} | |
build_caffe () { | |
local CPUS=$(sysctl -n hw.ncpu) | |
make -j${CPUS} | |
make pycaffe -j${CPUS} | |
if [ -e distribute ]; then | |
rm -rf distribute | |
fi | |
make distribute | |
install_name_tool \ | |
-add_rpath "${PWD}/distribute/lib" \ | |
-add_rpath "${CUDA7_PATH}/lib" \ | |
distribute/python/caffe/_caffe.so | |
} | |
install_ipython () { | |
if ! python -c "import pkg_resources; pkg_resources.require('ipython[notebook]')" 2>/dev/null; then | |
pip install ipython[notebook] | |
fi | |
} | |
clone_deepdream () { | |
if [ ! -e "${DEEPDREAM}/deepdream" ]; then | |
git clone https://github.com/google/deepdream.git "${DEEPDREAM}/deepdream" | |
fi | |
} | |
download_googlenet_model () { | |
local model="${DEEPDREAM}/caffe/models/bvlc_googlenet/bvlc_googlenet.caffemodel" | |
if [ ! -e "${model}" ]; then | |
echo "Downloading googlenet model" | |
curl http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel > "${model}.tmp" | |
mv "${model}.tmp" "${model}" | |
fi | |
} | |
install_xcode | |
XCODE_PATH=$(xcode_path) | |
install_cuda7 | |
CUDA7_PATH=$(cuda7_path) | |
install_cudnn | |
install_homebrew | |
BREW_PREFIX=$(brew --prefix) | |
brew_tap homebrew/science | |
install_packages snappy leveldb gflags glog szip lmdb python hdf5 opencv | |
install_protobuf | |
install_boost_1_57 | |
create_destination_dir | |
install_virtualenv | |
activate_virtualenv | |
install_caffe | |
install_ipython | |
clone_deepdream | |
download_googlenet_model | |
(cd "${DEEPDREAM}/deepdream" && ipython notebook dream.ipynb) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment