Getting started with TensorFlow and Caffe

UPDATE! Check out out my new post on How Prisma Works: An Overview of Artistic Style Transfer using Neural Networks.

Thanks to a deep learning course I have enrolled for in college, I got introduced to working with docker images and tools such as Tensorflow and Caffe. In this blog post, I ‘ll go over the setup of Docker, Tensorflow and Caffe as well as getting an IPython Notebook up and running.

All the commands are to be run on a Linux environment and this tutorial is for a CPU only version of TensorFlow and Caffe.

Installing Docker

So what is Docker and how is it different from a virtual machine? Unlike VMs which emulate virtual hardware, Docker uses containers, which share the operating system of the host machine, but still keep your applications and software isolated from your host environment. This reduces a lot of storage overhead that comes along with a VM. Basically, containers are great and we will be installing TensorFlow and Caffe in Docker containers.

To install Docker, follow the instructions given on this page.
https://docs.docker.com/linux/

You can test the installation by running the hello-world image.

Installing TensorFlow

TensorFlow is an opensource (yay!) deep learning toolkit which Google recently outsourced. I’ll be using it to run various toy problems using DL algorithms.

To install TensorFlow, follow the instructions on this page.
https://www.tensorflow.org/versions/v0.6.0/get_started/os_setup.html#docker_install

To run the image, type this into the command line if you had installed the binary image:

docker run -it b.gcr.io/tensorflow/tensorflow

You can now check the installation by opening a python shell and running the code below to make sure it’s installed correctly.

>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print sess.run(hello)
Hello, TensorFlow!
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print sess.run(a + b)
42

Installing Caffe

Caffe is supposed to be great for problems involving image recognition.

To install it, run the command:

docker pull tleyden5iwx/caffe-cpu-master

Installing nbviewer

If you want to run Jupyter notebooks, you would want to install nbviewer.

docker pull jupyter/nbviewer

Now run the notebook using:

docker run -p 8888:8888 -p 6006:6006 -v /home/rob/notebook:/notebook b.gcr.io/tensorflow/tensorflow sh -c "jupyter notebook /notebook"

Now navigate to localhost:8888 in your browser to get the notebook up and running. You can now test if the TensorFlow installation is working by running the word2vec example from the TensorFlow site.

To run an IPython Notebook on Caffe however, you will have to install it on the caffe image that was downloaded earlier. Run the image like

docker run -it tleyden5iwx/caffe-cpu-master

Now install jupyter

pip install jupyter

You now have  a Caffe image with Jupyter installed on it. You might want to save this image so that you can open it later. This can be done by using the commit command. For that, you will need the current container ID which can be found using the ‘docker ps’ command.  Commit this container as

$ docker commit  caffe_jupyter

You can now run the notebook using

docker run -p 8888:8888 -p 6006:6006 caffe_jupyter sh -c "jupyter-notebook --no-browser --ip=*"

Here make sure, you aren’t already using this port for running another notebook. You could try changing the port number if an error pops up. If you now type in ‘localhost:8888’ into the browser, the Jupyter interface will load. Navigate to opt/caffe/examples to get a list of example notebooks that you can run and test on Caffe.

NOTE: You should regularly remove unused docker containers. This can be done by first stopping the container and then removing it. You can see a list of all containers in existence and then choose which ones to remove. The sequence of commands is given below.

$ docker ps -a
$ docker stop 
$ docker rm
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: