glimpse.prototypes

UniformRandom(num_patches, patch_shape, low, high)

Create patches by sampling components uniformly.

SampleAndLearnPatches(model, images, learner, num_patches, patch_widths, layer, pool, num_samples=None, progress=None)

Draw samples of layer activity and build a simple patch model.

Parameters:
  • model – Glimpse model with which to generate layer activity.
  • layer – The layer from which to sample patches.
  • images – list of model states (e.g., image paths).
  • learner (callable) – Patch model estimator, with the signature learner(num_patches, samples, progress=progress) where samples is an (N+1)-dimensional array of samples.
  • num_patches (int) – Number of patches to generate.
  • patch_widths (list of int) – Size of patches to draw.
  • pool – Worker pool.
  • num_samples (int) – Number of patch samples to draw. By default, the number of samples is 10x the number of desired patches.
  • progress – Callback for progress updates.
Return type:

list of (N+1)-dimensional array, where N is the number of dimensions in a patch.

Returns:

Set of patches for each patch size.

For each size in patch_widths, a set of num_samples is drawn from the images and modelled via the learner.

Histogram(num_patches, samples, progress=None)

Create patches from a histogram over sample values.

Parameters:
  • num_patches (int) – number of patches to create
  • samples (2D array) – example patches
  • progress – ignored
Return type:

2D array with num_patches rows and N columns, where N is the number of columns in samples.

Returns:

created patches

NormalRandom(num_patches, samples, progress=None)

Estimate parameters of a normal distribution and sample from it.

Parameters:
  • num_patches (int) – number of patches to create
  • samples (2D array) – example patches
  • progress – ignored
Return type:

2D array with num_patches rows and N columns, where N is the number of columns in samples.

Returns:

created patches

Kmeans(num_patches, samples, progress=None, batch=False)

Estimate patches as centroids of samples using k-Means.

Parameters:
  • num_patches (int) – number of patches to create
  • samples (2D array) – example patches
  • progress – ignored
  • batch (bool) – whether to learn using batch or online k-Means
Return type:

2D array with num_patches rows and N columns, where N is the number of columns in samples.

Returns:

created patches

NearestKmeans(num_patches, samples, progress=None, batch=False)

Estimate patches as centroids of samples using nearest-value k-Means.

This performs k-Means cluster of the samples, and then replaces each centroid with its nearest cluster element.

Parameters:
  • num_patches (int) – number of patches to create
  • samples (2D array) – example patches
  • progress – ignored
  • batch (bool) – whether to learn using batch or online k-Means
Return type:

2D array with num_patches rows and N columns, where N is the number of columns in samples.

Returns:

created patches

Kmedoids(num_patches, samples, progress=None)

Estimate patches as centroids of samples using k-Medoids.

This requires the Pycluster library to be installed.

Parameters:
  • num_patches (int) – number of patches to create
  • samples (2D array) – example patches
  • progress – ignored
Return type:

2D array with num_patches rows and N columns, where N is the number of columns in samples.

Returns:

created patches

Ica(num_patches, samples, progress=None)

Estimate patches using Independent Components Analysis.

Parameters:
  • num_patches (int) – number of patches to create
  • samples (2D array) – example patches
  • progress – ignored
Return type:

2D array with num_patches rows and N columns, where N is the number of columns in samples.

Returns:

created patches

Pca(num_patches, samples, progress=None)

Estimate patches using Principal Components Analysis.

Parameters:
  • num_patches (int) – number of patches to create
  • samples (2D array) – example patches
  • progress – ignored
Return type:

2D array with num_patches rows and N columns, where N is the number of columns in samples.

Returns:

created patches

Nmf(num_patches, samples, progress=None)

Estimate patches using Non-negative Matrix Factorization.

Parameters:
  • num_patches (int) – number of patches to create
  • samples (2D array) – example patches
  • progress – ignored
Return type:

2D array with num_patches rows and N columns, where N is the number of columns in samples.

Returns:

created patches

SparsePca(num_patches, samples, progress=None)

Estimate patches using Sparse Principal Components Analysis.

See sklearn.decomposition.MiniBatchSparsePCA for more information.

Parameters:
  • num_patches (int) – number of patches to create
  • samples (2D array) – example patches
  • progress – ignored
Return type:

2D array with num_patches rows and N columns, where N is the number of columns in samples.

Returns:

created patches

SamplePatchesFromImages(model, sample_layer, kernel_sizes, num_kernels, input_states, pool=None, normalize=False, progress=None)

Sample patches of multiple sizes from a set of images.

Parameters:
  • model (sub-class of glimpse.models.base.Model) – Glimpse model.
  • sample_layer (LayerSpec) – Layer from which to sample.
  • kernel_sizes (int or list of int) – Kernel widths to extract.
  • num_kernels (int) – Number of patches to sample for each kernel width.
  • input_states (list of model.StateClass or list of str) – Initial network states from which to compute layer activity.
  • pool – Worker pool used to evaluate the model.
  • normalize (bool) – Whether to scale each kernel to have unit norm.
Returns:

Kernels, and their corresponding locations. Kernels are returned as a list of (N+1)-dimensional arrays, where N is the number of dimensions in a single kernel. The list axis and first axis of the array correspond to the kernel size and kernel offset, respectively. Locations are returned as a 2D list of 4-tuples, where list axes correspond to kernel size and kernel offset, respectively. Each location is given as a 4-tuple, with elements corresponding to the input state index, scale, y-offset, and x-offset of the corresponding kernel.

Note that the maximum kernel size must not be larger than the smallest response map at the given layer. To imprint a 7x7 kernel from C1, for example, the C1 response map for the lowest frequency band must be at least 7x7 in spatial extent.

Examples:

For each kernel size, sample 10 image patches from four (random) images. Kernels will be 7 and 11 pixel wide.

>>> model = BaseModel()
>>> images = np.random.random((4,100,100)).astype(ACTIVATION_DTYPE)
>>> states = map(model.MakeState, images)
>>> kernels,locations = SamplePatches(model, BaseLayer.IMAGE,
    kernel_sizes=(7,11), num_kernels=10, input_state=states)

The result will contain a sub-list for each of the two kernel sizes.

>>> assert len(kernels) == len(images)
>>> assert all(len(ks) == 10 for ks in kernels)
>>> assert len(locations) == len(images)
>>> assert all(len(ls) == 10 for ls in locations)

Previous topic

glimpse.models.ml

Next topic

glimpse.pools

This Page