Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
0760506
added output handlers, model checkpoint handler, added mnist example,…
jakubczakon Jan 26, 2020
ea65a1a
added exp link to examples, added tests
jakubczakon Jan 26, 2020
9372b8b
added neptune do docs
jakubczakon Jan 26, 2020
f3d81c5
Merge branch 'master' into master
jakubczakon Jan 26, 2020
c3d9556
fixed test
jakubczakon Jan 26, 2020
bfff60c
Merge branch 'master' of https://github.com/neptune-ai/ignite
jakubczakon Jan 26, 2020
741d3d7
fixed imports
jakubczakon Jan 26, 2020
0d3b1f5
added neptune-client to test dependencies
jakubczakon Jan 26, 2020
7f00f6a
fixed missing package message
jakubczakon Jan 26, 2020
cc5ca70
Merge branch 'master' into master
jakubczakon Jan 26, 2020
cc450b8
dropped model checkpoing handler
jakubczakon Jan 27, 2020
b0bf959
updated experiment link
jakubczakon Jan 27, 2020
73d4001
dropped __futures__ print_function
jakubczakon Jan 27, 2020
e47a994
updated fork
jakubczakon Feb 29, 2020
d69068c
added NeptuneSaver and tests
jakubczakon Feb 29, 2020
1cdfd83
autopep8 fix
Feb 29, 2020
39e2c66
updated token to anonymous user neptuner
jakubczakon Feb 29, 2020
de7000f
Merge branch 'master' of https://github.com/neptune-ai/ignite
jakubczakon Feb 29, 2020
5b345a6
updated experiment link
jakubczakon Mar 2, 2020
b2e0901
Merge branch 'master' into master
jakubczakon Mar 2, 2020
7ddf4d3
updated token to 'ANONYMOUS'
jakubczakon Mar 7, 2020
6b13741
Merge branch 'master' of https://github.com/neptune-ai/ignite
jakubczakon Mar 7, 2020
a0a43d9
Merge branch 'master' into master
jakubczakon Mar 7, 2020
05d3c6f
updated examples, fixed tests
jakubczakon Mar 7, 2020
4022987
autopep8 fix
Mar 7, 2020
d2a8b23
fixed serializable test
jakubczakon Mar 7, 2020
fb59d0c
Merge branch 'master' of https://github.com/neptune-ai/ignite
jakubczakon Mar 7, 2020
2a79216
fixed serializable model test
jakubczakon Mar 7, 2020
c2bc4c5
local
jakubczakon Apr 21, 2020
d363722
added __getattr__, fixed experiment_name, fixed api_token=None
jakubczakon Apr 21, 2020
3ebeffd
merged conflicts
jakubczakon Apr 21, 2020
e4a99f7
autopep8 fix
Apr 21, 2020
ae1669d
added self.experiment back
jakubczakon Apr 21, 2020
48b449e
Merge branch 'master' of https://github.com/neptune-ai/ignite
jakubczakon Apr 21, 2020
ae6439c
Merge remote-tracking branch 'upstream/master' into update-neptune-in…
twolodzko Feb 16, 2023
627e78e
Fix after merge
twolodzko Feb 16, 2023
06fa414
Update Neptune integration for compatibility with recent changes in N…
twolodzko Feb 21, 2023
4eda471
Merge branch 'pytorch:master' into update-neptune-integration
twolodzko Feb 21, 2023
1f22ab9
Track the version of Ignite in Neptune
twolodzko Feb 22, 2023
185acda
Ignore the future deprecation warnings when importing neptune.new
twolodzko Feb 22, 2023
e3a3ea3
autopep8 fix
twolodzko Feb 22, 2023
40a72a4
Merge branch 'master' into update-neptune-integration
twolodzko Feb 22, 2023
2dddf95
Make INTEGRATION_VERSION_KEY private
twolodzko Feb 22, 2023
15feb59
Add type annotations
twolodzko Feb 23, 2023
94aaeb5
Add type annotation to kwargs
twolodzko Feb 23, 2023
494826f
Use >= in requirements
twolodzko Feb 23, 2023
a432aed
Fix failing test
twolodzko Feb 23, 2023
a1576c9
update NeptuneLogger docstring and MNIST example
normandy7 Mar 10, 2023
cd2877e
Merge branch 'master' into update-neptunelogger-docstring
normandy7 Mar 10, 2023
0b0ff0d
remove Oxford comma per review suggestion
normandy7 Mar 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions examples/contrib/mnist/mnist_with_neptune_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
MNIST example with training and validation monitoring using Neptune.

