API reference: fastai integration#
The NeptuneCallback
class provided by the Neptune–fastai integration captures metadata that is generated when training models with fastai.
Related
- For an in-depth tutorial, see Integrations ≫ fastai integration guide.
- neptune-fastai repo on GitHub
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:
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.
|
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="h0dHBzOi8aHR0cHM6Lkc78ghs74kl0jvYh3Kb8"
export NEPTUNE_PROJECT="ml-team/classification"
You can, however, also pass them as arguments when initializing Neptune:
run = neptune.init_run(
api_token="h0dHBzOi8aHR0cHM6Lkc78ghs74kl0jvYh3Kb8", # your token here
project="ml-team/classification", # your full project name here
)
- API token: In the bottom-left corner, expand the user menu and select Get my API token.
- Project name: in the top-right menu: → Edit project details.
If you haven't registered, you can also log anonymously to a public project (make sure not to publish sensitive data through your code!):
Log a single training phase or all phases of the learner:
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)
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.