2020-08-20 02:03:22 +00:00
|
|
|
|
# Copyright The PyTorch Lightning team.
|
|
|
|
|
#
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
2020-01-14 03:20:01 +00:00
|
|
|
|
"""
|
2020-11-13 15:05:54 +00:00
|
|
|
|
Neptune Logger
|
|
|
|
|
--------------
|
2020-01-14 03:20:01 +00:00
|
|
|
|
"""
|
2021-03-02 09:47:55 +00:00
|
|
|
|
import logging
|
2020-03-04 14:33:39 +00:00
|
|
|
|
from argparse import Namespace
|
2020-12-21 09:15:04 +00:00
|
|
|
|
from typing import Any, Dict, Iterable, Optional, Union
|
2020-04-16 16:04:12 +00:00
|
|
|
|
|
2020-03-03 01:49:14 +00:00
|
|
|
|
import torch
|
2020-01-14 03:20:01 +00:00
|
|
|
|
from torch import is_tensor
|
|
|
|
|
|
2020-06-30 22:09:16 +00:00
|
|
|
|
from pytorch_lightning.loggers.base import LightningLoggerBase, rank_zero_experiment
|
2021-01-15 17:23:56 +00:00
|
|
|
|
from pytorch_lightning.utilities import _module_available, rank_zero_only
|
2021-01-05 19:34:47 +00:00
|
|
|
|
|
2021-03-02 09:47:55 +00:00
|
|
|
|
log = logging.getLogger(__name__)
|
2021-01-05 19:34:47 +00:00
|
|
|
|
_NEPTUNE_AVAILABLE = _module_available("neptune")
|
|
|
|
|
|
|
|
|
|
if _NEPTUNE_AVAILABLE:
|
|
|
|
|
import neptune
|
|
|
|
|
from neptune.experiments import Experiment
|
|
|
|
|
else:
|
|
|
|
|
# needed for test mocks, these tests shall be updated
|
|
|
|
|
neptune, Experiment = None, None
|
2020-01-14 03:20:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NeptuneLogger(LightningLoggerBase):
|
2020-02-11 04:55:22 +00:00
|
|
|
|
r"""
|
2020-11-13 15:05:54 +00:00
|
|
|
|
Log using `Neptune <https://neptune.ai>`_.
|
|
|
|
|
|
|
|
|
|
Install it with pip:
|
2020-04-16 16:04:12 +00:00
|
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
|
|
pip install neptune-client
|
|
|
|
|
|
|
|
|
|
The Neptune logger can be used in the online mode or offline (silent) mode.
|
2020-05-07 13:25:54 +00:00
|
|
|
|
To log experiment data in online mode, :class:`NeptuneLogger` requires an API key.
|
2020-05-10 17:19:18 +00:00
|
|
|
|
In offline mode, the logger does not connect to Neptune.
|
2020-04-16 16:04:12 +00:00
|
|
|
|
|
|
|
|
|
**ONLINE MODE**
|
|
|
|
|
|
2021-01-26 09:44:54 +00:00
|
|
|
|
.. testcode::
|
2020-10-05 01:20:06 +00:00
|
|
|
|
|
|
|
|
|
from pytorch_lightning import Trainer
|
|
|
|
|
from pytorch_lightning.loggers import NeptuneLogger
|
|
|
|
|
|
|
|
|
|
# arguments made to NeptuneLogger are passed on to the neptune.experiments.Experiment class
|
|
|
|
|
# We are using an api_key for the anonymous user "neptuner" but you can use your own.
|
|
|
|
|
neptune_logger = NeptuneLogger(
|
2021-07-30 12:10:15 +00:00
|
|
|
|
api_key="ANONYMOUS",
|
|
|
|
|
project_name="shared/pytorch-lightning-integration",
|
|
|
|
|
experiment_name="default", # Optional,
|
|
|
|
|
params={"max_epochs": 10}, # Optional,
|
|
|
|
|
tags=["pytorch-lightning", "mlp"], # Optional,
|
2020-10-05 01:20:06 +00:00
|
|
|
|
)
|
|
|
|
|
trainer = Trainer(max_epochs=10, logger=neptune_logger)
|
2020-04-16 16:04:12 +00:00
|
|
|
|
|
|
|
|
|
**OFFLINE MODE**
|
|
|
|
|
|
2021-01-26 09:44:54 +00:00
|
|
|
|
.. testcode::
|
2020-10-05 01:20:06 +00:00
|
|
|
|
|
|
|
|
|
from pytorch_lightning.loggers import NeptuneLogger
|
|
|
|
|
|
|
|
|
|
# arguments made to NeptuneLogger are passed on to the neptune.experiments.Experiment class
|
|
|
|
|
neptune_logger = NeptuneLogger(
|
|
|
|
|
offline_mode=True,
|
2021-07-30 12:10:15 +00:00
|
|
|
|
project_name="USER_NAME/PROJECT_NAME",
|
|
|
|
|
experiment_name="default", # Optional,
|
|
|
|
|
params={"max_epochs": 10}, # Optional,
|
|
|
|
|
tags=["pytorch-lightning", "mlp"], # Optional,
|
2020-10-05 01:20:06 +00:00
|
|
|
|
)
|
|
|
|
|
trainer = Trainer(max_epochs=10, logger=neptune_logger)
|
2020-04-16 16:04:12 +00:00
|
|
|
|
|
|
|
|
|
Use the logger anywhere in you :class:`~pytorch_lightning.core.lightning.LightningModule` as follows:
|
|
|
|
|
|
2020-10-05 01:20:06 +00:00
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
class LitModel(LightningModule):
|
|
|
|
|
def training_step(self, batch, batch_idx):
|
|
|
|
|
# log metrics
|
2021-07-30 12:10:15 +00:00
|
|
|
|
self.logger.experiment.log_metric("acc_train", ...)
|
2020-10-05 01:20:06 +00:00
|
|
|
|
# log images
|
2021-07-30 12:10:15 +00:00
|
|
|
|
self.logger.experiment.log_image("worse_predictions", ...)
|
2020-10-05 01:20:06 +00:00
|
|
|
|
# log model checkpoint
|
2021-07-30 12:10:15 +00:00
|
|
|
|
self.logger.experiment.log_artifact("model_checkpoint.pt", ...)
|
2020-10-05 01:20:06 +00:00
|
|
|
|
self.logger.experiment.whatever_neptune_supports(...)
|
|
|
|
|
|
|
|
|
|
def any_lightning_module_function_or_hook(self):
|
2021-07-30 12:10:15 +00:00
|
|
|
|
self.logger.experiment.log_metric("acc_train", ...)
|
|
|
|
|
self.logger.experiment.log_image("worse_predictions", ...)
|
|
|
|
|
self.logger.experiment.log_artifact("model_checkpoint.pt", ...)
|
2020-10-05 01:20:06 +00:00
|
|
|
|
self.logger.experiment.whatever_neptune_supports(...)
|
2020-04-16 16:04:12 +00:00
|
|
|
|
|
2020-05-10 17:19:18 +00:00
|
|
|
|
If you want to log objects after the training is finished use ``close_after_fit=False``:
|
2020-04-16 16:04:12 +00:00
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
2021-07-30 12:10:15 +00:00
|
|
|
|
neptune_logger = NeptuneLogger(..., close_after_fit=False, ...)
|
2020-04-16 16:04:12 +00:00
|
|
|
|
trainer = Trainer(logger=neptune_logger)
|
|
|
|
|
trainer.fit()
|
|
|
|
|
|
|
|
|
|
# Log test metrics
|
|
|
|
|
trainer.test(model)
|
|
|
|
|
|
|
|
|
|
# Log additional metrics
|
|
|
|
|
from sklearn.metrics import accuracy_score
|
|
|
|
|
|
|
|
|
|
accuracy = accuracy_score(y_true, y_pred)
|
2021-07-30 12:10:15 +00:00
|
|
|
|
neptune_logger.experiment.log_metric("test_accuracy", accuracy)
|
2020-04-16 16:04:12 +00:00
|
|
|
|
|
|
|
|
|
# Log charts
|
|
|
|
|
from scikitplot.metrics import plot_confusion_matrix
|
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
|
fig, ax = plt.subplots(figsize=(16, 12))
|
|
|
|
|
plot_confusion_matrix(y_true, y_pred, ax=ax)
|
2021-07-30 12:10:15 +00:00
|
|
|
|
neptune_logger.experiment.log_image("confusion_matrix", fig)
|
2020-04-16 16:04:12 +00:00
|
|
|
|
|
|
|
|
|
# Save checkpoints folder
|
2021-07-30 12:10:15 +00:00
|
|
|
|
neptune_logger.experiment.log_artifact("my/checkpoints")
|
2020-04-16 16:04:12 +00:00
|
|
|
|
|
|
|
|
|
# When you are done, stop the experiment
|
|
|
|
|
neptune_logger.experiment.stop()
|
|
|
|
|
|
|
|
|
|
See Also:
|
|
|
|
|
- An `Example experiment <https://ui.neptune.ai/o/shared/org/
|
|
|
|
|
pytorch-lightning-integration/e/PYTOR-66/charts>`_ showing the UI of Neptune.
|
|
|
|
|
- `Tutorial <https://docs.neptune.ai/integrations/pytorch_lightning.html>`_ on how to use
|
|
|
|
|
Pytorch Lightning with Neptune.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
api_key: Required in online mode.
|
|
|
|
|
Neptune API token, found on https://neptune.ai.
|
|
|
|
|
Read how to get your
|
|
|
|
|
`API key <https://docs.neptune.ai/python-api/tutorials/get-started.html#copy-api-token>`_.
|
|
|
|
|
It is recommended to keep it in the `NEPTUNE_API_TOKEN`
|
|
|
|
|
environment variable and then you can leave ``api_key=None``.
|
|
|
|
|
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.
|
2020-05-10 17:19:18 +00:00
|
|
|
|
offline_mode: Optional default ``False``. If ``True`` no logs will be sent
|
2020-04-16 16:04:12 +00:00
|
|
|
|
to Neptune. Usually used for debug purposes.
|
|
|
|
|
close_after_fit: Optional default ``True``. If ``False`` the experiment
|
|
|
|
|
will not be closed after training and additional metrics,
|
|
|
|
|
images or artifacts can be logged. Also, remember to close the experiment explicitly
|
|
|
|
|
by running ``neptune_logger.experiment.stop()``.
|
|
|
|
|
experiment_name: Optional. Editable name of the experiment.
|
|
|
|
|
Name is displayed in the experiment’s Details (Metadata section) and
|
|
|
|
|
in experiments view as a column.
|
2020-11-16 18:20:23 +00:00
|
|
|
|
experiment_id: Optional. Default is ``None``. The ID of the existing experiment.
|
|
|
|
|
If specified, connect to experiment with experiment_id in project_name.
|
|
|
|
|
Input arguments "experiment_name", "params", "properties" and "tags" will be overriden based
|
|
|
|
|
on fetched experiment data.
|
2020-11-22 05:38:58 +00:00
|
|
|
|
prefix: A string to put at the beginning of metric keys.
|
2020-09-19 16:51:43 +00:00
|
|
|
|
\**kwargs: Additional arguments like `params`, `tags`, `properties`, etc. used by
|
|
|
|
|
:func:`neptune.Session.create_experiment` can be passed as keyword arguments in this logger.
|
2021-02-25 20:08:32 +00:00
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
ImportError:
|
|
|
|
|
If required Neptune package is not installed on the device.
|
2020-02-11 04:55:22 +00:00
|
|
|
|
"""
|
2020-05-10 17:19:18 +00:00
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
|
LOGGER_JOIN_CHAR = "-"
|
2020-11-22 05:38:58 +00:00
|
|
|
|
|
2020-09-19 16:51:43 +00:00
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
api_key: Optional[str] = None,
|
|
|
|
|
project_name: Optional[str] = None,
|
|
|
|
|
close_after_fit: Optional[bool] = True,
|
|
|
|
|
offline_mode: bool = False,
|
|
|
|
|
experiment_name: Optional[str] = None,
|
2020-11-16 18:20:23 +00:00
|
|
|
|
experiment_id: Optional[str] = None,
|
2021-07-26 11:37:35 +00:00
|
|
|
|
prefix: str = "",
|
|
|
|
|
**kwargs,
|
2020-09-19 16:51:43 +00:00
|
|
|
|
):
|
2020-10-05 01:20:06 +00:00
|
|
|
|
if neptune is None:
|
2021-02-08 19:28:38 +00:00
|
|
|
|
raise ImportError(
|
2021-07-26 11:37:35 +00:00
|
|
|
|
"You want to use `neptune` logger which is not installed yet,"
|
|
|
|
|
" install it with `pip install neptune-client`."
|
2021-02-08 19:28:38 +00:00
|
|
|
|
)
|
2020-01-14 03:20:01 +00:00
|
|
|
|
super().__init__()
|
|
|
|
|
self.api_key = api_key
|
|
|
|
|
self.project_name = project_name
|
|
|
|
|
self.offline_mode = offline_mode
|
2020-03-14 17:02:40 +00:00
|
|
|
|
self.close_after_fit = close_after_fit
|
2020-01-14 03:20:01 +00:00
|
|
|
|
self.experiment_name = experiment_name
|
2020-11-22 05:38:58 +00:00
|
|
|
|
self._prefix = prefix
|
2020-01-14 03:20:01 +00:00
|
|
|
|
self._kwargs = kwargs
|
2020-11-16 18:20:23 +00:00
|
|
|
|
self.experiment_id = experiment_id
|
2021-01-23 16:07:13 +00:00
|
|
|
|
self._experiment = None
|
2020-01-14 03:20:01 +00:00
|
|
|
|
|
2020-05-10 17:19:18 +00:00
|
|
|
|
log.info(f'NeptuneLogger will work in {"offline" if self.offline_mode else "online"} mode')
|
2020-01-14 03:20:01 +00:00
|
|
|
|
|
2020-03-14 17:02:40 +00:00
|
|
|
|
def __getstate__(self):
|
|
|
|
|
state = self.__dict__.copy()
|
2020-05-10 17:19:18 +00:00
|
|
|
|
|
|
|
|
|
# Experiment cannot be pickled, and additionally its ID cannot be pickled in offline mode
|
2021-07-26 11:37:35 +00:00
|
|
|
|
state["_experiment"] = None
|
2020-05-10 17:19:18 +00:00
|
|
|
|
if self.offline_mode:
|
2021-07-26 11:37:35 +00:00
|
|
|
|
state["experiment_id"] = None
|
2020-05-10 17:19:18 +00:00
|
|
|
|
|
2020-03-14 17:02:40 +00:00
|
|
|
|
return state
|
|
|
|
|
|
2020-01-14 03:20:01 +00:00
|
|
|
|
@property
|
2020-06-30 22:09:16 +00:00
|
|
|
|
@rank_zero_experiment
|
2020-02-25 19:52:39 +00:00
|
|
|
|
def experiment(self) -> Experiment:
|
2020-01-17 11:03:31 +00:00
|
|
|
|
r"""
|
2020-04-16 16:04:12 +00:00
|
|
|
|
Actual Neptune object. To use neptune features in your
|
|
|
|
|
:class:`~pytorch_lightning.core.lightning.LightningModule` do the following.
|
2020-01-17 11:03:31 +00:00
|
|
|
|
|
|
|
|
|
Example::
|
|
|
|
|
|
|
|
|
|
self.logger.experiment.some_neptune_function()
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
2020-05-10 17:19:18 +00:00
|
|
|
|
# Note that even though we initialize self._experiment in __init__,
|
|
|
|
|
# it may still end up being None after being pickled and un-pickled
|
2020-03-14 17:02:40 +00:00
|
|
|
|
if self._experiment is None:
|
2020-05-10 17:19:18 +00:00
|
|
|
|
self._experiment = self._create_or_get_experiment()
|
|
|
|
|
|
2020-01-14 03:20:01 +00:00
|
|
|
|
return self._experiment
|
|
|
|
|
|
|
|
|
|
@rank_zero_only
|
2020-03-04 14:33:39 +00:00
|
|
|
|
def log_hyperparams(self, params: Union[Dict[str, Any], Namespace]) -> None:
|
|
|
|
|
params = self._convert_params(params)
|
2020-03-19 13:15:47 +00:00
|
|
|
|
params = self._flatten_dict(params)
|
2020-03-04 14:33:39 +00:00
|
|
|
|
for key, val in params.items():
|
2021-07-26 11:37:35 +00:00
|
|
|
|
self.experiment.set_property(f"param__{key}", val)
|
2020-01-14 03:20:01 +00:00
|
|
|
|
|
|
|
|
|
@rank_zero_only
|
2021-02-08 19:28:38 +00:00
|
|
|
|
def log_metrics(self, metrics: Dict[str, Union[torch.Tensor, float]], step: Optional[int] = None) -> None:
|
2020-04-16 16:04:12 +00:00
|
|
|
|
"""
|
|
|
|
|
Log metrics (numeric values) in Neptune experiments.
|
2020-01-14 03:20:01 +00:00
|
|
|
|
|
2020-02-25 19:52:39 +00:00
|
|
|
|
Args:
|
|
|
|
|
metrics: Dictionary with metric names as keys and measured quantities as values
|
2021-01-25 16:40:01 +00:00
|
|
|
|
step: Step number at which the metrics should be recorded, currently ignored
|
2020-01-14 03:20:01 +00:00
|
|
|
|
"""
|
2021-07-26 11:37:35 +00:00
|
|
|
|
assert rank_zero_only.rank == 0, "experiment tried to log from global_rank != 0"
|
2020-11-22 05:38:58 +00:00
|
|
|
|
|
|
|
|
|
metrics = self._add_prefix(metrics)
|
2020-01-14 03:20:01 +00:00
|
|
|
|
for key, val in metrics.items():
|
2021-01-25 16:40:01 +00:00
|
|
|
|
# `step` is ignored because Neptune expects strictly increasing step values which
|
|
|
|
|
# Lighting does not always guarantee.
|
|
|
|
|
self.log_metric(key, val)
|
2020-01-14 03:20:01 +00:00
|
|
|
|
|
|
|
|
|
@rank_zero_only
|
2020-03-04 14:33:39 +00:00
|
|
|
|
def finalize(self, status: str) -> None:
|
2020-04-15 00:32:33 +00:00
|
|
|
|
super().finalize(status)
|
2020-03-14 17:02:40 +00:00
|
|
|
|
if self.close_after_fit:
|
|
|
|
|
self.experiment.stop()
|
2020-01-14 03:20:01 +00:00
|
|
|
|
|
2020-07-05 23:57:22 +00:00
|
|
|
|
@property
|
|
|
|
|
def save_dir(self) -> Optional[str]:
|
|
|
|
|
# Neptune does not save any local files
|
|
|
|
|
return None
|
|
|
|
|
|
2020-01-14 03:20:01 +00:00
|
|
|
|
@property
|
2020-02-25 19:52:39 +00:00
|
|
|
|
def name(self) -> str:
|
2020-05-10 17:19:18 +00:00
|
|
|
|
if self.offline_mode:
|
2021-07-26 11:37:35 +00:00
|
|
|
|
return "offline-name"
|
2021-06-28 09:57:41 +00:00
|
|
|
|
return self.experiment.name
|
2020-01-14 03:20:01 +00:00
|
|
|
|
|
|
|
|
|
@property
|
2020-02-25 19:52:39 +00:00
|
|
|
|
def version(self) -> str:
|
2020-05-10 17:19:18 +00:00
|
|
|
|
if self.offline_mode:
|
2021-07-26 11:37:35 +00:00
|
|
|
|
return "offline-id-1234"
|
2021-06-28 09:57:41 +00:00
|
|
|
|
return self.experiment.id
|
2020-01-14 03:20:01 +00:00
|
|
|
|
|
|
|
|
|
@rank_zero_only
|
2020-03-03 01:49:14 +00:00
|
|
|
|
def log_metric(
|
2021-02-08 19:28:38 +00:00
|
|
|
|
self, metric_name: str, metric_value: Union[torch.Tensor, float, str], step: Optional[int] = None
|
2020-03-04 14:33:39 +00:00
|
|
|
|
) -> None:
|
2020-04-16 16:04:12 +00:00
|
|
|
|
"""
|
|
|
|
|
Log metrics (numeric values) in Neptune experiments.
|
2020-01-14 03:20:01 +00:00
|
|
|
|
|
2020-02-25 19:52:39 +00:00
|
|
|
|
Args:
|
2020-04-16 16:04:12 +00:00
|
|
|
|
metric_name: The name of log, i.e. mse, loss, accuracy.
|
2020-02-25 19:52:39 +00:00
|
|
|
|
metric_value: The value of the log (data-point).
|
|
|
|
|
step: Step number at which the metrics should be recorded, must be strictly increasing
|
2020-01-14 03:20:01 +00:00
|
|
|
|
"""
|
2020-03-03 01:49:14 +00:00
|
|
|
|
if is_tensor(metric_value):
|
|
|
|
|
metric_value = metric_value.cpu().detach()
|
|
|
|
|
|
2020-01-14 03:20:01 +00:00
|
|
|
|
if step is None:
|
|
|
|
|
self.experiment.log_metric(metric_name, metric_value)
|
|
|
|
|
else:
|
|
|
|
|
self.experiment.log_metric(metric_name, x=step, y=metric_value)
|
|
|
|
|
|
|
|
|
|
@rank_zero_only
|
2020-03-04 14:33:39 +00:00
|
|
|
|
def log_text(self, log_name: str, text: str, step: Optional[int] = None) -> None:
|
2020-04-16 16:04:12 +00:00
|
|
|
|
"""
|
|
|
|
|
Log text data in Neptune experiments.
|
2020-01-14 03:20:01 +00:00
|
|
|
|
|
2020-02-25 19:52:39 +00:00
|
|
|
|
Args:
|
2020-04-16 16:04:12 +00:00
|
|
|
|
log_name: The name of log, i.e. mse, my_text_data, timing_info.
|
2020-02-25 19:52:39 +00:00
|
|
|
|
text: The value of the log (data-point).
|
|
|
|
|
step: Step number at which the metrics should be recorded, must be strictly increasing
|
2020-01-14 03:20:01 +00:00
|
|
|
|
"""
|
2021-04-26 13:28:55 +00:00
|
|
|
|
if step is None:
|
|
|
|
|
self.experiment.log_text(log_name, text)
|
|
|
|
|
else:
|
|
|
|
|
self.experiment.log_text(log_name, x=step, y=text)
|
2020-01-14 03:20:01 +00:00
|
|
|
|
|
|
|
|
|
@rank_zero_only
|
2021-02-08 19:28:38 +00:00
|
|
|
|
def log_image(self, log_name: str, image: Union[str, Any], step: Optional[int] = None) -> None:
|
2020-04-16 16:04:12 +00:00
|
|
|
|
"""
|
|
|
|
|
Log image data in Neptune experiment
|
2020-01-14 03:20:01 +00:00
|
|
|
|
|
2020-02-25 19:52:39 +00:00
|
|
|
|
Args:
|
|
|
|
|
log_name: The name of log, i.e. bboxes, visualisations, sample_images.
|
2020-04-16 16:04:12 +00:00
|
|
|
|
image: The value of the log (data-point).
|
|
|
|
|
Can be one of the following types: PIL image, `matplotlib.figure.Figure`,
|
|
|
|
|
path to image file (str)
|
2020-02-25 19:52:39 +00:00
|
|
|
|
step: Step number at which the metrics should be recorded, must be strictly increasing
|
2020-01-14 03:20:01 +00:00
|
|
|
|
"""
|
|
|
|
|
if step is None:
|
|
|
|
|
self.experiment.log_image(log_name, image)
|
|
|
|
|
else:
|
|
|
|
|
self.experiment.log_image(log_name, x=step, y=image)
|
|
|
|
|
|
|
|
|
|
@rank_zero_only
|
2020-03-04 14:33:39 +00:00
|
|
|
|
def log_artifact(self, artifact: str, destination: Optional[str] = None) -> None:
|
2020-01-14 03:20:01 +00:00
|
|
|
|
"""Save an artifact (file) in Neptune experiment storage.
|
|
|
|
|
|
2020-02-25 19:52:39 +00:00
|
|
|
|
Args:
|
|
|
|
|
artifact: A path to the file in local filesystem.
|
2020-04-16 16:04:12 +00:00
|
|
|
|
destination: Optional. Default is ``None``. A destination path.
|
|
|
|
|
If ``None`` is passed, an artifact file name will be used.
|
2020-01-14 03:20:01 +00:00
|
|
|
|
"""
|
|
|
|
|
self.experiment.log_artifact(artifact, destination)
|
|
|
|
|
|
|
|
|
|
@rank_zero_only
|
2020-03-04 14:33:39 +00:00
|
|
|
|
def set_property(self, key: str, value: Any) -> None:
|
2020-04-16 16:04:12 +00:00
|
|
|
|
"""
|
|
|
|
|
Set key-value pair as Neptune experiment property.
|
2020-01-14 03:20:01 +00:00
|
|
|
|
|
2020-02-25 19:52:39 +00:00
|
|
|
|
Args:
|
|
|
|
|
key: Property key.
|
|
|
|
|
value: New value of a property.
|
2020-01-14 03:20:01 +00:00
|
|
|
|
"""
|
|
|
|
|
self.experiment.set_property(key, value)
|
|
|
|
|
|
|
|
|
|
@rank_zero_only
|
2020-03-04 14:33:39 +00:00
|
|
|
|
def append_tags(self, tags: Union[str, Iterable[str]]) -> None:
|
2020-04-16 16:04:12 +00:00
|
|
|
|
"""
|
|
|
|
|
Appends tags to the neptune experiment.
|
2020-01-14 03:20:01 +00:00
|
|
|
|
|
2020-02-25 19:52:39 +00:00
|
|
|
|
Args:
|
2020-04-16 16:04:12 +00:00
|
|
|
|
tags: Tags to add to the current experiment. If str is passed, a single tag is added.
|
2020-02-25 19:52:39 +00:00
|
|
|
|
If multiple - comma separated - str are passed, all of them are added as tags.
|
|
|
|
|
If list of str is passed, all elements of the list are added as tags.
|
2020-01-14 03:20:01 +00:00
|
|
|
|
"""
|
2020-03-03 01:49:14 +00:00
|
|
|
|
if str(tags) == tags:
|
2020-01-14 03:20:01 +00:00
|
|
|
|
tags = [tags] # make it as an iterable is if it is not yet
|
|
|
|
|
self.experiment.append_tags(*tags)
|
2020-05-10 17:19:18 +00:00
|
|
|
|
|
|
|
|
|
def _create_or_get_experiment(self):
|
|
|
|
|
if self.offline_mode:
|
2021-07-26 11:37:35 +00:00
|
|
|
|
project = neptune.Session(backend=neptune.OfflineBackend()).get_project("dry-run/project")
|
2020-05-10 17:19:18 +00:00
|
|
|
|
else:
|
|
|
|
|
session = neptune.Session.with_default_backend(api_token=self.api_key)
|
|
|
|
|
project = session.get_project(self.project_name)
|
|
|
|
|
|
2020-11-16 18:20:23 +00:00
|
|
|
|
if self.experiment_id is None:
|
2020-09-19 16:51:43 +00:00
|
|
|
|
exp = project.create_experiment(name=self.experiment_name, **self._kwargs)
|
2020-11-16 18:20:23 +00:00
|
|
|
|
self.experiment_id = exp.id
|
2020-05-10 17:19:18 +00:00
|
|
|
|
else:
|
2020-11-16 18:20:23 +00:00
|
|
|
|
exp = project.get_experiments(id=self.experiment_id)[0]
|
2021-07-26 11:37:35 +00:00
|
|
|
|
self.experiment_name = exp.get_system_properties()["name"]
|
2020-11-16 18:20:23 +00:00
|
|
|
|
self.params = exp.get_parameters()
|
|
|
|
|
self.properties = exp.get_properties()
|
|
|
|
|
self.tags = exp.get_tags()
|
2020-05-10 17:19:18 +00:00
|
|
|
|
|
|
|
|
|
return exp
|