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
|
|
Padded tensor to crop. |
required |
padding
|
|
Tuple of (padding_left, padding_right, padding_top, padding_bottom) returned by center_pad. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
x_cropped |
|
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
|
|
Input tensor with shape (B, C, H, W). |
required |
p
|
|
Padding divisor. Height and width will be padded to be divisible by p. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
x_padded |
|
Padded tensor with shape (B, C, new_H, new_W). |
padding |
|
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
|
|
A nested structure that may contain torch.Tensor, np.ndarray, bytes, dict, list, tuple, or other types. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
nested_structure |
|
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 |
|
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
|
|
Original key name to process. |
required |
rules
|
|
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 |
|
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
|
|
RGB tensor with shape (..., 3, H, W) or (..., C, H, W) where the last 3 channels are RGB. |
required |
is_bgr
|
|
If True, interpret input as BGR instead of RGB. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
ycbcr |
|
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
|
|
Name of the logger to create or retrieve. |
required |
root
|
|
Root directory path for log files. |
required |
phase
|
|
Phase name used to construct log file name (e.g., "train", "test"). |
required |
level
|
|
Logging level (e.g., logging.INFO, logging.DEBUG). Defaults to logging.INFO. |
|
screen
|
|
If True, add a StreamHandler for console output. Defaults to False. |
False
|
tofile
|
|
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
|
|
Input tensor with values in [0, 1] range. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
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
|
|
YCbCr tensor with shape (..., 3, H, W) or (..., C, H, W) where the last 3 channels are YCbCr. |
required |
is_bgr
|
|
If True, output as BGR instead of RGB. Defaults to False. |
False
|
clamp
|
|
If True, clamp output values to [0, 1] range. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
rgb |
|
RGB tensor with shape (..., 3, H, W), optionally clamped to [0, 1]. |