Transforms#
This module provides a set of transforms for converting various data formats.
It is primarily used to convert images and other data types into
xlnstorch.LNSTensor objects and is similar to the transforms
provided by torchvision.
This submodule requires the torchvision package to be installed, which can
be done via pip:
pip install torchvision
- class xlnstorch.transforms.ToLNSTensor(f=None, b=None, wrap_all=False, device=None)#
Convert an image-like object (PIL image or numpy.ndarray) with shape (H x W x C) in the range [0, 255] to an LNSTensor with shape (C x H x W) in the range [0., 1.]. This is analogous to torchvision.transforms.ToTensor but wraps the output in an LNSTensor.
Non-floating point inputs are not converted to LNSTensor unless specified by the wrap_all parameter.
- Parameters:
f (Optional[int], optional) – The precision of the output LNSTensor.
b (Optional[float], optional) – The base of the output LNSTensor.
wrap_all (bool, optional) – If True, all inputs are wrapped in an LNSTensor, even if they are not floating point. If False (default), only floating point inputs are wrapped.
device (Optional[Union[str, torch.device]], optional) – The device on which the output LNSTensor should be allocated. If None, it defaults to the current device.
- Raises:
ImportError – If torchvision is not available, an ImportError is raised when trying to use this class.
Note
By default, the ToLNSTensor transform will only wrap floating point tensors. If you want to wrap all tensor types, set the wrap_all parameter to True.
- class xlnstorch.transforms.LNSNormalize(mean, std, f=None, b=None)#
Bases:
objectNormalize an LNSTensor with mean and standard deviation. This is analogous to torchvision.transforms.Normalize but performs normalization on LNSTensor.
- Parameters:
mean (Union[float, Tuple[float, ...]]) – The mean value(s) for normalization.
std (Union[float, Tuple[float, ...]]) – The standard deviation value(s) for normalization.
f (Optional[int] = None, optional) – The precision of the input LNSTensor.
b (Optional[float] = None, optional) – The base of the input LNSTensor.
- Returns:
The normalized LNSTensor.
- Return type:
Quick Start#
Here’s a simple example of using the LNS transforms with image data:
import xlnstorch as xltorch
from xlnstorch.transforms import ToLNSTensor
from PIL import Image
# Create transform to convert image to LNSTensor
transform = ToLNSTensor(f=8, device="cpu")
# Load and transform an image
image = Image.open("example.jpg")
lns_tensor = transform(image)
print(type(lns_tensor)) # <class 'xlnstorch.LNSTensor'>
For use with PyTorch datasets:
import torch
from torch.utils.data import DataLoader
from torchvision import datasets
import xlnstorch as xltorch
from xlnstorch.transforms import ToLNSTensor
transform = ToLNSTensor(f=8)
# Download a dataset and apply the transform
dataset = datasets.MNIST("./data", train=True, download=True, transform=transform)
dataloader = DataLoader(dataset, batch_size=16, shuffle=True)
# Get the first batch of data
data, target = next(iter(dataloader))
print(data.shape) # torch.Size([16, 1, 28, 28])
print(type(data)) # <class 'xlnstorch.LNSTensor'>
print(target.shape) # torch.Size([16])
print(type(target)) # <class 'torch.Tensor'>