2020-03-03 20:16:57 +00:00
|
|
|
import glob
|
2020-02-01 20:47:58 +00:00
|
|
|
import logging as log
|
2019-12-04 11:48:53 +00:00
|
|
|
import os
|
2020-06-08 11:19:34 +00:00
|
|
|
import pickle
|
2019-10-23 10:10:13 +00:00
|
|
|
|
2020-06-09 20:51:30 +00:00
|
|
|
import cloudpickle
|
2020-02-25 15:36:44 +00:00
|
|
|
import pytest
|
2019-10-23 10:10:13 +00:00
|
|
|
import torch
|
|
|
|
|
2020-06-27 01:38:25 +00:00
|
|
|
import tests.base.develop_pipelines as tpipes
|
|
|
|
import tests.base.develop_utils as tutils
|
2019-10-23 10:10:13 +00:00
|
|
|
from pytorch_lightning import Trainer
|
|
|
|
from pytorch_lightning.callbacks import ModelCheckpoint
|
2020-05-04 15:38:08 +00:00
|
|
|
from tests.base import EvalModelTemplate
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
|
2020-03-30 22:29:23 +00:00
|
|
|
@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine")
|
2020-07-09 00:33:48 +00:00
|
|
|
def test_running_test_pretrained_model_distrib_dp(tmpdir):
|
2019-12-04 11:48:53 +00:00
|
|
|
"""Verify `test()` on pretrained model."""
|
2019-11-28 17:06:05 +00:00
|
|
|
tutils.set_random_master_port()
|
2019-10-23 10:10:13 +00:00
|
|
|
|
2020-05-10 17:15:28 +00:00
|
|
|
model = EvalModelTemplate()
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
# exp file to get meta
|
2020-04-22 00:33:10 +00:00
|
|
|
logger = tutils.get_default_logger(tmpdir)
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
# exp file to get weights
|
2019-11-28 17:06:05 +00:00
|
|
|
checkpoint = tutils.init_checkpoint_callback(logger)
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
trainer_options = dict(
|
2020-04-02 22:53:00 +00:00
|
|
|
progress_bar_refresh_rate=0,
|
2020-04-22 00:33:10 +00:00
|
|
|
max_epochs=2,
|
2020-06-17 17:42:28 +00:00
|
|
|
limit_train_batches=0.4,
|
2020-06-17 12:03:28 +00:00
|
|
|
limit_val_batches=0.2,
|
2019-10-23 10:10:13 +00:00
|
|
|
checkpoint_callback=checkpoint,
|
|
|
|
logger=logger,
|
|
|
|
gpus=[0, 1],
|
2020-07-09 00:33:48 +00:00
|
|
|
distributed_backend='dp',
|
2020-07-28 13:47:53 +00:00
|
|
|
default_root_dir=tmpdir,
|
2020-07-09 00:33:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# fit model
|
|
|
|
trainer = Trainer(**trainer_options)
|
|
|
|
result = trainer.fit(model)
|
|
|
|
|
|
|
|
# correct result and ok accuracy
|
|
|
|
assert result == 1, 'training failed to complete'
|
|
|
|
pretrained_model = EvalModelTemplate.load_from_checkpoint(trainer.checkpoint_callback.best_model_path)
|
|
|
|
|
|
|
|
# run test set
|
|
|
|
new_trainer = Trainer(**trainer_options)
|
|
|
|
results = new_trainer.test(pretrained_model)
|
|
|
|
pretrained_model.cpu()
|
|
|
|
|
|
|
|
# test we have good test accuracy
|
2020-07-14 18:20:45 +00:00
|
|
|
acc = results[0]['test_acc']
|
2020-07-09 00:33:48 +00:00
|
|
|
assert acc > 0.5, f"Model failed to get expected {0.5} accuracy. test_acc = {acc}"
|
|
|
|
|
|
|
|
dataloaders = model.test_dataloader()
|
|
|
|
if not isinstance(dataloaders, list):
|
|
|
|
dataloaders = [dataloaders]
|
|
|
|
|
|
|
|
for dataloader in dataloaders:
|
|
|
|
tpipes.run_prediction(dataloader, pretrained_model)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine")
|
|
|
|
def test_running_test_pretrained_model_distrib_ddp_spawn(tmpdir):
|
|
|
|
"""Verify `test()` on pretrained model."""
|
|
|
|
tutils.set_random_master_port()
|
|
|
|
|
|
|
|
model = EvalModelTemplate()
|
|
|
|
|
|
|
|
# exp file to get meta
|
|
|
|
logger = tutils.get_default_logger(tmpdir)
|
|
|
|
|
|
|
|
# exp file to get weights
|
|
|
|
checkpoint = tutils.init_checkpoint_callback(logger)
|
|
|
|
|
|
|
|
trainer_options = dict(
|
|
|
|
progress_bar_refresh_rate=0,
|
|
|
|
max_epochs=2,
|
|
|
|
limit_train_batches=0.4,
|
|
|
|
limit_val_batches=0.2,
|
|
|
|
checkpoint_callback=checkpoint,
|
|
|
|
logger=logger,
|
|
|
|
gpus=[0, 1],
|
|
|
|
distributed_backend='ddp_spawn',
|
2020-07-28 13:47:53 +00:00
|
|
|
default_root_dir=tmpdir,
|
2019-10-23 10:10:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# fit model
|
|
|
|
trainer = Trainer(**trainer_options)
|
|
|
|
result = trainer.fit(model)
|
|
|
|
|
2020-02-01 20:47:58 +00:00
|
|
|
log.info(os.listdir(tutils.get_data_path(logger, path_dir=tmpdir)))
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
# correct result and ok accuracy
|
|
|
|
assert result == 1, 'training failed to complete'
|
2020-07-09 00:33:48 +00:00
|
|
|
pretrained_model = EvalModelTemplate.load_from_checkpoint(trainer.checkpoint_callback.best_model_path)
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
# run test set
|
|
|
|
new_trainer = Trainer(**trainer_options)
|
2020-07-09 00:33:48 +00:00
|
|
|
results = new_trainer.test(pretrained_model)
|
|
|
|
pretrained_model.cpu()
|
2019-10-23 10:10:13 +00:00
|
|
|
|
2020-07-14 18:20:45 +00:00
|
|
|
acc = results[0]['test_acc']
|
2020-07-09 00:33:48 +00:00
|
|
|
assert acc > 0.5, f"Model failed to get expected {0.5} accuracy. test_acc = {acc}"
|
2020-04-22 00:33:10 +00:00
|
|
|
|
2020-02-25 03:23:25 +00:00
|
|
|
dataloaders = model.test_dataloader()
|
|
|
|
if not isinstance(dataloaders, list):
|
|
|
|
dataloaders = [dataloaders]
|
|
|
|
|
|
|
|
for dataloader in dataloaders:
|
2020-06-27 01:38:25 +00:00
|
|
|
tpipes.run_prediction(dataloader, pretrained_model)
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
|
2020-04-22 00:33:10 +00:00
|
|
|
def test_running_test_pretrained_model_cpu(tmpdir):
|
2020-02-01 23:44:05 +00:00
|
|
|
"""Verify test() on pretrained model."""
|
2020-05-10 17:15:28 +00:00
|
|
|
model = EvalModelTemplate()
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
# logger file to get meta
|
2020-04-22 00:33:10 +00:00
|
|
|
logger = tutils.get_default_logger(tmpdir)
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
# logger file to get weights
|
2019-11-28 17:06:05 +00:00
|
|
|
checkpoint = tutils.init_checkpoint_callback(logger)
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
trainer_options = dict(
|
2020-04-02 22:53:00 +00:00
|
|
|
progress_bar_refresh_rate=0,
|
2020-06-01 15:00:32 +00:00
|
|
|
max_epochs=3,
|
2020-06-17 17:42:28 +00:00
|
|
|
limit_train_batches=0.4,
|
2020-06-17 12:03:28 +00:00
|
|
|
limit_val_batches=0.2,
|
2019-10-23 10:10:13 +00:00
|
|
|
checkpoint_callback=checkpoint,
|
2020-06-27 01:38:25 +00:00
|
|
|
logger=logger,
|
2020-07-28 13:47:53 +00:00
|
|
|
default_root_dir=tmpdir,
|
2019-10-23 10:10:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# fit model
|
|
|
|
trainer = Trainer(**trainer_options)
|
|
|
|
result = trainer.fit(model)
|
|
|
|
|
|
|
|
# correct result and ok accuracy
|
|
|
|
assert result == 1, 'training failed to complete'
|
2020-07-07 16:24:56 +00:00
|
|
|
pretrained_model = EvalModelTemplate.load_from_checkpoint(trainer.checkpoint_callback.best_model_path)
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
new_trainer = Trainer(**trainer_options)
|
|
|
|
new_trainer.test(pretrained_model)
|
|
|
|
|
|
|
|
# test we have good test accuracy
|
2020-01-17 10:50:26 +00:00
|
|
|
tutils.assert_ok_model_acc(new_trainer)
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
|
2019-12-03 13:01:04 +00:00
|
|
|
def test_load_model_from_checkpoint(tmpdir):
|
2020-02-01 23:44:05 +00:00
|
|
|
"""Verify test() on pretrained model."""
|
2020-05-10 17:15:28 +00:00
|
|
|
hparams = EvalModelTemplate.get_default_hparams()
|
2020-05-24 22:59:08 +00:00
|
|
|
model = EvalModelTemplate(**hparams)
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
trainer_options = dict(
|
2020-04-02 22:53:00 +00:00
|
|
|
progress_bar_refresh_rate=0,
|
2020-01-14 03:25:27 +00:00
|
|
|
max_epochs=2,
|
2020-06-17 17:42:28 +00:00
|
|
|
limit_train_batches=0.4,
|
2020-06-17 12:03:28 +00:00
|
|
|
limit_val_batches=0.2,
|
2020-01-14 03:20:01 +00:00
|
|
|
checkpoint_callback=ModelCheckpoint(tmpdir, save_top_k=-1),
|
2020-04-10 16:02:59 +00:00
|
|
|
default_root_dir=tmpdir,
|
2019-10-23 10:10:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# fit model
|
|
|
|
trainer = Trainer(**trainer_options)
|
|
|
|
result = trainer.fit(model)
|
2020-06-15 12:02:37 +00:00
|
|
|
trainer.test(ckpt_path=None)
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
# correct result and ok accuracy
|
|
|
|
assert result == 1, 'training failed to complete'
|
2020-01-14 03:25:27 +00:00
|
|
|
|
|
|
|
# load last checkpoint
|
2020-03-03 20:16:57 +00:00
|
|
|
last_checkpoint = sorted(glob.glob(os.path.join(trainer.checkpoint_callback.dirpath, "*.ckpt")))[-1]
|
2020-05-04 15:38:08 +00:00
|
|
|
pretrained_model = EvalModelTemplate.load_from_checkpoint(last_checkpoint)
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
# test that hparams loaded correctly
|
2020-05-24 22:59:08 +00:00
|
|
|
for k, v in hparams.items():
|
|
|
|
assert getattr(pretrained_model, k) == v
|
2019-10-23 10:10:13 +00:00
|
|
|
|
2020-03-02 22:12:22 +00:00
|
|
|
# assert weights are the same
|
|
|
|
for (old_name, old_p), (new_name, new_p) in zip(model.named_parameters(), pretrained_model.named_parameters()):
|
|
|
|
assert torch.all(torch.eq(old_p, new_p)), 'loaded weights are not the same as the saved weights'
|
|
|
|
|
2019-10-23 10:10:13 +00:00
|
|
|
new_trainer = Trainer(**trainer_options)
|
|
|
|
new_trainer.test(pretrained_model)
|
|
|
|
|
|
|
|
# test we have good test accuracy
|
2020-01-17 10:50:26 +00:00
|
|
|
tutils.assert_ok_model_acc(new_trainer)
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
|
2020-03-30 22:29:23 +00:00
|
|
|
@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine")
|
2019-12-03 13:01:04 +00:00
|
|
|
def test_dp_resume(tmpdir):
|
2019-12-04 11:48:53 +00:00
|
|
|
"""Make sure DP continues training correctly."""
|
2020-05-10 17:15:28 +00:00
|
|
|
hparams = EvalModelTemplate.get_default_hparams()
|
2020-05-24 22:59:08 +00:00
|
|
|
model = EvalModelTemplate(**hparams)
|
2020-05-04 15:38:08 +00:00
|
|
|
|
|
|
|
trainer_options = dict(
|
|
|
|
max_epochs=1,
|
|
|
|
gpus=2,
|
|
|
|
distributed_backend='dp',
|
2020-06-29 01:36:46 +00:00
|
|
|
default_root_dir=tmpdir,
|
2020-05-04 15:38:08 +00:00
|
|
|
)
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
# get logger
|
2020-04-22 00:33:10 +00:00
|
|
|
logger = tutils.get_default_logger(tmpdir)
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
# exp file to get weights
|
|
|
|
# logger file to get weights
|
2019-11-28 17:06:05 +00:00
|
|
|
checkpoint = tutils.init_checkpoint_callback(logger)
|
2019-10-23 10:10:13 +00:00
|
|
|
|
2020-05-04 15:38:08 +00:00
|
|
|
# add these to the trainer options
|
|
|
|
trainer_options['logger'] = logger
|
|
|
|
trainer_options['checkpoint_callback'] = checkpoint
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
# fit model
|
|
|
|
trainer = Trainer(**trainer_options)
|
|
|
|
trainer.is_slurm_managing_tasks = True
|
|
|
|
result = trainer.fit(model)
|
|
|
|
|
2020-02-22 01:27:19 +00:00
|
|
|
# track epoch before saving. Increment since we finished the current epoch, don't want to rerun
|
2020-06-20 03:39:53 +00:00
|
|
|
real_global_epoch = trainer.current_epoch + 1
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
# correct result and ok accuracy
|
|
|
|
assert result == 1, 'amp + dp model failed to complete'
|
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
# HPC LOAD/SAVE
|
|
|
|
# ---------------------------
|
|
|
|
# save
|
2019-12-04 11:48:53 +00:00
|
|
|
trainer.hpc_save(tmpdir, logger)
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
# init new trainer
|
2020-04-22 00:33:10 +00:00
|
|
|
new_logger = tutils.get_default_logger(tmpdir, version=logger.version)
|
2020-05-04 15:38:08 +00:00
|
|
|
trainer_options['logger'] = new_logger
|
|
|
|
trainer_options['checkpoint_callback'] = ModelCheckpoint(tmpdir)
|
2020-06-17 17:42:28 +00:00
|
|
|
trainer_options['limit_train_batches'] = 0.5
|
2020-06-17 12:03:28 +00:00
|
|
|
trainer_options['limit_val_batches'] = 0.2
|
2020-05-04 15:38:08 +00:00
|
|
|
trainer_options['max_epochs'] = 1
|
2019-10-23 10:10:13 +00:00
|
|
|
new_trainer = Trainer(**trainer_options)
|
|
|
|
|
|
|
|
# set the epoch start hook so we can predict before the model does the full training
|
|
|
|
def assert_good_acc():
|
|
|
|
assert new_trainer.current_epoch == real_global_epoch and new_trainer.current_epoch > 0
|
|
|
|
|
|
|
|
# if model and state loaded correctly, predictions will be good even though we
|
|
|
|
# haven't trained with the new loaded model
|
|
|
|
dp_model = new_trainer.model
|
|
|
|
dp_model.eval()
|
|
|
|
|
2020-02-25 03:23:25 +00:00
|
|
|
dataloader = trainer.train_dataloader
|
2020-06-27 01:38:25 +00:00
|
|
|
tpipes.run_prediction(dataloader, dp_model, dp=True)
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
# new model
|
2020-05-24 22:59:08 +00:00
|
|
|
model = EvalModelTemplate(**hparams)
|
2019-12-07 13:52:06 +00:00
|
|
|
model.on_train_start = assert_good_acc
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
# fit new model which should load hpc weights
|
|
|
|
new_trainer.fit(model)
|
|
|
|
|
|
|
|
# test freeze on gpu
|
|
|
|
model.freeze()
|
|
|
|
model.unfreeze()
|
|
|
|
|
|
|
|
|
2019-12-03 13:01:04 +00:00
|
|
|
def test_model_saving_loading(tmpdir):
|
2019-12-04 11:48:53 +00:00
|
|
|
"""Tests use case where trainer saves the model, and user loads it from tags independently."""
|
2020-05-10 17:15:28 +00:00
|
|
|
model = EvalModelTemplate()
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
# logger file to get meta
|
2020-04-22 00:33:10 +00:00
|
|
|
logger = tutils.get_default_logger(tmpdir)
|
2019-10-23 10:10:13 +00:00
|
|
|
|
2020-07-28 13:47:53 +00:00
|
|
|
# fit model
|
|
|
|
trainer = Trainer(
|
2019-12-07 13:50:21 +00:00
|
|
|
max_epochs=1,
|
2019-10-23 10:10:13 +00:00
|
|
|
logger=logger,
|
2020-07-28 13:47:53 +00:00
|
|
|
checkpoint_callback=ModelCheckpoint(tmpdir),
|
|
|
|
default_root_dir=tmpdir,
|
2019-10-23 10:10:13 +00:00
|
|
|
)
|
|
|
|
result = trainer.fit(model)
|
|
|
|
|
|
|
|
# traning complete
|
|
|
|
assert result == 1, 'amp + ddp model failed to complete'
|
|
|
|
|
|
|
|
# make a prediction
|
2020-02-25 03:23:25 +00:00
|
|
|
dataloaders = model.test_dataloader()
|
|
|
|
if not isinstance(dataloaders, list):
|
|
|
|
dataloaders = [dataloaders]
|
|
|
|
|
|
|
|
for dataloader in dataloaders:
|
2019-10-23 10:10:13 +00:00
|
|
|
for batch in dataloader:
|
|
|
|
break
|
|
|
|
|
|
|
|
x, y = batch
|
|
|
|
x = x.view(x.size(0), -1)
|
|
|
|
|
|
|
|
# generate preds before saving model
|
|
|
|
model.eval()
|
|
|
|
pred_before_saving = model(x)
|
|
|
|
|
|
|
|
# save model
|
2019-12-04 11:48:53 +00:00
|
|
|
new_weights_path = os.path.join(tmpdir, 'save_test.ckpt')
|
2019-10-23 10:10:13 +00:00
|
|
|
trainer.save_checkpoint(new_weights_path)
|
|
|
|
|
|
|
|
# load new model
|
2020-05-13 13:05:15 +00:00
|
|
|
hparams_path = tutils.get_data_path(logger, path_dir=tmpdir)
|
|
|
|
hparams_path = os.path.join(hparams_path, 'hparams.yaml')
|
2020-05-04 15:38:08 +00:00
|
|
|
model_2 = EvalModelTemplate.load_from_checkpoint(
|
2020-03-03 02:05:38 +00:00
|
|
|
checkpoint_path=new_weights_path,
|
2020-07-31 09:18:32 +00:00
|
|
|
hparams_file=hparams_path,
|
2020-03-03 02:05:38 +00:00
|
|
|
)
|
2019-10-23 10:10:13 +00:00
|
|
|
model_2.eval()
|
|
|
|
|
|
|
|
# make prediction
|
|
|
|
# assert that both predictions are the same
|
|
|
|
new_pred = model_2(x)
|
|
|
|
assert torch.all(torch.eq(pred_before_saving, new_pred)).item() == 1
|
|
|
|
|
2020-02-25 15:36:44 +00:00
|
|
|
|
2020-05-24 22:59:08 +00:00
|
|
|
def test_model_pickle(tmpdir):
|
|
|
|
model = EvalModelTemplate()
|
|
|
|
pickle.dumps(model)
|
2020-06-09 20:51:30 +00:00
|
|
|
cloudpickle.dumps(model)
|