MCPcopy Create free account
hub / github.com/modelscope/modelscope / evaluate

Method evaluate

modelscope/trainers/trainer.py:762–805  ·  view source on GitHub ↗

Start evaluation. Args: checkpoint_path(`str`, `optional`): The previous saving checkpoint to read, usually it's a `some-file-name.pth` file or a pure PyTorch `some-file.bin` file generated by this trainer. saving_fn(`Callable`): The

(self, checkpoint_path=None, saving_fn=None, **kwargs)

Source from the content-addressed store, hash-verified

760 self.evaluation_loop(predict_dataloader, metric_classes)
761
762 def evaluate(self, checkpoint_path=None, saving_fn=None, **kwargs):
763 """Start evaluation.
764
765 Args:
766 checkpoint_path(`str`, `optional`): The previous saving checkpoint to read,
767 usually it's a `some-file-name.pth` file or a pure PyTorch `some-file.bin` file
768 generated by this trainer.
769
770 saving_fn(`Callable`): The callable used to save the prediction values to files. Like:
771 >>> class SavingFn:
772 >>> def __init__(self):
773 >>> self.filename = '/tmp/results.txt'
774 >>>
775 >>> def __call__(self, inputs, outputs):
776 >>> import numpy as np
777 >>> ids = inputs.ids
778 >>> predictions = np.argmax(outputs['logits'].cpu().numpy(), axis=1)
779 >>> with open(self.filename, 'a') as f:
780 >>> for id, pred in zip(ids, predictions):
781 >>> f.writelines(f'{id}, {pred}')
782 kwargs:
783 strict(`boolean`): If strict, any unmatched keys will cause an error.
784 """
785 self.register_processors()
786 self.print_hook_info()
787 if checkpoint_path is not None:
788 from modelscope.trainers.hooks import LoadCheckpointHook
789 LoadCheckpointHook.load_checkpoint(
790 checkpoint_path, self, strict=kwargs.get('strict', False))
791 self.model.eval()
792 self._mode = ModeKeys.EVAL
793 self.eval_dataloader = self.get_eval_data_loader()
794 self.data_loader = self.eval_dataloader
795 metric_classes = [build_metric(metric) for metric in self.metrics]
796 if saving_fn is not None:
797 metric_classes.append(PredictionSavingWrapper(saving_fn=saving_fn))
798 for m in metric_classes:
799 m.trainer = self
800
801 metric_values = self.evaluation_loop(self.eval_dataloader,
802 metric_classes)
803
804 self._metric_values = metric_values
805 return metric_values
806
807 @property
808 def metric_values(self):

Callers

nothing calls this directly

Calls 10

register_processorsMethod · 0.95
print_hook_infoMethod · 0.95
get_eval_data_loaderMethod · 0.95
evaluation_loopMethod · 0.95
build_metricFunction · 0.90
load_checkpointMethod · 0.45
getMethod · 0.45
evalMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected