pixano_inference.frameworks.torch
PyTorch framework adapter and torch-specific helpers.
This module is the single home for torch-specific operations (device/dtype resolution,
image-to-tensor conversion, RLE encoding, and NDArray<->tensor bridging). The core never
imports it; model backends and impls/_helpers do. Every torch import here is lazy, so
merely importing this module does not require torch to be installed.
TorchAdapter
:class:~pixano_inference.frameworks.base.FrameworkAdapter for PyTorch.
from_numpy(array, device=None, dtype=None)
Convert a numpy array to a torch tensor on device with dtype.
Source code in pixano_inference/frameworks/torch.py
is_available()
resolve_device(num_gpus)
resolve_dtype(dtype)
convert_image_pil_to_tensor(image, device, size=None)
Convert a PIL image to a (C, H, W) float tensor, optionally resizing it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Any
|
PIL image. |
required |
device
|
'torch.device'
|
Torch device. |
required |
size
|
int | None
|
Optional target size (both height and width). |
None
|
Returns:
| Type | Description |
|---|---|
'Tensor'
|
Image as a |
Source code in pixano_inference/frameworks/torch.py
encode_mask_to_rle(mask)
Encode a binary mask tensor using RLE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
'Tensor'
|
A binary mask of shape (height, width). |
required |
Returns:
| Type | Description |
|---|---|
dict[str, list[int]]
|
RLE encoded mask as a dictionary. |
Source code in pixano_inference/frameworks/torch.py
ndarray_to_tensor(array)
Convert an :class:NDArray wire type to a torch tensor (via numpy).
resolve_device(config)
Return the torch device for a deployment config (see :func:resolve_device_from_num_gpus).
resolve_device_from_num_gpus(num_gpus)
Return torch.device('cuda') when GPUs are requested and CUDA is available.
Falls back to Apple-Silicon MPS when requested and available, else CPU.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_gpus
|
float
|
Number of GPUs requested for the deployment. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
A |
Source code in pixano_inference/frameworks/torch.py
resolve_torch_dtype(dtype_str)
Map a dtype string to a torch.dtype.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dtype_str
|
str
|
One of |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Corresponding |
Raises:
| Type | Description |
|---|---|
ValueError
|
If dtype_str is not recognised. |
Source code in pixano_inference/frameworks/torch.py
tensor_to_ndarray(tensor)
Convert a torch tensor to an :class:NDArrayFloat wire type (via numpy).
Source code in pixano_inference/frameworks/torch.py
vector_to_tensor(vector)
Coerce an :class:NDArrayFloat or tensor to a torch tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vector
|
'NDArrayFloat | Tensor | None'
|
An |
required |
Returns:
| Type | Description |
|---|---|
'Tensor'
|
The tensor. |