Skip to content

Instantly share code, notes, and snippets.

@yoonjechoi
Created May 2, 2020 15:52
Show Gist options
  • Save yoonjechoi/c496d34a0a7ee04508d9654f6f6aa29a to your computer and use it in GitHub Desktop.
Save yoonjechoi/c496d34a0a7ee04508d9654f6f6aa29a to your computer and use it in GitHub Desktop.
cuda + opencv + pytorch in ubuntu18.04
# ==================================================================
# module list
# ------------------------------------------------------------------
# darknet latest (git)
# torch latest (git)
# python 3.8 (apt)
# pytorch latest (pip)
# onnx latest (pip)
# theano latest (git)
# tensorflow latest (pip)
# chainer latest (pip)
# jupyter latest (pip)
# mxnet latest (pip)
# opencv 4.1.2 (git)
# keras latest (pip)
# sonnet latest (pip)
# lasagne latest (git)
# cntk latest (pip)
# caffe latest (git)
# ==================================================================
ARG CUDA_VERSION=10.2
ARG CUDNN_VERSION=7
FROM nvidia/cuda:${CUDA_VERSION}-cudnn${CUDNN_VERSION}-devel-ubuntu18.04
ARG PYTHON_VERSION=3.8
ARG OPENCV_VERSION=4.3.0
# Needed for string substitution
SHELL ["/bin/bash", "-c"]
ENV LANG C.UTF-8
ENV DEBIAN_FRONTEND noninteractive
ENV APT_INSTALL="apt-get install -y"
ENV PIP_INSTALL="pip install --no-cache-dir "
ENV GIT_CLONE="git clone --depth 10"
RUN sed -i.bak -re "s/([a-z]{2}.)?archive.ubuntu.com|security.ubuntu.com/mirror.kakao.com/g" /etc/apt/sources.list
RUN rm -rf /var/lib/apt/lists/* \
/etc/apt/sources.list.d/cuda.list \
/etc/apt/sources.list.d/nvidia-ml.list && \
apt-get update && \
$APT_INSTALL \
software-properties-common \
&& \
apt-get update
# ==================================================================
# tools
# ------------------------------------------------------------------
RUN $APT_INSTALL \
build-essential \
apt-utils \
ca-certificates \
wget \
git \
vim \
libssl-dev \
curl \
unzip \
unrar \
cmake
# ==================================================================
# python
# ------------------------------------------------------------------
RUN $APT_INSTALL \
software-properties-common
RUN add-apt-repository ppa:deadsnakes/ppa && \
apt-get update
RUN $APT_INSTALL \
python${PYTHON_VERSION} \
python${PYTHON_VERSION}-dev \
python3-distutils-extra
RUN wget -O ~/get-pip.py \
https://bootstrap.pypa.io/get-pip.py
RUN python${PYTHON_VERSION} ~/get-pip.py && \
ln -s /usr/bin/python${PYTHON_VERSION} /usr/local/bin/python3 && \
ln -s /usr/bin/python${PYTHON_VERSION} /usr/local/bin/python
RUN pip --version && which pip
RUN $PIP_INSTALL \
numpy \
scipy \
pandas \
cloudpickle \
scikit-image \
scikit-learn \
matplotlib \
Cython \
tqdm
# ==================================================================
# darknet
# ------------------------------------------------------------------
RUN $GIT_CLONE https://github.com/pjreddie/darknet.git ~/darknet && \
cd ~/darknet && \
sed -i 's/GPU=0/GPU=1/g' ~/darknet/Makefile && \
sed -i 's/CUDNN=0/CUDNN=1/g' ~/darknet/Makefile && \
make -j"$(nproc)" && \
cp ~/darknet/include/* /usr/local/include && \
cp ~/darknet/*.a /usr/local/lib && \
cp ~/darknet/*.so /usr/local/lib && \
cp ~/darknet/darknet /usr/local/bin
# ==================================================================
# torch
# ------------------------------------------------------------------
RUN $APT_INSTALL \
sudo
RUN $GIT_CLONE https://github.com/nagadomi/distro.git ~/torch --recursive && \
cd ~/torch && \
bash install-deps && \
sed -i 's/${THIS_DIR}\/install/\/usr\/local/g' ./install.sh && \
./install.sh
# ==================================================================
# pytorch
# ------------------------------------------------------------------
RUN $PIP_INSTALL \
future \
numpy \
protobuf \
enum34 \
pyyaml \
typing
RUN $PIP_INSTALL \
torch torchvision
# ==================================================================
# onnx
# ------------------------------------------------------------------
RUN $APT_INSTALL \
protobuf-compiler \
libprotoc-dev
RUN $PIP_INSTALL \
--no-binary onnx onnx
RUN $PIP_INSTALL \
onnxruntime
# ==================================================================
# theano
# ------------------------------------------------------------------
RUN $APT_INSTALL \
libblas-dev
RUN wget -qO- https://github.com/Theano/libgpuarray/archive/v0.7.6.tar.gz | tar xz -C ~ && \
cd ~/libgpuarray* && mkdir -p build && cd build && \
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
.. && \
make -j"$(nproc)" install && \
cd ~/libgpuarray* && \
python setup.py build && \
python setup.py install && \
printf '[global]\nfloatX = float32\ndevice = cuda0\n\n[dnn]\ninclude_path = /usr/local/cuda/targets/x86_64-linux/include\n' > ~/.theanorc
RUN $PIP_INSTALL \
https://github.com/Theano/Theano/archive/master.zip
# ==================================================================
# tensorflow
# ------------------------------------------------------------------
RUN $PIP_INSTALL \
tensorflow
# ==================================================================
# boost
# ------------------------------------------------------------------
RUN wget -O ~/boost.tar.gz https://dl.bintray.com/boostorg/release/1.73.0/source/boost_1_73_0.tar.gz && \
tar -zxf ~/boost.tar.gz -C ~
RUN cd ~/boost_* && \
./bootstrap.sh --with-python=python3.8 && \
./b2 install -j"$(nproc)" --prefix=/usr/local
# ==================================================================
# chainer
# ------------------------------------------------------------------
RUN $PIP_INSTALL \
cupy \
chainer
# ==================================================================
# jupyter
# ------------------------------------------------------------------
RUN $PIP_INSTALL \
jupyter
# ==================================================================
# mxnet
# ------------------------------------------------------------------
RUN $APT_INSTALL \
libatlas-base-dev \
graphviz
RUN $PIP_INSTALL \
mxnet-cu101 \
graphviz
# ==================================================================
# opencv
# ------------------------------------------------------------------
# Add CUDA libs paths
RUN CUDA_PATH=(/usr/local/cuda-*) && \
CUDA=`basename $CUDA_PATH` && \
echo "$CUDA_PATH/compat" >> /etc/ld.so.conf.d/${CUDA/./-}.conf && \
ldconfig && \
# Install all dependencies for OpenCV
apt-get -y update -qq --fix-missing && \
apt-get -y install --no-install-recommends \
python${PYTHON_VERSION} \
python${PYTHON_VERSION}-dev \
$( [ ${PYTHON_VERSION%%.*} -ge 3 ] && echo "python${PYTHON_VERSION%%.*}-distutils" ) \
wget \
unzip \
cmake \
libtbb2 \
gfortran \
apt-utils \
pkg-config \
checkinstall \
qt5-default \
build-essential \
libatlas-base-dev \
libgtk2.0-dev \
libavcodec57 \
libavcodec-dev \
libavformat57 \
libavformat-dev \
libavutil-dev \
libswscale4 \
libswscale-dev \
libjpeg8-dev \
libpng-dev \
libtiff5-dev \
libdc1394-22 \
libdc1394-22-dev \
libxine2-dev \
libv4l-dev \
libgstreamer1.0 \
libgstreamer1.0-dev \
libgstreamer-plugins-base1.0-0 \
libgstreamer-plugins-base1.0-dev \
libglew-dev \
libpostproc-dev \
libeigen3-dev \
libtbb-dev \
zlib1g-dev \
libsm6 \
libxext6 \
libxrender1
# Install OpenCV
RUN CUDA_PATH=(/usr/local/cuda-*) && \
CUDA=`basename $CUDA_PATH` && \
echo "$CUDA_PATH/compat" >> /etc/ld.so.conf.d/${CUDA/./-}.conf && \
ldconfig && \
wget https://github.com/opencv/opencv/archive/$OPENCV_VERSION.zip -O opencv.zip --progress=bar:force:noscroll && \
unzip -q opencv.zip && \
mv /opencv-$OPENCV_VERSION /opencv && \
rm opencv.zip && \
wget https://github.com/opencv/opencv_contrib/archive/$OPENCV_VERSION.zip -O opencv_contrib.zip --progress=bar:force:noscroll && \
unzip -q opencv_contrib.zip && \
mv /opencv_contrib-$OPENCV_VERSION /opencv_contrib && \
rm opencv_contrib.zip && \
# Prepare build
RUN CUDA_PATH=(/usr/local/cuda-*) && \
CUDA=`basename $CUDA_PATH` && \
echo "$CUDA_PATH/compat" >> /etc/ld.so.conf.d/${CUDA/./-}.conf && \
ldconfig && \
mkdir /opencv/build && \
cd /opencv/build && \
cmake \
-D CMAKE_BUILD_TYPE=RELEASE \
-D BUILD_PYTHON_SUPPORT=ON \
-D BUILD_DOCS=ON \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_TESTS=OFF \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=/opencv_contrib/modules \
-D BUILD_opencv_python3=$( [ ${PYTHON_VERSION%%.*} -ge 3 ] && echo "ON" || echo "OFF" ) \
-D BUILD_opencv_python2=$( [ ${PYTHON_VERSION%%.*} -lt 3 ] && echo "ON" || echo "OFF" ) \
-D PYTHON${PYTHON_VERSION%%.*}_EXECUTABLE=$(which python${PYTHON_VERSION}) \
-D PYTHON_DEFAULT_EXECUTABLE=$(which python${PYTHON_VERSION}) \
-D BUILD_EXAMPLES=OFF \
-D WITH_IPP=OFF \
-D WITH_FFMPEG=ON \
-D WITH_GSTREAMER=ON \
-D WITH_V4L=ON \
-D WITH_LIBV4L=ON \
-D WITH_TBB=ON \
-D WITH_QT=ON \
-D WITH_OPENGL=ON \
-D WITH_CUDA=ON \
-D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda \
-D CMAKE_LIBRARY_PATH=/usr/local/cuda/lib64/stubs \
# https://kezunlin.me/post/6580691f
# https://stackoverflow.com/questions/28010399/build-opencv-with-cuda-support
-D CUDA_ARCH_BIN="5.3 6.1 7.0 7.5" \
-D CUDA_ARCH_PTX="" \
-D WITH_CUBLAS=ON \
-D WITH_NVCUVID=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D ENABLE_PRECOMPILED_HEADERS=OFF \
.. \
&& \
# Build, Test and Install
cd /opencv/build && \
make -j$(nproc) && \
make install && \
ldconfig && \
# Set the default python and install PIP packages
update-alternatives --install /usr/bin/python${PYTHON_VERSION%%.*} python${PYTHON_VERSION%%.*} /usr/bin/python${PYTHON_VERSION} 1 && \
update-alternatives --install /usr/bin/python python /usr/bin/python${PYTHON_VERSION} 1 && \
# Call default command.
python --version && \
python -c "import cv2 ; print(cv2.__version__)"
# ==================================================================
# keras
# ------------------------------------------------------------------
RUN $PIP_INSTALL \
h5py \
keras
# ==================================================================
# sonnet
# ------------------------------------------------------------------
RUN $PIP_INSTALL \
tensorflow_probability \
"dm-sonnet>=2.0.0b0" --pre
# ==================================================================
# lasagne
# ------------------------------------------------------------------
RUN $GIT_CLONE https://github.com/Lasagne/Lasagne ~/lasagne && \
cd ~/lasagne
RUN $PIP_INSTALL \
.
# ==================================================================
# cntk
# ------------------------------------------------------------------
RUN $APT_INSTALL \
openmpi-bin \
libpng-dev \
libjpeg-dev \
libtiff-dev
# Fix ImportError for CNTK
RUN ln -s /usr/lib/x86_64-linux-gnu/libmpi_cxx.so.20 /usr/lib/x86_64-linux-gnu/libmpi_cxx.so.1 && \
ln -s /usr/lib/x86_64-linux-gnu/libmpi.so.20.10.1 /usr/lib/x86_64-linux-gnu/libmpi.so.12 && \
wget --no-verbose -O - https://github.com/01org/mkl-dnn/releases/download/v0.14/mklml_lnx_2018.0.3.20180406.tgz | tar -xzf - && \
cp mklml*/* /usr/local -r && \
wget --no-verbose -O - https://github.com/01org/mkl-dnn/archive/v0.14.tar.gz | tar -xzf - && \
cd mkl-dnn-0.14 && mkdir build && cd build && \
ln -s /usr/local external && \
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
.. && \
make -j"$(nproc)" install
RUN $PIP_INSTALL \
cntk-gpu
# ==================================================================
# caffe
# ------------------------------------------------------------------
RUN apt-get update && \
$APT_INSTALL \
caffe-cuda
# ==================================================================
# config & cleanup
# ------------------------------------------------------------------
RUN ldconfig && \
apt-get clean && \
apt-get autoremove && \
rm -rf /var/lib/apt/lists/* /tmp/* ~/*
EXPOSE 6006 8888
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment