Skip to content

Instantly share code, notes, and snippets.

@yuriploc
Last active March 30, 2023 12:23
Show Gist options
  • Save yuriploc/be4907ab1bdaa1f398543dff98d62118 to your computer and use it in GitHub Desktop.
Save yuriploc/be4907ab1bdaa1f398543dff98d62118 to your computer and use it in GitHub Desktop.
TensorFlow, CUDA, cuDNN, OpenCV 4.5.5, Ubuntu 18.04
ARG CUDA_VERSION=11.2.2
ARG CUDNN_VERSION=8
ARG UBUNTU_VERSION=18.04
# https://hub.docker.com/r/nvidia/cuda
FROM nvidia/cuda:${CUDA_VERSION}-cudnn${CUDNN_VERSION}-devel-ubuntu${UBUNTU_VERSION}
ARG PYTHON_VERSION=3.8
ARG OPENCV_VERSION=4.5.5
ENV DEBIAN_FRONTEND=noninteractive
# https://github.com/NVIDIA/nvidia-docker/issues/1632
# RUN rm /etc/apt/sources.list.d/cuda.list
# RUN rm /etc/apt/sources.list.d/nvidia-ml.list
ARG CUDA_SHORT_VERSION=11.2
# Compiling for NVIDIA A100
# https://study.marearts.com/2021/06/cudaarchbin-table-for-gpu-type.html
# https://en.wikipedia.org/wiki/CUDA
# https://developer.nvidia.com/cuda-gpus
ARG CUDA_ARCH=8.0
# https://www.tensorflow.org/install/source?hl=en#gpu
ARG TF_VERSION=2.7.0
ENV PATH=${PATH}:/usr/local/cuda-${CUDA_SHORT_VERSION}/bin
ENV LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/cuda-${CUDA_SHORT_VERSION}/lib64:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64
ENV LIBRARY_PATH=${LIBRARY_PATH}:/usr/local/cuda-${CUDA_SHORT_VERSION}/lib64
ENV CPATH=${CPATH}:/usr/local/cuda/include
RUN apt-get -qq update && \
apt-get -qq install \
# python :
python${PYTHON_VERSION} \
python${PYTHON_VERSION}-dev \
libpython${PYTHON_VERSION} \
libpython${PYTHON_VERSION}-dev \
python-dev \
python3-pip \
python3-dev \
python3-setuptools \
# developement tools, opencv image/video/GUI dependencies, optimiztion packages , etc ... :
apt-utils \
autoconf \
automake \
build-essential \
checkinstall \
cmake \
g++ \
gcc \
gfortran \
git \
libatlas-base-dev \
libavcodec-dev \
libavformat-dev \
libavresample-dev \
libeigen3-dev \
libexpat1-dev \
libglew-dev \
libgtk-3-dev \
libjpeg-dev \
libopenexr-dev \
libpng-dev \
libpostproc-dev \
libpq-dev \
libprotobuf-dev \
libqt5opengl5-dev \
libsm6 \
libswscale-dev \
libtbb2 \
libtbb-dev \
libtiff-dev \
libtool \
libv4l-dev \
libwebp-dev \
libxext6 \
libxrender1 \
libxvidcore-dev \
ninja-build \
pkg-config \
protobuf-compiler \
qt5-default \
unzip \
wget \
yasm \
zlib1g-dev \
# GStreamer :
libgstreamer1.0-0 \
gstreamer1.0-plugins-base \
gstreamer1.0-plugins-good \
gstreamer1.0-plugins-bad \
gstreamer1.0-plugins-ugly \
gstreamer1.0-libav \
gstreamer1.0-doc \
gstreamer1.0-tools \
gstreamer1.0-x \
gstreamer1.0-alsa \
gstreamer1.0-gl \
gstreamer1.0-gtk3 \
gstreamer1.0-qt5 \
gstreamer1.0-pulseaudio \
libgstreamer1.0-dev \
libgstreamer-plugins-base1.0-dev && \
rm -rf /var/lib/apt/lists/* && \
apt-get purge --auto-remove && \
apt-get clean
# install new pyhton system wide :
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1 && \
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python${PYTHON_VERSION} 2 && \
update-alternatives --config python3
# numpy for the newly installed python :
RUN wget https://bootstrap.pypa.io/get-pip.py && \
python${PYTHON_VERSION} get-pip.py --no-setuptools --no-wheel && \
rm get-pip.py && \
python${PYTHON_VERSION} -m pip uninstall -y numpy && \
python${PYTHON_VERSION} -m pip install numpy==1.19.5
# opencv and opencv-contrib :
RUN cd /opt/ && \
wget https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.zip -O opencv.zip && \
unzip -qq opencv.zip && \
rm opencv.zip && \
wget https://github.com/opencv/opencv_contrib/archive/${OPENCV_VERSION}.zip -O opencv-co.zip && \
unzip -qq opencv-co.zip && \
rm opencv-co.zip && \
mkdir /opt/opencv-${OPENCV_VERSION}/build && cd /opt/opencv-${OPENCV_VERSION}/build && \
# cmake \
cmake -GNinja \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_EXAMPLES=OFF \
-D BUILD_opencv_java=OFF \
-D CMAKE_LIBRARY_PATH=/usr/local/cuda/lib64/stubs \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CUDA_FAST_MATH=ON \
-D CUDA_ARCH_BIN=${CUDA_ARCH} \
-D CUDA_ARCH_PTX=${CUDA_ARCH} \
-D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda \
-D ENABLE_FAST_MATH=ON \
-D HAVE_opencv_python3=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_DNN_CUDA=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_EXTRA_MODULES_PATH=/opt/opencv_contrib-${OPENCV_VERSION}/modules \
-D OPENCV_PYTHON3_INSTALL_PATH=/usr/lib/python3/dist-packages \
-D PYTHON_EXECUTABLE=/usr/bin/python3 \
-D WITH_CUBLAS=ON \
-D WITH_CUDA=ON \
-D WITH_CUDNN=ON \
-D WITH_CUFFT=ON \
-D WITH_EIGEN=ON \
-D WITH_IPP=ON \
-D WITH_NVCUVID=ON \
# -D WITH_OPENGL=ON \
# -D WITH_QT=ON \
# -D WITH_TBB=ON \
# -D PYTHON2_EXECUTABLE=$(python${PYTHON_VERSION} -c "import sys; print(sys.prefix)") \
# -D CMAKE_INSTALL_PREFIX=$(python${PYTHON_VERSION} -c "import sys; print(sys.prefix)") \
# -D PYTHON_INCLUDE_DIR=$(python${PYTHON_VERSION} -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
# -D PYTHON_PACKAGES_PATH=$(python${PYTHON_VERSION} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \
.. &&\
# make -j$(nproc) && \
# make install && \
ninja && \
ninja install && \
ldconfig && \
rm -rf /opt/opencv-${OPENCV_VERSION} && rm -rf /opt/opencv_contrib-${OPENCV_VERSION}
ENV NVIDIA_DRIVER_CAPABILITIES all
ENV XDG_RUNTIME_DIR "/tmp"
RUN pip3 install testresources setuptools scikit-build
# numpy==1.19.5 is already installed before opencv compilation
RUN pip3 install -U cython pkgconfig wrapt==1.12 future==0.18.2 mock==3.0.5 keras_preprocessing==1.1.2 keras_applications==1.0.8 gast==0.4.0 protobuf==3.19.6 pybind11 pkgconfig packaging h5py==3.1.0 shapely easydict scipy requests gdown
RUN pip3 install gunicorn==20.1.0 celery==5.0.3 flask==2.0.3 redis==4.3.5 tensorflow==${TF_VERSION}
WORKDIR /app
RUN echo "alias python=python3" >> ~/.bashrc
RUN echo "alias pip=pip3" >> ~/.bashrc
COPY . ./
# Download files from GDrive
RUN gdown https://drive.google.com/uc?id=1zGnGkTvFAsUDacYaVcay3CFiZDkNXT0P
RUN mv yolo.zip ./yolo
RUN unzip -o ./yolo/yolo.zip -d ./yolo
RUN rm ./yolo/yolo.zip
# RUN gdown https://drive.google.com/uc?id=1HNg9cdWAXdJoc9aeBQb0-gaw0ZXOc9Rk
# RUN mv yolov4.onnx ./yolo
RUN gdown https://drive.google.com/uc?id=12afPUXgyJC0n-GhiSGRA5MOoC3GHC93x
RUN mv weights.pb.zip ./deep_sort
RUN unzip -o ./deep_sort/weights.pb.zip -d ./deep_sort
RUN rm ./deep_sort/weights.pb.zip
# ENTRYPOINT ["/bin/bash"]
CMD ["sh", "-c", "gunicorn -b 0.0.0.0:8000 wsgi:app --daemon && celery -A server.celery worker -E"]
@yuriploc
Copy link
Author

yuriploc commented Mar 29, 2023

Revision 2 works but it consumes 8GB of GPU memory for a single stream.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment