# dataset_metadata

Python API: `luxonis_train.utils.dataset_metadata`

The dataset metadata that the nodes read.

The metadata holds the class names, the keypoint counts, and the metadata label types that come from the loader.

## Classes

| Name | Description |
| --- | --- |
| [`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) | The class names, keypoint counts, and metadata types of a dataset. |

### class `DatasetMetadata`

```python
class DatasetMetadata
```

The class names, keypoint counts, and metadata types of a dataset.

A node reads its class count, class names, and keypoint count from this object, so the config does not have to set them.
[from_loader](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-from_loader)
creates the object from a loader.
[dump](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-dump)
returns a dictionary that a checkpoint stores and that the constructor accepts back as keyword arguments.

> **Example**
> ```pycon
>>> metadata = DatasetMetadata(
...     classes={"detection": {"car": 0, "person": 1}},
...     n_keypoints={"detection": 17},
... )
>>> metadata.n_classes("detection")
2
>>> metadata.classes().inverse[0]
'car'
>>> metadata.n_keypoints("segmentation")
0
```

Properties

| Name | Type | Description |
| --- | --- | --- |
| [`metadata_types`](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-metadata_types) | `dict[str, type[int] | type[Category] | type[float] | type[str]]` | The metadata label names mapped to the type of their values. |
| [`task_names`](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-task_names) | `set[str]` | The names of all tasks in the class mapping. |

Methods

| Name | Description |
| --- | --- |
| [`__init__`](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-__init__) | Initialize the metadata from plain dictionaries. |
| [`classes`](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-classes) | Get the class names and indices of a task. |
| [`dump`](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-dump) | Dump the metadata to a dictionary of plain values. |
| [`from_loader`](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-from_loader) | Create the metadata from a loader. |
| [`n_classes`](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-n_classes) | Get the number of classes of a task. |
| [`n_keypoints`](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-n_keypoints) | Get the number of keypoints of a task. |

#### property `metadata_types`

```python
metadata_types: dict[str, type[int] | type[Category] | type[float] | type[str]]
```

The metadata label names mapped to the type of their values.

The dictionary is empty when the dataset has no metadata labels.

#### property `task_names`

```python
task_names: set[str]
```

The names of all tasks in the class mapping.

A task with an empty class mapping is also in the set.

#### method `__init__`

```python
def __init__(
    *,
    classes: dict[str, dict[str, int]] | None = None,
    n_keypoints: dict[str, int] | None = None,
    metadata_types: dict[str, type[int] | type[Category] | type[float] | type[str]] | None = None,
    loader: BaseLoaderTorch | None = None,
)
```

Initialize the metadata from plain dictionaries.

A value in `metadata_types` can also be a type name: one of `"int"`, `"float"`, `"str"`, or `"Category"`. The constructor converts the name to the type. This is the form [dump](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-dump) writes and a checkpoint stores.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `classes` | `dict[str, dict[str, int]] | None` | Task names mapped to the class names of the task and their indices. `None` means no tasks. | `None` |
| `n_keypoints` | `dict[str, int] | None` | Task names mapped to the number of keypoints of the task. `None` means no keypoints. | `None` |
| `metadata_types` | `dict[str, type[int] | type[Category] | type[float] | type[str]] | None` | Metadata label names, such as `"<task>/metadata/<name>"`, mapped to the type of their values. `None` means no metadata labels. | `None` |
| `loader` | `BaseLoaderTorch | None` | The loader that gave the metadata. The object keeps a reference to it and does not use it. | `None` |

Raises

| Type | Description |
| --- | --- |
| `ValueError` | When a type name in `metadata_types` is not one of the four supported names. |

#### method `classes`

```python
def classes(task_name: str | None = None) -> bidict[str, int]
```

Get the class names and indices of a task.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `task_name` | `str | None` | The task to read. `None` means all tasks, which must then have the same classes. | `None` |

Returns

| Type | Description |
| --- | --- |
| `bidict[str, int]` | A new bidirectional dictionary that maps the class names to the class indices. Its `inverse` maps the indices back to the names. |

Raises

| Type | Description |
| --- | --- |
| `ValueError` | When `task_name` is not a task of the dataset. |
| `RuntimeError` | When `task_name` is `None` and the tasks have different classes. |
| `StopIteration` | When `task_name` is `None` and the metadata has no tasks. |

#### method `dump`

```python
def dump() -> dict[str, Any]
```

Dump the metadata to a dictionary of plain values.

The constructor accepts the result back as keyword arguments. This is how a checkpoint stores and restores the metadata.

> **Example**
> ```pycon
>>> metadata = DatasetMetadata(
...     classes={"detection": {"car": 0}},
...     metadata_types={"color": str},
... )
>>> metadata.dump()
{'classes': {'detection': {'car': 0}},
 'n_keypoints': {},
 'metadata_types': {'color': 'str'}}
>>> DatasetMetadata(**metadata.dump()).metadata_types
{'color': <class 'str'>}
```

Returns

| Type | Description |
| --- | --- |
| `dict[str, Any]` | A dictionary with the keys `"classes"`, `"n_keypoints"`, and `"metadata_types"`. The metadata types appear as type names, for example `"str"`. |

#### class method `from_loader`

```python
def from_loader(loader: BaseLoaderTorch) -> DatasetMetadata
```

Create the metadata from a loader.

The method reads `loader.get_classes()`, `loader.get_n_keypoints()`, and `loader.get_metadata_types()`. The new object keeps a
reference to `loader`.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `loader` | `BaseLoaderTorch` | The loader to read. | required |

Returns

| Type | Description |
| --- | --- |
| `DatasetMetadata` | The metadata of the dataset of the loader. |

#### method `n_classes`

```python
def n_classes(task_name: str | None = None) -> int
```

Get the number of classes of a task.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `task_name` | `str | None` | The task to read. `None` means all tasks, which must then have the same number of classes. | `None` |

Returns

| Type | Description |
| --- | --- |
| `int` | The number of classes of the task. |

Raises

| Type | Description |
| --- | --- |
| `ValueError` | When `task_name` is not a task of the dataset. |
| `RuntimeError` | When `task_name` is `None` and the tasks have different numbers of classes. |
| `StopIteration` | When `task_name` is `None` and the metadata has no tasks. |

#### method `n_keypoints`

```python
def n_keypoints(task_name: str | None = None) -> int
```

Get the number of keypoints of a task.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `task_name` | `str | None` | The task to read. `None` means all tasks, which must then have the same number of keypoints. | `None` |

Returns

| Type | Description |
| --- | --- |
| `int` | The number of keypoints of the task. `0` when `task_name` has no keypoint count, for example a task that is not in the dataset. |

Raises

| Type | Description |
| --- | --- |
| `RuntimeError` | When `task_name` is `None` and the tasks have different numbers of keypoints. |
| `StopIteration` | When `task_name` is `None` and the metadata has no keypoint counts. |
