2020-11-08 17:16: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.
|
|
|
|
import os
|
2021-01-14 12:51:20 +00:00
|
|
|
|
2021-02-02 17:06:11 +00:00
|
|
|
from pytorch_lightning import Trainer
|
|
|
|
from pytorch_lightning.callbacks import ModelCheckpoint
|
|
|
|
from pytorch_lightning.loggers import TensorBoardLogger
|
2021-02-03 07:24:46 +00:00
|
|
|
from tests.base.boring_model import BoringModel
|
2021-02-02 17:06:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestModel(BoringModel):
|
|
|
|
def __init__(self, expected_log_dir):
|
|
|
|
super().__init__()
|
|
|
|
self.expected_log_dir = expected_log_dir
|
|
|
|
|
|
|
|
def training_step(self, *args, **kwargs):
|
|
|
|
assert self.trainer.log_dir == self.expected_log_dir
|
|
|
|
return super().training_step(*args, **kwargs)
|
|
|
|
|
2020-11-08 17:16:22 +00:00
|
|
|
|
|
|
|
def test_logdir(tmpdir):
|
|
|
|
"""
|
|
|
|
Tests that the path is correct when checkpoint and loggers are used
|
|
|
|
"""
|
2021-02-02 17:06:11 +00:00
|
|
|
expected = os.path.join(tmpdir, 'lightning_logs', 'version_0')
|
2020-11-08 17:16:22 +00:00
|
|
|
|
2021-02-02 17:06:11 +00:00
|
|
|
model = TestModel(expected)
|
2020-11-08 17:16:22 +00:00
|
|
|
|
|
|
|
trainer = Trainer(
|
|
|
|
default_root_dir=tmpdir,
|
2021-02-02 17:06:11 +00:00
|
|
|
max_steps=2,
|
|
|
|
callbacks=[ModelCheckpoint(dirpath=tmpdir)],
|
2020-11-08 17:16:22 +00:00
|
|
|
)
|
|
|
|
|
2021-02-02 17:06:11 +00:00
|
|
|
assert trainer.log_dir == expected
|
2020-11-08 17:16:22 +00:00
|
|
|
trainer.fit(model)
|
2021-02-02 17:06:11 +00:00
|
|
|
assert trainer.log_dir == expected
|
2020-11-08 17:16:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_logdir_no_checkpoint_cb(tmpdir):
|
|
|
|
"""
|
|
|
|
Tests that the path is correct with no checkpoint
|
|
|
|
"""
|
2021-02-02 17:06:11 +00:00
|
|
|
expected = os.path.join(tmpdir, 'lightning_logs', 'version_0')
|
|
|
|
model = TestModel(expected)
|
2020-11-08 17:16:22 +00:00
|
|
|
|
|
|
|
trainer = Trainer(
|
|
|
|
default_root_dir=tmpdir,
|
2021-02-02 17:06:11 +00:00
|
|
|
max_steps=2,
|
2020-11-08 17:16:22 +00:00
|
|
|
checkpoint_callback=False
|
|
|
|
)
|
|
|
|
|
2021-02-02 17:06:11 +00:00
|
|
|
assert trainer.log_dir == expected
|
2020-11-08 17:16:22 +00:00
|
|
|
trainer.fit(model)
|
2021-02-02 17:06:11 +00:00
|
|
|
assert trainer.log_dir == expected
|
2020-11-08 17:16:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_logdir_no_logger(tmpdir):
|
|
|
|
"""
|
|
|
|
Tests that the path is correct even when there is no logger
|
|
|
|
"""
|
2021-02-02 17:06:11 +00:00
|
|
|
expected = os.path.join(tmpdir)
|
|
|
|
model = TestModel(expected)
|
2020-11-08 17:16:22 +00:00
|
|
|
|
|
|
|
trainer = Trainer(
|
|
|
|
default_root_dir=tmpdir,
|
2021-02-02 17:06:11 +00:00
|
|
|
max_steps=2,
|
2020-11-08 17:16:22 +00:00
|
|
|
logger=False,
|
2021-02-02 17:06:11 +00:00
|
|
|
callbacks=[ModelCheckpoint(dirpath=tmpdir)],
|
2020-11-08 17:16:22 +00:00
|
|
|
)
|
|
|
|
|
2021-02-02 17:06:11 +00:00
|
|
|
assert trainer.log_dir == expected
|
2020-11-08 17:16:22 +00:00
|
|
|
trainer.fit(model)
|
2021-02-02 17:06:11 +00:00
|
|
|
assert trainer.log_dir == expected
|
2020-11-08 17:16:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_logdir_no_logger_no_checkpoint(tmpdir):
|
|
|
|
"""
|
|
|
|
Tests that the path is correct even when there is no logger
|
|
|
|
"""
|
2021-02-02 17:06:11 +00:00
|
|
|
expected = os.path.join(tmpdir)
|
|
|
|
model = TestModel(expected)
|
2020-11-08 17:16:22 +00:00
|
|
|
|
|
|
|
trainer = Trainer(
|
|
|
|
default_root_dir=tmpdir,
|
2021-02-02 17:06:11 +00:00
|
|
|
max_steps=2,
|
2020-11-08 17:16:22 +00:00
|
|
|
logger=False,
|
|
|
|
checkpoint_callback=False
|
|
|
|
)
|
|
|
|
|
2021-02-02 17:06:11 +00:00
|
|
|
assert trainer.log_dir == expected
|
|
|
|
trainer.fit(model)
|
|
|
|
assert trainer.log_dir == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_logdir_custom_callback(tmpdir):
|
|
|
|
"""
|
|
|
|
Tests that the path is correct even when there is a custom callback
|
|
|
|
"""
|
|
|
|
expected = os.path.join(tmpdir, 'lightning_logs', 'version_0')
|
|
|
|
model = TestModel(expected)
|
|
|
|
|
|
|
|
trainer = Trainer(
|
|
|
|
default_root_dir=tmpdir,
|
|
|
|
max_steps=2,
|
|
|
|
callbacks=[ModelCheckpoint(dirpath=os.path.join(tmpdir, 'ckpts'))],
|
|
|
|
)
|
|
|
|
|
|
|
|
assert trainer.log_dir == expected
|
|
|
|
trainer.fit(model)
|
|
|
|
assert trainer.log_dir == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_logdir_custom_logger(tmpdir):
|
|
|
|
"""
|
|
|
|
Tests that the path is correct even when there is a custom logger
|
|
|
|
"""
|
|
|
|
expected = os.path.join(tmpdir, 'custom_logs', 'version_0')
|
|
|
|
model = TestModel(expected)
|
|
|
|
|
|
|
|
trainer = Trainer(
|
|
|
|
default_root_dir=tmpdir,
|
|
|
|
max_steps=2,
|
|
|
|
callbacks=[ModelCheckpoint(dirpath=tmpdir)],
|
|
|
|
logger=TensorBoardLogger(save_dir=tmpdir, name='custom_logs')
|
|
|
|
)
|
|
|
|
|
|
|
|
assert trainer.log_dir == expected
|
2020-11-08 17:16:22 +00:00
|
|
|
trainer.fit(model)
|
2021-02-02 17:06:11 +00:00
|
|
|
assert trainer.log_dir == expected
|