Skip to content

Instantly share code, notes, and snippets.

@jasonbunk
Last active September 9, 2018 21:30
Show Gist options
  • Save jasonbunk/9d61a49cc8402f32b1dabcf7b85bdcc6 to your computer and use it in GitHub Desktop.
Save jasonbunk/9d61a49cc8402f32b1dabcf7b85bdcc6 to your computer and use it in GitHub Desktop.
Build caffe2 locally (not in /usr/lib) for compatibility with Python virtual environments (including Python 3)
#!/bin/bash
set -ex
THISSCRIPTPATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd $THISSCRIPTPATH
echo "This script is for building Caffe2 locally (not in /usr/lib) on Ubuntu 16.04"
echo "Can be used for Python virtual environments"
echo "This assumes OpenCV is already installed: https://www.pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/"
my_python_version=$(python -c "import sys; print('.'.join(map(str, sys.version_info[:2])))")
#do_sudo_cmds=1
####################################################################
echo "installing dependencies..."
if [ -z ${do_sudo_cmds} ]; then echo "skipping sudo installs"; else
sudo apt-get update
sudo apt-get install -y --no-install-recommends \
build-essential \
git \
libgoogle-glog-dev \
libgtest-dev \
libiomp-dev \
libleveldb-dev \
liblmdb-dev \
libopenmpi-dev \
libsnappy-dev \
libprotobuf-dev \
openmpi-bin \
openmpi-doc \
protobuf-compiler \
libgflags-dev
exit 0
fi
pip install \
future \
numpy \
scipy \
cython \
protobuf \
pyyaml
#######################
# require latest cmake: https://github.com/caffe2/caffe2/issues/1810
pip install --upgrade cmake
####################################################################
CAFFE2PAT=$THISSCRIPTPATH/pytorch
# use in a virtual environment, so this can install to the virtual env's path
# if you are using docker, or don't mind,
# change this and remove the specified CMAKE_INSTALL_PREFIX below to let it install to system paths
caf2releasepath=$THISSCRIPTPATH
if ! [ -d pytorch/caffe2 ]; then
git clone --recursive https://github.com/pytorch/pytorch.git
fi
# Caffe2 currently has trouble with Ubuntu 16.04's libeigen3-dev
# force it to use its own thirdparty/eigen by forcing "EIGEN3_FOUND=0"
sed -i -e 's/find_package(Eigen3)//g' $CAFFE2PAT/cmake/Dependencies.cmake
(cd pytorch && mkdir -p build && cd build/ && \
cmake -D CMAKE_INSTALL_PREFIX=$caf2releasepath \
-DEIGEN3_FOUND=0 \
-DUSE_ROCKSDB=OFF \
-DCUDA_ARCH_NAME=Manual \
-DCUDA_ARCH_BIN="50 52 61" \
-DCUDA_ARCH_PTX="50 52 61" \
-D PYTHON_EXECUTABLE:FILEPATH=$(which python) \
$(python $CAFFE2PAT/scripts/get_python_cmake_flags.py) .. \
&& make all -j8 && make install -j8)
# verify the installation
python -c "from caffe2.python import core"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment