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-04-16 16:04:12 +00:00
|
|
|
|
"""
|
2020-11-13 15:05:54 +00:00
|
|
|
|
Test Tube Logger
|
|
|
|
|
----------------
|
2020-04-16 16:04:12 +00:00
|
|
|
|
"""
|
2020-03-04 14:33:39 +00:00
|
|
|
|
from argparse import Namespace
|
2020-09-19 16:51:43 +00:00
|
|
|
|
from typing import Any, Dict, Optional, Union
|
2020-02-25 19:52:39 +00:00
|
|
|
|
|
2021-06-25 19:16:11 +00:00
|
|
|
|
import pytorch_lightning as pl
|
2020-06-30 22:09:16 +00:00
|
|
|
|
from pytorch_lightning.loggers.base import LightningLoggerBase, rank_zero_experiment
|
2021-08-26 10:28:14 +00:00
|
|
|
|
from pytorch_lightning.utilities import _module_available, rank_zero_deprecation, rank_zero_warn
|
2021-06-21 16:51:53 +00:00
|
|
|
|
from pytorch_lightning.utilities.distributed import rank_zero_only
|
2019-11-27 03:39:18 +00:00
|
|
|
|
|
2021-01-05 19:34:47 +00:00
|
|
|
|
_TESTTUBE_AVAILABLE = _module_available("test_tube")
|
|
|
|
|
|
|
|
|
|
if _TESTTUBE_AVAILABLE:
|
|
|
|
|
from test_tube import Experiment
|
|
|
|
|
else:
|
|
|
|
|
Experiment = None
|
|
|
|
|
|
2019-11-27 03:39:18 +00:00
|
|
|
|
|
|
|
|
|
class TestTubeLogger(LightningLoggerBase):
|
2020-01-17 11:03:31 +00:00
|
|
|
|
r"""
|
2020-04-16 16:04:12 +00:00
|
|
|
|
Log to local file system in `TensorBoard <https://www.tensorflow.org/tensorboard>`_ format
|
|
|
|
|
but using a nicer folder structure (see `full docs <https://williamfalcon.github.io/test-tube>`_).
|
2020-11-13 15:05:54 +00:00
|
|
|
|
|
2021-08-26 10:28:14 +00:00
|
|
|
|
Warning:
|
|
|
|
|
The test-tube package is no longer maintained and PyTorch Lightning will remove the :class:´TestTubeLogger´
|
|
|
|
|
in v1.7.0.
|
|
|
|
|
|
2020-04-16 16:04:12 +00:00
|
|
|
|
Install it with pip:
|
|
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
|
|
pip install test_tube
|
|
|
|
|
|
2020-10-07 03:49:06 +00:00
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
from pytorch_lightning import Trainer
|
|
|
|
|
from pytorch_lightning.loggers import TestTubeLogger
|
2021-07-28 16:08:31 +00:00
|
|
|
|
|
2020-10-07 03:49:06 +00:00
|
|
|
|
logger = TestTubeLogger("tt_logs", name="my_exp_name")
|
|
|
|
|
trainer = Trainer(logger=logger)
|
2020-04-16 16:04:12 +00:00
|
|
|
|
|
|
|
|
|
Use the logger anywhere in your :class:`~pytorch_lightning.core.lightning.LightningModule` as follows:
|
|
|
|
|
|
2020-10-07 03:49:06 +00:00
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
from pytorch_lightning import LightningModule
|
2021-07-28 16:08:31 +00:00
|
|
|
|
|
|
|
|
|
|
2020-10-07 03:49:06 +00:00
|
|
|
|
class LitModel(LightningModule):
|
|
|
|
|
def training_step(self, batch, batch_idx):
|
|
|
|
|
# example
|
|
|
|
|
self.logger.experiment.whatever_method_summary_writer_supports(...)
|
|
|
|
|
|
|
|
|
|
def any_lightning_module_function_or_hook(self):
|
|
|
|
|
self.logger.experiment.add_histogram(...)
|
2020-04-16 16:04:12 +00:00
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
save_dir: Save directory
|
|
|
|
|
name: Experiment name. Defaults to ``'default'``.
|
|
|
|
|
description: A short snippet about this experiment
|
|
|
|
|
debug: If ``True``, it doesn't log anything.
|
|
|
|
|
version: Experiment version. If version is not specified the logger inspects the save
|
|
|
|
|
directory for existing versions, then automatically assigns the next available version.
|
|
|
|
|
create_git_tag: If ``True`` creates a git tag to save the code used in this experiment.
|
2020-08-19 23:08:46 +00:00
|
|
|
|
log_graph: Adds the computational graph to tensorboard. This requires that
|
|
|
|
|
the user has defined the `self.example_input_array` attribute in their
|
|
|
|
|
model.
|
2020-11-22 05:38:58 +00:00
|
|
|
|
prefix: A string to put at the beginning of metric keys.
|
2021-02-25 20:08:32 +00:00
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
ImportError:
|
|
|
|
|
If required TestTube package is not installed on the device.
|
2020-01-17 11:03:31 +00:00
|
|
|
|
"""
|
|
|
|
|
|
2019-11-27 03:39:18 +00:00
|
|
|
|
__test__ = False
|
2021-07-26 11:37:35 +00:00
|
|
|
|
LOGGER_JOIN_CHAR = "-"
|
2019-11-27 03:39:18 +00:00
|
|
|
|
|
2020-08-22 10:35:09 +00:00
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
save_dir: str,
|
|
|
|
|
name: str = "default",
|
|
|
|
|
description: Optional[str] = None,
|
|
|
|
|
debug: bool = False,
|
|
|
|
|
version: Optional[int] = None,
|
|
|
|
|
create_git_tag: bool = False,
|
2020-11-22 05:38:58 +00:00
|
|
|
|
log_graph: bool = False,
|
2021-07-26 11:37:35 +00:00
|
|
|
|
prefix: str = "",
|
2020-08-22 10:35:09 +00:00
|
|
|
|
):
|
2021-08-26 10:28:14 +00:00
|
|
|
|
rank_zero_deprecation(
|
|
|
|
|
"The TestTubeLogger is deprecated since v1.5 and will be removed in v1.7. We recommend switching to the"
|
|
|
|
|
" `pytorch_lightning.loggers.TensorBoardLogger` as an alternative."
|
|
|
|
|
)
|
2020-10-07 03:49:06 +00:00
|
|
|
|
if Experiment is None:
|
2021-02-08 19:28:38 +00:00
|
|
|
|
raise ImportError(
|
2021-07-26 11:37:35 +00:00
|
|
|
|
"You want to use `test_tube` logger which is not installed yet,"
|
|
|
|
|
" install it with `pip install test-tube`."
|
2021-02-08 19:28:38 +00:00
|
|
|
|
)
|
2019-11-27 03:39:18 +00:00
|
|
|
|
super().__init__()
|
2020-07-09 11:15:41 +00:00
|
|
|
|
self._save_dir = save_dir
|
2019-11-27 03:39:18 +00:00
|
|
|
|
self._name = name
|
|
|
|
|
self.description = description
|
|
|
|
|
self.debug = debug
|
|
|
|
|
self._version = version
|
|
|
|
|
self.create_git_tag = create_git_tag
|
2020-08-19 23:08:46 +00:00
|
|
|
|
self._log_graph = log_graph
|
2020-11-22 05:38:58 +00:00
|
|
|
|
self._prefix = prefix
|
2019-11-27 03:39:18 +00:00
|
|
|
|
self._experiment = None
|
|
|
|
|
|
|
|
|
|
@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 TestTube object. To use TestTube features in your
|
|
|
|
|
:class:`~pytorch_lightning.core.lightning.LightningModule` do the following.
|
2020-01-17 11:03:31 +00:00
|
|
|
|
|
2020-04-16 16:04:12 +00:00
|
|
|
|
Example::
|
2020-01-17 11:03:31 +00:00
|
|
|
|
|
2020-04-16 16:04:12 +00:00
|
|
|
|
self.logger.experiment.some_test_tube_function()
|
2020-01-17 11:03:31 +00:00
|
|
|
|
|
2020-04-16 16:04:12 +00:00
|
|
|
|
"""
|
2019-11-27 03:39:18 +00:00
|
|
|
|
if self._experiment is not None:
|
|
|
|
|
return self._experiment
|
|
|
|
|
|
|
|
|
|
self._experiment = Experiment(
|
|
|
|
|
save_dir=self.save_dir,
|
|
|
|
|
name=self._name,
|
|
|
|
|
debug=self.debug,
|
|
|
|
|
version=self.version,
|
|
|
|
|
description=self.description,
|
|
|
|
|
create_git_tag=self.create_git_tag,
|
2020-04-24 21:21:00 +00:00
|
|
|
|
rank=rank_zero_only.rank,
|
2019-11-27 03:39:18 +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:
|
2019-11-27 03:39:18 +00:00
|
|
|
|
# TODO: HACK figure out where this is being set to true
|
|
|
|
|
self.experiment.debug = self.debug
|
2020-03-04 14:33:39 +00:00
|
|
|
|
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
|
|
|
|
self.experiment.argparse(Namespace(**params))
|
2019-11-27 03:39:18 +00:00
|
|
|
|
|
|
|
|
|
@rank_zero_only
|
2020-03-04 14:33:39 +00:00
|
|
|
|
def log_metrics(self, metrics: Dict[str, float], step: Optional[int] = None) -> None:
|
2019-11-27 03:39:18 +00:00
|
|
|
|
# TODO: HACK figure out where this is being set to true
|
2020-11-22 05:38:58 +00:00
|
|
|
|
metrics = self._add_prefix(metrics)
|
2019-11-27 03:39:18 +00:00
|
|
|
|
self.experiment.debug = self.debug
|
2019-12-07 13:50:21 +00:00
|
|
|
|
self.experiment.log(metrics, global_step=step)
|
2019-11-27 03:39:18 +00:00
|
|
|
|
|
2020-08-19 23:08:46 +00:00
|
|
|
|
@rank_zero_only
|
2021-07-26 11:37:35 +00:00
|
|
|
|
def log_graph(self, model: "pl.LightningModule", input_array=None):
|
2020-08-19 23:08:46 +00:00
|
|
|
|
if self._log_graph:
|
|
|
|
|
if input_array is None:
|
|
|
|
|
input_array = model.example_input_array
|
|
|
|
|
|
|
|
|
|
if input_array is not None:
|
2021-02-18 11:58:12 +00:00
|
|
|
|
self.experiment.add_graph(model, model._apply_batch_transfer_handler(input_array))
|
2020-08-19 23:08:46 +00:00
|
|
|
|
else:
|
2021-02-08 19:28:38 +00:00
|
|
|
|
rank_zero_warn(
|
2021-07-26 11:37:35 +00:00
|
|
|
|
"Could not log computational graph since neither the"
|
|
|
|
|
" `model.example_input_array` attribute is set nor"
|
|
|
|
|
" `input_array` was given",
|
|
|
|
|
UserWarning,
|
2021-02-08 19:28:38 +00:00
|
|
|
|
)
|
2020-08-19 23:08:46 +00:00
|
|
|
|
|
2019-11-27 03:39:18 +00:00
|
|
|
|
@rank_zero_only
|
2020-03-04 14:33:39 +00:00
|
|
|
|
def save(self) -> None:
|
2020-04-15 00:32:33 +00:00
|
|
|
|
super().save()
|
2019-11-27 03:39:18 +00:00
|
|
|
|
# TODO: HACK figure out where this is being set to true
|
|
|
|
|
self.experiment.debug = self.debug
|
|
|
|
|
self.experiment.save()
|
|
|
|
|
|
|
|
|
|
@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)
|
2019-11-27 03:39:18 +00:00
|
|
|
|
# TODO: HACK figure out where this is being set to true
|
|
|
|
|
self.experiment.debug = self.debug
|
|
|
|
|
self.save()
|
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
|
|
@rank_zero_only
|
2020-03-04 14:33:39 +00:00
|
|
|
|
def close(self) -> None:
|
2020-04-15 00:32:33 +00:00
|
|
|
|
super().save()
|
2019-11-27 03:39:18 +00:00
|
|
|
|
# TODO: HACK figure out where this is being set to true
|
|
|
|
|
self.experiment.debug = self.debug
|
2020-01-21 19:26:43 +00:00
|
|
|
|
if not self.debug:
|
|
|
|
|
exp = self.experiment
|
|
|
|
|
exp.close()
|
2019-11-27 03:39:18 +00:00
|
|
|
|
|
2020-07-09 11:15:41 +00:00
|
|
|
|
@property
|
|
|
|
|
def save_dir(self) -> Optional[str]:
|
2021-08-25 19:15:00 +00:00
|
|
|
|
"""
|
|
|
|
|
Gets the save directory.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
The path to the save directory.
|
|
|
|
|
"""
|
2020-07-09 11:15:41 +00:00
|
|
|
|
return self._save_dir
|
|
|
|
|
|
2019-11-27 03:39:18 +00:00
|
|
|
|
@property
|
2020-02-25 19:52:39 +00:00
|
|
|
|
def name(self) -> str:
|
2021-08-25 19:15:00 +00:00
|
|
|
|
"""
|
|
|
|
|
Gets the experiment name.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
The experiment name if the experiment exists, else the name specified in the constructor.
|
|
|
|
|
"""
|
2019-11-27 03:39:18 +00:00
|
|
|
|
if self._experiment is None:
|
|
|
|
|
return self._name
|
2021-02-18 11:58:12 +00:00
|
|
|
|
|
|
|
|
|
return self.experiment.name
|
2019-11-27 03:39:18 +00:00
|
|
|
|
|
|
|
|
|
@property
|
2020-02-25 19:52:39 +00:00
|
|
|
|
def version(self) -> int:
|
2021-08-25 19:15:00 +00:00
|
|
|
|
"""
|
|
|
|
|
Gets the experiment version.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
The experiment version if the experiment exists, else the next version.
|
|
|
|
|
"""
|
2019-11-27 03:39:18 +00:00
|
|
|
|
if self._experiment is None:
|
|
|
|
|
return self._version
|
2021-02-18 11:58:12 +00:00
|
|
|
|
|
|
|
|
|
return self.experiment.version
|
2019-11-27 03:39:18 +00:00
|
|
|
|
|
|
|
|
|
# Test tube experiments are not pickleable, so we need to override a few
|
|
|
|
|
# methods to get DDP working. See
|
|
|
|
|
# https://docs.python.org/3/library/pickle.html#handling-stateful-objects
|
|
|
|
|
# for more info.
|
2020-02-25 19:52:39 +00:00
|
|
|
|
def __getstate__(self) -> Dict[Any, Any]:
|
2019-11-27 03:39:18 +00:00
|
|
|
|
state = self.__dict__.copy()
|
|
|
|
|
state["_experiment"] = self.experiment.get_meta_copy()
|
|
|
|
|
return state
|
|
|
|
|
|
2020-02-25 19:52:39 +00:00
|
|
|
|
def __setstate__(self, state: Dict[Any, Any]):
|
2019-11-27 03:39:18 +00:00
|
|
|
|
self._experiment = state["_experiment"].get_non_ddp_exp()
|
|
|
|
|
del state["_experiment"]
|
|
|
|
|
self.__dict__.update(state)
|