Requirements:
Neptune: `pip install neptune-client`
Neptune: `pip install neptune`

Usage:

Expand All @@ -11,11 +11,11 @@
python mnist_with_neptune_logger.py
```

Go to https://neptune.ai and explore your experiment.
Go to https://neptune.ai and explore your run.

Note:
You can see an example experiment here:
https://ui.neptune.ai/o/shared/org/pytorch-ignite-integration/e/PYTOR-26/charts
You can view example runs here:
https://app.neptune.ai/o/common/org/pytorch-ignite-integration/
"""
from argparse import ArgumentParser

Expand Down Expand Up @@ -100,17 +100,18 @@ def compute_metrics(engine):

npt_logger = NeptuneLogger(
api_token="ANONYMOUS",
project_name="shared/pytorch-ignite-integration",
project="common/pytorch-ignite-integration",
name="ignite-mnist-example",
params={
"train_batch_size": train_batch_size,
"val_batch_size": val_batch_size,
"epochs": epochs,
"lr": lr,
"momentum": momentum,
},
)

npt_logger.experiment["params"] = {
"train_batch_size": train_batch_size,
"val_batch_size": val_batch_size,
"epochs": epochs,
"lr": lr,
"momentum": momentum,
}

npt_logger.attach_output_handler(
trainer,
event_name=Events.ITERATION_COMPLETED(every=100),
Expand Down
83 changes: 40 additions & 43 deletions ignite/contrib/handlers/neptune_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,47 +32,49 @@

class NeptuneLogger(BaseLogger):
"""
`Neptune <https://neptune.ai/>`_ handler to log metrics, model/optimizer parameters, gradients during the training
and validation. It can also log model checkpoints to Neptune server.
`Neptune <https://neptune.ai/>`_ handler to log metrics, model/optimizer parameters and gradients during training
and validation. It can also log model checkpoints to Neptune.

.. code-block:: bash

pip install neptune-client
pip install neptune

Args:
api_token: Required in online mode. Neptune API token, found on https://neptune.ai.
project_name: Required in online mode. Qualified name of a project in a form of
"namespace/project_name" for example "tom/minst-classification".
If None, the value of NEPTUNE_PROJECT environment variable will be taken.
You need to create the project in https://neptune.ai first.
**kwargs: Other arguments to be passed to Neptune's `init_run`.
api_token: Neptune API token, found on https://neptune.ai -> User menu -> "Get your API token".
If None, the value of the NEPTUNE_API_TOKEN environment variable is used. To keep your token
secure, you should set it to the environment variable rather than including it in your code.
project: Name of a Neptune project, in the form "workspace-name/project-name".
For example "tom/mnist-classification".
If None, the value of the NEPTUNE_PROJECT environment variable is used.
**kwargs: Other arguments to be passed to the `init_run()` function.

Examples:
.. code-block:: python

from ignite.contrib.handlers.neptune_logger import *

# Create a logger
# We are using the api_token for the anonymous user neptuner but you can use your own.
# Note: We are using the API token for anonymous logging. You can pass your own token, or save it as an
# environment variable and leave out the api_token argument.

npt_logger = NeptuneLogger(
api_token="ANONYMOUS",
project="shared/pytorch-ignite-integration",
name="cnn-mnist", # Optional,
params={"max_epochs": 10}, # Optional,
tags=["pytorch-ignite","minst"] # Optional
project="common/pytorch-ignite-integration",
name="cnn-mnist", # Optional,
tags=["pytorch-ignite", "minst"], # Optional
)

# Attach the logger to the trainer to log training loss at each iteration
# Attach the logger to the trainer to log training loss at each iteration.
npt_logger.attach_output_handler(
trainer,
event_name=Events.ITERATION_COMPLETED,
tag="training",
output_transform=lambda loss: {'loss': loss}
output_transform=lambda loss: {"loss": loss},
)

