# convert_on_train_end

Python API: `luxonis_train.callbacks.convert_on_train_end`

Exports, archives, and converts the model when training ends.

## Classes

| Name | Description |
| --- | --- |
| [`ConvertOnTrainEnd`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/callbacks/convert_on_train_end.md#convert_on_train_end-Classes-ConvertOnTrainEnd) | Export, archive, and convert the model when training ends. |

### class `ConvertOnTrainEnd`

```python
class ConvertOnTrainEnd(NeedsCheckpoint)
```

Export, archive, and convert the model when training ends.

The callback passes the best checkpoint to
[LuxonisModel.convert](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/core/core.md#core-Classes-LuxonisModel-convert),
which runs these steps in order:

 1. Export the model to ONNX.
 2. Build an NN Archive around it.
 3. Run `blobconverter` when `exporter.blobconverter.active` is true.
 4. Run the HubAI SDK conversion when `exporter.hubai.active` is true.

The `preferred_checkpoint` parameter of
[NeedsCheckpoint](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/callbacks/needs_checkpoint.md#needs_checkpoint-Classes-NeedsCheckpoint)
selects the best main metric or the lowest validation loss.

Prefer this callback over a separate
[ExportOnTrainEnd](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/callbacks/export_on_train_end.md#export_on_train_end-Classes-ExportOnTrainEnd)
and
[ArchiveOnTrainEnd](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/callbacks/archive_on_train_end.md#archive_on_train_end-Classes-ArchiveOnTrainEnd),
which together do the first two steps only. When `trainer.callbacks` lists an active instance of this callback, the config
deactivates those two.

When `trainer.smart_cfg_auto_populate` is set, the config adds this callback to `trainer.callbacks` if it is missing. The config
adds it after the deactivation step, so the added instance leaves the other two active.

Methods

| Name | Description |
| --- | --- |
| [`on_train_end`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/callbacks/convert_on_train_end.md#convert_on_train_end-Classes-ConvertOnTrainEnd-on_train_end) | Export, archive, and convert the best checkpoint. |

#### method `on_train_end`

```python
def on_train_end(trainer: pl.Trainer, pl_module: lxt.LuxonisLightningModule)
```

Export, archive, and convert the best checkpoint.

Lightning calls this hook once when `trainer.fit` ends. The hook selects a checkpoint with
[NeedsCheckpoint.get_checkpoint](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/callbacks/needs_checkpoint.md#needs_checkpoint-Classes-NeedsCheckpoint-get_checkpoint).
When no checkpoint exists, it logs a warning and stops.

Otherwise the hook calls the `_stop_progress` method of the progress bar of `trainer`, when the bar has one. For a rich progress
bar, the call stops its live display, because the conversion shows its own progress bar. The hook then passes the checkpoint to
[LuxonisModel.convert](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/core/core.md#core-Classes-LuxonisModel-convert).
It does not catch the errors of that method.

The conversion loads the checkpoint into `pl_module.core.lightning_module`, which is `pl_module` in a
[LuxonisModel.train](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/core/core.md#core-Classes-LuxonisModel-train)
run. It loads the checkpoint only for the export and for the archive. After the hook, that module holds its earlier weights again.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `trainer` | `pl.Trainer` | The trainer. The hook reads its progress bar callback. | required |
| `pl_module` | `lxt.LuxonisLightningModule` | The model to convert. | required |
