2020-03-14 17:02:40 +00:00
|
|
|
from unittest.mock import patch, MagicMock
|
2020-03-03 01:49:14 +00:00
|
|
|
|
2020-03-12 16:41:37 +00:00
|
|
|
import torch
|
|
|
|
|
2020-03-03 01:49:14 +00:00
|
|
|
from pytorch_lightning import Trainer
|
|
|
|
from pytorch_lightning.loggers import NeptuneLogger
|
2020-05-04 11:13:52 +00:00
|
|
|
from tests.base import EvalModelTemplate
|
2020-03-03 01:49:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
@patch('pytorch_lightning.loggers.neptune.neptune')
|
|
|
|
def test_neptune_online(neptune):
|
2020-05-10 17:19:18 +00:00
|
|
|
logger = NeptuneLogger(api_key='test', project_name='project')
|
2020-03-03 01:49:14 +00:00
|
|
|
|
2020-05-10 17:19:18 +00:00
|
|
|
created_experiment = neptune.Session.with_default_backend().get_project().create_experiment()
|
|
|
|
|
|
|
|
# It's important to check if the internal variable _experiment was initialized in __init__.
|
|
|
|
# Calling logger.experiment would cause a side-effect of initializing _experiment,
|
|
|
|
# if it wasn't already initialized.
|
|
|
|
assert logger._experiment == created_experiment
|
|
|
|
assert logger.name == created_experiment.name
|
|
|
|
assert logger.version == created_experiment.id
|
2020-03-03 01:49:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
@patch('pytorch_lightning.loggers.neptune.neptune')
|
2020-05-10 17:19:18 +00:00
|
|
|
def test_neptune_offline(neptune):
|
2020-03-03 01:49:14 +00:00
|
|
|
logger = NeptuneLogger(offline_mode=True)
|
|
|
|
|
2020-05-10 17:19:18 +00:00
|
|
|
neptune.Session.assert_called_once_with(backend=neptune.OfflineBackend())
|
|
|
|
assert logger.experiment == neptune.Session().get_project().create_experiment()
|
|
|
|
|
|
|
|
|
|
|
|
@patch('pytorch_lightning.loggers.neptune.neptune')
|
|
|
|
def test_neptune_additional_methods(neptune):
|
|
|
|
logger = NeptuneLogger(api_key='test', project_name='project')
|
|
|
|
|
|
|
|
created_experiment = neptune.Session.with_default_backend().get_project().create_experiment()
|
|
|
|
|
2020-03-03 01:49:14 +00:00
|
|
|
logger.log_metric('test', torch.ones(1))
|
2020-05-10 17:19:18 +00:00
|
|
|
created_experiment.log_metric.assert_called_once_with('test', torch.ones(1))
|
|
|
|
created_experiment.log_metric.reset_mock()
|
2020-03-03 01:49:14 +00:00
|
|
|
|
|
|
|
logger.log_metric('test', 1.0)
|
2020-05-10 17:19:18 +00:00
|
|
|
created_experiment.log_metric.assert_called_once_with('test', 1.0)
|
|
|
|
created_experiment.log_metric.reset_mock()
|
2020-03-03 01:49:14 +00:00
|
|
|
|
|
|
|
logger.log_metric('test', 1.0, step=2)
|
2020-05-10 17:19:18 +00:00
|
|
|
created_experiment.log_metric.assert_called_once_with('test', x=2, y=1.0)
|
|
|
|
created_experiment.log_metric.reset_mock()
|
2020-03-03 01:49:14 +00:00
|
|
|
|
|
|
|
logger.log_text('test', 'text')
|
2020-05-10 17:19:18 +00:00
|
|
|
created_experiment.log_metric.assert_called_once_with('test', 'text')
|
|
|
|
created_experiment.log_metric.reset_mock()
|
2020-03-03 01:49:14 +00:00
|
|
|
|
|
|
|
logger.log_image('test', 'image file')
|
2020-05-10 17:19:18 +00:00
|
|
|
created_experiment.log_image.assert_called_once_with('test', 'image file')
|
|
|
|
created_experiment.log_image.reset_mock()
|
2020-03-03 01:49:14 +00:00
|
|
|
|
|
|
|
logger.log_image('test', 'image file', step=2)
|
2020-05-10 17:19:18 +00:00
|
|
|
created_experiment.log_image.assert_called_once_with('test', x=2, y='image file')
|
|
|
|
created_experiment.log_image.reset_mock()
|
2020-03-03 01:49:14 +00:00
|
|
|
|
|
|
|
logger.log_artifact('file')
|
2020-05-10 17:19:18 +00:00
|
|
|
created_experiment.log_artifact.assert_called_once_with('file', None)
|
2020-03-03 01:49:14 +00:00
|
|
|
|
|
|
|
logger.set_property('property', 10)
|
2020-05-10 17:19:18 +00:00
|
|
|
created_experiment.set_property.assert_called_once_with('property', 10)
|
2020-03-03 01:49:14 +00:00
|
|
|
|
|
|
|
logger.append_tags('one tag')
|
2020-05-10 17:19:18 +00:00
|
|
|
created_experiment.append_tags.assert_called_once_with('one tag')
|
|
|
|
created_experiment.append_tags.reset_mock()
|
2020-03-03 01:49:14 +00:00
|
|
|
|
|
|
|
logger.append_tags(['two', 'tags'])
|
2020-05-10 17:19:18 +00:00
|
|
|
created_experiment.append_tags.assert_called_once_with('two', 'tags')
|
2020-03-03 01:49:14 +00:00
|
|
|
|
|
|
|
|
2020-03-14 17:02:40 +00:00
|
|
|
def test_neptune_leave_open_experiment_after_fit(tmpdir):
|
|
|
|
"""Verify that neptune experiment was closed after training"""
|
2020-05-10 17:15:28 +00:00
|
|
|
model = EvalModelTemplate()
|
2020-03-14 17:02:40 +00:00
|
|
|
|
|
|
|
def _run_training(logger):
|
|
|
|
logger._experiment = MagicMock()
|
2020-05-01 14:43:58 +00:00
|
|
|
trainer = Trainer(
|
2020-04-10 16:02:59 +00:00
|
|
|
default_root_dir=tmpdir,
|
2020-03-14 17:02:40 +00:00
|
|
|
max_epochs=1,
|
|
|
|
train_percent_check=0.05,
|
|
|
|
logger=logger
|
|
|
|
)
|
|
|
|
trainer.fit(model)
|
|
|
|
return logger
|
|
|
|
|
|
|
|
logger_close_after_fit = _run_training(NeptuneLogger(offline_mode=True))
|
|
|
|
assert logger_close_after_fit._experiment.stop.call_count == 1
|
|
|
|
|
2020-04-15 00:32:33 +00:00
|
|
|
logger_open_after_fit = _run_training(NeptuneLogger(offline_mode=True, close_after_fit=False))
|
2020-03-14 17:02:40 +00:00
|
|
|
assert logger_open_after_fit._experiment.stop.call_count == 0
|