fastai integration API reference

API reference for the NeptuneCallback of the Neptune-fastai integration.

TheNeptuneCallbackclass 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 eachlearner.fit()call. For example, when you calllearner.fit(n)the first time, it will create a folder namedfit_0under themetricsfolder that contains optimizer hyperparameters, batch, and loader level metrics:

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

Parameters

Name Type Default Description
run RunorHandler - (required) An existing run reference, as returned byneptune.init_run(), or anamespace 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 bySaveModelCallback()to upload."all"→ uploads all model checkpoints."last"→ uploads the last model checkpoint.
  • "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)

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 takesapi_tokenandprojectas 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:

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 addingSaveModelCallback()to the callbacks list of your learner or fit method.

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)
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 onGitHub



This page is originally sourced from the legacy docs.