caffeCustom Python Layers


Introduction

This tutorial will guide through the steps to create a simple custom layer for Caffe using python. By the end of it, there are some examples of custom layers. Usually you would create a custom layer to implement a functionality that isn't available in Caffe, tuning it for your requirements.

Creating a python custom layer adds some overhead to your network and probably isn't as efficient as a C++ custom layer. However, this way, you won't have to compile the whole caffe with your new layer.

Parameters

ParameterDetails
topAn array with the top blobs of your layer. Access data passed to it by using top[i].data, where i is the index of a specific blob
bottomAn array with the bottom blobs of your layer. Access data passed to it by using bottom[i].data, where i is the index of a specific blob

Remarks

- Caffe build with Python layer

Caffe needs to be compiled with WITH_PYTHON_LAYER option:

WITH_PYTHON_LAYER=1 make && make pycaffe

- Where should I save the class file?

You have two options (at least that I know of). Either you can save the custom layer file in the same folder as you are going to run the caffe command (probably where your prototxt files would be). Another way, also my favorite one, is to save all your custom layers in a folder and adding this folder to your PYTHONPATH.

References

  1. Christopher Bourez's blog
  2. Caffe Github
  3. StackOverflow