# archive_utils

Python API: `luxonis_train.core.utils.archive_utils`

The NN Archive entries of an exported model and of its heads.

[LuxonisModel.archive](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/core/core.md#core-Classes-LuxonisModel-archive)
builds the `inputs`, the `outputs`, and the `heads` of the archive config with these functions.
[LuxonisModel.export](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/core/core.md#core-Classes-LuxonisModel-export)
also reads the inputs and the outputs for the `modelconverter` config that it writes next to the ONNX file.

## Classes

| Name | Description |
| --- | --- |
| [`ArchiveMetadataDict`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/core/utils/archive_utils.md#archive_utils-Classes-ArchiveMetadataDict) | The shape and the data type of one input or output of a model. |

### class `ArchiveMetadataDict`

```python
class ArchiveMetadataDict(TypedDict)
```

The shape and the data type of one input or output of a model.

Attributes

| Name | Type | Description |
| --- | --- | --- |
| `dtype` | `DataType` | The data type of the tensor, one of `int8`, `int32`, `uint8`, `float32`, and `float16`. |
| `shape` | `list[int]` | The dimensions of the tensor. A dimension without a fixed size in the model file is `0`. |

## Functions

| Name | Description |
| --- | --- |
| [`get_head_configs`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/core/utils/archive_utils.md#archive_utils-Functions-get_head_configs) | Build the `heads` entries of the NN Archive config. |
| [`get_inputs`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/core/utils/archive_utils.md#archive_utils-Functions-get_inputs) | Read the inputs of an exported model file. |
| [`get_outputs`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/core/utils/archive_utils.md#archive_utils-Functions-get_outputs) | Read the outputs of an exported model file. |

### function `get_head_configs`

```python
def get_head_configs(
    lightning_module: LuxonisLightningModule,
    outputs: list[dict],
) -> list[dict]
```

Build the `heads` entries of the NN Archive config.

The function visits the nodes of `lightning_module` in build order. It skips a node that is not a
[BaseHead](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/base_head.md#base_head-Classes-BaseHead),
and a head with `remove_on_export` set. For each other head, it starts from
[BaseHead.get_head_config](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/base_head.md#base_head-Classes-BaseHead-get_head_config)
and adds two keys:

 * `"name"`: the name of the node. When an earlier head already took that name, the function appends `_<n>`, where `<n>` is the
   number of names taken so far.
 * `"outputs"`: the
   [BaseNode.export_output_names](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/base_node.md#base_node-Classes-BaseNode-export_output_names)
   of the head. When they are `None` or empty, the function selects the names in `outputs` that belong to the node name.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `lightning_module` | `LuxonisLightningModule` | The module whose heads the archive describes. | required |
| `outputs` | `list[dict]` | The outputs of the NN Archive config, each with a `"name"` key. | required |

Returns

| Type | Description |
| --- | --- |
| `list[dict]` | One config dictionary for each exported head, with the keys `"parser"`, `"metadata"`, `"name"`, and `"outputs"`. |

### function `get_inputs`

```python
def get_inputs(path: Path) -> dict[str, ArchiveMetadataDict]
```

Read the inputs of an exported model file.

The function supports only ONNX files. It reads every entry of `graph.input` of the model.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `path` | `Path` | The model file, with the suffix `.onnx`. | required |

Returns

| Type | Description |
| --- | --- |
| `dict[str, ArchiveMetadataDict]` | The shape and the data type of each input, keyed by input name, in graph order. |

Raises

| Type | Description |
| --- | --- |
| `NotImplementedError` | When the suffix of `path` is not `.onnx`. |
| `ValueError` | When the file does not load as an ONNX model, or when an input has an unsupported data type. |

### function `get_outputs`

```python
def get_outputs(path: Path) -> dict[str, ArchiveMetadataDict]
```

Read the outputs of an exported model file.

The function supports only ONNX files. It reads every entry of `graph.output` of the model.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `path` | `Path` | The model file, with the suffix `.onnx`. | required |

Returns

| Type | Description |
| --- | --- |
| `dict[str, ArchiveMetadataDict]` | The shape and the data type of each output, keyed by output name, in graph order. |

Raises

| Type | Description |
| --- | --- |
| `NotImplementedError` | When the suffix of `path` is not `.onnx`. |
| `ValueError` | When the file does not load as an ONNX model, or when an output has an unsupported data type. |
