# Exporting

## Overview

Export your trained models for downstream deployment workflows. LuxonisTrain supports direct ONNX export, NN Archive generation,
and a unified conversion flow for Luxonis target platforms.

## Export Format

 * ONNX: Open Neural Network Exchange format used as the main interchange and downstream conversion format.

To configure export and conversion behavior, you can specify the exporter section in the config file:

```yaml
exporter:
  onnx:
    opset_version: 11
```

exporter.hubai and exporter.blobconverter are used by convert or ConvertOnTrainEnd, not by export alone.

### CLI

Use the following command to export your model:

```bash
luxonis_train export --config configs/example_export.yaml --weights path/to/weights.ckpt
```

### Python API

```python
from luxonis_train import LuxonisModel

model = LuxonisModel("configs/example_export.yaml")
model.export(weights="path/to/weights.ckpt")
```

> Exporting the model can happen automatically at the end of the training by using the
> `ExportOnTrainEnd`
> callback.

## NN Archive

Create an [NN Archive](https://docs.luxonis.com/software-v3/ai-inference/nn-archive.md) file for easy deployment with the DepthAI
API. If you do not provide an ONNX executable, LuxonisTrain exports the model to ONNX first.

The archive contains the exported model together with all the metadata needed for running the model seamlessly.

### CLI

To create an NN Archive using the CLI, use the following command:

```bash
luxonis_train archive                         \
  --config configs/detection_light_model.yaml \
  --weights path/to/checkpoint.ckpt
```

### Python API

You can also create an NN Archive using the Python API:

```python
from luxonis_train import LuxonisModel

model = LuxonisModel("configs/detection_light_model.yaml")
model.archive(weights="path/to/checkpoint.ckpt")
```

> The archive can be created automatically at the end of the training by using the
> `ArchiveOnTrainEnd`
> callback.

## Convert

convert is the unified deployment flow in LuxonisTrain. It performs:

 1. export from checkpoint to ONNX
 2. archive from ONNX to NN Archive
 3. optional platform-specific conversion for RVC2, RVC3, or RVC4

Configure this behavior through exporter.hubai or exporter.blobconverter.

### CLI

```bash
luxonis_train convert --config configs/detection_light_model.yaml --weights path/to/checkpoint.ckpt
```

### Python API

```python
from luxonis_train import LuxonisModel

model = LuxonisModel("configs/detection_light_model.yaml")
archive_path, conversion_artifacts = model.convert(
    weights="path/to/checkpoint.ckpt",
)
```

convert() returns the ONNX-based NN Archive path together with a dictionary of any additional conversion artifacts, such as a
platform-specific archive or legacy blob output.

> The full conversion flow can be run automatically at the end of training by using the
> `ConvertOnTrainEnd`
> callback.
