# training_progress_callback

Python API: `luxonis_train.callbacks.training_progress_callback`

记录每个批次和每个 epoch 的进度和时间。

## 类

### TrainingProgressCallback

回调函数，记录每个循环的进度和时间。

回调函数将这些键发送到 `trainer.logger`，并使用 `log_metrics`，其中 `<mode>` 是 `train`、`val` 或 `test`：

 * `<mode>/epoch_progress_percent`：已完成的 epoch 批次百分比。
 * `<mode>/epoch_duration_sec`：从 epoch 开始到日志条目的秒数。
 * `<mode>/batch_total_sec`：从日志批次的开始钩子到结束钩子的秒数。
 * `<mode>/epoch_completion_sec`：从 epoch 开始到结束的秒数。

构造函数将每个循环的一个批次计数器设置为 0。计数器在 epoch 之间不重置。循环的计数器是该循环每个键的步长。`val/epoch_completion_sec` 和 `test/epoch_completion_sec` 是例外。它们的步长是
`trainer.current_epoch`。

批次结束钩子仅在 epoch 中完成的批次数量是 `log_every_n_batches` 的倍数时才记录。回调函数忽略验证健全性检查。批次结束和 epoch 结束钩子仅在 rank zero 上运行。如果没有
`trainer.logger`，回调函数不发送任何键。每个训练 epoch
的开始会记录一个警告。[LuxonisLightningModule.get_mlflow_logging_keys](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/lightning/luxonis_lightning.md)
列出了当配置中包含此回调函数时的十二个键。

将回调函数添加到配置的 `trainer.callbacks` 中：

```yaml
trainer:
  callbacks:
    - name: TrainingProgressCallback
      params:
        log_every_n_batches: 10
```

#### 方法

##### init

```python
def __init__(log_every_n_batches: int = 1):
```

初始化回调函数。

参数

 * `log_every_n_batches` (`int`)：每隔多少个 epoch 批次记录一次批次结束。`1` 记录每个批次。较高的值会减少日志记录开销。小于 `1` 的值等同于 `1`。

##### on_test_batch_end

```python
def on_test_batch_end(trainer: pl.Trainer, pl_module: lxt.LuxonisLightningModule, outputs: STEP_OUTPUT, batch: Any, batch_idx: int, dataloader_idx: int = 0):
```

记录测试批次的进度和时间。

Lightning 在每个测试批次结束时调用此钩子。钩子仅在 rank zero 上运行。它将 epoch 的批次计数和测试批次计数器加 1。当批次计数是 `log_every_n_batches` 的倍数时，它会在测试批次计数器的步长处记录这些键：

 * `test/batch_total_sec`：自
   [on_test_batch_start](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/callbacks/training_progress_callback.md)
   以来的秒数。
 * `test/epoch_progress_percent`：100c ⁄ n，其中 c 是批次计数，n 是 `trainer.num_test_batches` 的有限条目之和。当 n 为 `0` 时，值为 `0.0`。
 * `test/epoch_duration_sec`：自
   [on_test_epoch_start](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/callbacks/training_progress_callback.md)
   以来的秒数。

批次计数涵盖每个测试数据加载器的批次。如果没有 `trainer.logger`，钩子不记录任何内容。

参数

 * `trainer` (`pl.Trainer`)：训练器。钩子读取 `num_test_batches` 并记录到 `logger`。
 * `pl_module` (`lxt.LuxonisLightningModule`)：模型。未使用。
 * `outputs` (`STEP_OUTPUT`)：测试步骤的输出。未使用。
 * `batch` (`Any`)：批次。未使用。
 * `batch_idx` (`int`)：批次的索引。未使用。
 * `dataloader_idx` (`int`)：数据加载器的索引。未使用。

##### on_test_batch_start

```python
def on_test_batch_start(trainer: pl.Trainer, pl_module: lxt.LuxonisLightningModule, batch: Any, batch_idx: int, dataloader_idx: int = 0):
```

启动测试批次的计时器。

Lightning 在每个批次的测试步骤之前调用此钩子。钩子存储 `test/batch_total_sec` 的开始时间。

参数

 * `trainer` (`pl.Trainer`)：训练器。未使用。
 * `pl_module` (`lxt.LuxonisLightningModule`)：模型。未使用。
 * `batch` (`Any`)：批次。未使用。
 * `batch_idx` (`int`)：批次的索引。未使用。
 * `dataloader_idx` (`int`)：数据加载器的索引。未使用。

##### on_test_epoch_end

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

记录已完成测试 epoch 的持续时间。

