# base_loader

Python API: `luxonis_train.loaders.base_loader`

The base class every loader inherits, and the type of one sample.

[LuxonisLoaderTorchOutput](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Module-Attributes-LuxonisLoaderTorchOutput)
is the type of one sample. It is a pair of the input and the labels. The input is an image of shape `[C, H, W]`, or a dictionary
that maps each input name to its image. The labels map each `"<task_name>/<label>"` key to a tensor.

## Classes

| Name | Description |
| --- | --- |
| [`BaseLoaderTorch`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch) | Base class for the loaders of the training pipeline. |

### class `BaseLoaderTorch`

```python
class BaseLoaderTorch(Dataset, ABC)
```

Base class for the loaders of the training pipeline.

A subclass registers in
[luxonis_train.registry.LOADERS](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/registry.md#registry-Module-Attributes-LOADERS)
under its class name, so the `loader.name` field of a config can name it. A subclass must implement
[input_shapes](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-input_shapes),
`__len__`, `__getitem__`, and
[get_classes](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-get_classes).
A loader with keypoint labels must also override
[get_n_keypoints](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-get_n_keypoints).

Properties

| Name | Type | Description |
| --- | --- | --- |
| [`augmentation_config`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-augmentation_config) | `list[AugmentationConfig]` | The augmentations of the loader. |
| [`augmentation_engine`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-augmentation_engine) | `str` | The name of the augmentation engine. |
| [`color_space`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-color_space) | `Literal['RGB', 'BGR']` | The color space of the output image. |
| [`height`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-height) | `int` | The height of the output image. |
| [`image_source`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-image_source) | `str` | The name of the main image source, such as `"image"`. |
| [`input_shape`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-input_shape) | `Size` | The shape `[C, H, W]` of the [image_source](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-image_source) input. |
| [`input_shapes`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-input_shapes) | `dict[str, Size]` | The shape of each input of one sample, keyed by the input name. |
| [`keep_aspect_ratio`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-keep_aspect_ratio) | `bool` | Whether the resize keeps the aspect ratio of the image. |
| [`seed`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-seed) | `int | None` | The random seed of the augmentations, or `None`. |
| [`view`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-view) | `list[str]` | The splits that form the view, such as `["train"]`. |
| [`width`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-width) | `int` | The width of the output image. |

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-__init__) | Store the settings that every loader shares. |
| [`augment_test_image`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-augment_test_image) | Apply the augmentations of the loader to one raw image. |
| [`collate_fn`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-collate_fn) | Merge a list of samples into one batch. |
| [`dict_numpy_to_torch`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-dict_numpy_to_torch) | Convert a dictionary of NumPy arrays to `torch.float32` tensors. |
| [`get_categorical_encodings`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-get_categorical_encodings) | Return the integer code of each category of each metadata label. |
| [`get_classes`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-get_classes) | Return the class names and the class IDs of each task. |
| [`get_metadata_types`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-get_metadata_types) | Return the Python type of each metadata label. |
| [`get_n_keypoints`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-get_n_keypoints) | Return the number of keypoints of each task. |
| [`img_numpy_to_torch`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-img_numpy_to_torch) | Convert a NumPy image to a `torch.float32` tensor. |
| [`read_image`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-read_image) | Read an image file into an unnormalized NumPy array. |
| [`set_class_mapping`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-set_class_mapping) | Align emitted labels with model class IDs without changing data. |

#### property `augmentation_config`

```python
augmentation_config: list[AugmentationConfig]
```

The augmentations of the loader.

The property raises `ValueError` when the constructor got `None`.

#### property `augmentation_engine`

```python
augmentation_engine: str
```

The name of the augmentation engine.

#### property `color_space`

```python
color_space: Literal['RGB', 'BGR']
```

The color space of the output image.

The value is `"RGB"`, `"BGR"`, or `"GRAY"`.

#### property `height`

```python
height: int
```

The height of the output image.

The property raises `ValueError` when the constructor got `None`.

#### property `image_source`

```python
image_source: str
```

The name of the main image source, such as `"image"`.

#### property `input_shape`

```python
input_shape: Size
```

The shape `[C, H, W]` of the
[image_source](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-image_source)
input.

The shape comes from
[input_shapes](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-input_shapes)
and has no batch dimension.

#### property `input_shapes`

```python
input_shapes: dict[str, Size]
```

The shape of each input of one sample, keyed by the input name.

An implementation returns one shape for each input, without the batch dimension. An image has the shape `[C, H, W]`. The result
must hold the
[image_source](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-image_source)
key, because
[input_shape](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-input_shape)
reads it.

> **Examples**
> A loader with one image:

```python
{
    "image": torch.Size([3, 224, 224]),
}
```

A loader with an image and a segmentation input:

```python
{
    "image": torch.Size([3, 224, 224]),
    "segmentation": torch.Size([1, 224, 224]),
}
```

