PERENNIAL FASCINATION WITH ALL THINGS TECH

Main menu

Post navigation

How To Programmatically Create A Deep Neural Network In Python Caffe

When you are working with Caffe, you need to define your deep neural network architecture in a ‘.prototxt’ file. These prototxt files usually consist of hundreds of lines, defining layers and corresponding parameters. Before you start training your neural network, you need to create these files and define your architecture. One way to do this is manually write all these lines into a file. But sometimes, it’s beneficial to dynamically create this architecture depending on our needs. In such cases, creating a deep neural network programmatically can be very useful. Let’s go ahead and see how to do it in Python Caffe, shall we?

Prerequisites

Before you proceed, make sure you have Caffe installed on your machine. Since we will be using Python here, you need to have PyCaffe installed too. We also need training data to define the architecture. Create a file called “train.h5list” and add the following line:

/path/to/train/data.h5

The file “data.h5” is the HDF5 dataset file that you need to create from your training data. We don’t really need this file for our code to work, so it’s okay if this file doesn’t exist for now. Just make sure you have it before you start training because Caffe will look for this file. Once we go through the steps, you will see that this can be extended to other types of datasets too. We are just taking this as a representative example to create a neural network programmatically.

We will be using the file ‘train.h5list’ as input here. The following code will create an output file called ‘train.prototxt’. The first argument to the code is the input file (train.h5list) and the second argument is the output file (train.prototxt):