Lightning 在每个测试 epoch 结束时调用此钩子。钩子仅在 rank zero 上运行。如果没有 `trainer.logger`，它不执行任何操作。

当 epoch 的批次计数大于 0 且不是 `log_every_n_batches` 的倍数时，最后一个批次没有日志条目。钩子然后将 `test/epoch_progress_percent` 记录为 `100.0`，并将
`test/epoch_duration_sec` 记录在测试批次计数器的步长处。在任何情况下，它都会在 `trainer.current_epoch` 步长处记录 `test/epoch_completion_sec`，即自
[on_test_epoch_start](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/callbacks/training_progress_callback.md)
以来的秒数。

参数

 * `trainer` (`pl.Trainer`)：训练器。钩子读取 `current_epoch` 并记录到 `logger`。
 * `pl_module` (`lxt.LuxonisLightningModule`)：模型。未使用。

##### on_test_epoch_start

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

启动测试 epoch 的计时器并记录零进度。

Lightning 在每个测试 epoch 开始时调用此钩子。钩子存储开始时间并将 epoch 的批次计数设置为 0。然后它在测试批次计数器的步长处将 `test/epoch_progress_percent` 记录为 `0.0`。如果没有
`trainer.logger`，它不记录任何内容。

参数

 * `trainer` (`pl.Trainer`)：训练器。钩子记录到其 `logger`。
 * `pl_module` (`lxt.LuxonisLightningModule`)：模型。未使用。

##### on_train_batch_end

```python
def on_train_batch_end(trainer: pl.Trainer, pl_module: lxt.LuxonisLightningModule, outputs: STEP_OUTPUT, batch: Any, batch_idx: int):
```

记录训练批次的进度和时间。

Lightning 在每个训练批次结束时调用此钩子。钩子仅在 rank zero 上运行。它将训练批次计数器加 1。当 `batch_idx + 1` 是 `log_every_n_batches` 的倍数时，它会在该计数器的步长处记录这些键：

 * `train/epoch_progress_percent`：100(i + 1) ⁄ n，其中 i 是 `batch_idx`，n 是 `trainer.num_training_batches`。当 n 为 `0` 或无限时，值为 `0.0`。
 * `train/epoch_duration_sec`：自
   [on_train_epoch_start](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/callbacks/training_progress_callback.md)
   以来的秒数。
 * `train/batch_total_sec`：自
   [on_train_batch_start](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/callbacks/training_progress_callback.md)
   以来的秒数。

如果没有 `trainer.logger`，钩子不记录任何内容。

参数

 * `trainer` (`pl.Trainer`)：训练器。钩子读取 `num_training_batches` 并记录到 `logger`。
 * `pl_module` (`lxt.LuxonisLightningModule`)：模型。未使用。
 * `outputs` (`STEP_OUTPUT`)：训练步骤的输出。未使用。
 * `batch` (`Any`)：批次。未使用。
 * `batch_idx` (`int`)：epoch 中批次的索引。

##### on_train_batch_start

```python
def on_train_batch_start(trainer: pl.Trainer, pl_module: lxt.LuxonisLightningModule, batch: Any, batch_idx: int):
```

启动训练批次的计时器。

Lightning 在每个批次的训练步骤之前调用此钩子。钩子存储 `train/batch_total_sec` 的开始时间。

参数

 * `trainer` (`pl.Trainer`)：训练器。未使用。
 * `pl_module` (`lxt.LuxonisLightningModule`)：模型。未使用。
 * `batch` (`Any`)：批次。未使用。
 * `batch_idx` (`int`)：epoch 中批次的索引。未使用。

##### on_train_epoch_end

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

记录已完成训练 epoch 的持续时间。

Lightning 在每个训练 epoch 结束时调用此钩子。当 epoch 中运行验证时，调用会在验证之后，因此持续时间包括验证时间。钩子仅在 rank zero 上运行。它将 `train/epoch_completion_sec`（自
[on_train_epoch_start](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/callbacks/training_progress_callback.md)
以来的秒数）和 `train/epoch_progress_percent` 记录为 `100.0`。这两个键都使用训练批次计数器的步长。如果没有 `trainer.logger`，钩子不记录任何内容。

参数

 * `trainer` (`pl.Trainer`)：训练器。钩子记录到其 `logger`。
 * `pl_module` (`lxt.LuxonisLightningModule`)：模型。未使用。

##### on_train_epoch_start

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

启动训练 epoch 的计时器并记录零进度。

