Skip to content

API reference: fastai integration#

The NeptuneCallback class provided by the Neptune-fastai integration captures metadata that is generated when training models with fastai.


NeptuneCallback#

Creates a Neptune callback for logging metadata during a fastai training loop.

The callback logs parameters, metrics, losses, model configuration, optimizer configuration, and info about the dataset: path, number of samples, and hash value.

Metrics and losses are logged separately for each learner.fit() call. For example, when you call learner.fit(n) the first time, it will create a folder named fit_0 under the metrics folder that contains optimizer hyperparameters, batch, and loader level metrics:

metrics
    |--> fit_0
         |--> batch
         |--> loader
         |--> optimizer hyperparameters
    |--> ...
    |--> fit_n

Parameters

Name         Type Default Description
run Run or Handler - (required) An existing run reference, as returned by neptune.init_run(), or a namespace handler.
base_namespace str, optional "" Namespace under which all metadata logged by the Neptune callback will be stored. If omitted, defaults to an empty string, in which case the metadata is logged directly under the run, without a common "root" namespace.
upload_saved_models str, optional "all" Which model checkpoints created by SaveModelCallback() to upload.
  • "all" → uploads all model checkpoints.
  • "last" → uploads the last model checkpoint.

Examples

import torch
from fastai.callback.all import SaveModelCallback
from fastai.vision.all import (
    ImageDataLoaders,
    URLs,
    accuracy,
    resnet18,
    untar_data,
    vision_learner,
)
from neptune.integrations.fastai import NeptuneCallback

Create a Neptune run:

import neptune

run = neptune.init_run()

path = untar_data(URLs.MNIST_TINY)
dls = ImageDataLoaders.from_csv(path, num_workers=0)
If Neptune can't find your project name or API token

As a best practice, you should save your Neptune API token and project name as environment variables:

export NEPTUNE_API_TOKEN="h0dHBzOi8aHR0cHM6Lkc78ghs74kl0jv...Yh3Kb8"
export NEPTUNE_PROJECT="ml-team/classification"

Alternatively, you can pass the information when using a function that takes api_token and project as arguments:

run = neptune.init_run(
    api_token="h0dHBzOi8aHR0cHM6Lkc78ghs74kl0jv...Yh3Kb8", # (1)!
    project="ml-team/classification", # (2)!
)
  1. In the bottom-left corner, expand the user menu and select Get my API token.
  2. You can copy the path from the project details ( Details & privacy).

If you haven't registered, you can log anonymously to a public project:

api_token=neptune.ANONYMOUS_API_TOKEN
project="common/quickstarts"

Make sure not to publish sensitive data through your code!

Log a single training phase or all phases of the learner:

Single phase
learn = cnn_learner(dls, resnet18, metrics=accuracy)
learn.fit_one_cycle(1, cbs=[NeptuneCallback(run=run, base_namespace="experiment_1")])
learn.fit_one_cycle(2)
All phases
learn = cnn_learner(
    dls, resnet18, cbs=[NeptuneCallback(run=run, base_namespace="experiment_2")]
)
learn.fit_one_cycle(1)
learn.fit_one_cycle(2)

You can log your model weight files during a single training phase or all training phases by adding SaveModelCallback() to the callbacks list of your learner or fit method.

Log every n epochs
n = 2
learn = vision_learner(
    dls,
    resnet18,
    metrics=accuracy,
    cbs=[
        SaveModelCallback(every_epoch=n),
        NeptuneCallback(
            run=run,
            base_namespace="experiment_3",
            upload_saved_models="last",
        ),
    ],
)

learn.fit_one_cycle(5)
Best model
learn = vision_learner(
    dls,
    resnet18,
    metrics=accuracy,
    cbs=[
        SaveModelCallback(), NeptuneCallback(run=run, base_namespace="experiment_4")
    ],
)

learn.fit_one_cycle(5)

See also

neptune-fastai repo on GitHub