-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy pathutils.py
110 lines (83 loc) · 3.38 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# 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 functools
import os
import re
from contextlib import contextmanager
from typing import Optional, Type
import numpy as np
import pytest
from pytorch_lightning import seed_everything
from pytorch_lightning.callbacks import ModelCheckpoint
from pytorch_lightning.demos.boring_classes import BoringModel
from pytorch_lightning.loggers import TensorBoardLogger
from tests_pytorch import _TEMP_PATH
def get_default_logger(save_dir, version=None):
# set up logger object without actually saving logs
logger = TensorBoardLogger(save_dir, name="lightning_logs", version=version)
return logger
def get_data_path(expt_logger, path_dir=None):
# some calls contain only experiment not complete logger
# each logger has to have these attributes
name, version = expt_logger.name, expt_logger.version
# the other experiments...
if not path_dir:
if hasattr(expt_logger, "save_dir") and expt_logger.save_dir:
path_dir = expt_logger.save_dir
else:
path_dir = _TEMP_PATH
path_expt = os.path.join(path_dir, name, "version_%s" % version)
# try if the new sub-folder exists, typical case for test-tube
if not os.path.isdir(path_expt):
path_expt = path_dir
return path_expt
def load_model_from_checkpoint(root_weights_dir, module_class=BoringModel):
trained_model = module_class.load_from_checkpoint(root_weights_dir)
assert trained_model is not None, "loading model failed"
return trained_model
def assert_ok_model_acc(trainer, key="test_acc", thr=0.5):
# this model should get 0.80+ acc
acc = trainer.callback_metrics[key]
assert acc > thr, f"Model failed to get expected {thr} accuracy. {key} = {acc}"
def reset_seed(seed=0):
seed_everything(seed)
# generate a list of random seeds for each test
RANDOM_PORTS = list(np.random.randint(12000, 19000, 1000))
def set_random_main_port():
reset_seed()
port = RANDOM_PORTS.pop()
os.environ["MASTER_PORT"] = str(port)
def init_checkpoint_callback(logger):
checkpoint = ModelCheckpoint(dirpath=logger.save_dir)
return checkpoint
def getattr_recursive(obj, attr):
return functools.reduce(getattr, [obj] + attr.split("."))
@contextmanager
def no_warning_call(expected_warning: Type[Warning] = UserWarning, match: Optional[str] = None):
with pytest.warns(None) as record:
yield
if match is None:
try:
w = record.pop(expected_warning)
except AssertionError:
# no warning raised
return
else:
for w in record.list:
if w.category is expected_warning and re.compile(match).search(w.message.args[0]):
break
else:
return
msg = "A warning" if expected_warning is None else f"`{expected_warning.__name__}`"
raise AssertionError(f"{msg} was raised: {w}")