API reference: fastai integration#
NeptuneCallback class provided by the Neptune–fastai integration captures metadata that is generated when training models with fastai.
- For an in-depth tutorial, see Integrations ≫ Working with fastai.
- neptune-fastai repo on GitHub
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:
||-||(required) An existing run reference, as returned by
||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.|
||Which model checkpoints created by
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 )
- Find and copy your API token by clicking your avatar and selecting Get my API token.
- Find and copy your project name in the project Settings → Properties.
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.
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)