chainer.links.caffe.CaffeFunction¶
-
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
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 thedata
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 thefc8
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. Methods
-
__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, usechainer.using_config('train', train)
. Seechainer.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: - inputs (dict) – A dictionary whose key-value pairs indicate initial
correspondences between blob names and
-
add_link
(name, link)[source]¶ Registers a child link to this chain.
Deprecated since version v2.0.0: Assign the child link directly to an attribute within
init_scope()
instead. For example, the following codechain.add_link('l1', L.Linear(3, 5))
can be replaced by the following line.
with chain.init_scope(): chain.l1 = L.Linear(3, 5)
The latter is easier for IDEs to keep track of the attribute’s type.
Parameters:
-
add_param
(name, shape=None, dtype=<class 'numpy.float32'>, initializer=None)[source]¶ Registers a parameter to the link.
Deprecated since version v2.0.0: Assign a
Parameter
object directly to an attribute withininit_scope()
instead. For example, the following codelink.add_param('W', shape=(5, 3))
can be replaced by the following assignment.
with link.init_scope(): link.W = chainer.Parameter(None, (5, 3))
The latter is easier for IDEs to keep track of the attribute’s type.
Parameters: - name (str) – Name of the parameter. This name is also used as the attribute name.
- shape (int or tuple of ints) – Shape of the parameter array. If it is omitted, the parameter variable is left uninitialized.
- dtype – Data type of the parameter array.
- initializer – If it is not
None
, the data is initialized with the given initializer. If it is an array, the data is directly initialized by it. If it is callable, it is used as a weight initializer. Note that in these cases,dtype
argument is ignored.
-
add_persistent
(name, value)[source]¶ Registers a persistent value to the link.
The registered value is saved and loaded on serialization and deserialization. The value is set to an attribute of the link.
Parameters: - name (str) – Name of the persistent value. This name is also used for the attribute name.
- value – Value to be registered.
-
cleargrads
()[source]¶ Clears all gradient arrays.
This method should be called before the backward computation at every iteration of the optimization.
-
disable_update
()[source]¶ Disables update rules of all parameters under the link hierarchy.
This method sets the
enabled
flag of the update rule of each parameter variable toFalse
.
-
enable_update
()[source]¶ Enables update rules of all parameters under the link hierarchy.
This method sets the
enabled
flag of the update rule of each parameter variable toTrue
.
-
init_scope
()[source]¶ Creates an initialization scope.
This method returns a context manager object that enables registration of parameters (and links for
Chain
) by an assignment. AParameter
object can be automatically registered by assigning it to an attribute under this context manager.Example
In most cases, the parameter registration is done in the initializer method. Using the
init_scope
method, we can simply assign aParameter
object to register it to the link.class MyLink(chainer.Link): def __init__(self): super().__init__() with self.init_scope(): self.W = chainer.Parameter(0, (10, 5)) self.b = chainer.Parameter(0, (5,))
-
register_persistent
(name)[source]¶ Registers an attribute of a given name as a persistent value.
This is a convenient method to register an existing attribute as a persistent value. If
name
has been already registered as a parameter, this method removes it from the list of parameter names and re-registers it as a persistent value.Parameters: name (str) – Name of the attribute to be registered.
-
zerograds
()[source]¶ Initializes all gradient arrays by zero.
This method can be used for the same purpose of cleargrads, but less efficient. This method is left for backward compatibility.
Deprecated since version v1.15: Use
cleargrads()
instead.