Last active
May 17, 2018 16:59
-
-
Save troykelly/9aaba3ab76363930c23e7d83a0ed05ee to your computer and use it in GitHub Desktop.
This file contains 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 | |
# Builds tensorflow on a Pi 3 b+ | |
# | |
# Start with a fresh image, put a USB stick in the side (all data will be lost) | |
# This is not fault tolerant, and barely tested | |
# | |
# With guidance from | |
# https://medium.com/ml-everything/offline-object-detection-and-tracking-on-a-raspberry-pi-fddb3bde130 | |
# | |
# Run with: | |
# source <(curl -s https://gist.githubusercontent.com/troykelly/9aaba3ab76363930c23e7d83a0ed05ee/raw/pi_build_tensorflow.sh) | |
# | |
export SWAPDISK=/dev/sda && \ | |
export BAZELVER=0.12.0 && \ | |
export TENSORVER=1.8.0 && \ | |
export REMOVE_PKGS="openjdk-8-jre-headless openjdk-8-jre gcj-6-jre gcj-6-jre-headless gcj-6-jre-lib gcj-jre gcj-jre-headless oracle-java7-jdk" && \ | |
export BUILD_PKGS="bsdtar pkg-config zip g++ zlib1g-dev unzip python3-pip python3-numpy swig python3-dev gcc-4.8 g++-4.8 python3-numpy libblas-dev liblapack-dev libatlas-base-dev gfortran python3-setuptools python3-scipy python3-h5py git oracle-java8-jdk" && \ | |
export RUNTIME_PKGS="" && \ | |
sudo raspi-config --expand-rootfs && \ | |
sudo partprobe && \ | |
mkdir -p $HOME/tmp && \ | |
export TMPDIR=$HOME/tmp && \ | |
sudo umount $SWAPDISK || echo "${SWAPDISK} not mounted" && \ | |
sudo mkswap $SWAPDISK && \ | |
sudo grep -q -F "${SWAPDISK}" /etc/fstab || sudo sed -i 's!# a swapfile!'"${SWAPDISK}"' none swap sw,pri=5 0 0\n# a swapfile!g' /etc/fstab && \ | |
sudo swapon -a && \ | |
sudo apt-get -y update && \ | |
sudo apt-get -y upgrade && \ | |
sudo raspi-config nonint do_camera 0 && \ | |
sudo raspi-config nonint set_config_var gpu_mem 128 /boot/config.txt && \ | |
sudo apt-get -y purge $REMOVE_PKGS $BUILD_PKGS $RUNTIME_PKGS && \ | |
sudo apt autoremove -y && \ | |
sudo apt-get -y install $BUILD_PKGS $RUNTIME_PKGS && \ | |
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 100 && \ | |
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 100 && \ | |
sudo update-alternatives --config java && \ | |
sudo pip3 install wheel && \ | |
sudo pip3 install scikit-learn && \ | |
sudo pip3 install pillow && \ | |
sudo pip3 install --upgrade --no-deps git+git://github.com/Theano/Theano.git && \ | |
sudo pip3 install keras && \ | |
sudo pip3 install --upgrade six && \ | |
cd ~ && \ | |
mkdir -p ~/.keras && \ | |
echo -e "{\n \"image_dim_ordering\": \"th\"\n \"image_data_format\": \"channels_last\",\n \"epsilon\": 1e-07,\n \"floatx\": \"float32\",\n \"backend\": \"theano\"\n}\n" > ~/.keras/keras.json && \ | |
sudo rm -Rf ~/tf && \ | |
mkdir -p ~/tf/bazel && \ | |
cd ~/tf/bazel && \ | |
wget -qO - https://github.com/bazelbuild/bazel/releases/download/${BAZELVER}/bazel-${BAZELVER}-dist.zip | bsdtar -xvf- && \ | |
wget -qO - https://gist.githubusercontent.com/troykelly/0299c107a5c122496847eee81892c4d0/raw/bazel.patch | patch -s -p0 && \ | |
find . -name \*.sh -type f -print0 | xargs -0 chmod +x && \ | |
sudo TMPDIR=$HOME/tmp ./compile.sh && \ | |
sudo cp output/bazel /usr/local/bin/bazel && \ | |
/usr/local/bin/bazel && \ | |
cd .. && \ | |
wget -qO - https://github.com/tensorflow/tensorflow/archive/v${TENSORVER}.tar.gz | tar xzf - -C ~/tf && \ | |
cd ~/tf/tensorflow-${TENSORVER} && \ | |
grep -Rl 'lib64' | xargs sed -i 's/lib64/lib/g' && \ | |
wget -qO - https://gist.githubusercontent.com/troykelly/7fb6cb44207f0c9debd505ffb54a7d10/raw/tensorflow_pi.patch | sed 's!/home/pi/tf/tensorflow!'"$HOME"'/tf/tensorflow-'"${TENSORVER}"'!g' | patch -s -p0 && \ | |
bazel build -c opt --copt="-mfpu=neon-vfpv4" --copt="-funsafe-math-optimizations" --copt="-ftree-vectorize" --copt="-fomit-frame-pointer" --local_resources 1024,1.0,1.0 --verbose_failures tensorflow/tools/pip_package:build_pip_package --action_env="LD_LIBRARY_PATH=${LD_LIBRARY_PATH}" && \ | |
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg && \ | |
sudo pip install /tmp/tensorflow_pkg/tensorflow-${TENSORVER}-cp27-none-linux_armv7l.whl && \ | |
sudo swapoff $SWAPDISK && \ | |
sleep 2 && \ | |
sudo sed -i 's!'"${SWAPDISK}"' none swap sw,pri=5 0 0!!g' /etc/fstab && \ | |
sudo partprobe |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment