# 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)
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)
also reads the inputs and the outputs for the `modelconverter` config that it writes next to the ONNX file.

## Classes

### ArchiveMetadataDict

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

#### Attributes

##### dtype

The data type of the tensor, one of `int8`, `int32`, `uint8`, `float32`, and `float16`.

##### shape

The dimensions of the tensor. A dimension without a fixed size in the model file is `0`.

## Functions

### 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),
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)
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)
   of the head. When they are `None` or empty, the function selects the names in `outputs` that belong to the node name.

Parameters

 * `lightning_module` (`LuxonisLightningModule`): The module whose heads the archive describes.
 * `outputs` (`list[dict]`): The outputs of the NN Archive config, each with a `"name"` key.

Returns

 * `list[dict]`: One config dictionary for each exported head, with the keys `"parser"`, `"metadata"`, `"name"`, and `"outputs"`.

### 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.

Parameters

 * `path` (`Path`): The model file, with the suffix `.onnx`.

Returns

 * `dict[str, ArchiveMetadataDict]`: The shape and the data type of each input, keyed by input name, in graph order.

Raises

 * `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.

### 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.

Parameters

 * `path` (`Path`): The model file, with the suffix `.onnx`.

Returns

 * `dict[str, ArchiveMetadataDict]`: The shape and the data type of each output, keyed by output name, in graph order.

Raises

 * `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.
