solt.core

class solt.core.BaseTransform(p=None, data_indices=None)[source]

Bases: Serializable

Transformation abstract class.

Parameters:
  • p (float or None) – Probability of executing this transform

  • data_indices (tuple or None) – Indices where the transforms need to be applied

apply(data: DataContainer)[source]

Applies transformation to a DataContainer items depending on the type.

Parameters:

data (DataContainer) – Data to be augmented

Returns:

out – Result

Return type:

DataContainer

sample_transform(data: DataContainer)[source]

Samples transform parameters based on data.

Parameters:

data (DataContainer) – Data container to be used for sampling.

Returns:

out – Coordinate frame (h, w).

Return type:

tuple

use_transform()[source]

Method to randomly determine whether to use this transform.

Returns:

out – Boolean flag. True if the transform is used.

Return type:

bool

class solt.core.DataContainer(data, fmt, transform_settings=None)[source]

Bases: object

Data container to encapsulate different types of data, such as images, bounding boxes, etc.

The container itself is iterable according to the format. Data container assumes the same coordinate system for all the items it is storing. For instance, the behavior shown below is not supported:

from solt.core import DataContainer
import numpy as np

img1 = np.ones((100, 100, 3), dtype=np.int32)
img2 = np.ones((110, 110, 3), dtype=np.int32)
trf_img = DataContainer.from_dict({'images': (img1, img2)})
Parameters:
  • data (tuple) – Data items stored in a tuple

  • fmt (str) – Data format. Example: ‘IMMM’ - image and three masks.

  • transform_settings (dict or None) – Settings for each data item. At this stage, the settings include only padding and interpolation. The key in this dict corresponds to the index of the element in the given data tuple. The value is another dict, which has all the settings. Segmentation masks have nearest neighbor interpolation by default, this can be changed manually if needed. Example: transform_settings={0:{'interpolation':'bilinear'}, 1: {'interpolation':'bicubic'}}

static from_dict(data)[source]

Creates a data container from a dictionary.

If data is a dict, then the solt.data.DataContainer will be created so that the image data will be stored first. Subsequently, multiple images under the key images will be stored. The same applies to masks (first mask and then masks), labels (label and labels), and the keypoints (keypoints and keypoints_array). You must use solt.data.KeyPoints object here. Labels will always be stored last.

For example, if the input dict looks like this: d = {'label': l1, 'image': i1, 'mask': m1} or d = {'mask': m1, 'image': i1, 'label': l1}, the DataContainer will convert this into sld.DataContainer((i1, m1, l1), 'IML').

More complex case:

d = {'image': i1, masks: (m1, m2, m3, m4), 'labels': (l1, l2, l3, l4, l5),
'keypoints': solt.core.KeyPoints(k, h, w)}
dc_from_dict = solt.core.DataContainer.from_dict(d)

will be equivalent to

dc = solt.core.DataContainer((i1, m1, m2, m3, m4, solt.core.KeyPoints(k, h, w), l1, l2, l3, l4, l5),
'IMMMMPLLLLLL').

Please note, that when you create DataContainer using such a simplified interface, you cannot setup the transform parameters per item. Use a proper constructor instead.

Parameters:

data (dict) – Data stored in a dictionary

Returns:

out – Newly instantiated data container object

Return type:

sld.DataContainer

to_torch(as_dict=False, scale_keypoints=True, normalize=False, mean=None, std=None)[source]

This method converts the DataContainer Content into a dict or a list PyTorch objects

Parameters:
  • as_dict (bool) – Whether to return the result as a dictionary. If a single item is present, then the singular naming will be used. If plural, then the plural will be used. The items will be stored and sorted a similar manner to the method from_dict: images, masks, keypoints_array, and labels. The same applies to a singular case/

  • scale_keypoints (bool) – Whether to scale keypoints to 0-1 range. True by default.

  • normalize (bool) – Whether to subtract mean

  • mean (torch.Tensor) – Mean to subtract. If None, then the ImageNet mean will be subtracted.

  • std (torch.Tensor) – Std to subtract. If None, then the ImageNet std will be subtracted.

class solt.core.ImageTransform(p=None, data_indices=None)[source]

Bases: BaseTransform

Abstract class, allowing the application of a transform only to an image

class solt.core.InterpolationPropertyHolder(interpolation=None)[source]

Bases: object

Adds interpolation property to a class and validates it using the allowed interpolations from constants.

Parameters:

interpolation (None or str or tuple) – Interpolation mode. Inheritance can be specified as the second argument of the interpolation tuple.

class solt.core.Keypoints(pts=None, height=None, width=None)[source]

Bases: object

Keypoints class

