text: # Copyright 2022 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.
  # ============================================================================
  """Evaluation callback when traning"""
  
  import os
  import stat
  from mindspore import log as logger
  from mindspore.train.callback import Callback
  
  class EvalCallBack(Callback):
      """
      Evaluation callback when training.
  
      Args:
          eval_function (function): evaluation function.
          eval_param_dict (dict): evaluation parameters' configure dict.
          interval (int): run evaluation interval, default is 1.
          eval_start_epoch (int): evaluation start epoch, default is 1.
          metrics_name (str): evaluation metrics name, default is `acc`.
  
      Returns:
          None
  
      Examples:
          >>> EvalCallBack(eval_function, eval_param_dict)
      """
  
      def __init__(self, eval_function, model, lfw_dataloader, interval=1, eval_start_epoch=1):
          super(EvalCallBack, self).__init__()
          self.eval_function = eval_function
          self.model = model
          self.dataloader = lfw_dataloader
          self.eval_start_epoch = eval_start_epoch
          if interval < 1:
              raise ValueError("interval should >= 1.")
          self.interval = interval
  
      def remove_ckpoint_file(self, file_name):
          """Remove the specified checkpoint file from this checkpoint manager and also from the directory."""
          try:
              os.chmod(file_name, stat.S_IWRITE)
              os.remove(file_name)
          except OSError:
              logger.warning("OSError, failed to remove the older ckpt file %s.", file_name)
          except ValueError:
              logger.warning("ValueError, failed to remove the older ckpt file %s.", file_name)
  
      def epoch_end(self, run_context):
          """Callback when epoch end."""
          cb_params = run_context.original_args()
          cur_epoch = cb_params.cur_epoch_num
          if cur_epoch >= self.eval_start_epoch and (cur_epoch - self.eval_start_epoch) % self.interval == 0:
              self.eval_function(self.model, self.dataloader)
  
      def end(self, run_context):
          pass
  