|
- # Copyright 2021 Huawei Technologies Co., Ltd
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- # ============================================================================
- """callback function"""
- from mindspore.train.callback import Callback
-
-
- class EvaluateCallBack(Callback):
- """EvaluateCallBack"""
- def __init__(self, model, eval_dataset, per_print_time=1000):
- super(EvaluateCallBack, self).__init__()
- self.model = model
- self.per_print_time = per_print_time
- self.eval_dataset = eval_dataset
-
- def step_end(self, run_context):
- cb_params = run_context.original_args()
- if cb_params.cur_step_num % self.per_print_time == 0:
- result = self.model.eval(self.eval_dataset, dataset_sink_mode=False)
- print('cur epoch {}, cur_step {}, top1 accuracy {}, top5 accuracy {}.'.format(cb_params.cur_epoch_num,
- cb_params.cur_step_num,
- result['top_1_accuracy'],
- result['top_5_accuracy']))
-
- def epoch_end(self, run_context):
- cb_params = run_context.original_args()
- result = self.model.eval(self.eval_dataset, dataset_sink_mode=False)
- print('cur epoch {}, cur_step {}, top1 accuracy {}, top5 accuracy {}.'.format(cb_params.cur_epoch_num,
- cb_params.cur_step_num,
- result['top_1_accuracy'],
- result['top_5_accuracy']))
|