Skip to content

mlvbench.stimuli.transforms

Image transforms.

BinarizeSegmentation

Threshold the segmentation map in a sample dict to the values 0 and 1.

Non-segmentation values are passed through unchanged.

__call__(sample)

Binarize the segmentation in the sample.

Parameters:

Name Type Description Default
sample dict[str, str | Tensor]

A dictionary with a "segmentation" tensor.

required

Returns:

Type Description
dict[str, str | Tensor]

A new dictionary where "segmentation" is a uint8 tensor of the same shape with values 0 and 1.

__init__(threshold=127)

Initialize the transform.

Parameters:

Name Type Description Default
threshold int

Values strictly greater than the threshold are mapped to 1, all other values to 0.

127

CenterCrop

Crop a single square at the center.

The crop size is the shorter dimension of the input image.

__call__(sample)

Transform the given sample.

Resize

Resize images and segmentation maps so the shorter side equals image_size.

__call__(sample)

Transform the given sample.

__init__(image_size)

Initialize the transform.

Parameters:

Name Type Description Default
image_size int

The target size for the shorter side.

required

ToNumpy

Convert tensors in a sample dict to numpy arrays.

__call__(sample)

Convert tensors in the sample to numpy arrays.

Parameters:

Name Type Description Default
sample dict[str, str | Tensor]

A dictionary whose values may be tensors.

required

Returns:

Type Description
dict[str, str | ndarray]

A new dictionary with tensors replaced by numpy arrays.

ToTensor

Convert PIL Images in a sample dict to tensors.

Images with shape (H, W) are converted to (1, H, W). Images with shape (H, W, C) are converted to (C, H, W). The "image" key is additionally converted from grayscale to RGB if it has a single channel.

Non-image values are passed through unchanged.

__call__(sample)

Convert PIL Images in the sample to tensors.

Parameters:

Name Type Description Default
sample dict[str, str | Image]

A dictionary whose values may be PIL Images or other types.

required

Returns:

Type Description
dict[str, str | Tensor]

A new dictionary with PIL Images replaced by uint8 tensors of shape (1, H, W) or (C, H, W) (the "image" key is expanded to (3, H, W)).