From e8bca5a11e97e8d4c2c1b718e578616ba6078938 Mon Sep 17 00:00:00 2001 From: Xu Wenqing <121550081+Xu-Wenqing@users.noreply.github.com> Date: Tue, 18 Apr 2023 20:30:13 +0800 Subject: [PATCH] Set default topk when num_classes < 5 (#269) * set default topk when num_classes < 6 * add comments * format changes with yapf --------- Co-authored-by: Xu Wenqing --- modelscope/trainers/cv/image_classifition_trainer.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/modelscope/trainers/cv/image_classifition_trainer.py b/modelscope/trainers/cv/image_classifition_trainer.py index f15fd5e3..0ecfe173 100644 --- a/modelscope/trainers/cv/image_classifition_trainer.py +++ b/modelscope/trainers/cv/image_classifition_trainer.py @@ -498,6 +498,11 @@ class ImageClassifitionTrainer(BaseTrainer): metric_options = self.cfg.evaluation.get('metric_options', {}) if 'topk' in metric_options.keys(): metric_options['topk'] = tuple(metric_options['topk']) + # mmcls will set the default value of topk to (1, 5) which + # will cause error when number of classes less then 5. + # set topk as (1,) if len(CLASSES) < 5: + elif len(CLASSES) < 5: + metric_options['topk'] = (1, ) if self.cfg.evaluation.metrics: eval_results = dataset.evaluate( results=outputs,