imgtools.utils
Support tools for programming, e.g., type and dtype conversion.
Links
Helpers
|
Represent a union type |
|
Convert shape of coefficeints such that coeff (op) img is valid, where op may be one of arithmetic operations or other operator. |
Mathes the device and dtype of the source tensor to the target tensor. |
|
Converts an item to a np.ndarray. |
|
Converts an item to a contiguous torch.Tensor. |
Documents
- imgtools.utils.align_device_type(source: Tensor, target: Tensor)
Mathes the device and dtype of the source tensor to the target tensor.
- Parameters:
- sourcetorch.Tensor
A tensor need to align.
- targettorch.Tensor
A tensor provides device and dtype.
- Returns:
- torch.Tensor
Source tensor with
same device as target tensor
same dtype as target tensor if target.dtype is a floating point
float32 dtype if target.dtype is not a floating point
- imgtools.utils.arrayize(img: Tensor | ndarray | list[int | float | bool] | int | float | bool) ndarray
Converts an item to a np.ndarray.
- If input is a torch.Tensor:
Moves -3-axis to the last for ndim >= 3.
Without any handling for the other cases.
For other types, convert to a ndarray by np.array.
This function is not jit-able.
- Parameters:
- imgTensorlike
An item to be converted to tensor.
- imgtools.utils.tensorize(img: Tensor | ndarray | list[int | float | bool] | int | float | bool) Tensor
Converts an item to a contiguous torch.Tensor.
- If input is a np.ndarray:
Moves -1-axis to -3-axis if ndim >= 3.
Reshape to (1, H, W) if ndim = 2.
Otherwise, without any handling.
For other types, convert to a tensor by torch.tensor.
This function is not jit-able.
- Parameters:
- imgTensorlike
An item to be converted to tensor.
Examples
>>> import numpy as np >>> from imgtools.utils import tensorize >>> from PIL import Image >>> >>> img = np.asarray(Image.open(file)) >>> img.shape # (H, W, C) >>> img_t = tensorize(img) >>> img_t.shape # (C, H, W)