As I have been playing around with image recognition lately, I was happy to hear about the new Caffe2 deep learning framework release last week. My previous experience with TensorFlow has taught me that setting everything up and running can be an irritating task if you want to include GPU acceleration. It turned out that the process was easier for Caffe2. Anyway, I thought it might be useful to share what I learned. As Caffe2 comes with some very nice tutorials, I think it is a very good way to get started with machine learning, if you are new to the field.
But first, what is Caffe2? It is a deep learning framework from Facebook. As you probably guessed from the number to, it is based on Caffe, which was developed by BVLC or Berkeley Vision and Learning Center. It really provides a quite easy way to prototype and get your hands dirty with machine learning. It has native Python API and C++ as well, but it’s Python that interests me.
It has also close ties to NVIDIA and makes good use of GPU acceleration. Caffe2 has immediate support for AWS and Azure, but not Google Cloud. But I like Google, so let’s do that. You could install it on your own computer of course, but one of the nice things with VM instances is, if you mess up, you can just delete everything and start over without using virtualenv.
NVIDIA has a pretty good set of instructions on how to do this, but at least for me, it didn’t get me all the way. Actually, the instructions failed at installing CUDA drivers…
First of all, you need to setup an instance on Google Compute engine with a GPU (or more). I assume that you have Cloud SDK installed and billing enabled. Now, navigate on Google Cloud Console to compute engine instances:
and click Create instance. For machine learning you probably don’t want get going with a micro instance as it would be insufferably slow. You can try something like this:
Note that this won’t come in cheap! It costs about one dollar per hour. When you don’t need it anymore, kill the instance. Also note, that GPUs are not available in every zone. If you want to follow my instructions, pick an ubuntu base image:
And press Create. After a few minutes, you are ready to connect. Use your terminal and ssh to connect to the instance. You don’t even have to type it yourself, as you can copy and paste from Google:
Anyway, it is going to be something like
gcloud compute --project "<YOUR-PROJECT>;" ssh --zone "europe-west1-b" "caffe2-1"
That’s it, now you are the commander-in-chief of a GPU capable instance. Let the world tremble! First thing to do is install a whole lot of dependencies for Python and Caffe2:
sudo apt-get update sudo apt-get install libprotobuf-dev protobuf-compiler libatlas-base-dev libgoogle-glog-dev \ libgtest-dev liblmdb-dev libleveldb-dev libsnappy-dev python-dev python-pip libiomp-dev \ libopencv-dev libpthread-stubs0-dev cmake python-protobuf git
The pip you just got is an old version, so you need to update it:
pip install --upgrade pip
If this fails with a locale.Error: unsupported locale setting. You can fix this for now and for the future by adding environment settings to your .bashrc:
echo 'export LC_CTYPE=en_US.UTF-8' >> ~/.bashrc echo 'export LC_ALL=en_US.UTF-8' >> ~/.bashrc source ~/.bashrc
With pip in order, you can install all the useful stuff for Python. You don’t necessarily need Jupyter and iPython, but I certainly recommend it and these are needed for the Caffe2 tutorials. Pydot is needed for some of the tutorials. So, let’s get installing:
sudo pip install numpy matplotlib ipython jupyter pydot
Now, it is time to install nvidia drivers, cuda and cudnn. This will take a few minutes and several gigabytes.
wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_8.0.61-1_amd64.deb sudo dpkg -i cuda-repo-ubuntu1604_8.0.61-1_amd64.deb wget http://developer.download.nvidia.com/compute/redist/cudnn/v5.1/libcudnn5_5.1.10-1+cuda8.0_amd64.deb wget http://developer.download.nvidia.com/compute/redist/cudnn/v5.1/libcudnn5-dev_5.1.10-1+cuda8.0_amd64.deb sudo dpkg -i libcudnn5* sudo apt-get update sudo apt-get install cuda
With all these installs, we are ready to get the actual Caffe2. Clone the GitHub repository:
git clone --recursive https://github.com/caffe2/caffe2.git
Now, create a build directory and let make work it’s magic. This installation will take about 15 minutes, so grab a caffe for yourself as well
mkdir caffe2/build cd caffe2/build/ cmake .. make
We are almost there. You still need to set some environment settings so that Python finds the new module:
echo 'export CAFFE2_ROOT=~/caffe2' >> ~/.bashrc echo 'export PYTHONPATH=$PYTHONPATH:$CAFFE2_ROOT/build' >> ~/.bashrc source ~/.bashrc
That’s it! You can test that everything is in order by running for example some benchmarks
cd ~/caffe2/build/caffe2/python/ python convnet_benchmarks.py --batch_size 1 --model AlexNet --cudnn_ws 500 --iterations 50
Everything is running as it should. Now you try to get your hands dirty with some of the tutorials provided. Launch a Jupyter in a screen at your home folder and forward it to e.g. port 8888. We are going to connect to this port from a browser at our local machine.
cd ~ screen jupyter notebook --ip=0.0.0.0 --port=8888 --no-browser
Now, open a new terminal at your local machine enter
gcloud compute ssh "caffe2-1" --project "<YOUR-PROJECT>" --zone"europe-west1-b" --ssh-flag="-fnNT" --ssh-flag="-L" --ssh-flag="8888:localhost:8888"
This will give a token, which you are going to need soon. Open a browser and navigate to http://localhost:8888/ after which should see something like
Copy the token from the Jupyter screen here and you are in. Navigate to /tree/caffe2/caffe2/python/tutorials and there you will find the tutorials. Have fun!
P.S. If you want to try Loading_Pretrained_Models.ipynb , that is try recognize some image with the pretrained models, you need to
sudo apt-get install python-skimage