Fix test configuration check and testing (#1804)
* Fix test configuration check and testing * Fix test configuration check and testing * Remove check_testing_configuration during test * Fix docstring * fix function name * remove conflicts
This commit is contained in:
parent
4cdebf9a64
commit
56d521a317
|
@ -334,19 +334,12 @@ class TrainerEvaluationLoopMixin(ABC):
|
||||||
return eval_results
|
return eval_results
|
||||||
|
|
||||||
def run_evaluation(self, test_mode: bool = False):
|
def run_evaluation(self, test_mode: bool = False):
|
||||||
# when testing make sure user defined a test step
|
|
||||||
if test_mode and not self.is_overridden('test_step'):
|
|
||||||
raise MisconfigurationException(
|
|
||||||
"You called `.test()` without defining model's `.test_step()`."
|
|
||||||
" Please define and try again")
|
|
||||||
|
|
||||||
# hook
|
# hook
|
||||||
model = self.get_model()
|
model = self.get_model()
|
||||||
model.on_pre_performance_check()
|
model.on_pre_performance_check()
|
||||||
|
|
||||||
# select dataloaders
|
# select dataloaders
|
||||||
if test_mode:
|
if test_mode:
|
||||||
if self.test_dataloaders is None:
|
|
||||||
self.reset_test_dataloader(model)
|
self.reset_test_dataloader(model)
|
||||||
|
|
||||||
dataloaders = self.test_dataloaders
|
dataloaders = self.test_dataloaders
|
||||||
|
|
|
@ -1055,9 +1055,6 @@ class Trainer(
|
||||||
else:
|
else:
|
||||||
self.__attach_dataloaders(self.model, test_dataloaders=test_dataloaders)
|
self.__attach_dataloaders(self.model, test_dataloaders=test_dataloaders)
|
||||||
|
|
||||||
# give proper warnings if user only passed in loader without hooks
|
|
||||||
self.check_testing_model_configuration(model if model else self.model)
|
|
||||||
|
|
||||||
if model is not None:
|
if model is not None:
|
||||||
self.model = model
|
self.model = model
|
||||||
self.fit(model)
|
self.fit(model)
|
||||||
|
@ -1076,27 +1073,28 @@ class Trainer(
|
||||||
|
|
||||||
def check_model_configuration(self, model: LightningModule):
|
def check_model_configuration(self, model: LightningModule):
|
||||||
r"""
|
r"""
|
||||||
Checks that the model is configured correctly before training is started.
|
Checks that the model is configured correctly before training or testing is started.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
model: The model to test.
|
model: The model to check the configuration.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
# Check training_step, train_dataloader, configure_optimizer methods
|
# Check training_step, train_dataloader, configure_optimizer methods
|
||||||
|
if not self.testing:
|
||||||
if not self.is_overridden('training_step', model):
|
if not self.is_overridden('training_step', model):
|
||||||
raise MisconfigurationException(
|
raise MisconfigurationException(
|
||||||
'No `training_step()` method defined. Lightning `Trainer` expects as minimum a'
|
'No `training_step()` method defined. Lightning `Trainer` expects as minimum a'
|
||||||
' `training_step()`, `train_dataloader()` and `configure_optimizers()` to be defined.')
|
' `training_step()`, `training_dataloader()` and `configure_optimizers()` to be defined.')
|
||||||
|
|
||||||
if not self.is_overridden('train_dataloader', model):
|
if not self.is_overridden('train_dataloader', model):
|
||||||
raise MisconfigurationException(
|
raise MisconfigurationException(
|
||||||
'No `train_dataloader()` method defined. Lightning `Trainer` expects as minimum a'
|
'No `train_dataloader()` method defined. Lightning `Trainer` expects as minimum a'
|
||||||
' `training_step()`, `train_dataloader()` and `configure_optimizers()` to be defined.')
|
' `training_step()`, `training_dataloader()` and `configure_optimizers()` to be defined.')
|
||||||
|
|
||||||
if not self.is_overridden('configure_optimizers', model):
|
if not self.is_overridden('configure_optimizers', model):
|
||||||
raise MisconfigurationException(
|
raise MisconfigurationException(
|
||||||
'No `configure_optimizers()` method defined. Lightning `Trainer` expects as minimum a'
|
'No `configure_optimizers()` method defined. Lightning `Trainer` expects as minimum a'
|
||||||
' `training_step()`, `train_dataloader()` and `configure_optimizers()` to be defined.')
|
' `training_step()`, `training_dataloader()` and `configure_optimizers()` to be defined.')
|
||||||
|
|
||||||
# Check val_dataloader, validation_step and validation_epoch_end
|
# Check val_dataloader, validation_step and validation_epoch_end
|
||||||
if self.is_overridden('val_dataloader', model):
|
if self.is_overridden('val_dataloader', model):
|
||||||
|
@ -1113,7 +1111,7 @@ class Trainer(
|
||||||
else:
|
else:
|
||||||
if self.is_overridden('validation_step', model):
|
if self.is_overridden('validation_step', model):
|
||||||
raise MisconfigurationException('You have defined `validation_step()`,'
|
raise MisconfigurationException('You have defined `validation_step()`,'
|
||||||
' but have not passed in a val_dataloader().')
|
' but have not passed in a `val_dataloader()`.')
|
||||||
|
|
||||||
# Check test_dataloader, test_step and test_epoch_end
|
# Check test_dataloader, test_step and test_epoch_end
|
||||||
if self.is_overridden('test_dataloader', model):
|
if self.is_overridden('test_dataloader', model):
|
||||||
|
@ -1126,25 +1124,10 @@ class Trainer(
|
||||||
'You have defined a `test_dataloader()` and have defined a `test_step()`, you may also want to'
|
'You have defined a `test_dataloader()` and have defined a `test_step()`, you may also want to'
|
||||||
' define `test_epoch_end()` for accumulating stats.', RuntimeWarning
|
' define `test_epoch_end()` for accumulating stats.', RuntimeWarning
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
def check_testing_model_configuration(self, model: LightningModule):
|
if self.testing and self.is_overridden('test_step', model):
|
||||||
|
raise MisconfigurationException('You have defined `test_step()` but did not'
|
||||||
has_test_step = self.is_overridden('test_step', model)
|
' implement `test_dataloader` nor passed in `.test(test_dataloader)`.')
|
||||||
has_test_epoch_end = self.is_overridden('test_epoch_end', model)
|
|
||||||
gave_test_loader = self.is_overridden('test_dataloader', model)
|
|
||||||
|
|
||||||
if gave_test_loader and not has_test_step:
|
|
||||||
raise MisconfigurationException('You passed in a `test_dataloader` but did not implement `test_step()`')
|
|
||||||
|
|
||||||
if has_test_step and not gave_test_loader:
|
|
||||||
raise MisconfigurationException('You defined `test_step()` but did not implement'
|
|
||||||
' `test_dataloader` nor passed in `.fit(test_dataloaders`.')
|
|
||||||
|
|
||||||
if has_test_step and gave_test_loader and not has_test_epoch_end:
|
|
||||||
rank_zero_warn(
|
|
||||||
'You passed in a `test_dataloader` and have defined a `test_step()`, you may also want to'
|
|
||||||
' define `test_epoch_end()` for accumulating stats.', RuntimeWarning
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class _PatchDataLoader(object):
|
class _PatchDataLoader(object):
|
||||||
|
|
Loading…
Reference in New Issue