# Convolutional Neural Networks with Matlab, Caffe and TensorFlow

**Introduction**

For an elaborated introduction to machine learning we would like to refer to the lecture of Nando de Freitas (University of Oxford). Lecture notes are available on his homepage. The lectures are available on Youtube. A brief and illustrative example how convolutional neural networks (CNNs) work is given in Brandon Rohrer's 'Data Science and Robots' blog. Two simple image categories (X and O images) are used. The figure below illustrates how an exemplary CNN solves this task.

Our CNN has three convolutional layers (16, 32 and 32 filters) with pooling and activation layers followed by a fully connected layer and softmax classification. Exemplary filters and feature maps for an example of each category are plotted. While the first convolution layer detects simple features (e.g. edges) and has 2D weight matrices, higher convolutional layers combine multiple (lower level) features at different spatial positions (illustrated by red lines in the figure) and have 3D weight matrices. This hierarchy of feature detection is the core of CNN function.

**CNNs with Matlab**

The example figures above were generated with Matlab. Convolutional Neural Networks were introduced in the Neural Network Toolbox in Matlab R2016a** **(e.g. Webinare on CNNs with Matlab)**.**

Here is our corresponding Matlab code for training the CNN and image classification. The RAW circle and cross image files are available here. The code is also awailable on GitHub.

**CNNs with Caffe **

The Caffe framework offers more flexible CNN architectures than Matlab and is highly optimized for speed (CUDA and CuDNN support). It is developed by the Berkeley Vision and Learning Center (BVLC) and is released under the BSD 2-Clause license. Nvidia Digits is based on Caffe and can be used as GUI and convenient interface for multi-GPU systems. For demonstration purpose we also implemented the X' and O' example from above in Caffe.

First the network structure has to be defined (ANet_3conv.prototxt). Solver instructions are given in a seperate file (ANet_3conv_solver.prototxt). To test single imput images we wrote this Python program (ClassifySingleImagesWithPython.py). Note that you need a deploy file (ANet_3conv_deploy.prototxt) as described here.

1) train the network ~/caffe/build/tools/caffe train --solver=ANet_3conv_solver.prototxt --gpu 0 2) test the network performance ~/caffe/build/tools/caffe test --model=ANet_3conv.prototxt --weights=ANet_3conv_1strun_snapshot_iter_5000.caffemodel --gpu 0 --iterations 10 3) run a Python script to pass single images to the trained network python ClassifySingleImagesWithPython.py

This ZIP archive contains the corresponding Caffe code for training the CNN and image classification and the RAW circle and cross images (HDF5 and List format). The code is also awailable on GitHub.

**CNNs with TensorFlow **

The TensorFlow framework for machine learning also offers flexible CNN architectures and is optimized for speed. TensorFlow is developed by Google and is published under the Apache open source license 2.0. For demonstration purpose we also implemented the X' and O' example from above in TensorFlow.

You just need the following two Python files TensorFlow_XO_example_2-categories.py and TensorFlow_XO_dataReadIn.py. The RAW circle and cross image files are available here.

**Acknowledgment**

Dr. Alexander Hanuschkin gratefully acknowledges the support of NVIDIA Corporation for our research.