Caffe Reference Model Support

Caffe is a popular framework maintained by BVLC at UC Berkeley. It is widely used by computer vision communities, and aims at fast computation and easy usage without any programming. The BVLC team provides trained reference models in their Model Zoo, one of the reason why this framework gets popular.

Chainer can import the reference models and emulate the network by Link implementations. This functionality is provided by the chainer.links.caffe.CaffeFunction class.

class chainer.links.caffe.CaffeFunction(model_path)[source]

Caffe emulator based on the model file of Caffe.

Given a protocol buffers file of a Caffe model, this class loads and emulates it on Variable objects. It supports the official reference models provided by BVLC.

Note

protobuf>=3.0.0 is required if you use Python 3 because protobuf 2 is not supported on Python 3.

Note

CaffeFunction ignores the following layers:

  • Layers that CaffeFunction does not support (including data layers)
  • Layers that have no top blobs
  • Layers whose bottom blobs are incomplete (i.e., some or all of them are not given nor computed)

Warning

It does not support full compatibility against Caffe. Some layers and configurations are not implemented in Chainer yet, though the reference models provided by the BVLC team are supported except data layers.

Example

Consider we want to extract the (unnormalized) log class probability of given images using BVLC reference CaffeNet. The model can be downloaded from:

http://dl.caffe.berkeleyvision.org/bvlc_reference_caffenet.caffemodel

We want to compute the fc8 blob from the data blob. It is simply written as follows:

# Load the model
func = CaffeFunction('path/to/bvlc_reference_caffenet.caffemodel')

# Minibatch of size 10
x_data = numpy.ndarray((10, 3, 227, 227), dtype=numpy.float32)
...  # (Fill the minibatch here)

# Forward the pre-trained net
x = Variable(x_data)
y, = func(inputs={'data': x}, outputs=['fc8'])

The result y contains the Variable corresponding to the fc8 blob. The computational graph is memorized as a usual forward computation in Chainer, so we can run backprop through this pre-trained net.

Parameters:model_path (str) – Path to the binary-proto model file of Caffe.
Variables:forwards (dict) – A mapping from layer names to corresponding functions.
__call__(self, inputs, outputs, disable=())[source]

Executes a sub-network of the network.

This function acts as an interpreter of the network definition for Caffe. On execution, it interprets each layer one by one, and if the bottom blobs are already computed, then emulates the layer and stores output blobs as Variable objects.

Warning

train argument is not supported anymore since v2. Instead, use chainer.using_config('train', train). See chainer.using_config().

Parameters:
  • inputs (dict) – A dictionary whose key-value pairs indicate initial correspondences between blob names and Variable objects.
  • outputs (Iterable) – A list of blob names whose corresponding Variable objects are returned.
  • disable (Iterable) – A list of layer names that will be ignored during the forward computation.
Returns:

A tuple of output Variable objects

corresponding to elements of the outputs argument.

Return type:

tuple