# Attach the logger to the evaluator on the training dataset and log NLL, Accuracy metrics after each epoch
# We setup `global_step_transform=global_step_from_engine(trainer)` to take the epoch
# Attach the logger to the evaluator on the training dataset and log NLL
# and accuracy metrics after each epoch.
# We set up `global_step_transform=global_step_from_engine(trainer)` to take the epoch
# of the `trainer` instead of `train_evaluator`.
npt_logger.attach_output_handler(
train_evaluator,
Expand All @@ -82,76 +84,71 @@ class NeptuneLogger(BaseLogger):
global_step_transform=global_step_from_engine(trainer),
)

# Attach the logger to the evaluator on the validation dataset and log NLL, Accuracy metrics after
# each epoch. We setup `global_step_transform=global_step_from_engine(trainer)` to take the epoch of the
# Attach the logger to the evaluator on the validation dataset and log NLL and accuracy metrics after
# each epoch. We set up `global_step_transform=global_step_from_engine(trainer)` to take the epoch of the
# `trainer` instead of `evaluator`.
npt_logger.attach_output_handler(
evaluator,
event_name=Events.EPOCH_COMPLETED,
tag="validation",
metric_names=["nll", "accuracy"],
global_step_transform=global_step_from_engine(trainer)),
global_step_transform=global_step_from_engine(trainer),
)

# Attach the logger to the trainer to log optimizer's parameters, e.g. learning rate at each iteration
# Attach the logger to the trainer to log optimizer parameters, such as learning rate at each iteration.
npt_logger.attach_opt_params_handler(
trainer,
event_name=Events.ITERATION_STARTED,
optimizer=optimizer,
param_name='lr' # optional
param_name="lr", # optional
)

# Attach the logger to the trainer to log model's weights norm after each iteration
# Attach the logger to the trainer to log model's weights norm after each iteration.
npt_logger.attach(
trainer,
event_name=Events.ITERATION_COMPLETED,
log_handler=WeightsScalarHandler(model)
log_handler=WeightsScalarHandler(model),
)

Explore an experiment with neptune tracking here:
https://ui.neptune.ai/o/shared/org/pytorch-ignite-integration/e/PYTOR1-18/charts
You can save model checkpoints to a Neptune server:
Explore runs with Neptune tracking here:
https://app.neptune.ai/o/common/org/pytorch-ignite-integration/

You can also save model checkpoints to a Neptune:

.. code-block:: python

from ignite.handlers import Checkpoint


def score_function(engine):
return engine.state.metrics["accuracy"]


to_save = {"model": model}
handler = Checkpoint(
to_save,
NeptuneSaver(npt_logger), n_saved=2,
filename_prefix="best",
score_function=score_function,
score_name="validation_accuracy",
global_step_transform=global_step_from_engine(trainer)
global_step_transform=global_step_from_engine(trainer),
)
validation_evaluator.add_event_handler(Events.COMPLETED, handler)

It is also possible to use the logger as context manager:
It is also possible to use the logger as a context manager:

.. code-block:: python

from ignite.contrib.handlers.neptune_logger import *

# We are using the api_token for the anonymous user neptuner but you can use your own.

with NeptuneLogger(api_token="ANONYMOUS",
project="shared/pytorch-ignite-integration",
name="cnn-mnist", # Optional,
params={"max_epochs": 10}, # Optional,
tags=["pytorch-ignite","mnist"] # Optional
) as npt_logger:

with NeptuneLogger() as npt_logger:
trainer = Engine(update_fn)
# Attach the logger to the trainer to log training loss at each iteration
npt_logger.attach_output_handler(
trainer,
event_name=Events.ITERATION_COMPLETED,
tag="training",
output_transform=lambda loss: {"loss": loss}
output_transform=lambda loss: {"loss": loss},
)

"""
Expand Down Expand Up @@ -180,8 +177,8 @@ def __init__(self, api_token: Optional[str] = None, project: Optional[str] = Non
import neptune
except ImportError:
raise ModuleNotFoundError(
"This contrib module requires neptune client to be installed. "
"You may install neptune with command: \n pip install neptune \n"
"This contrib module requires the Neptune client library to be installed. "
"Install neptune with the command: \n pip install neptune \n"
)

run = neptune.init_run(
Expand All @@ -204,7 +201,7 @@ def _create_opt_params_handler(self, *args: Any, **kwargs: Any) -> "OptimizerPar


class OutputHandler(BaseOutputHandler):
"""Helper handler to log engine's output and/or metrics
"""Helper handler to log engine's output and/or metrics.

Args:
tag: common title for all produced plots. For example, "training"
Expand Down