Skip to content

API reference: Detectron2 integration#

You can use a Neptune hook to capture model training metadata during training and validation of detectron2 models.

Related

For an in-depth tutorial, see IntegrationsDetectron2 integration guide.


NeptuneHook#

Captures model training metadata and logs them to Neptune.

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 "training" Namespace under which all metadata logged by the Neptune hook will be stored.
metrics_update_freq int, optional 20 How often NeptuneHook should log metrics (every nth epoch). The value must be greater than zero.

Example: Setting it to 10 will log metrics every 10th epoch.

Also applies to checkpoints, if log_checkpoints is set to True

log_model bool, optional False Whether to upload the final model checkpoint, whenever it is saved by the Trainer. Expects CheckpointHook to be present.
log_checkpoints bool, optional False Whether to upload checkpoints whenever they are saved by the Trainer. Expects CheckpointHook to be present.

Examples#

Creating a hook that sends the logs to an existing Neptune run object#

import neptune

neptune_run = neptune.init_run()

neptune_hook = NeptuneHook(run=neptune_run)
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: 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!):

run = neptune.init_run(
    api_token=neptune.ANONYMOUS_API_TOKEN,
    project="common/quickstarts",
)

Creating a Neptune run and hook with more options#

In the following example, we set the Trainer to save model checkpoints every 10th epoch. Neptune will upload those checkpoints and metrics at the same interval.

neptune_run = neptune.init_run(
    project="workspace-name/project-name",  # (1)!
    name="My detectron2 run",
    tags = ["validation"],
    capture_stdout=False,
)

neptune_hook = NeptuneHook(
    run=neptune_run,
    log_checkpoints=True,
    metrics_update_freq=10,
)
  1. The full project name. For example, "ml-team/classification".

    To copy it, navigate to the project settings in the top-right () and select Properties.

Additionally, we pass an extra option not to capture the stdout during the run (capture_stdout=False). For all the keyword arguments you can use, see neptuneinit_run().

Passing a handler to the hook#

To log the metadata to a specific namespace of the run, you can create a namespace handler object and pass that to the hook.

import neptune

neptune_run = neptune.init_run()

handler = neptune_run["validation"]

neptune_hook = NeptuneHook(run=handler)

In this case, all the metadata will be logged under the validation namespace (folder) inside the run.