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

Method predict

modelscope/trainers/trainer.py:714–760  ·  view source on GitHub ↗

Start prediction. Args: predict_datasets(Union[Dataset, List[Dataset]]): The datasets used to predict ground truth. saving_fn(`Callable`): The callable used to save the prediction values to files. Like: >>> class SavingFn: >>> def

(self,
                predict_datasets: Union[Dataset, List[Dataset]],
                saving_fn,
                checkpoint_path=None,
                strict=False)

Source from the content-addressed store, hash-verified

712 self.train_loop(self.train_dataloader)
713
714 def predict(self,
715 predict_datasets: Union[Dataset, List[Dataset]],
716 saving_fn,
717 checkpoint_path=None,
718 strict=False):
719 """Start prediction.
720
721 Args:
722 predict_datasets(Union[Dataset, List[Dataset]]): The datasets used to predict ground truth.
723
724 saving_fn(`Callable`): The callable used to save the prediction values to files. Like:
725 >>> class SavingFn:
726 >>> def __init__(self):
727 >>> self.filename = '/tmp/results.txt'
728 >>>
729 >>> def __call__(self, inputs, outputs):
730 >>> import numpy as np
731 >>> ids = inputs.ids
732 >>> predictions = np.argmax(outputs['logits'].cpu().numpy(), axis=1)
733 >>> with open(self.filename, 'a') as f:
734 >>> for id, pred in zip(ids, predictions):
735 >>> f.writelines(f'{id}, {pred}')
736
737 This saving_fn's result will not be collected to one file, Training with multiprocessing please
738 consider combining these files manually.
739
740 checkpoint_path(`str`, `optional`): The previous saving checkpoint to read,
741 usually it's a `some-file-name.pth` file or a pure PyTorch `some-file.bin` file
742 generated by this trainer.
743
744 strict(`boolean`): If strict, any unmatched keys will cause an error.
745 """
746 self.register_processors()
747 self.print_hook_info()
748 if checkpoint_path is not None:
749 from modelscope.trainers.hooks import LoadCheckpointHook
750 LoadCheckpointHook.load_checkpoint(
751 checkpoint_path, self, strict=strict)
752 self.model.eval()
753 self._mode = ModeKeys.EVAL
754 predict_dataloader = self.get_predict_dataloader(predict_datasets)
755 metric_classes = [PredictionSavingWrapper(saving_fn=saving_fn)]
756
757 for m in metric_classes:
758 m.trainer = self
759
760 self.evaluation_loop(predict_dataloader, metric_classes)
761
762 def evaluate(self, checkpoint_path=None, saving_fn=None, **kwargs):
763 """Start evaluation.

Callers 2

evaluateMethod · 0.45
trainMethod · 0.45

Calls 7

register_processorsMethod · 0.95
print_hook_infoMethod · 0.95
evaluation_loopMethod · 0.95
load_checkpointMethod · 0.45
evalMethod · 0.45

Tested by

no test coverage detected