FROM nvidia/cuda:8.0-cudnn5-devel

WORKDIR /root

# training dependencies

RUN apt-get update
RUN apt-get install -y --no-install-recommends git ca-certificates sudo

RUN git clone https://github.com/torch/distro.git torch --recursive && \
    cd torch && \
    git checkout 49c5b4fd478cb2e7f87ba5853510d26bf28a3d83 && \
    bash install-deps && \
    bash install.sh -b

ENV PATH="/root/torch/install/bin/:${PATH}"

# tool dependencies

# datasets/download_dataset.sh and models/download_model.sh
# wget
RUN apt-get install -y --no-install-recommends wget

# scripts/combine_A_and_B.py and scripts/edges/batch_hed.py
# opencv
RUN apt-get install -y --no-install-recommends python python-dev
RUN curl -O https://bootstrap.pypa.io/get-pip.py && python get-pip.py
RUN pip install numpy scipy

RUN curl -OL https://github.com/Itseez/opencv/archive/2.4.13.zip && \
    unzip 2.4.13.zip && \
    cd opencv-2.4.13 && \
    mkdir release && \
    cd release && \
    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. && \
    make && \
    make install

# scripts/edges/batch_hed.py
# caffe
# based on https://github.com/BVLC/caffe/blob/master/docker/gpu/Dockerfile
RUN apt-get update && apt-get install -y --no-install-recommends \
        build-essential \
        cmake \
        git \
        wget \
        libatlas-base-dev \
        libboost-all-dev \
        libgflags-dev \
        libgoogle-glog-dev \
        libhdf5-serial-dev \
        libleveldb-dev \
        liblmdb-dev \
        libopencv-dev \
        libprotobuf-dev \
        libsnappy-dev \
        protobuf-compiler \
        python-dev \
        python-numpy \
        python-pip \
        python-setuptools \
        python-scipy && \
    rm -rf /var/lib/apt/lists/*

ENV CAFFE_ROOT=/opt/caffe

RUN mkdir -p $CAFFE_ROOT && \
    cd $CAFFE_ROOT && \
    git clone --depth 1 https://github.com/s9xie/hed . && \
    git checkout 9e74dd710773d8d8a469ad905c76f4a7fa08f945 && \
    pip install --upgrade pip && \
    cd python && for req in $(cat requirements.txt) pydot; do pip install $req; done && cd .. && \
    # https://github.com/s9xie/hed/pull/23
    sed -i "s|add_subdirectory(examples)||g" CMakeLists.txt && \
    mkdir build && cd build && \
    # /opt/caffe/include/caffe/util/cudnn.hpp(123): error: argument of type "int" is incompatible with parameter of type "cudnnNanPropagation_t" => -DUSE_CUDNN=OFF 
    # /usr/bin/ld: cannot find -lopencv_dep_cudart => -DCUDA_USE_STATIC_CUDA_RUNTIME=OFF 
    cmake -DUSE_CUDNN=OFF -DCUDA_USE_STATIC_CUDA_RUNTIME=OFF .. && \
    make -j"$(nproc)"

ENV PYCAFFE_ROOT $CAFFE_ROOT/python
ENV PYTHONPATH $PYCAFFE_ROOT:$PYTHONPATH
ENV PATH $CAFFE_ROOT/build/tools:$PYCAFFE_ROOT:$PATH
RUN echo "$CAFFE_ROOT/build/lib" >> /etc/ld.so.conf.d/caffe.conf && ldconfig

RUN cd $CAFFE_ROOT && curl -O http://vcl.ucsd.edu/hed/hed_pretrained_bsds.caffemodel

# scripts/edges/PostprocessHED.m

RUN apt-get update && \
    apt-get install -y --no-install-recommends octave liboctave-dev && \
    octave --eval "pkg install -forge image" && \
    echo "pkg load image;" >> /root/.octaverc

RUN curl -O https://pdollar.github.io/toolbox/archive/piotr_toolbox.zip && \
    unzip piotr_toolbox.zip && \
    octave --eval "addpath(genpath('/root/toolbox')); savepath;" && \
    echo "#include <stdlib.h>" > wrappers.hpp && \
    cat /root/toolbox/channels/private/wrappers.hpp >> wrappers.hpp && \
    mv wrappers.hpp /root/toolbox/channels/private/wrappers.hpp && \
    mkdir /root/mex && \
    cd /root/toolbox/channels/private && \
    mkoctfile --mex -DMATLAB_MEX_FILE -o /root/mex/convConst.mex convConst.cpp && \
    mkoctfile --mex -DMATLAB_MEX_FILE -o /root/mex/gradientMex.mex gradientMex.cpp && \
    mkoctfile --mex -DMATLAB_MEX_FILE -o /root/mex/imPadMex.mex imPadMex.cpp && \
    mkoctfile --mex -DMATLAB_MEX_FILE -o /root/mex/imResampleMex.mex imResampleMex.cpp && \
    mkoctfile --mex -DMATLAB_MEX_FILE -o /root/mex/rgbConvertMex.mex rgbConvertMex.cpp && \
    octave --eval "addpath('/root/mex'); savepath;" && \
    # gradient2 causes a segfault, use builtin gradient instead
    echo "function [a, b] = gradient2(x)\n[a, b] = gradient(x, 1);\nend" > /root/mex/gradient2.m

RUN curl -O https://raw.githubusercontent.com/pdollar/edges/master/private/edgesNmsMex.cpp && \
    octave --eval "mex edgesNmsMex.cpp" && \
    mv edgesNmsMex.mex /root/mex/

RUN apt-get install -y --no-install-recommends python-imaging