2020-03-14 17:02:05 +00:00
|
|
|
from argparse import Namespace
|
2020-03-03 01:49:14 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
import torch
|
|
|
|
|
2020-03-19 13:14:29 +00:00
|
|
|
from pytorch_lightning.loggers import TensorBoardLogger
|
2020-03-03 01:49:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_tensorboard_automatic_versioning(tmpdir):
|
|
|
|
"""Verify that automatic versioning works"""
|
|
|
|
|
|
|
|
root_dir = tmpdir.mkdir("tb_versioning")
|
|
|
|
root_dir.mkdir("version_0")
|
|
|
|
root_dir.mkdir("version_1")
|
|
|
|
|
|
|
|
logger = TensorBoardLogger(save_dir=tmpdir, name="tb_versioning")
|
|
|
|
|
|
|
|
assert logger.version == 2
|
|
|
|
|
|
|
|
|
|
|
|
def test_tensorboard_manual_versioning(tmpdir):
|
|
|
|
"""Verify that manual versioning works"""
|
|
|
|
|
|
|
|
root_dir = tmpdir.mkdir("tb_versioning")
|
|
|
|
root_dir.mkdir("version_0")
|
|
|
|
root_dir.mkdir("version_1")
|
|
|
|
root_dir.mkdir("version_2")
|
|
|
|
|
|
|
|
logger = TensorBoardLogger(save_dir=tmpdir, name="tb_versioning", version=1)
|
|
|
|
|
|
|
|
assert logger.version == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_tensorboard_named_version(tmpdir):
|
|
|
|
"""Verify that manual versioning works for string versions, e.g. '2020-02-05-162402' """
|
|
|
|
|
|
|
|
tmpdir.mkdir("tb_versioning")
|
|
|
|
expected_version = "2020-02-05-162402"
|
|
|
|
|
|
|
|
logger = TensorBoardLogger(save_dir=tmpdir, name="tb_versioning", version=expected_version)
|
|
|
|
logger.log_hyperparams({"a": 1, "b": 2}) # Force data to be written
|
|
|
|
|
|
|
|
assert logger.version == expected_version
|
|
|
|
# Could also test existence of the directory but this fails
|
|
|
|
# in the "minimum requirements" test setup
|
|
|
|
|
|
|
|
|
2020-04-15 00:32:33 +00:00
|
|
|
@pytest.mark.parametrize("name", ['', None])
|
|
|
|
def test_tensorboard_no_name(tmpdir, name):
|
2020-03-03 01:49:14 +00:00
|
|
|
"""Verify that None or empty name works"""
|
2020-04-15 00:32:33 +00:00
|
|
|
logger = TensorBoardLogger(save_dir=tmpdir, name=name)
|
2020-03-03 01:49:14 +00:00
|
|
|
assert logger.root_dir == tmpdir
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("step_idx", [10, None])
|
|
|
|
def test_tensorboard_log_metrics(tmpdir, step_idx):
|
|
|
|
logger = TensorBoardLogger(tmpdir)
|
|
|
|
metrics = {
|
|
|
|
"float": 0.3,
|
|
|
|
"int": 1,
|
|
|
|
"FloatTensor": torch.tensor(0.1),
|
|
|
|
"IntTensor": torch.tensor(1)
|
|
|
|
}
|
|
|
|
logger.log_metrics(metrics, step_idx)
|
|
|
|
|
|
|
|
|
|
|
|
def test_tensorboard_log_hyperparams(tmpdir):
|
|
|
|
logger = TensorBoardLogger(tmpdir)
|
|
|
|
hparams = {
|
|
|
|
"float": 0.3,
|
|
|
|
"int": 1,
|
|
|
|
"string": "abc",
|
2020-03-14 17:02:05 +00:00
|
|
|
"bool": True,
|
2020-03-19 13:15:47 +00:00
|
|
|
"dict": {'a': {'b': 'c'}},
|
2020-03-14 17:02:05 +00:00
|
|
|
"list": [1, 2, 3],
|
2020-03-19 13:15:47 +00:00
|
|
|
"namespace": Namespace(foo=Namespace(bar='buzz')),
|
2020-03-14 17:02:05 +00:00
|
|
|
"layer": torch.nn.BatchNorm1d
|
2020-03-03 01:49:14 +00:00
|
|
|
}
|
|
|
|
logger.log_hyperparams(hparams)
|
2020-04-27 07:52:31 +00:00
|
|
|
|
|
|
|
|
2020-04-27 07:53:59 +00:00
|
|
|
def test_tensorboard_log_hparams_and_metrics(tmpdir):
|
2020-04-27 07:50:01 +00:00
|
|
|
logger = TensorBoardLogger(tmpdir)
|
|
|
|
hparams = {
|
|
|
|
"float": 0.3,
|
|
|
|
"int": 1,
|
|
|
|
"string": "abc",
|
|
|
|
"bool": True,
|
|
|
|
"dict": {'a': {'b': 'c'}},
|
|
|
|
"list": [1, 2, 3],
|
|
|
|
"namespace": Namespace(foo=Namespace(bar='buzz')),
|
|
|
|
"layer": torch.nn.BatchNorm1d
|
|
|
|
}
|
|
|
|
metrics = {'abc': torch.tensor([0.54])}
|
|
|
|
logger.log_hyperparams(hparams, metrics)
|