mirror of
https://github.com/wassname/pytorch-lightning.git
synced 2026-09-11 12:31:23 +08:00
Fixing tests (#936)
* abs import * rename test model * update trainer * revert test_step check * move tags * fix test_step * clean tests * fix template * update dataset path * fix parent order
This commit is contained in:
@@ -216,13 +216,13 @@ class TrainerEvaluationLoopMixin(ABC):
|
||||
# this is just empty shell for code from other class
|
||||
pass
|
||||
|
||||
def evaluate(self, model, dataloaders, max_batches, test=False):
|
||||
def evaluate(self, model, dataloaders, max_batches, test_mode: bool = False):
|
||||
"""Run evaluation code.
|
||||
|
||||
:param model: PT model
|
||||
:param dataloaders: list of PT dataloaders
|
||||
:param max_batches: Scalar
|
||||
:param test: boolean
|
||||
:param test_mode
|
||||
:return:
|
||||
"""
|
||||
# enable eval mode
|
||||
@@ -260,18 +260,14 @@ class TrainerEvaluationLoopMixin(ABC):
|
||||
# -----------------
|
||||
# RUN EVALUATION STEP
|
||||
# -----------------
|
||||
output = self.evaluation_forward(model,
|
||||
batch,
|
||||
batch_idx,
|
||||
dataloader_idx,
|
||||
test)
|
||||
output = self.evaluation_forward(model, batch, batch_idx, dataloader_idx, test_mode)
|
||||
|
||||
# track outputs for collation
|
||||
dl_outputs.append(output)
|
||||
|
||||
# batch done
|
||||
if batch_idx % self.progress_bar_refresh_rate == 0:
|
||||
if test:
|
||||
if test_mode:
|
||||
self.test_progress_bar.update(self.progress_bar_refresh_rate)
|
||||
else:
|
||||
self.val_progress_bar.update(self.progress_bar_refresh_rate)
|
||||
@@ -286,7 +282,7 @@ class TrainerEvaluationLoopMixin(ABC):
|
||||
|
||||
# give model a chance to do something with the outputs (and method defined)
|
||||
model = self.get_model()
|
||||
if test and self.is_overriden('test_end'):
|
||||
if test_mode and self.is_overriden('test_end'):
|
||||
eval_results = model.test_end(outputs)
|
||||
elif self.is_overriden('validation_end'):
|
||||
eval_results = model.validation_end(outputs)
|
||||
@@ -299,11 +295,11 @@ class TrainerEvaluationLoopMixin(ABC):
|
||||
|
||||
return eval_results
|
||||
|
||||
def run_evaluation(self, test=False):
|
||||
def run_evaluation(self, test_mode: bool = False):
|
||||
# when testing make sure user defined a test step
|
||||
if test and not self.is_overriden('test_step'):
|
||||
m = '''You called `.test()` without defining model's `.test_step()`.
|
||||
Please define and try again'''
|
||||
if test_mode and not self.is_overriden('test_step'):
|
||||
m = "You called `.test()` without defining model's `.test_step()`." \
|
||||
" Please define and try again"
|
||||
raise MisconfigurationException(m)
|
||||
|
||||
# hook
|
||||
@@ -311,7 +307,7 @@ class TrainerEvaluationLoopMixin(ABC):
|
||||
model.on_pre_performance_check()
|
||||
|
||||
# select dataloaders
|
||||
if test:
|
||||
if test_mode:
|
||||
if self.reload_dataloaders_every_epoch or self.test_dataloaders is None:
|
||||
self.reset_test_dataloader(model)
|
||||
|
||||
@@ -331,18 +327,15 @@ class TrainerEvaluationLoopMixin(ABC):
|
||||
|
||||
# init validation or test progress bar
|
||||
# main progress bar will already be closed when testing so initial position is free
|
||||
position = 2 * self.process_position + (not test)
|
||||
desc = 'Testing' if test else 'Validating'
|
||||
pbar = tqdm(desc=desc, total=max_batches, leave=test, position=position,
|
||||
position = 2 * self.process_position + (not test_mode)
|
||||
desc = 'Testing' if test_mode else 'Validating'
|
||||
pbar = tqdm(desc=desc, total=max_batches, leave=test_mode, position=position,
|
||||
disable=not self.show_progress_bar, dynamic_ncols=True,
|
||||
file=sys.stdout)
|
||||
setattr(self, f'{"test" if test else "val"}_progress_bar', pbar)
|
||||
setattr(self, f'{"test" if test_mode else "val"}_progress_bar', pbar)
|
||||
|
||||
# run evaluation
|
||||
eval_results = self.evaluate(self.model,
|
||||
dataloaders,
|
||||
max_batches,
|
||||
test)
|
||||
eval_results = self.evaluate(self.model, dataloaders, max_batches, test_mode)
|
||||
_, prog_bar_metrics, log_metrics, callback_metrics, _ = self.process_output(
|
||||
eval_results)
|
||||
|
||||
@@ -359,27 +352,27 @@ class TrainerEvaluationLoopMixin(ABC):
|
||||
model.on_post_performance_check()
|
||||
|
||||
# add model specific metrics
|
||||
if not test:
|
||||
if not test_mode:
|
||||
self.main_progress_bar.set_postfix(**self.training_tqdm_dict)
|
||||
|
||||
# close progress bar
|
||||
if test:
|
||||
if test_mode:
|
||||
self.test_progress_bar.close()
|
||||
else:
|
||||
self.val_progress_bar.close()
|
||||
|
||||
# model checkpointing
|
||||
if self.proc_rank == 0 and self.checkpoint_callback is not None and not test:
|
||||
if self.proc_rank == 0 and self.checkpoint_callback is not None and not test_mode:
|
||||
self.checkpoint_callback.on_validation_end()
|
||||
|
||||
def evaluation_forward(self, model, batch, batch_idx, dataloader_idx, test=False):
|
||||
def evaluation_forward(self, model, batch, batch_idx, dataloader_idx, test_mode: bool = False):
|
||||
# make dataloader_idx arg in validation_step optional
|
||||
args = [batch, batch_idx]
|
||||
|
||||
if test and len(self.test_dataloaders) > 1:
|
||||
if test_mode and len(self.test_dataloaders) > 1:
|
||||
args.append(dataloader_idx)
|
||||
|
||||
elif not test and len(self.val_dataloaders) > 1:
|
||||
elif not test_mode and len(self.val_dataloaders) > 1:
|
||||
args.append(dataloader_idx)
|
||||
|
||||
# handle DP, DDP forward
|
||||
@@ -402,7 +395,7 @@ class TrainerEvaluationLoopMixin(ABC):
|
||||
args[0] = batch
|
||||
|
||||
# CPU
|
||||
if test:
|
||||
if test_mode:
|
||||
output = model.test_step(*args)
|
||||
else:
|
||||
output = model.validation_step(*args)
|
||||
|
||||
Reference in New Issue
Block a user