ChainerCV
stable
  • Installation Guide
  • ChainerCV Tutorial
  • ChainerCV Reference Manual
    • Chainer Experimental
    • Datasets
    • Evaluations
    • Experimental
    • Extensions
    • Functions
    • Links
      • Model
        • General Chain
        • Feature Extraction
        • Detection
        • Semantic Segmentation
        • Links for Multiple Tasks
        • Classifiers
      • Connection
    • Transforms
    • Visualizations
    • Utils
  • Naming Conventions
  • License
ChainerCV
  • Docs »
  • ChainerCV Reference Manual »
  • Links »
  • General Chain
  • Edit on GitHub

General Chain¶

FeaturePredictor¶

class chainercv.links.FeaturePredictor(extractor, crop_size, scale_size=None, crop='center', mean=None)[source]¶

Wrapper that adds a prediction method to a feature extraction link.

The predict() takes three steps to make a prediction.

  1. Preprocess input images

  2. Forward the preprocessed images to the network

  3. Average features in the case when more than one crops are extracted.

Example

>>> from chainercv.links import VGG16
>>> from chainercv.links import FeaturePredictor
>>> base_model = VGG16()
>>> model = FeaturePredictor(base_model, 224, 256)
>>> prob = model.predict([img])
# Predicting multiple features
>>> model.extractor.pick = ['conv5_3', 'fc7']
>>> conv5_3, fc7 = model.predict([img])

When self.crop == 'center', predict() extracts features from the center crop of the input images. When self.crop == '10', predict() extracts features from patches that are ten-cropped from the input images.

When extracting more than one crops from an image, the output of predict() returns the average of the features computed from the crops.

Parameters
  • extractor – A feature extraction link. This is a callable chain that takes a batch of images and returns a variable or a tuple of variables.

  • crop_size (int or tuple) – The height and the width of an image after cropping in preprocessing. If this is an integer, the image is cropped to \((crop\_size, crop\_size)\).

  • scale_size (int or tuple) – If scale_size is None, neither scaling nor resizing is conducted during preprocessing. This is the default behavior. If this is an integer, an image is resized so that the length of the shorter edge is equal to scale_size. If this is a tuple (height, width), the image is resized to \((height, width)\).

  • crop ({'center', '10'}) – Determines the style of cropping.

  • mean (numpy.ndarray) – A mean value. If this is None, extractor.mean is used as the mean value.

predict(imgs)[source]¶

Predict features from images.

Given \(N\) input images, this method outputs a batched array with batchsize \(N\).

Parameters

imgs (iterable of numpy.ndarray) – Array-images. All images are in CHW format and the range of their value is \([0, 255]\).

Returns

A batch of features or a tuple of them.

Return type

numpy.ndarray or tuple of numpy.ndarray

PickableSequentialChain¶

class chainercv.links.PickableSequentialChain[source]¶

A sequential chain that can pick intermediate layers.

Callable objects, such as chainer.Link and chainer.Function, can be registered to this chain with init_scope(). This chain keeps the order of registrations and forward() executes callables in that order. A chainer.Link object in the sequence will be added as a child link of this link.

forward() returns single or multiple layers that are picked up through a stream of computation. These layers can be specified by pick, which contains the names of the layers that are collected. When pick is a string, single layer is returned. When pick is an iterable of strings, a tuple of layers is returned. The order of the layers is the same as the order of the strings in pick. When pick is None, the last layer is returned.

Examples

>>> import chainer.functions as F
>>> import chainer.links as L
>>> model = PickableSequentialChain()
>>> with model.init_scope():
>>>     model.l1 = L.Linear(None, 1000)
>>>     model.l1_relu = F.relu
>>>     model.l2 = L.Linear(None, 1000)
>>>     model.l2_relu = F.relu
>>>     model.l3 = L.Linear(None, 10)
>>> # This is layer l3
>>> layer3 = model(x)
>>> # The layers to be collected can be changed.
>>> model.pick = ('l2_relu', 'l1_relu')
>>> # These are layers l2_relu and l1_relu.
>>> layer2, layer1 = model(x)
Parameters
  • pick (string or iterable of strings) – Names of layers that are collected during the forward pass.

  • layer_names (iterable of strings) – Names of layers that can be collected from this chain. The names are ordered in the order of computation.

copy(*args, **kargs)[source]¶

Copies the link hierarchy to new one.

The whole hierarchy rooted by this link is copied. There are three modes to perform copy. Please see the documentation for the argument mode below.

The name of the link is reset on the copy, since the copied instance does not belong to the original parent chain (even if exists).

Parameters

mode (str) – It should be either init, copy, or share. init means parameter variables under the returned link object is re-initialized by calling their initialize() method, so that all the parameters may have different initial values from the original link. copy means that the link object is deeply copied, so that its parameters are not re-initialized but are also deeply copied. Thus, all parameters have same initial values but can be changed independently. share means that the link is shallowly copied, so that its parameters’ arrays are shared with the original one. Thus, their values are changed synchronously. The default mode is share.

Returns

Copied link object.

Return type

Link

forward(x)[source]¶

Forward this model.

Parameters

x (chainer.Variable or array) – Input to the model.

Returns

The returned layers are determined by pick.

Return type

chainer.Variable or tuple of chainer.Variable

remove_unused()[source]¶

Delete all layers that are not needed for the forward pass.

Next Previous

© Copyright 2017, Preferred Networks, inc. Revision dcce9fc5.

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