Last active
August 20, 2021 08:04
-
-
Save jkjung-avt/2b76fe095b64ca067f382d680e28290a to your computer and use it in GitHub Desktop.
Scripts for building/installing opencv, pytorch, and tensorflow on my Ubuntu-18.04 x86_64 PC
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
# Reference: https://michhar.github.io/how-i-built-pytorch-gpu/ | |
# I have a GTX-1080 and a GTX-2080 Ti, thus CUDA_ARCH "6.1" and "7.5". | |
# Patch cmake if necessary: https://github.com/arrayfire/arrayfire/issues/2330 | |
git clone https://github.com/pytorch/pytorch.git pytorch-v1.3.1 | |
cd pytorch-v1.3.1 | |
git checkout v1.3.1 | |
git submodule update --init --recursive | |
pip3 install -r requirements.txt | |
USE_OPENCV=1 \ | |
BUILD_TORCH=ON \ | |
CMAKE_PREFIX_PATH="/usr/bin/" \ | |
CUDA_BIN_PATH=/usr/local/cuda/bin \ | |
CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda/ \ | |
CUDNN_LIB_DIR=/usr/local/cuda/lib64 \ | |
CUDA_HOST_COMPILER=gcc \ | |
USE_CUDA=1 \ | |
USE_NNPACK=1 \ | |
CC=gcc \ | |
CXX=g++ \ | |
TORCH_CUDA_ARCH_LIST="6.1 7.5+PTX" \ | |
TORCH_NVCC_FLAGS="-Xfatbin -compress-all" \ | |
python3 setup.py bdist_wheel | |
pip3 install dist/*.whl |
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
#!/bin/bash | |
# | |
# Reference: https://docs.bazel.build/versions/master/install-ubuntu.html#install-with-installer-ubuntu | |
set -ex | |
folder=${HOME}/src | |
mkdir -p ${folder} | |
echo "** Install requirements" | |
sudo apt-get install -y pkg-config zip g++ zlib1g-dev unzip | |
sudo apt-get install -y openjdk-8-jdk | |
sudo apt-get install -y python-is-python3 | |
echo "** Download bazel-3.7.2 sources" | |
pushd ${folder} | |
if [ ! -f bazel-3.7.2-dist.zip ]; then | |
wget https://github.com/bazelbuild/bazel/releases/download/3.7.2/bazel-3.7.2-dist.zip | |
fi | |
echo "** Build and install bazel-3.7.2" | |
unzip bazel-3.7.2-dist.zip -d bazel-3.7.2-dist | |
cd bazel-3.7.2-dist | |
EXTRA_BAZEL_ARGS="--host_javabase=@local_jdk//:jdk" ./compile.sh | |
mkdir -p ${HOME}/bin | |
cp output/bazel ${HOME}/bin | |
if [[ ${PATH} != *${HOME}/bin* ]]; then | |
export PATH=${HOME}/bin:${PATH} | |
fi | |
bazel help | |
popd | |
echo "** Build bazel-3.7.2 successfully" |
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
#!/bin/bash | |
set -ex | |
cuda_compute=7.5,6.1 | |
folder=${HOME}/src | |
mkdir -p ${folder} | |
echo "** Purge old opencv installation" | |
sudo apt-get purge -y libopencv* | |
echo "** Install requirements" | |
sudo apt-get update | |
sudo apt-get install -y build-essential make cmake cmake-curses-gui git g++ pkg-config curl | |
sudo apt-get install -y libavcodec-dev libavformat-dev libavutil-dev libswscale-dev libeigen3-dev libglew-dev libgtk2.0-dev | |
sudo apt-get install -y libtbb2 libtbb-dev libv4l-dev v4l-utils qv4l2 | |
sudo apt-get install -y libdc1394-22-dev libxine2-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev | |
#sudo apt-get install -y libjasper-dev | |
sudo apt-get install -y libjpeg8-dev libjpeg-turbo8-dev libtiff-dev libpng-dev | |
sudo apt-get install -y libxvidcore-dev libx264-dev libgtk-3-dev | |
sudo apt-get install -y libatlas-base-dev libopenblas-dev liblapack-dev liblapacke-dev gfortran | |
sudo apt-get install -y qt5-default | |
pip3 install -U --user pip setuptools Cython | |
pip3 install --user protobuf numpy matplotlib | |
py3_ver=$(python3 -c "import sys; print(sys.version_info[1])") | |
echo "** Download opencv-4.5.2" | |
pushd ${folder} | |
if [ ! -f opencv-4.5.2.zip ]; then | |
wget https://github.com/opencv/opencv/archive/4.5.2.zip -O opencv-4.5.2.zip | |
fi | |
if [ -d opencv-4.5.2 ]; then | |
echo "** WARNING: Removing the old opencv-4.5.2/build/ directory" | |
rm -rf opencv-4.5.2/build/ | |
else | |
unzip opencv-4.5.2.zip | |
fi | |
if [ ! -d opencv_contrib ]; then | |
git clone https://github.com/opencv/opencv_contrib.git | |
fi | |
mkdir opencv-4.5.2/build | |
cd opencv-4.5.2/build | |
echo "** Building opencv..." | |
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local \ | |
-D OPENCV_GENERATE_PKGCONFIG=YES \ | |
-D WITH_CUDA=ON -D CUDA_ARCH_BIN=${cuda_compute} -D CUDA_ARCH_PTX="" \ | |
-D WITH_CUBLAS=ON -D ENABLE_FAST_MATH=ON -D CUDA_FAST_MATH=ON \ | |
-D OPENCV_EXTRA_MODULES_PATH=${folder}/opencv_contrib/modules \ | |
-D BUILD_opencv_cudacodec=OFF \ | |
-D WITH_TBB=ON -D WITH_EIGEN=ON \ | |
-D WITH_GSTREAMER=ON -D WITH_LIBV4L=ON \ | |
-D EIGEN_INCLUDE_PATH=/usr/include/eigen3 \ | |
-D BUILD_opencv_python2=OFF -D BUILD_opencv_python3=ON \ | |
-D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF \ | |
-D WITH_QT=ON -D WITH_OPENGL=ON .. | |
make -j4 | |
sudo make install | |
sudo ldconfig | |
popd | |
python3 -c 'import cv2; print("python3 cv2 version: %s" % cv2.__version__)' | |
echo "** Install opencv-4.5.2 successfully" |
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
#!/bin/bash | |
# Ubuntu 20.04.2, python 3.8 | |
# CUDA 11.3, cuDNN 8.2.0, NCCL 2.9.8-1, TensorRT 8.0.0.3 | |
set -ex | |
cuda_compute=7.5,6.1 | |
script_path=$(realpath $0) | |
src_folder=${HOME}/src | |
mkdir -p ${src_folder} | |
if pip3 list | grep tensorflow > /dev/null; then | |
echo "ERROR: tensorflow is installed already" | |
exit 1 | |
fi | |
if ! which bazel > /dev/null; then | |
echo "ERROR: bazel has not been installled" | |
exit 1 | |
fi | |
echo "** Install requirements" | |
sudo apt-get install -y libhdf5-serial-dev hdf5-tools | |
pip3 install -U --user pip setuptools wheel numpy | |
pip3 install -U --user keras_applications --no-deps | |
pip3 install -U --user keras_preprocessing --no-deps | |
echo "** Download tensorflow-2.4" | |
pushd ${src_folder} | |
git clone https://github.com/tensorflow/tensorflow.git tensorflow-2.4 | |
cd tensorflow-2.4 | |
git checkout r2.4 | |
echo "** Configure and build tensorflow-2.4" | |
export TMP=/tmp | |
PYTHON_BIN_PATH=$(which python3) \ | |
PYTHON_LIB_PATH=$(python3 -c 'import site; print(site.getsitepackages()[0])') \ | |
TF_CUDA_COMPUTE_CAPABILITIES=${cuda_compute} \ | |
TF_CUDA_VERSION=11.3 \ | |
TF_CUDA_CLANG=0 \ | |
TF_CUDNN_VERSION=8 \ | |
TF_TENSORRT_VERSION=8 \ | |
CUDA_TOOLKIT_PATH=/usr/local/cuda \ | |
CUDNN_INSTALL_PATH=/usr/local/cuda \ | |
TENSORRT_INSTALL_PATH=/usr/local/TensorRT-8.0.0.3 \ | |
TF_NEED_IGNITE=0 \ | |
TF_ENABLE_XLA=0 \ | |
TF_NEED_OPENCL_SYCL=0 \ | |
TF_NEED_COMPUTECPP=0 \ | |
TF_NEED_ROCM=0 \ | |
TF_NEED_CUDA=1 \ | |
TF_NEED_TENSORRT=1 \ | |
TF_NEED_OPENCL=0 \ | |
TF_NEED_MPI=0 \ | |
GCC_HOST_COMPILER_PATH=$(which gcc) \ | |
CC_OPT_FLAGS="-march=native" \ | |
TF_SET_ANDROID_WORKSPACE=0 \ | |
./configure | |
bazel build --config=opt \ | |
--config=cuda \ | |
--config=noaws \ | |
--local_cpu_resources=HOST_CPUS*0.25 \ | |
--local_ram_resources=HOST_RAM*0.5 \ | |
//tensorflow/tools/pip_package:build_pip_package | |
bazel-bin/tensorflow/tools/pip_package/build_pip_package wheel/tensorflow_pkg | |
echo "** Install tensorflow-2.4" | |
pip3 install --user wheel/tensorflow_pkg/tensorflow-2.4*.whl | |
popd | |
TF_CPP_MIN_LOG_LEVEL=3 python3 -c "import tensorflow as tf; tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR); print('tensorflow version: %s' % tf.__version__); print('tensorflow.test.is_built_with_cuda(): %s' % tf.test.is_built_with_cuda()); print('tensorflow.test.is_gpu_available(): %s' % tf.test.is_gpu_available(cuda_only=False, min_cuda_compute_capability=None))" | |
echo "** Build and install tensorflow-2.4 successfully" |
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
#!/bin/bash | |
set -e | |
cuda_compute=8.6,7.5,6.1 | |
cuda_version=11.2 | |
cudnn_version=8 | |
trt_version=8 | |
trt_install_path=/usr/local/TensorRT-8.0.1.6 | |
script_path=$(realpath $0) | |
src_folder=${HOME}/src | |
mkdir -p ${src_folder} | |
if pip3 list | grep tensorflow > /dev/null; then | |
echo "ERROR: tensorflow is installed already" | |
exit 1 | |
fi | |
if ! which bazel > /dev/null; then | |
echo "ERROR: bazel has not been installled" | |
exit 1 | |
fi | |
echo "** Install requirements" | |
sudo apt-get install -y libhdf5-serial-dev hdf5-tools | |
#sudo pip3 install -U pip wheel setuptools numpy | |
pip3 install -U --user keras_applications --no-deps | |
pip3 install -U --user keras_preprocessing --no-deps | |
#export LD_LIBRARY_PATH=/usr/local/cuda/extras/CUPTI/lib64:$LD_LIBRARY_PATH | |
echo "** Download tensorflow-2.6" | |
pushd ${src_folder} | |
git clone https://github.com/tensorflow/tensorflow.git tensorflow-2.6 | |
cd tensorflow-2.6 | |
git checkout r2.6 | |
echo "** Configure and build tensorflow-2.6" | |
export TMP=/tmp | |
PYTHON_BIN_PATH=$(which python3) \ | |
PYTHON_LIB_PATH=$(python3 -c 'import site; print(site.getsitepackages()[0])') \ | |
TF_CUDA_COMPUTE_CAPABILITIES=${cuda_compute} \ | |
TF_CUDA_VERSION=${cuda_version} \ | |
TF_CUDA_CLANG=0 \ | |
TF_CUDNN_VERSION=${cudnn_version} \ | |
TF_TENSORRT_VERSION=${trt_version} \ | |
CUDA_TOOLKIT_PATH=/usr/local/cuda \ | |
CUDNN_INSTALL_PATH=/usr/local/cuda \ | |
TENSORRT_INSTALL_PATH=${trt_install_path} \ | |
TF_NEED_IGNITE=0 \ | |
TF_ENABLE_XLA=0 \ | |
TF_NEED_OPENCL_SYCL=0 \ | |
TF_NEED_COMPUTECPP=0 \ | |
TF_NEED_ROCM=0 \ | |
TF_NEED_CUDA=1 \ | |
TF_NEED_TENSORRT=1 \ | |
TF_NEED_OPENCL=0 \ | |
TF_NEED_MPI=0 \ | |
GCC_HOST_COMPILER_PATH=$(which gcc) \ | |
CC_OPT_FLAGS="-march=native" \ | |
TF_SET_ANDROID_WORKSPACE=0 \ | |
./configure | |
bazel build --config=opt \ | |
--config=cuda \ | |
--config=noaws \ | |
--local_cpu_resources=HOST_CPUS*0.25 \ | |
--local_ram_resources=HOST_RAM*0.5 \ | |
//tensorflow/tools/pip_package:build_pip_package | |
bazel-bin/tensorflow/tools/pip_package/build_pip_package wheel/tensorflow_pkg | |
echo "** Install tensorflow-2.6" | |
pip3 install --user wheel/tensorflow_pkg/tensorflow-2.6*.whl | |
popd | |
TF_CPP_MIN_LOG_LEVEL=3 python3 -c "import tensorflow as tf; tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR); print('tensorflow version: %s' % tf.__version__); print('tensorflow.test.is_built_with_cuda(): %s' % tf.test.is_built_with_cuda()); print('tensorflow.test.is_gpu_available(): %s' % tf.test.is_gpu_available(cuda_only=False, min_cuda_compute_capability=None))" | |
echo "** Build and install tensorflow-2.6 successfully" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment