> ## 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.

# fastai

> Integrate W&B with fastai using the WandbCallback to track experiments, log metrics, and visualize model performance.

You can integrate **fastai** with W\&B using the `WandbCallback` class to track experiments, log metrics, and visualize model performance during training. This page shows how to set up authentication, add the callback to your training loop, and configure logging for both single-process and distributed training. Check out these [interactive docs with examples](https://app.wandb.ai/borisd13/demo_config/reports/Visualize-track-compare-Fastai-models--Vmlldzo4MzAyNA) for more details.

## Sign up and create an API key

An API key authenticates your machine to W\&B. You can generate an API key from your user profile.

<Note>
  For a more streamlined approach, go to [User Settings](https://wandb.ai/settings) and create an API key. Copy the API key immediately and save it in a secure location such as a password manager.
</Note>

1. Click your user profile icon in the upper right corner.
2. Select **User Settings**, then scroll to the **API Keys** section.

## Install the `wandb` library and log in

To install the `wandb` library locally and log in:

<Tabs>
  <Tab title="Command Line">
    1. Set the `WANDB_API_KEY` [environment variable](/models/track/environment-variables/) to your API key. Replace values enclosed in `<>` with your own:

       ```bash theme={null}
       export WANDB_API_KEY=<your_api_key>
       ```

    2. Install the `wandb` library and log in.

       ```shell theme={null}
       pip install wandb

       wandb login
       ```
  </Tab>

  <Tab title="Python">
    ```bash theme={null}
    pip install wandb
    ```

    ```python theme={null}
    import wandb
    wandb.login()
    ```
  </Tab>

  <Tab title="Python notebook">
    ```python theme={null}
    !pip install wandb

    import wandb
    wandb.login()
    ```
  </Tab>
</Tabs>

## Add the `WandbCallback` to the `learner` or `fit` method

To start logging your fastai training runs to W\&B, attach the `WandbCallback` to either a single `fit` call or the `learner` itself.

```python theme={null}
import wandb
from fastai.callback.wandb import *

# start logging a wandb run
wandb.init(project="my_project")

# To log only during one training phase
learn.fit(..., cbs=WandbCallback())

# To log continuously for all training phases
learn = learner(..., cbs=WandbCallback())
```

<Note>
  If you use version 1 of fastai, refer to the [fastai v1 docs](/models/integrations/fastai/v1/).
</Note>

## WandbCallback arguments

Use the following arguments to control what `WandbCallback` logs during training:

| Args                    | Description                                                                                                                                                                                                                                                     |
| ----------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `log`                   | Whether to log the model's: `gradients`, `parameters`, `all`, or `None` (default). Losses and metrics are always logged.                                                                                                                                        |
| `log_preds`             | Whether to log prediction samples (default to `True`).                                                                                                                                                                                                          |
| `log_preds_every_epoch` | Whether to log predictions every epoch or at the end (default to `False`).                                                                                                                                                                                      |
| `log_model`             | Whether to log the model (default to `False`). This also requires `SaveModelCallback`.                                                                                                                                                                          |
| `model_name`            | The name of the `file` to save, overrides `SaveModelCallback`.                                                                                                                                                                                                  |
| `log_dataset`           | <ul><li><code>False</code> (default).</li><li><code>True</code> logs the folder referenced by `learn.dls.path`.</li><li>A path can be defined explicitly to reference which folder to log.</li></ul><p><em>Note: subfolder "models" is always ignored.</em></p> |
| `dataset_name`          | Name of the logged dataset (default to `folder name`).                                                                                                                                                                                                          |
| `valid_dl`              | `DataLoaders` containing items used for prediction samples (default to random items from `learn.dls.valid`).                                                                                                                                                    |
| `n_preds`               | Number of logged predictions (default to 36).                                                                                                                                                                                                                   |
| `seed`                  | Used for defining random samples.                                                                                                                                                                                                                               |

For custom workflows, you can manually log your datasets and models:

* `log_dataset(path, name=None, metadata={})`
* `log_model(path, name=None, metadata={})`

*Note: any subfolder "models" is ignored.*

## Distributed training

`fastai` supports distributed training by using the context manager `distrib_ctx`. W\&B supports this automatically and enables you to track your multi-GPU experiments without additional configuration. The following sections describe how to integrate W\&B with distributed training and how to limit logging to the main process.

Review this minimal example:

<Tabs>
  <Tab title="Script">
    ```python theme={null}
    import wandb
    from fastai.vision.all import *
    from fastai.distributed import *
    from fastai.callback.wandb import WandbCallback

    wandb.require(experiment="service")
    path = rank0_first(lambda: untar_data(URLs.PETS) / "images")

    def train():
        dls = ImageDataLoaders.from_name_func(
            path,
            get_image_files(path),
            valid_pct=0.2,
            label_func=lambda x: x[0].isupper(),
            item_tfms=Resize(224),
        )
        wandb.init("fastai_ddp", entity="capecape")
        cb = WandbCallback()
        learn = vision_learner(dls, resnet34, metrics=error_rate, cbs=cb).to_fp16()
        with learn.distrib_ctx(sync_bn=False):
            learn.fit(1)

    if __name__ == "__main__":
        train()
    ```

    Then, in your terminal, execute:

    ```shell theme={null}
    torchrun --nproc_per_node 2 train.py
    ```

    In this case, the machine has 2 GPUs.
  </Tab>

  <Tab title="Python notebook">
    You can now run distributed training directly inside a notebook.

    ```python theme={null}
    import wandb
    from fastai.vision.all import *

    from accelerate import notebook_launcher
    from fastai.distributed import *
    from fastai.callback.wandb import WandbCallback

    wandb.require(experiment="service")
    path = untar_data(URLs.PETS) / "images"

    def train():
        dls = ImageDataLoaders.from_name_func(
            path,
            get_image_files(path),
            valid_pct=0.2,
            label_func=lambda x: x[0].isupper(),
            item_tfms=Resize(224),
        )
        wandb.init("fastai_ddp", entity="capecape")
        cb = WandbCallback()
        learn = vision_learner(dls, resnet34, metrics=error_rate, cbs=cb).to_fp16()
        with learn.distrib_ctx(in_notebook=True, sync_bn=False):
            learn.fit(1)

    notebook_launcher(train, num_processes=2)
    ```
  </Tab>
</Tabs>

### Log only on the main process

In the previous examples, W\&B launches one run per process. At the end of the training, you have two runs. This can sometimes be confusing, and you may want to log only on the main process. To do so, you must manually detect which process you are in and avoid creating runs (calling `wandb.init()` in all other processes).

<Tabs>
  <Tab title="Script">
    ```python theme={null}
    import wandb
    from fastai.vision.all import *
    from fastai.distributed import *
    from fastai.callback.wandb import WandbCallback

    wandb.require(experiment="service")
    path = rank0_first(lambda: untar_data(URLs.PETS) / "images")

    def train():
        cb = []
        dls = ImageDataLoaders.from_name_func(
            path,
            get_image_files(path),
            valid_pct=0.2,
            label_func=lambda x: x[0].isupper(),
            item_tfms=Resize(224),
        )
        if rank_distrib() == 0:
            run = wandb.init("fastai_ddp", entity="capecape")
            cb = WandbCallback()
        learn = vision_learner(dls, resnet34, metrics=error_rate, cbs=cb).to_fp16()
        with learn.distrib_ctx(sync_bn=False):
            learn.fit(1)

    if __name__ == "__main__":
        train()
    ```

    In your terminal, call:

    ```shell theme={null}
    torchrun --nproc_per_node 2 train.py
    ```
  </Tab>

  <Tab title="Python notebook">
    ```python theme={null}
    import wandb
    from fastai.vision.all import *

    from accelerate import notebook_launcher
    from fastai.distributed import *
    from fastai.callback.wandb import WandbCallback

    wandb.require(experiment="service")
    path = untar_data(URLs.PETS) / "images"

    def train():
        cb = []
        dls = ImageDataLoaders.from_name_func(
            path,
            get_image_files(path),
            valid_pct=0.2,
            label_func=lambda x: x[0].isupper(),
            item_tfms=Resize(224),
        )
        if rank_distrib() == 0:
            run = wandb.init("fastai_ddp", entity="capecape")
            cb = WandbCallback()
        learn = vision_learner(dls, resnet34, metrics=error_rate, cbs=cb).to_fp16()
        with learn.distrib_ctx(in_notebook=True, sync_bn=False):
            learn.fit(1)

    notebook_launcher(train, num_processes=2)
    ```
  </Tab>
</Tabs>

## Examples

For end-to-end demonstrations of the fastai integration, see the following references:

* [Visualize, track, and compare fastai models](https://app.wandb.ai/borisd13/demo_config/reports/Visualize-track-compare-Fastai-models--Vmlldzo4MzAyNA): A documented walkthrough.
* [Image segmentation on CamVid](https://colab.research.google.com/drive/1IWrhwcJoncCKHm6VXsNwOr9Yukhz3B49?usp=sharing): A sample use case of the integration.
