Skip to content

mpcompress.utils

center_crop

center_crop(x, padding)

Remove center padding from a tensor.

Crops the tensor by removing the padding that was added by center_pad. Uses negative padding values to crop from the edges.

Parameters:

Name Type Description Default
x Tensor

Padded tensor to crop.

required
padding tuple

Tuple of (padding_left, padding_right, padding_top, padding_bottom) returned by center_pad.

required

Returns:

Name Type Description
x_cropped Tensor

Cropped tensor with original spatial dimensions restored.

center_pad

center_pad(x, p)

Pad a tensor to make its spatial dimensions divisible by p, using center padding.

Pads the tensor symmetrically (centered) so that both height and width become multiples of p. The padding is distributed evenly on both sides when possible, with any remainder added to the right/bottom.

Parameters:

Name Type Description Default
x Tensor

Input tensor with shape (B, C, H, W).

required
p int

Padding divisor. Height and width will be padded to be divisible by p.

required

Returns:

Name Type Description
x_padded Tensor

Padded tensor with shape (B, C, new_H, new_W).

padding tuple

Tuple of (padding_left, padding_right, padding_top, padding_bottom) that can be used with center_crop to restore original size.

extract_shapes

extract_shapes(nested_structure)

Extract shape information from nested data structures.

Recursively processes nested structures (tensors, arrays, dicts, lists, tuples) and returns a structure with the same nesting but containing shape information instead of the actual data.

Parameters:

Name Type Description Default
nested_structure any

A nested structure that may contain torch.Tensor, np.ndarray, bytes, dict, list, tuple, or other types.

required

Returns:

Name Type Description
nested_structure any

A structure with the same nesting as the input.

get_timestamp

get_timestamp()

Generate a timestamp string in the format YYMMDD-HHMMSS.

Returns:

Name Type Description
timestamp str

Timestamp string in format "YYMMDD-HHMMSS".

register

register(name: str)

Public decorator to register a class in the global registry.

rename_key_by_rules

rename_key_by_rules(key: str, rules: list) -> str

Rename a key according to a list of rules, using preset semantics first, then regex matching.

Rules are processed in order. The first matching rule is applied and the function returns. Rule types include: "startswith", "endswith", "contains", "exact", "regex".

Parameters:

Name Type Description Default
key str

Original key name to process.

required
rules list

List of rules, each rule is a list of [type, pattern, replacement]. type can be: "startswith", "endswith", "contains", "exact", "regex".

required

Returns:

Name Type Description
renamed_key str

Renamed key if a rule matches, otherwise returns the original key.

rgb2ycbcr

rgb2ycbcr(rgb: Tensor, is_bgr=False)

Convert RGB tensor to YCbCr color space.

Converts RGB values to YCbCr using ITU-R BT.709 standard weights. The output values are clamped to [0, 1] range.

Parameters:

Name Type Description Default
rgb Tensor

RGB tensor with shape (..., 3, H, W) or (..., C, H, W) where the last 3 channels are RGB.

required
is_bgr bool

If True, interpret input as BGR instead of RGB. Defaults to False.

False

Returns:

Name Type Description
ycbcr Tensor

YCbCr tensor with shape (..., 3, H, W), values clamped to [0, 1].

setup_logger

setup_logger(logger_name, root, phase, level=logging.INFO, screen=False, tofile=False)

Set up a logger with optional file and screen handlers.

Configures a logger with the specified name and level. Can optionally add a file handler (appending to log file) and/or a stream handler (outputting to console).

Parameters:

Name Type Description Default
logger_name str

Name of the logger to create or retrieve.

required
root str

Root directory path for log files.

required
phase str

Phase name used to construct log file name (e.g., "train", "test").

required
level int

Logging level (e.g., logging.INFO, logging.DEBUG). Defaults to logging.INFO.

INFO
screen bool

If True, add a StreamHandler for console output. Defaults to False.

False
tofile bool

If True, add a FileHandler for file output. Defaults to False.

False

tensor2image

tensor2image(x: Tensor) -> Image.Image

Convert a tensor to a PIL Image.

Clamps tensor values to [0, 1] range, removes dimensions of size 1, and converts to PIL Image format.

Parameters:

Name Type Description Default
x Tensor

Input tensor with values in [0, 1] range.

required

Returns:

Name Type Description
image Image

The converted PIL Image.

ycbcr2rgb

ycbcr2rgb(ycbcr: Tensor, is_bgr=False, clamp=True)

Convert YCbCr tensor to RGB color space.

Converts YCbCr values to RGB using ITU-R BT.709 standard weights. Optionally clamps output values to [0, 1] range.

Parameters:

Name Type Description Default
ycbcr Tensor

YCbCr tensor with shape (..., 3, H, W) or (..., C, H, W) where the last 3 channels are YCbCr.

required
is_bgr bool

If True, output as BGR instead of RGB. Defaults to False.

False
clamp bool

If True, clamp output values to [0, 1] range. Defaults to True.

True

Returns:

Name Type Description
rgb Tensor

RGB tensor with shape (..., 3, H, W), optionally clamped to [0, 1].