A loader with a left image, a right image, and a disparity map:

```python
{
    "left": torch.Size([3, 224, 224]),
    "right": torch.Size([3, 224, 224]),
    "disparity": torch.Size([1, 224, 224]),
}
```

A loader with an image, keypoints, and a point cloud:

```python
{
    "image": torch.Size([3, 224, 224]),
    "keypoints": torch.Size([17, 2]),
    "point_cloud": torch.Size([20000, 3]),
}
```

#### property `keep_aspect_ratio`

```python
keep_aspect_ratio: bool
```

Whether the resize keeps the aspect ratio of the image.

#### property `seed`

```python
seed: int | None
```

The random seed of the augmentations, or `None`.

#### property `view`

```python
view: list[str]
```

The splits that form the view, such as `["train"]`.

#### property `width`

```python
width: int
```

The width of the output image.

The property raises `ValueError` when the constructor got `None`.

#### method `__init__`

```python
def __init__(
    view: list[str],
    height: int | None = None,
    width: int | None = None,
    augmentation_engine: str = 'albumentations',
    augmentation_config: list[AugmentationConfig] | None = None,
    image_source: str = 'image',
    keep_aspect_ratio: bool = True,
    color_space: Literal['RGB', 'BGR', 'GRAY'] = 'RGB',
    seed: int | None = None,
)
```

Store the settings that every loader shares.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `view` | `list[str]` | The splits that form the view. The list usually holds one split, such as `["train"]`. A dataset can also combine splits, such as `["train_synthetic", "train_real"]`. | required |
| `height` | `int | None` | The height of the output image. With `None`, the [height](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-height) property raises `ValueError`. | `None` |
| `width` | `int | None` | The width of the output image. With `None`, the [width](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-width) property raises `ValueError`. | `None` |
| `augmentation_engine` | `str` | The name of the augmentation engine, such as `"albumentations"`. | `'albumentations'` |
| `augmentation_config` | `list[AugmentationConfig] | None` | The augmentations. Each item has a `name` and a `params` dictionary. With `None`, the [augmentation_config](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-augmentation_config) property raises `ValueError`. | `None` |
| `image_source` | `str` | The name of the main image source. For a dataset with more than one source, such as `"left"` and `"right"`, the visualizations use this source. | `'image'` |
| `keep_aspect_ratio` | `bool` | Whether the resize keeps the aspect ratio of the image. | `True` |
| `color_space` | `Literal['RGB', 'BGR', 'GRAY']` | The color space of the output image. | `'RGB'` |
| `seed` | `int | None` | The random seed of the augmentations, or `None`. | `None` |

#### method `augment_test_image`

```python
def augment_test_image(img: dict[str, Tensor] | Tensor) -> Tensor
```

Apply the augmentations of the loader to one raw image.

