> ## Documentation Index
> Fetch the complete documentation index at: https://docs.wandb.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# MMEngine

> Use W&B with OpenMMLab's MMEngine through the WandbVisBackend to log training metrics, configs, and visual records.

This page shows you how to use W\&B with OpenMMLab's MMEngine to track and visualize training runs. Use it if you train deep learning models with MMEngine or OpenMMLab computer vision libraries and want to log metrics, configs, and visualizations to W\&B.

MMEngine by [OpenMMLab](https://github.com/open-mmlab) is a foundational library for training deep learning models based on PyTorch. MMEngine implements a training architecture for the OpenMMLab algorithm library, providing a unified execution foundation for over 30 algorithm libraries within OpenMMLab. Its core components include the training engine, evaluation engine, and module management.

MMEngine integrates directly with [W\&B](https://wandb.ai/site) through a dedicated [`WandbVisBackend`](https://mmengine.readthedocs.io/en/latest/api/generated/mmengine.visualization.WandbVisBackend.html#mmengine.visualization.WandbVisBackend) that you can use to:

* Log training and evaluation metrics.
* Log and manage experiment configs.
* Log additional records such as graphs, images, and scalars.

## Get started

Install `openmim` and `wandb`.

<Tabs>
  <Tab title="Command Line">
    ```bash theme={null}
    pip install -q -U openmim wandb
    ```
  </Tab>

  <Tab title="Notebook">
    ```bash theme={null}
    !pip install -q -U openmim wandb
    ```
  </Tab>
</Tabs>

Next, install `mmengine` and `mmcv` using `mim`.

<Tabs>
  <Tab title="Command Line">
    ```bash theme={null}
    mim install -q mmengine mmcv
    ```
  </Tab>

  <Tab title="Notebook">
    ```bash theme={null}
    !mim install -q mmengine mmcv
    ```
  </Tab>
</Tabs>

## Use the `WandbVisBackend` with MMEngine runner

This section demonstrates a typical workflow using `WandbVisBackend` with [`mmengine.runner.Runner`](https://mmengine.readthedocs.io/en/latest/api/generated/mmengine.runner.Runner.html#mmengine.runner.Runner). The visualizer wraps the W\&B backend so the MMEngine runner can route logs to W\&B during training.

1. Define a `visualizer` from a visualization config. The visualizer is what the runner uses to dispatch logs to the configured backend.

   ```python theme={null}
   from mmengine.visualization import Visualizer

   # define the visualization configs
   visualization_cfg = dict(
       name="wandb_visualizer",
       vis_backends=[
           dict(
               type='WandbVisBackend',
               init_kwargs=dict(project="mmengine"),
           )
       ],
       save_dir="runs/wandb"
   )

   # get the visualizer from the visualization configs
   visualizer = Visualizer.get_instance(**visualization_cfg)
   ```

   You pass a dictionary of arguments for [W\&B run initialization](/models/ref/python/functions/init) input parameters to `init_kwargs`.

2. Initialize a `runner` with the `visualizer`, and call `runner.train()` to start training. The runner uses the visualizer to stream metrics and configs to W\&B.

   ```python theme={null}
   from mmengine.runner import Runner

   # build the mmengine Runner which is a training helper for PyTorch
   runner = Runner(
       model,
       work_dir='runs/gan/',
       train_dataloader=train_dataloader,
       train_cfg=train_cfg,
       optim_wrapper=opt_wrapper_dict,
       visualizer=visualizer, # pass the visualizer
   )

   # start training
   runner.train()
   ```

## Use the `WandbVisBackend` with OpenMMLab computer vision libraries

You can also use the `WandbVisBackend` to track experiments with OpenMMLab computer vision libraries such as [MMDetection](https://mmdetection.readthedocs.io/). The following example overrides the `vis_backends` entry from a base config so that the existing visualizer logs to W\&B.

```python theme={null}
# inherit base configs from the default runtime configs
_base_ = ["../_base_/default_runtime.py"]

# Assign the `WandbVisBackend` config dictionary to the
# `vis_backends` of the `visualizer` from the base configs
_base_.visualizer.vis_backends = [
    dict(
        type='WandbVisBackend',
        init_kwargs={
            'project': 'mmdet',
            'entity': 'geekyrakshit'
        },
    ),
]
```
