Building MXNet in docker

This blog shows how I built the MXNet deep learning framework in a docker container. If you follow this guide, you should be able to build your own. You can find the Dockerfile I created at the end of this post.

Pre-requisite

Since I’m building a GPU-enabled MXNet, the following pre-requisites are needed:

  • nvidia-docker
  • NVIDIA GPU (with support for Compute Capability >= 2.0)

Building MXNet

To create a docker container, you start with a base image. For this case, I chose the nvidia/cuda:7.5-cudnn4-devel image as the base image. This images container CUDA 7.5 as well as cuDNN v4. Since we are building a container on top of that, we’ll run a container in interactive mode with the following command:

$ nvidia-docker run -ti --name mxnet nvidia/cuda:7.5-cudnn4-devel /bin/bash

This will create a container named mxnet and run a bash shell inside. Now let’s follow MXNet’s documentation to build MXNet. First, we need to install the dependencies for MXNet, run the following command:

$ apt-get update
$ apt-get install -y build-essential git libatlas-base-dev libopencv-dev

After the dependencies are installed, we can clone the MXNet repository and build it. Last thing before we build MXNet is to edit config.mk. Make a copy in the root directory of MXNet, and add your corresponding CUDA path and CUDA support in it. Now run the following command:

$ cd root && git clone --recursive https://github.com/dmlc/mxnet
$ cd mxnet && make -j$(nproc)

This will utilize all the available cores according to nproc to build MXNet.

Adding Python support

Personally, I use Python a lot, so I’ll build the Python support of MXNet. Alternatively, you can also build support for languages like R, Julia and etc. Use the following command to add Python support for MXNet:

$ cd python
$ apt-get install -y python-numpy python-setuptools
$ python setup.py install

Once we are done, we should test whether the Python support works. Run the following command to test:

$ apt-get install -y wget unzip
$ cd .. && python example/image-classification/train_mnist.py --network lenet --gpus 0

The wget and unzip utility is used for getting the MNIST dataset and extracting it. The second command trains a LeNet with the MNIST dataset on GPU 0. If everything works fine, you should see that the network is trained and you should be able to achieve a very high accuracy (close to 100%).

Writing a Dockerfile

Since there are so many steps, why not make a Dockerfile for it? Well, to save you guys’ time, I’ve created a Dockerfile which does the same thing as I described above. It’s available on my Github here.

 
comments powered by Disqus