Inference calls this method to prepare an image like the samples of the view. The base implementation only raises. A loader that
supports inference on raw images must override it.
[LuxonisLoaderTorch](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/luxonis_loader_torch.md#luxonis_loader_torch-Classes-LuxonisLoaderTorch)
overrides it.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `img` | `dict[str, Tensor] | Tensor` | The raw image of shape `[H, W, C]`. A dictionary maps each source name to its image. | required |

Returns

| Type | Description |
| --- | --- |
| `Tensor` | An override returns the augmented image of shape `[H, W, C]`. |

Raises

| Type | Description |
| --- | --- |
| `NotImplementedError` | Always, in the base implementation. |

#### method `collate_fn`

```python
def collate_fn(
    batch: list[LuxonisLoaderTorchOutput],
) -> tuple[dict[str, Tensor] | Tensor, Labels]
```

Merge a list of samples into one batch.

The method stacks the inputs along a new first dimension. For dictionary inputs, it stacks each input name separately. It merges
each label of the first sample by the label type:

 * `boundingbox` and `keypoints`: The method adds the index of the sample as a new first column, and joins the rows of all
   samples. A `[N, 5]` box label becomes `[N, 6]`.
 * `instance_segmentation`: The method joins the masks of all samples along the first dimension.
 * `metadata/text`: The method pads the character codes of each sample with zeros to the longest text. The result is a
   `torch.int32` tensor of shape `[B, S]`.
 * Other `metadata/<name>` labels: The method joins the values of all samples along the first dimension.
 * Other labels, such as `classification` and `segmentation`: The method stacks them along a new first dimension.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `batch` | `list[LuxonisLoaderTorchOutput]` | The samples. All samples must have the label keys of the first sample. | required |

Returns

| Type | Description |
| --- | --- |
| `tuple[dict[str, Tensor] | Tensor, Labels]` | The batched input and the batched labels, with the keys of the first sample. |

Raises

| Type | Description |
| --- | --- |
| `TypeError` | If the batch mixes tensor inputs and dictionary inputs. |

#### method `dict_numpy_to_torch`

```python
def dict_numpy_to_torch(numpy_dictionary: dict[str, np.ndarray]) -> dict[str, Tensor]
```

Convert a dictionary of NumPy arrays to `torch.float32` tensors.

A string array becomes the character codes of its first string. The method converts the codes to `torch.float32` too.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `numpy_dictionary` | `dict[str, np.ndarray]` | The arrays, such as the labels of one sample. | required |

Returns

| Type | Description |
| --- | --- |
| `dict[str, Tensor]` | A new dictionary with the same keys and one tensor for each array. |

#### method `get_categorical_encodings`

```python
def get_categorical_encodings() -> dict[str, dict[str, int]]
```

Return the integer code of each category of each metadata label.

The base implementation returns an empty dictionary, which means that the loader has no categorical metadata labels.

Returns

| Type | Description |
| --- | --- |
| `dict[str, dict[str, int]]` | The category to code mapping of each categorical metadata label, keyed by the label name. |

#### method `get_classes`

```python
def get_classes() -> dict[str, dict[str, int]]
```

Return the class names and the class IDs of each task.

Returns

| Type | Description |
| --- | --- |
| `dict[str, dict[str, int]]` | The class name to class ID mapping of each task, keyed by the task name. |

#### method `get_metadata_types`

```python
def get_metadata_types() -> dict[str, type[int] | type[Category] | type[float] | type[str]]
```

Return the Python type of each metadata label.

The base implementation returns an empty dictionary, which means that the loader has no metadata labels.
[DatasetMetadata](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/utils/dataset_metadata.md#dataset_metadata-Classes-DatasetMetadata)
reads the result.

Returns

| Type | Description |
| --- | --- |
| `dict[str, type[int] | type[Category] | type[float] | type[str]]` | The type of each metadata label, keyed by the label name, such as `"task_name/metadata/color"`. |

#### method `get_n_keypoints`

```python
def get_n_keypoints() -> dict[str, int] | None
```

Return the number of keypoints of each task.

The base implementation returns `None`. A loader with keypoint labels must override it.

Returns

| Type | Description |
| --- | --- |
| `dict[str, int] | None` | The number of keypoints, keyed by the task name, or `None` when the loader has no keypoints. |

#### static method `img_numpy_to_torch`

```python
def img_numpy_to_torch(img: np.ndarray) -> Tensor
```

Convert a NumPy image to a `torch.float32` tensor.

The method moves the channels of a 3D image first. A 2D image keeps its shape.

> **Example**
> ```pycon
>>> import numpy as np
>>> image = np.zeros((4, 6, 3), dtype=np.uint8)
>>> BaseLoaderTorch.img_numpy_to_torch(image).shape
torch.Size([3, 4, 6])
>>> gray = np.zeros((4, 6), dtype=np.uint8)
>>> BaseLoaderTorch.img_numpy_to_torch(gray).shape
torch.Size([4, 6])
```

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `img` | `np.ndarray` | The image of shape `[H, W, C]` or `[H, W]`. | required |

Returns

| Type | Description |
| --- | --- |
| `Tensor` | The image of shape `[C, H, W]` or `[H, W]`. |

#### method `read_image`

```python
def read_image(path: str) -> npt.NDArray[np.uint8]
```

Read an image file into an unnormalized NumPy array.

OpenCV reads the file as a BGR color image. The method then converts it to [color_space](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Classes-BaseLoaderTorch-color_space).

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `path` | `str` | The path to the image file. | required |

Returns

| Type | Description |
| --- | --- |
| `npt.NDArray[np.uint8]` | The image of shape `[H, W, 3]`, or `[H, W]` for the `"GRAY"` color space. |

Raises

| Type | Description |
| --- | --- |
| `ValueError` | If OpenCV cannot read the file. |

#### method `set_class_mapping`

```python
def set_class_mapping(classes: dict[str, dict[str, int]])
```

Align emitted labels with model class IDs without changing data.

Loaders must override this method to support a different class order or a subset of the model's classes.

## Module Attributes

| Name | Type | Description |
| --- | --- | --- |
| [`LuxonisLoaderTorchOutput`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Module-Attributes-LuxonisLoaderTorchOutput) |  |  |
| [`MIXED_INPUT_TYPES_ERROR`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/base_loader.md#base_loader-Module-Attributes-MIXED_INPUT_TYPES_ERROR) | `str` |  |

### type alias `LuxonisLoaderTorchOutput`

```python
LuxonisLoaderTorchOutput = tuple[dict[str, Tensor] | Tensor, Labels]
```

### constant `MIXED_INPUT_TYPES_ERROR`

```python
MIXED_INPUT_TYPES_ERROR: str = 'All samples in a batch must have the same input type. Got a mix of tensors and dictionaries.'
```
