FROM nvidia/cuda:8.0-cudnn6-devel-ubuntu16.04

MAINTAINER Felix Abecassis "fabecassis@nvidia.com"

ENV CUDA_ARCH_BIN "30 35 50 52 60"
ENV CUDA_ARCH_PTX "60"

# Install dependencies.
RUN apt-get update && apt-get install -y --no-install-recommends \
        ca-certificates \
        cmake \
        git \
        libatlas-base-dev \
        libatlas-dev \
        libboost-all-dev \
        libgflags-dev \
        libgoogle-glog-dev \
        libhdf5-dev \
        libprotobuf-dev \
        pkg-config \
        protobuf-compiler \
        python-yaml \
        python-six \
        wget && \
    rm -rf /var/lib/apt/lists/*

# OpenCV 3.2.0 is needed to support custom allocators for GpuMat objects.
RUN git clone --depth 1 -b 3.2.0 https://github.com/Itseez/opencv.git /opencv && \
    cd /opencv && \
    cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON \
          -DWITH_CUDA=ON -DCUDA_ARCH_BIN="${CUDA_ARCH_BIN}" -DCUDA_ARCH_PTX="${CUDA_ARCH_PTX}" \
          -DWITH_JPEG=ON -DBUILD_JPEG=ON -DWITH_PNG=ON -DBUILD_PNG=ON \
          -DBUILD_TESTS=OFF -DBUILD_EXAMPLES=OFF -DWITH_FFMPEG=OFF -DWITH_GTK=OFF \
          -DWITH_OPENCL=OFF -DWITH_QT=OFF -DWITH_V4L=OFF -DWITH_JASPER=OFF \
          -DWITH_1394=OFF -DWITH_TIFF=OFF -DWITH_OPENEXR=OFF -DWITH_IPP=OFF -DWITH_WEBP=OFF \
          -DBUILD_opencv_superres=OFF -DBUILD_opencv_java=OFF -DBUILD_opencv_python2=OFF \
          -DBUILD_opencv_videostab=OFF -DBUILD_opencv_apps=OFF -DBUILD_opencv_flann=OFF \
          -DBUILD_opencv_ml=OFF -DBUILD_opencv_photo=OFF -DBUILD_opencv_shape=OFF \
          -DBUILD_opencv_cudabgsegm=OFF -DBUILD_opencv_cudaoptflow=OFF -DBUILD_opencv_cudalegacy=OFF \
          -DCUDA_NVCC_FLAGS="-O3" -DCUDA_FAST_MATH=ON && \
    make -j"$(nproc)" install && \
    rm -rf /opencv

# A modified version of Caffe is used to properly handle multithreading and CUDA streams.
RUN git clone --depth 1 -b bvlc_inference https://github.com/flx42/caffe.git /caffe && \
    cd /caffe && \
    cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON \
          -DCUDA_ARCH_NAME=Manual -DCUDA_ARCH_BIN="${CUDA_ARCH_BIN}" -DCUDA_ARCH_PTX="${CUDA_ARCH_PTX}" \
          -DUSE_CUDNN=ON -DUSE_OPENCV=ON -DUSE_LEVELDB=OFF -DUSE_LMDB=OFF \
          -DBUILD_python=OFF -DBUILD_python_layer=OFF -DBUILD_matlab=OFF \
          -DCMAKE_INSTALL_PREFIX=/usr/local \
          -DCUDA_NVCC_FLAGS="-O3" && \
    make -j"$(nproc)" install && \
    make clean

# Download Caffenet
RUN /caffe/scripts/download_model_binary.py /caffe/models/bvlc_reference_caffenet && \
    /caffe/data/ilsvrc12/get_ilsvrc_aux.sh

RUN echo "/usr/local/lib" >> /etc/ld.so.conf && \
    ldconfig

# Install golang
ENV GOLANG_VERSION 1.8.1
RUN wget -O - https://storage.googleapis.com/golang/go${GOLANG_VERSION}.linux-amd64.tar.gz \
    | tar -v -C /usr/local -xz
ENV GOPATH /go
ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH

# Build inference server
COPY caffe /go/src/caffe-server
COPY common.h /go/src/common.h
RUN go get -ldflags="-s" caffe-server

CMD ["caffe-server", \
     "/caffe/models/bvlc_reference_caffenet/deploy.prototxt", \
     "/caffe/models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel", \
     "/caffe/data/ilsvrc12/imagenet_mean.binaryproto", \
     "/caffe/data/ilsvrc12/synset_words.txt"]