Utils
image_to_tensor
¶
image_to_tensor(
image: Image,
device: device | str | None = None,
dtype: dtype | None = None,
) -> Tensor
Convert a PIL Image to a Tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image
|
Image
|
The image to convert. |
required |
device
|
device | str | None
|
The device to use for the tensor. |
None
|
dtype
|
dtype | None
|
The dtype to use for the tensor. |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
The converted tensor. |
Note
If the image is in mode RGB
the tensor will have shape [3, H, W]
,
otherwise [1, H, W]
for mode L
(grayscale) or [4, H, W]
for mode RGBA
.
Values are normalized to the range [0, 1]
.
Source code in src/refiners/fluxion/utils.py
load_from_safetensors
¶
Load tensors from a SafeTensor file from disk.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
Path | str
|
The path to the file. |
required |
device
|
device | str
|
The device to use for the tensors. |
'cpu'
|
Returns:
Type | Description |
---|---|
dict[str, Tensor]
|
The loaded tensors. |
Source code in src/refiners/fluxion/utils.py
load_tensors
¶
Load tensors from a file saved with torch.save
from disk.
Note
This function uses the weights_only
mode of torch.load
for additional safety.
Warning
Still, only load data you trust and favor using
load_from_safetensors
instead.
Source code in src/refiners/fluxion/utils.py
save_to_safetensors
¶
save_to_safetensors(
path: Path | str,
tensors: dict[str, Tensor],
metadata: dict[str, str] | None = None,
) -> None
Save tensors to a SafeTensor file on disk.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
Path | str
|
The path to the file. |
required |
tensors
|
dict[str, Tensor]
|
The tensors to save. |
required |
metadata
|
dict[str, str] | None
|
The metadata to save. |
None
|
Source code in src/refiners/fluxion/utils.py
str_to_dtype
¶
Converts a string dtype to a torch.dtype.
See also https://pytorch.org/docs/stable/tensor_attributes.html#torch-dtype
Source code in src/refiners/fluxion/utils.py
summarize_tensor
¶
Summarize a tensor.
This helper function prints the shape, dtype, device, min, max, mean, std, norm and grad of a tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
Tensor
|
The tensor to summarize. |
required |
Returns:
Type | Description |
---|---|
str
|
The summary string. |
Source code in src/refiners/fluxion/utils.py
tensor_to_image
¶
tensor_to_image(tensor: Tensor) -> Image
Convert a Tensor to a PIL Image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
Tensor
|
The tensor to convert. |
required |
Returns:
Type | Description |
---|---|
Image
|
The converted image. |
Note
The tensor must have shape [1, channels, height, width]
where the number of
channels is either 1 (grayscale) or 3 (RGB) or 4 (RGBA).
Expected values are in the range [0, 1]
and are clamped to this range.