# convert_on_train_end

Python API: `luxonis_train.callbacks.convert_on_train_end`

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

## Classes

### ConvertOnTrainEnd

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),
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)
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)
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),
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

##### 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).
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).
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)
run. It loads the checkpoint only for the export and for the archive. After the hook, that module holds its earlier weights again.

Parameters

 * `trainer` (`pl.Trainer`): The trainer. The hook reads its progress bar callback.
 * `pl_module` (`lxt.LuxonisLightningModule`): The model to convert.
