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.


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 iteration). The value must be greater than zero.

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

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="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!

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".

    • You can copy the name from the project details ( Details & privacy)
    • You can also find a pre-filled project string in Experiments Create a new run.

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.


See also

neptune-detectron2 repo on GitHub