Chainer
v2.0.2
  • Installation Guide
  • Chainer Tutorial
  • Chainer Reference Manual
    • Core functionalities
    • Utilities
    • Assertion and Testing
    • Standard Function implementations
    • Standard Link implementations
      • Learnable connections
      • Activation/loss/normalization functions with parameters
      • Machine learning models
      • Pre-trained models
        • VGG16Layers
        • GoogLeNet
        • Residual Networks
    • Optimizers
    • Serializers
    • Function hooks
    • Weight Initializers
    • Dataset examples
    • Iterator examples
    • Trainer extensions
    • Trainer triggers
    • Caffe Reference Model Support
    • Visualization of Computational Graph
    • Environment variables
  • Upgrade Guide from v1 to v2
  • Contribution Guide
  • API Compatibility Policy
  • Tips and FAQs
  • Comparison with Other Frameworks
  • License
Chainer
  • Docs »
  • Chainer Reference Manual »
  • Standard Link implementations »
  • chainer.links.VGG16Layers
  • Edit on GitHub

chainer.links.VGG16Layers¶

class chainer.links.VGG16Layers(pretrained_model='auto')[source]¶

A pre-trained CNN model with 16 layers provided by VGG team.

During initialization, this chain model automatically downloads the pre-trained caffemodel, convert to another chainer model, stores it on your local directory, and initializes all the parameters with it. This model would be useful when you want to extract a semantic feature vector from a given image, or fine-tune the model on a different dataset. Note that this pre-trained model is released under Creative Commons Attribution License.

If you want to manually convert the pre-trained caffemodel to a chainer model that can be specified in the constructor, please use convert_caffemodel_to_npz classmethod instead.

See: K. Simonyan and A. Zisserman, Very Deep Convolutional Networks for Large-Scale Image Recognition

Parameters:pretrained_model (str) – the destination of the pre-trained chainer model serialized as a .npz file. If this argument is specified as auto, it automatically downloads the caffemodel from the internet. Note that in this case the converted chainer model is stored on $CHAINER_DATASET_ROOT/pfnet/chainer/models directory, where $CHAINER_DATASET_ROOT is set as $HOME/.chainer/dataset unless you specify another value as a environment variable. The converted chainer model is automatically used from the second time. If the argument is specified as None, all the parameters are not initialized by the pre-trained model, but the default initializer used in the original paper, i.e., chainer.initializers.Normal(scale=0.01).
Variables:available_layers (list of str) – The list of available layer names used by __call__ and extract methods.

Methods

__call__(self, x, layers=['prob'])[source]¶

Computes all the feature maps specified by layers.

Warning

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

Parameters:
  • x (Variable) – Input variable.
  • layers (list of str) – The list of layer names you want to extract.
Returns:

A directory in which the key contains the layer name and the value contains the corresponding feature map variable.

Return type:

Dictionary of ~chainer.Variable

__getitem__(name)[source]¶

Equivalent to getattr.

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 an initialization scope, instead. For example, the following code

chain.add_link('l1', L.Linear(3, 5))

can be replaced by the following line.

with self.init_scope():
    chain.l1 = L.Linear(3, 5)

The latter one is easier for IDEs to keep track of the attribute’s type.

Parameters:
  • name (str) – Name of the child link. This name is also used as the attribute name.
  • link (Link) – The link object to be registered.
add_param(name, shape=None, dtype=<type '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 within an initialization scope instead. For example, the following code

link.add_param('W', shape=(5, 3))

can be replaced by the following assignment.

with self.init_scope():
    link.W = chainer.Parameter(None, (5, 3))

The latter one 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.
addgrads(link)[source]¶
children()[source]¶
cleargrads()[source]¶

Clears all gradient arrays.

This method should be called before the backward computation at every iteration of the optimization.

classmethod convert_caffemodel_to_npz(path_caffemodel, path_npz)[source]¶

Converts a pre-trained caffemodel to a chainer model.

Parameters:
  • path_caffemodel (str) – Path of the pre-trained caffemodel.
  • path_npz (str) – Path of the converted chainer model.
copy()[source]¶
copyparams(link)[source]¶
disable_update()[source]¶

Disables update rules of all parameters under the link hierarchy.

This method sets the :attr:~chainer.UpdateRule.enabled` flag of the update rule of each parameter variable to False.

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 to True.

extract(self, images, layers=['fc7'], size=(224, 224))[source]¶

Extracts all the feature maps of given images.

The difference of directly executing __call__ is that it directly accepts images as an input and automatically transforms them to a proper variable. That is, it is also interpreted as a shortcut method that implicitly calls prepare and __call__ functions.

Warning

test and volatile arguments are not supported anymore since v2. Instead, use chainer.using_config('train', train) and chainer.using_config('enable_backprop', not volatile) respectively. See chainer.using_config().

Parameters:
  • images (iterable of PIL.Image or numpy.ndarray) – Input images.
  • layers (list of str) – The list of layer names you want to extract.
  • size (pair of ints) – The resolution of resized images used as an input of CNN. All the given images are not resized if this argument is None, but the resolutions of all the images should be the same.
Returns:

A directory in which the key contains the layer name and the value contains the corresponding feature map variable.

Return type:

Dictionary of ~chainer.Variable

init_scope(*args, **kwds)[source]¶

Creates an initialization scope.

This method returns a context manager object that enables registration of parameters (and links for Chain) by an assignment. A Parameter 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 a Parameter 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,))
links(skipself=False)[source]¶
namedlinks(skipself=False)[source]¶
namedparams(include_uninit=True)[source]¶
params(include_uninit=True)[source]¶
predict(images, oversample=True)[source]¶

Computes all the probabilities of given images.

Parameters:
  • images (iterable of PIL.Image or numpy.ndarray) – Input images.
  • oversample (bool) – If True, it averages results across center, corners, and mirrors. Otherwise, it uses only the center.
Returns:

Output that contains the class probabilities of given images.

Return type:

Variable

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.
serialize(serializer)[source]¶
to_cpu()[source]¶
to_gpu(device=None)[source]¶
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.

Attributes

available_layers¶
functions¶
update_enabled¶

True if at least one parameter has an update rule enabled.

within_init_scope¶

True if the current code is inside of an initialization scope.

See init_scope() for the details of the initialization scope.

xp¶

Array module for this link.

Depending on which of CPU/GPU this link is on, this property returns numpy or cupy.

Next Previous

© Copyright 2015, Preferred Networks, inc. and Preferred Infrastructure, inc.. Revision 0f6e5bd1.

Built with Sphinx using a theme provided by Read the Docs.