2020-10-13 11:18:07 +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.
|
2020-06-27 01:38:25 +00:00
|
|
|
import torch
|
|
|
|
|
|
|
|
from pytorch_lightning import Trainer
|
2021-01-12 00:36:48 +00:00
|
|
|
from pytorch_lightning.trainer.states import TrainerState
|
2021-01-12 10:22:37 +00:00
|
|
|
from pytorch_lightning.utilities import DistributedType
|
2021-02-09 10:10:52 +00:00
|
|
|
from tests.helpers import BoringModel
|
2021-02-08 10:52:02 +00:00
|
|
|
from tests.helpers.utils import get_default_logger, load_model_from_checkpoint, reset_seed
|
2020-06-27 01:38:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
def run_model_test_without_loggers(trainer_options, model, min_acc: float = 0.50):
|
|
|
|
reset_seed()
|
|
|
|
|
|
|
|
# fit model
|
|
|
|
trainer = Trainer(**trainer_options)
|
2021-01-12 00:36:48 +00:00
|
|
|
trainer.fit(model)
|
2020-06-27 01:38:25 +00:00
|
|
|
|
|
|
|
# correct result and ok accuracy
|
2021-01-12 00:36:48 +00:00
|
|
|
assert trainer.state == TrainerState.FINISHED, f"Training failed with {trainer.state}"
|
2020-06-27 01:38:25 +00:00
|
|
|
|
|
|
|
pretrained_model = load_model_from_checkpoint(
|
2021-02-06 13:22:10 +00:00
|
|
|
trainer.logger, trainer.checkpoint_callback.best_model_path, type(model)
|
2020-06-27 01:38:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# test new model accuracy
|
|
|
|
test_loaders = model.test_dataloader()
|
|
|
|
if not isinstance(test_loaders, list):
|
|
|
|
test_loaders = [test_loaders]
|
|
|
|
|
|
|
|
for dataloader in test_loaders:
|
2021-01-07 10:50:08 +00:00
|
|
|
run_prediction(pretrained_model, dataloader, min_acc=min_acc)
|
2020-06-27 01:38:25 +00:00
|
|
|
|
2021-01-12 10:22:37 +00:00
|
|
|
if trainer._distrib_type in (DistributedType.DDP, DistributedType.DDP_SPAWN):
|
2020-06-27 01:38:25 +00:00
|
|
|
# on hpc this would work fine... but need to hack it for the purpose of the test
|
|
|
|
trainer.model = pretrained_model
|
|
|
|
trainer.optimizers, trainer.lr_schedulers = pretrained_model.configure_optimizers()
|
|
|
|
|
|
|
|
|
2021-02-06 13:22:10 +00:00
|
|
|
def run_model_test(
|
|
|
|
trainer_options, model, on_gpu: bool = True, version=None, with_hpc: bool = True, min_acc: float = 0.25
|
|
|
|
):
|
2020-07-07 16:24:56 +00:00
|
|
|
|
2020-06-27 01:38:25 +00:00
|
|
|
reset_seed()
|
|
|
|
save_dir = trainer_options['default_root_dir']
|
|
|
|
|
|
|
|
# logger file to get meta
|
|
|
|
logger = get_default_logger(save_dir, version=version)
|
|
|
|
trainer_options.update(logger=logger)
|
|
|
|
|
|
|
|
trainer = Trainer(**trainer_options)
|
2020-09-29 13:38:09 +00:00
|
|
|
initial_values = torch.tensor([torch.sum(torch.abs(x)) for x in model.parameters()])
|
2021-01-12 00:36:48 +00:00
|
|
|
trainer.fit(model)
|
2020-09-29 13:38:09 +00:00
|
|
|
post_train_values = torch.tensor([torch.sum(torch.abs(x)) for x in model.parameters()])
|
2020-06-27 01:38:25 +00:00
|
|
|
|
2021-01-12 00:36:48 +00:00
|
|
|
assert trainer.state == TrainerState.FINISHED, f"Training failed with {trainer.state}"
|
2020-09-29 13:38:09 +00:00
|
|
|
# Check that the model is actually changed post-training
|
2021-01-07 10:50:08 +00:00
|
|
|
change_ratio = torch.norm(initial_values - post_train_values)
|
|
|
|
assert change_ratio > 0.1, f"the model is changed of {change_ratio}"
|
2020-06-27 01:38:25 +00:00
|
|
|
|
|
|
|
# test model loading
|
2021-01-07 10:50:08 +00:00
|
|
|
pretrained_model = load_model_from_checkpoint(logger, trainer.checkpoint_callback.best_model_path, type(model))
|
2020-06-27 01:38:25 +00:00
|
|
|
|
|
|
|
# test new model accuracy
|
|
|
|
test_loaders = model.test_dataloader()
|
|
|
|
if not isinstance(test_loaders, list):
|
|
|
|
test_loaders = [test_loaders]
|
|
|
|
|
2020-07-07 18:54:07 +00:00
|
|
|
for dataloader in test_loaders:
|
2021-01-07 10:50:08 +00:00
|
|
|
run_prediction(pretrained_model, dataloader, min_acc=min_acc)
|
2020-06-27 01:38:25 +00:00
|
|
|
|
|
|
|
if with_hpc:
|
2021-01-12 10:22:37 +00:00
|
|
|
if trainer._distrib_type in (DistributedType.DDP, DistributedType.DDP_SPAWN, DistributedType.DDP2):
|
2020-06-27 01:38:25 +00:00
|
|
|
# on hpc this would work fine... but need to hack it for the purpose of the test
|
|
|
|
trainer.model = pretrained_model
|
2021-01-07 10:50:08 +00:00
|
|
|
trainer.optimizers, trainer.lr_schedulers, trainer.optimizer_frequencies = trainer.init_optimizers(
|
|
|
|
pretrained_model
|
|
|
|
)
|
2020-06-27 01:38:25 +00:00
|
|
|
|
2020-12-13 16:13:50 +00:00
|
|
|
# test HPC saving
|
2020-09-12 12:42:27 +00:00
|
|
|
trainer.checkpoint_connector.hpc_save(save_dir, logger)
|
2020-12-13 16:13:50 +00:00
|
|
|
# test HPC loading
|
|
|
|
checkpoint_path = trainer.checkpoint_connector.get_max_ckpt_path_from_folder(save_dir)
|
|
|
|
trainer.checkpoint_connector.hpc_load(checkpoint_path, on_gpu=on_gpu)
|
2020-06-27 01:38:25 +00:00
|
|
|
|
|
|
|
|
2021-01-07 10:50:08 +00:00
|
|
|
def run_prediction(trained_model, dataloader, dp=False, min_acc=0.25):
|
|
|
|
if isinstance(trained_model, BoringModel):
|
|
|
|
return _boring_model_run_prediction(trained_model, dataloader, dp, min_acc)
|
|
|
|
else:
|
|
|
|
return _eval_model_template_run_prediction(trained_model, dataloader, dp, min_acc)
|
|
|
|
|
|
|
|
|
|
|
|
def _eval_model_template_run_prediction(trained_model, dataloader, dp=False, min_acc=0.50):
|
2020-06-27 01:38:25 +00:00
|
|
|
# run prediction on 1 batch
|
2020-07-07 18:54:07 +00:00
|
|
|
batch = next(iter(dataloader))
|
2020-06-27 01:38:25 +00:00
|
|
|
x, y = batch
|
|
|
|
x = x.view(x.size(0), -1)
|
|
|
|
|
|
|
|
if dp:
|
2020-07-09 00:33:48 +00:00
|
|
|
with torch.no_grad():
|
|
|
|
output = trained_model(batch, 0)
|
2021-01-07 10:50:08 +00:00
|
|
|
acc = output['val_acc']
|
2020-06-27 01:38:25 +00:00
|
|
|
acc = torch.mean(acc).item()
|
|
|
|
|
|
|
|
else:
|
2020-07-09 00:33:48 +00:00
|
|
|
with torch.no_grad():
|
|
|
|
y_hat = trained_model(x)
|
|
|
|
y_hat = y_hat.cpu()
|
2020-06-27 01:38:25 +00:00
|
|
|
|
|
|
|
# acc
|
|
|
|
labels_hat = torch.argmax(y_hat, dim=1)
|
2020-07-09 00:33:48 +00:00
|
|
|
|
|
|
|
y = y.cpu()
|
2020-06-27 01:38:25 +00:00
|
|
|
acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)
|
|
|
|
acc = torch.tensor(acc)
|
|
|
|
acc = acc.item()
|
|
|
|
|
|
|
|
assert acc >= min_acc, f"This model is expected to get > {min_acc} in test set (it got {acc})"
|
2021-01-07 10:50:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _boring_model_run_prediction(trained_model, dataloader, dp=False, min_acc=0.25):
|
|
|
|
# run prediction on 1 batch
|
|
|
|
batch = next(iter(dataloader))
|
|
|
|
with torch.no_grad():
|
|
|
|
output = trained_model(batch)
|
|
|
|
acc = trained_model.loss(batch, output)
|
|
|
|
|
|
|
|
assert acc >= min_acc, f"This model is expected to get, {min_acc} in test set but got {acc}"
|