glimpse.models.base

class LayerSpec(id_, name, depends=None)

Bases: glimpse.util.dataflow.node.Node

Describes a single layer in a model.

name = None

(str) Name of model layer.

class Layer

Bases: glimpse.models.base.layer.Layer

classmethod AllLayers()

Return the unordered set of all layers.

Return type:list of LayerSpec

Example:

>>> assert(Layer.IMAGE in Layer.AllLayers())
classmethod FromId(id_)

Lookup a LayerSpec object by ID.

Parameters:id – Model-unique layer id_ifier.
Return type:LayerSpec

Example:

>>> lyr = Layer.FromId(Layer.IMAGE.id_)
>>> assert(lyr == Layer.IMAGE)
classmethod FromName(name)

Lookup a LayerSpec object by name.

This method is not case sensitive.

Parameters:name (str) – Layer name.
Return type:LayerSpec

Example:

>>> lyr = Layer.FromName(Layer.IMAGE.name)
>>> assert(lyr == Layer.IMAGE)
classmethod IsSublayer(sub_layer, super_layer)

Determine if one layer appears later in the network than another.

Parameters:
Return type:

bool

Examples:

>>> assert(Layer.IsSublayer(Layer.SOURCE, Layer.IMAGE))
>>> assert(not Layer.IsSublayer(Layer.IMAGE, Layer.SOURCE))
classmethod TopLayer()

Determine the top layer in this network.

The top-most layer is defined as the layer on which no other layer depends. If multiple layers meet this criteria, then the first such layer (as returned by AllLayers()) is returned.

Return type:LayerSpec

Example:

>>> assert(Layer.TopLayer() == Layer.IMAGE)
IMAGE = LayerSpec(id_='i', name='image', depends=[LayerSpec(id_='s', name='source', depends=None)])

Specifier for the raw input data.

SOURCE = LayerSpec(id_='s', name='source', depends=None)

Specifier for the source of the image data. This may be None if the data was generated programatically, or an InputSource object if the data came from an image file.

class State(elements=None, **kw)

Bases: glimpse.util.dataflow.state.State

A dictionary container for the Model state.

The main purpose of extending the dictionary (instead of just storing states as dictionary objects) is to indicate with which model a given state is associated. Each model has a seperate state object, so it is always clear which model generated a given state object.

ModelClass

The datatype associated with the model for this state. Should be set by sub-class.

alias of Model

class Model(backend=None, params=None)

Bases: glimpse.util.dataflow.dataflow.DataFlow

Abstract base class for a Glimpse model.

LayerClass

The datatype associated with layer descriptors for this model. This should be over-ridden by the sub-class with a descendent of layer.Layer.

alias of Layer

ParamClass

The type of the parameter collection associated with this model. This should be over-ridden by the sub-class.

alias of Params

StateClass

The datatype associated with network states for this model. This should be over-ridden by the sub-class, and should generally be a descendent of state.State.

alias of State

MakeState(source, copy=False)

Create a model state wrapper for the given image source.

Parameters:
  • state (str or Image.Image or 2D array of ACTIVATION_DTYPE or State subclass) – Source information
  • copy (bool) – If the input is already a state object, this argument determines whether the state is copied.
Return type:

state.State subclass

If source is an array, values should lie in the range [0,1).

Register(node, f)

Add a callback to compute a value for a node.

Note: be careful of passing a lambda function for f, as this will likely cause an error if serialized.

class Params(**kw)

Parameter container for the model.Model.

PrepareImage(img, params)

Prepare an image for input into the model.

Parameters:
  • img (Image) – Input data.
  • params (params.Params) – Parameters controlling image transformation.
Returns:

Image layer data with values in the range [0, 1].

Return type:

2D ndarray of float

The image may be scaled and/or cropped, depending on the settings of the image_resize_method attribute of the params argument. If the value is NONE, the image is returned unchanged. Given a value of SCALE_SHORT_EDGE, SCALE_LONG_EDGE, SCALE_WIDTH, or SCALE_HEIGHT, the given image edge will be rescaled to match params.image_resize_length (preserving the aspect ratio).

Finally, the image_resize_method attribute could be SCALE_AND_CROP. In this case, the image is scaled and cropped to a fixed size of (w, w/rho), where w is image_resize_length and rho is image_resize_aspect_ratio. This is achieved scaling the short edge (preserving aspect ratio) and then cropping from the long edge.

See also ScaleImage() and ScaleAndCropImage().

BuildLayer(model, layer, state, save_all=True)

Apply the model through to the given layer.

Parameters:
  • layer (LayerSpec or tuple of LayerSpec) – Output layer(s) to compute.
  • state (StateClass or str or Image) – Initial model state from which to compute the output layer, or input image as path or in-memory image.
  • save_all (bool) – Whether the resulting state should contain values for all computed layers in the network, or just the output layer. Note that source information is always preserved.
Returns:

Output state containing the given layer

Return type:

StateClass

Examples:

Get the IMAGE layer for an image.

>>> model = Model()
>>> input_state = model.MakeState(glab.GetExampleImage())
>>> output_state = BuildLayer(model, Layer.IMAGE, input_state)
>>> assert(Layer.IMAGE in output_state)
SamplePatches(model, layer, num_patches, patch_size, state)

Extract patches from the given layer for a single image.

Patches are sampled from random locations and scales.

Parameters:
  • layer (LayerSpec) – Layer from which to extract patches.
  • num_patches (int) – Number of patches to extract.
  • patch_size (int) – Spatial extent of patches.
  • state (State) – Input state for which layer activity is computed.
Returns:

list of (patch, location) pairs. The location is a triple, whose axes correspond to the scale, y-offset, and x-offset of the patch’s top-left corner.

Return type:

list of (patch, location) pairs

See also SamplePatchesFromData().

SamplePatchesFromData(data, patch_width, num_patches)

Sample patches from a layer of activity.

Parameters:
  • data (ND ndarray, or list of (N-1)D ndarray. Must have N >= 3.) – 3D activity maps, with one map per scale. Note that the smallest map in the data must be at least as large as the patch width.
  • patch_width (int) – Spatial extent of patch.
Return type:

pair of arrays

Returns:

Array of patches and array of corresponding locations (elements are 3-tuples). Location given as (s, y, x), where s is scale, and (y, x) gives the top-left corner of the region.

Each location (s, y, x) is guaranteed to lie in

0 <= s < S

0 <= y < H - patch_width

0 <= x < W - patch_width

where S is the number of scales in data, and H and W give its spatial height and width, respectively.

Throws InputSizeError if the input is (spatially) smaller than patch_width.

Examples:

Extract 50 patches of size 10x10 from a stack of 2D arrays:

>>> shape = (4, 100, 100)
>>> data = numpy.random.random(shape)
>>> patches, locs = SamplePatchesFromData(data, patch_width = 10, 50)

Table Of Contents

Previous topic

glimpse.models

Next topic

glimpse.models.base.layer

This Page