Lightning 在每个训练 epoch 开始时调用此钩子。钩子存储开始时间。然后它在训练批次计数器的步长处将 `train/epoch_progress_percent` 记录为 `0.0`。如果没有 `trainer.logger`，它会记录一个警告。

参数

 * `trainer` (`pl.Trainer`)：训练器。钩子记录到其 `logger`。
 * `pl_module` (`lxt.LuxonisLightningModule`)：模型。未使用。

##### on_validation_batch_end

```python
def on_validation_batch_end(trainer: pl.Trainer, pl_module: lxt.LuxonisLightningModule, outputs: STEP_OUTPUT, batch: Any, batch_idx: int, dataloader_idx: int = 0):
```

记录验证批次的进度和时间。

Lightning 在每个验证批次结束时调用此钩子。钩子仅在 rank zero 上运行，并且在健全性检查期间不执行任何操作。它将 epoch 的批次计数和验证批次计数器加 1。当批次计数是 `log_every_n_batches`
的倍数时，它会在验证批次计数器的步长处记录这些键：

 * `val/batch_total_sec`：自
   [on_validation_batch_start](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/callbacks/training_progress_callback.md)
   以来的秒数。
 * `val/epoch_progress_percent`：100c ⁄ n，其中 c 是批次计数，n 是 `trainer.num_val_batches` 的有限条目之和。当 n 为 `0` 时，值为 `0.0`。
 * `val/epoch_duration_sec`：自
   [on_validation_epoch_start](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/callbacks/training_progress_callback.md)
   以来的秒数。

批次计数涵盖每个验证数据加载器的批次。如果没有 `trainer.logger`，钩子不记录任何内容。

参数

 * `trainer` (`pl.Trainer`)：训练器。钩子读取 `sanity_checking` 和 `num_val_batches`，并记录到 `logger`。
 * `pl_module` (`lxt.LuxonisLightningModule`)：模型。未使用。
 * `outputs` (`STEP_OUTPUT`)：验证步骤的输出。未使用。
 * `batch` (`Any`)：批次。未使用。
 * `batch_idx` (`int`)：批次的索引。未使用。
 * `dataloader_idx` (`int`)：数据加载器的索引。未使用。

##### on_validation_batch_start

```python
def on_validation_batch_start(trainer: pl.Trainer, pl_module: lxt.LuxonisLightningModule, batch: Any, batch_idx: int, dataloader_idx: int = 0):
```

启动验证批次的计时器。

Lightning 在每个批次的验证步骤之前调用此钩子。在健全性检查之外，钩子存储 `val/batch_total_sec` 的开始时间。

参数

 * `trainer` (`pl.Trainer`)：训练器。钩子读取 `sanity_checking`。
 * `pl_module` (`lxt.LuxonisLightningModule`)：模型。未使用。
 * `batch` (`Any`)：批次。未使用。
 * `batch_idx` (`int`)：批次的索引。未使用。
 * `dataloader_idx` (`int`)：数据加载器的索引。未使用。

##### on_validation_epoch_end

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

记录已完成验证 epoch 的持续时间。

Lightning 在每个验证 epoch 结束时调用此钩子。钩子仅在 rank zero 上运行。在健全性检查期间或没有 `trainer.logger` 的情况下，它不执行任何操作。

当 epoch 的批次计数大于 0 且不是 `log_every_n_batches` 的倍数时，最后一个批次没有日志条目。钩子然后将 `val/epoch_progress_percent` 记录为 `100.0`，并将
`val/epoch_duration_sec` 记录在验证批次计数器的步长处。在任何情况下，它都会在 `trainer.current_epoch` 步长处记录 `val/epoch_completion_sec`，即自
[on_validation_epoch_start](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/callbacks/training_progress_callback.md)
以来的秒数。

参数

 * `trainer` (`pl.Trainer`)：训练器。钩子读取 `sanity_checking` 和 `current_epoch`，并记录到 `logger`。
 * `pl_module` (`lxt.LuxonisLightningModule`)：模型。未使用。

##### on_validation_epoch_start

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

启动验证 epoch 计时器并记录零进度。

Lightning 在每个验证 epoch 开始时调用此钩子，包括健全性检查。钩子存储开始时间并将 epoch 的批次计数设置为 0。在健全性检查之外，它在验证批次计数器的步长处将 `val/epoch_progress_percent` 记录为
`0.0`。如果没有 `trainer.logger`，它不记录任何内容。

参数

 * `trainer` (`pl.Trainer`)：训练器。钩子读取 `sanity_checking` 并记录到 `logger`。
 * `pl_module` (`lxt.LuxonisLightningModule`)：模型。未使用。