Parameters:
  • pts (numpy.ndarray) – Key points as an numpy.ndarray in (x, y) format.

  • height (int) – Height of the coordinate frame.

  • width (int) – Width of the coordinate frame.

class solt.core.MatrixTransform(interpolation='bilinear', padding='z', p=0.5, ignore_state=True, affine=True, ignore_fast_mode=False)[source]

Bases: BaseTransform, InterpolationPropertyHolder, PaddingPropertyHolder

Matrix Transform abstract class. (Affine and Homography). Does all the transforms around the image / center.

Parameters:
  • interpolation (str) – Interpolation mode.

  • padding (str or None) – Padding Mode.

  • p (float) – Probability of transform’s execution.

  • ignore_state (bool) – Whether to ignore the pre-calculated transformation or not. If False, then it will lead to an incorrect behavior when the objects are of different sizes. Should be used only when it is assumed that the image, mask and keypoints are of the same size.

static correct_for_frame_change(transform_matrix: ndarray, width: int, height: int)[source]

Method takes a matrix transform, and modifies its origin.

Parameters:
  • transform_matrix (numpy.ndarray) – Transform (3x3) matrix

  • width (int) – Width of the coordinate frame

  • height (int) – Height of the coordinate frame

Returns:

out – Modified Transform matrix

Return type:

numpy.ndarray

fuse_with(trf)[source]

Takes a transform an performs a matrix fusion. This is useful to optimize the computations

Parameters:

trf (MatrixTransform)

sample_transform(data)[source]

Samples the transform and corrects for frame change.

Return type:

None

abstract sample_transform_matrix(data)[source]

Method that is called to sample the transform matrix

class solt.core.PaddingPropertyHolder(padding=None)[source]

Bases: object

Adds padding property to a class and validates it using the allowed paddings from constants.

Parameters:

padding (None or str or tuple) – Padding mode. Inheritance can be specified as the second argument of the padding tuple.

class solt.core.SelectiveStream(transforms=None, n=1, probs=None, optimize_stack=False, ignore_fast_mode=False)[source]

Bases: Stream

Stream that uniformly selects n out of k given transforms.

serializable_name = 'selective_stream'

How the class should be stored in the registry

class solt.core.Stream(transforms=None, interpolation=None, padding=None, optimize_stack=False, ignore_fast_mode=False, shuffle=False)[source]

Bases: Serializable

Stream class. Executes the list of transformations.

The stream can be called directly on a dict of items, which get converted into a DataContainer. Note that the DataContainer assumes the same coordinate system for all the items it is storing. For instance, the behavior shown below is not supported for any stream:

import solt.transforms as slt
import numpy as np

trf = solt.Stream([slt.Resize(resize_to=(50, 50)),
                       slt.Flip()])
img1 = np.ones((100, 100, 3), dtype=np.int32)
img2 = np.ones((110, 110, 3), dtype=np.int32)
trf({'images': (img1, img2)})
Parameters:
  • transforms (list or None) – List of transforms to execute

  • interpolation (str or None) – Stream-wide settings for interpolation. If for some particular transform your would like to still use its own mode, simply pass (<interpolation_value>, 'strict') in the constructor of that transform.

  • padding (str or None) – Stream-wide settings for padding. If for some particular transform your would like to still use its own mode, simply pass (<padding_value>, 'strict') in the constructor of that transform.

  • optimize_stack (bool) – Whether to run transforms stack optimization. It can only be useful if many matrix transformations are in a row.

  • ignore_fast_mode (bool) – Whether to ignore the fast mode. This option enables full geometric transforms.

  • shuffle (bool) – Whether to shuffle the transforms before applying them.

static exec_stream(transforms, data, optimize_stack, shuffle=False)[source]

Static method, executes the list of transformations for a given data point.

Parameters:
  • transforms (list) – List of transformations to execute

  • data (DataContainer or dict) – Data to be augmented. See solt.data.DataContainer.from_dict to check how a conversion from dict is done.

  • optimize_stack (bool) – Whether to execute augmentations stack optimization.

Returns:

out – Result

Return type:

DataContainer

static optimize_transforms_stack(transforms, data)[source]

Static method which fuses the transformations

Parameters:
  • transforms (list) – A list of transforms

  • data (DataContainer) – Data container to be used to sample the transforms

Returns:

out – An optimized list of transforms

Return type:

list

reset_interpolation(value)[source]

Resets the interpolation for the whole pipeline of transforms.

Parameters:

value (str or None) – A value from solt.constants.ALLOWED_INTERPOLATIONS

reset_padding(value)[source]

Allows to reset the padding for the whole Stream

Parameters:

value (str) – Should be a string from solt.constants.ALLOWED_PADDINGS

serializable_name = 'stream'

How the class should be stored in the registry