# metadata_logger

Python API: `luxonis_train.callbacks.metadata_logger`

Logs chosen config values as hyperparameters and saves them to `metadata.yaml`.

## Classes

| Name | Description |
| --- | --- |
| [`MetadataLogger`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/callbacks/metadata_logger.md#metadata_logger-Classes-MetadataLogger) | Callback that logs chosen config values as hyperparameters. |

### class `MetadataLogger`

```python
class MetadataLogger(pl.Callback)
```

Callback that logs chosen config values as hyperparameters.

When a fit starts, the callback reads each key of `hyperparams` from the config. It logs the values with the logger of the model
and saves them to `metadata.yaml` in the save directory of the model.

The callback is in the `CALLBACKS` registry, so a config can add it:

```yaml
trainer:
  callbacks:
    - name: MetadataLogger
      params:
        hyperparams: ["trainer.epochs", "trainer.batch_size"]
```

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/callbacks/metadata_logger.md#metadata_logger-Classes-MetadataLogger-__init__) | Initialize the callback. |
| [`on_fit_start`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/callbacks/metadata_logger.md#metadata_logger-Classes-MetadataLogger-on_fit_start) | Log and save the chosen config values. |

#### method `__init__`

```python
def __init__(hyperparams: list[str])
```

Initialize the callback.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `hyperparams` | `list[str]` | The config keys to log. A key separates its levels with dots, for example `"trainer.epochs"`. A level of a list is an integer index, for example `"model.nodes.0.name"`. | required |

#### method `on_fit_start`

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

Log and save the chosen config values.

Lightning calls this hook at the start of a fit. The hook reads each key of `hyperparams` with `cfg.get`. A key that is not in the
config gives `None`. `cfg.get` raises `ValueError` for a level of a list that is not an integer. The hook passes the dictionary of
keys and values to `log_hyperparams` of `pl_module.logger`. Then it writes the dictionary with `yaml.safe_dump` to `metadata.yaml`
in `pl_module.save_dir` and replaces an existing file. `yaml.safe_dump` raises `yaml.representer.RepresenterError` for a value
that is not a plain YAML type, for example a nested config section.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `_` | `pl.Trainer` | The trainer. Unused. | required |
| `pl_module` | `lxt.LuxonisLightningModule` | The model. It gives the config, the logger, and the save directory. | required |
