Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TensorBoardLogger sub_dir parameter for grouping logs #6195

Merged
merged 40 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
cfb9281
fixed a small typo
Feb 22, 2021
7e45bf4
cleaning up
Feb 22, 2021
da21a24
Merge branch 'master' of github.com:PyTorchLightning/pytorch-lightnin…
Feb 23, 2021
47efa56
added sub_dir argument to tensorboard and wrote test
Feb 23, 2021
8058668
sub dir arg exclusively for tensorboard, linted
Feb 24, 2021
36b8e20
resolving merge conflict
Feb 27, 2021
74a7bc2
resolved merge conflict
Feb 27, 2021
d027ca9
resolved merge conflict
janhenriklambrechts Feb 27, 2021
5728ba4
resolved merge conflict
janhenriklambrechts Feb 27, 2021
ce5855e
resolve merge conflict before revert
janhenriklambrechts Feb 27, 2021
3a5483f
resolving merge conflict
janhenriklambrechts Feb 27, 2021
372bf3c
reverted to pre-lint
janhenriklambrechts Feb 27, 2021
7aebd4f
added tensorboard sub_dir test
janhenriklambrechts Feb 27, 2021
e5bf94b
pep8 formatting
janhenriklambrechts Feb 27, 2021
1d64624
removed sub_dir arg from test_all function:
janhenriklambrechts Feb 27, 2021
da8c691
Merge remote-tracking branch 'upstream/master' into feature/logging-s…
janhenriklambrechts Feb 27, 2021
a5ebc0b
updated feature description
janhenriklambrechts Feb 27, 2021
eb31d3b
typo in doc description
janhenriklambrechts Feb 27, 2021
7791946
updated CHANGELOG
janhenriklambrechts Feb 27, 2021
ed5a7f0
Update pytorch_lightning/loggers/tensorboard.py
janhenriklambrechts Feb 27, 2021
657ba57
swapped argument position
janhenriklambrechts Mar 5, 2021
0e52ae6
Merge branch 'feature/logging-sub-dir' of github.com:janhenriklambrec…
janhenriklambrechts Mar 5, 2021
a697ca8
Merge branch 'master' into feature/logging-sub-dir
janhenriklambrechts Mar 10, 2021
73cad71
Merge branch 'master' into feature/logging-sub-dir
Borda May 11, 2021
18f4c05
added expandvars tests
janhenriklambrechts May 13, 2021
ffb87d0
added expandvars
janhenriklambrechts May 13, 2021
c2d6706
removed model init
janhenriklambrechts May 13, 2021
d28b57b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 13, 2021
ba7a085
Merge branch 'master' into feature/logging-sub-dir
s-rog May 17, 2021
8000b0d
fix tests
s-rog May 17, 2021
50b34c6
fix failed test
janhenriklambrechts May 17, 2021
11d01f0
Merge branch 'feature/logging-sub-dir' of github.com:janhenriklambrec…
janhenriklambrechts May 17, 2021
7a0bebd
Revert "fix failed test"
janhenriklambrechts May 17, 2021
fd92de4
add env var to test
s-rog May 17, 2021
5feae2d
fix typo in tests
s-rog May 17, 2021
6bb31ce
fix tests
s-rog May 17, 2021
98088cf
for test consistency
s-rog May 17, 2021
106effd
fix typo
s-rog May 17, 2021
fbe724c
fix typo 2
s-rog May 17, 2021
3fb1bec
Merge branch 'master' into feature/logging-sub-dir
s-rog May 18, 2021
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Added `clip_grad_by_value` support for TPUs ([#7025](https://github.com/PyTorchLightning/pytorch-lightning/pull/7025))


- Added `sub_dir` parameter to `TensorBoardLogger` ([#6195](https://github.com/PyTorchLightning/pytorch-lightning/pull/6195))


### Changed

- Log epoch metrics before the `on_evaluation_end` hook ([#7272](https://github.com/PyTorchLightning/pytorch-lightning/pull/7272))
Expand Down
12 changes: 11 additions & 1 deletion pytorch_lightning/loggers/tensorboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class TensorBoardLogger(LightningLoggerBase):
directory for existing versions, then automatically assigns the next available version.
If it is a string then it is used as the run-specific subdirectory name,
otherwise ``'version_${version}'`` is used.
sub_dir: Sub-directory to group TensorBoard logs. If a sub_dir argument is passed
then logs are saved in ``/save_dir/version/sub_dir/``. Defaults to ``None`` in which
janhenriklambrechts marked this conversation as resolved.
Show resolved Hide resolved
logs are saved in ``/save_dir/version/``.
log_graph: Adds the computational graph to tensorboard. This requires that
the user has defined the `self.example_input_array` attribute in their
model.
Expand All @@ -83,12 +86,14 @@ def __init__(
log_graph: bool = False,
default_hp_metric: bool = True,
prefix: str = '',
sub_dir: Optional[str] = None,
**kwargs
):
super().__init__()
self._save_dir = save_dir
self._name = name or ''
self._version = version
self._sub_dir = sub_dir
self._log_graph = log_graph
self._default_hp_metric = default_hp_metric
self._prefix = prefix
Expand Down Expand Up @@ -119,13 +124,18 @@ def log_dir(self) -> str:
"""
# create a pseudo standard path ala test-tube
version = self.version if isinstance(self.version, str) else f"version_{self.version}"
log_dir = os.path.join(self.root_dir, version)
sub_dir = self.sub_dir if isinstance(self.sub_dir, str) else ""
log_dir = os.path.join(self.root_dir, version, sub_dir)
s-rog marked this conversation as resolved.
Show resolved Hide resolved
return log_dir

@property
def save_dir(self) -> Optional[str]:
return self._save_dir

@property
def sub_dir(self) -> Optional[str]:
return self._sub_dir

@property
@rank_zero_experiment
def experiment(self) -> SummaryWriter:
Expand Down
16 changes: 8 additions & 8 deletions tests/loggers/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def _get_logger_args(logger_class, save_dir):
return logger_args


def _instantiate_logger(logger_class, save_idr, **override_kwargs):
args = _get_logger_args(logger_class, save_idr)
def _instantiate_logger(logger_class, save_dir, **override_kwargs):
args = _get_logger_args(logger_class, save_dir)
args.update(**override_kwargs)
logger = logger_class(**args)
return logger
Expand Down Expand Up @@ -368,38 +368,38 @@ def test_logger_with_prefix_all(tmpdir, monkeypatch):
with mock.patch('pytorch_lightning.loggers.comet.comet_ml'), \
mock.patch('pytorch_lightning.loggers.comet.CometOfflineExperiment'):
_patch_comet_atexit(monkeypatch)
logger = _instantiate_logger(CometLogger, save_idr=tmpdir, prefix=prefix)
logger = _instantiate_logger(CometLogger, save_dir=tmpdir, prefix=prefix)
logger.log_metrics({"test": 1.0}, step=0)
logger.experiment.log_metrics.assert_called_once_with({"tmp-test": 1.0}, epoch=None, step=0)

# MLflow
with mock.patch('pytorch_lightning.loggers.mlflow.mlflow'), \
mock.patch('pytorch_lightning.loggers.mlflow.MlflowClient'):
logger = _instantiate_logger(MLFlowLogger, save_idr=tmpdir, prefix=prefix)
logger = _instantiate_logger(MLFlowLogger, save_dir=tmpdir, prefix=prefix)
logger.log_metrics({"test": 1.0}, step=0)
logger.experiment.log_metric.assert_called_once_with(ANY, "tmp-test", 1.0, ANY, 0)

# Neptune
with mock.patch('pytorch_lightning.loggers.neptune.neptune'):
logger = _instantiate_logger(NeptuneLogger, save_idr=tmpdir, prefix=prefix)
logger = _instantiate_logger(NeptuneLogger, save_dir=tmpdir, prefix=prefix)
logger.log_metrics({"test": 1.0}, step=0)
logger.experiment.log_metric.assert_called_once_with("tmp-test", 1.0)

# TensorBoard
with mock.patch('pytorch_lightning.loggers.tensorboard.SummaryWriter'):
logger = _instantiate_logger(TensorBoardLogger, save_idr=tmpdir, prefix=prefix)
logger = _instantiate_logger(TensorBoardLogger, save_dir=tmpdir, prefix=prefix)
logger.log_metrics({"test": 1.0}, step=0)
logger.experiment.add_scalar.assert_called_once_with("tmp-test", 1.0, 0)

# TestTube
with mock.patch('pytorch_lightning.loggers.test_tube.Experiment'):
logger = _instantiate_logger(TestTubeLogger, save_idr=tmpdir, prefix=prefix)
logger = _instantiate_logger(TestTubeLogger, save_dir=tmpdir, prefix=prefix)
logger.log_metrics({"test": 1.0}, step=0)
logger.experiment.log.assert_called_once_with({"tmp-test": 1.0}, global_step=0)

# WandB
with mock.patch('pytorch_lightning.loggers.wandb.wandb') as wandb:
logger = _instantiate_logger(WandbLogger, save_idr=tmpdir, prefix=prefix)
logger = _instantiate_logger(WandbLogger, save_dir=tmpdir, prefix=prefix)
wandb.run = None
wandb.init().step = 0
logger.log_metrics({"test": 1.0}, step=0)
Expand Down
32 changes: 32 additions & 0 deletions tests/loggers/test_tensorboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,38 @@ def test_tensorboard_no_name(tmpdir, name):
assert os.listdir(tmpdir / "version_0")


def test_tensorboard_log_sub_dir(tmpdir):

class TestLogger(TensorBoardLogger):
# for reproducibility
@property
def version(self):
return "version"

@property
def name(self):
return "name"

model = BoringModel()
trainer_args = dict(
default_root_dir=tmpdir,
max_steps=1,
)

# no sub_dir specified
save_dir = tmpdir / "logs"
logger = TestLogger(save_dir)
trainer = Trainer(**trainer_args, logger=logger)
trainer.fit(model)
assert trainer.logger.log_dir[:-1] == save_dir / "name" / "version"

# sub_dir specified
logger = TestLogger(save_dir, sub_dir="sub_dir")
trainer = Trainer(**trainer_args, logger=logger)
trainer.fit(model)
assert trainer.logger.log_dir == save_dir / "name" / "version" / "sub_dir"


@pytest.mark.parametrize("step_idx", [10, None])
def test_tensorboard_log_metrics(tmpdir, step_idx):
logger = TensorBoardLogger(tmpdir)
Expand Down