lightning/tests/debug.py

114 lines
3.2 KiB
Python
Raw Normal View History

2019-07-24 14:28:44 +00:00
from pytorch_lightning import Trainer
2019-08-05 07:51:47 +00:00
from examples import LightningTemplateModel
Refactor test modules (#180) * Expectopatronum implement #89 (#182) * rename validate -> evaluate; implement test logic; allow multiple test_loaders * add test_step and test_end to LightningModule * add in_test_mode to pretraining to implement case 2 (test pretrained model) * fix code style issues * LightningTestModel: add optional second test set, implement test_step and test_end * implemented test for multiple test_dataloaders; fixed typo * add two test cases for #89 * add documentation for test_step, test_end; fix computation of loss in validation_step example * Update trainer.py * Update trainer.py * Update trainer.py * Update trainer.py * Update trainer.py * Update trainer.py * Added proper dp ddp routing calls for test mode * Update trainer.py * Update test_models.py * Update trainer.py * Update trainer.py * Update override_data_parallel.py * Update test_models.py * Update test_models.py * Update trainer.py * Update trainer.py * Update trainer.py * Update test_models.py * Update test_models.py * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * Update trainer.py * Update override_data_parallel.py * Update debug.py * Update lm_test_module.py * Update test_models.py * release v0.4.8 * Update README.md * add training loop docs * testing loop docs * testing loop docs * Convert __dataloader to _dataloader This will let inherited classes use it * Factor common test model setup into base class * Specialized test modules inherit from LightningTestModelBase * Fix __is_overriden so that it works with more complicated inheritance * Use mixins to add functionality to test models * Fix test with no val_dataloader * Remove unused imports * Get rid of wild card import * Update trainer.py * Update lm_test_module.py
2019-09-02 19:46:16 +00:00
from pytorch_lightning.testing import LightningTestModel
from argparse import Namespace
2019-07-24 14:28:44 +00:00
from test_tube import Experiment
2019-07-24 15:09:50 +00:00
from pytorch_lightning.callbacks import ModelCheckpoint
2019-07-24 14:28:44 +00:00
import os
import shutil
2019-07-24 14:32:21 +00:00
2019-08-07 06:02:55 +00:00
import pytorch_lightning as pl
2019-07-25 16:01:52 +00:00
import torch
from torch.nn import functional as F
from torch.utils.data import DataLoader
from torchvision.datasets import MNIST
import numpy as np
Expectopatronum implement #89 (#182) * rename validate -> evaluate; implement test logic; allow multiple test_loaders * add test_step and test_end to LightningModule * add in_test_mode to pretraining to implement case 2 (test pretrained model) * fix code style issues * LightningTestModel: add optional second test set, implement test_step and test_end * implemented test for multiple test_dataloaders; fixed typo * add two test cases for #89 * add documentation for test_step, test_end; fix computation of loss in validation_step example * Update trainer.py * Update trainer.py * Update trainer.py * Update trainer.py * Update trainer.py * Update trainer.py * Added proper dp ddp routing calls for test mode * Update trainer.py * Update test_models.py * Update trainer.py * Update trainer.py * Update override_data_parallel.py * Update test_models.py * Update test_models.py * Update trainer.py * Update trainer.py * Update trainer.py * Update test_models.py * Update test_models.py * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * Update trainer.py * Update override_data_parallel.py * Update debug.py * Update lm_test_module.py * Update test_models.py
2019-08-30 22:56:09 +00:00
import pdb
# from test_models import assert_ok_test_acc, load_model, \
# clear_save_dir, get_test_tube_logger, get_hparams, init_save_dir, \
# init_checkpoint_callback, reset_seed, set_random_master_port
2019-07-25 16:01:52 +00:00
2019-08-07 06:02:55 +00:00
class CoolModel(pl.LightningModule):
2019-07-25 16:01:52 +00:00
def __init(self):
super(CoolModel, self).__init__()
# not the best model...
self.l1 = torch.nn.Linear(28 * 28, 10)
def forward(self, x):
return torch.relu(self.l1(x))
def my_loss(self, y_hat, y):
return F.cross_entropy(y_hat, y)
def training_step(self, batch, batch_nb):
x, y = batch
y_hat = self.forward(x)
return {'training_loss': self.my_loss(y_hat, y)}
2019-07-25 16:01:52 +00:00
def validation_step(self, batch, batch_nb):
x, y = batch
y_hat = self.forward(x)
return {'val_loss': self.my_loss(y_hat, y)}
def validation_end(self, outputs):
avg_loss = torch.stack([x for x in outputs['val_loss']]).mean()
return avg_loss
def configure_optimizers(self):
return [torch.optim.Adam(self.parameters(), lr=0.02)]
2019-08-07 06:02:55 +00:00
@pl.data_loader
def train_dataloader(self):
2019-07-25 16:01:52 +00:00
return DataLoader(MNIST('path/to/save', train=True), batch_size=32)
2019-08-07 06:02:55 +00:00
@pl.data_loader
2019-07-25 16:01:52 +00:00
def val_dataloader(self):
return DataLoader(MNIST('path/to/save', train=False), batch_size=32)
2019-08-07 06:02:55 +00:00
@pl.data_loader
2019-07-25 16:01:52 +00:00
def test_dataloader(self):
return DataLoader(MNIST('path/to/save', train=False), batch_size=32)
#
# def main():
# reset_seed()
# set_random_master_port()
#
# hparams = get_hparams()
# model = LightningTestModel(hparams)
#
# save_dir = init_save_dir()
#
# # exp file to get meta
# logger = get_test_tube_logger(False)
#
# print(logger.debug)
#
# # exp file to get weights
# checkpoint = init_checkpoint_callback(logger)
#
# trainer_options = dict(
# show_progress_bar=False,
# max_nb_epochs=1,
# train_percent_check=0.4,
# val_percent_check=0.2,
# checkpoint_callback=checkpoint,
# logger=logger,
# gpus=[0, 1],
# distributed_backend='ddp'
# )
#
# # fit model
# trainer = Trainer(**trainer_options)
# result = trainer.fit(model)
#
# exp = logger.experiment
# print(os.listdir(exp.get_data_path(exp.name, exp.version)))
#
# # correct result and ok accuracy
# assert result == 1, 'training failed to complete'
# pretrained_model = load_model(logger.experiment, save_dir,
# module_class=LightningTestModel)
#
# # run test set
# new_trainer = Trainer(**trainer_options)
# new_trainer.test(pretrained_model)
#
# # test we have good test accuracy
# clear_save_dir()
#
# if __name__ == '__main__':
# main()