# archive_on_train_end

Python API: `luxonis_train.callbacks.archive_on_train_end`

Creates an NN Archive when training ends.

## Classes

### ArchiveOnTrainEnd

Create an NN Archive when training ends.

The callback archives the ONNX file of an earlier export, for example the file of an
[ExportOnTrainEnd](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/callbacks/export_on_train_end.md)
that comes before it in `trainer.callbacks`. Without such a file, it exports the best checkpoint first. 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.

[ConvertOnTrainEnd](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/callbacks/convert_on_train_end.md)
also builds the archive. When `trainer.callbacks` lists an active
[ConvertOnTrainEnd](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/callbacks/convert_on_train_end.md),
the config deactivates this callback. A
[ConvertOnTrainEnd](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/callbacks/convert_on_train_end.md)
that `trainer.smart_cfg_auto_populate` adds does not deactivate it.

#### Methods

##### on_train_end

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

Build an NN Archive from the ONNX file of the model.

Lightning calls this hook once when `trainer.fit` ends. The hook takes the last ONNX file that
[LuxonisModel.export](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/core/core.md)
wrote for `pl_module.core`. When no ONNX export ran, 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)
and exports it to ONNX first. It then passes the ONNX file to
[LuxonisModel.archive](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/core/core.md),
which writes the archive to `<run_save_dir>/archive`. That method also uploads the archive to the run when
`archiver.upload_to_run` is set.

When no ONNX export ran and no checkpoint exists, the hook logs a warning and builds no archive. It logs an error and builds no
archive when its own export produced no ONNX file.

The export of the hook loads the checkpoint into `pl_module` only for the export. When the
[LuxonisModel](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/core/core.md)
constructor or `model.weights` of the config gives weights,
[LuxonisModel.archive](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/core/core.md)
loads them only for the archive. After the hook, `pl_module` holds its earlier weights again.

Parameters

 * `_` (`pl.Trainer`): The trainer. Unused.
 * `pl_module` (`lxt.LuxonisLightningModule`): The model to archive.
