You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

logging.py 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Custom logger."""
  16. import logging
  17. import os
  18. import sys
  19. from datetime import datetime
  20. logger_name_1 = 'yolov3_face_detection'
  21. class LOGGER(logging.Logger):
  22. '''LOGGER'''
  23. def __init__(self, logger_name):
  24. super(LOGGER, self).__init__(logger_name)
  25. console = logging.StreamHandler(sys.stdout)
  26. console.setLevel(logging.INFO)
  27. formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(message)s')
  28. console.setFormatter(formatter)
  29. self.addHandler(console)
  30. self.local_rank = 0
  31. def setup_logging_file(self, log_dir, local_rank=0):
  32. '''setup_logging_file'''
  33. self.local_rank = local_rank
  34. if self.local_rank == 0:
  35. if not os.path.exists(log_dir):
  36. os.makedirs(log_dir, exist_ok=True)
  37. log_name = datetime.now().strftime('%Y-%m-%d_time_%H_%M_%S') + '.log'
  38. self.log_fn = os.path.join(log_dir, log_name)
  39. fh = logging.FileHandler(self.log_fn)
  40. fh.setLevel(logging.INFO)
  41. formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(message)s')
  42. fh.setFormatter(formatter)
  43. self.addHandler(fh)
  44. def info(self, msg, *args, **kwargs):
  45. if self.isEnabledFor(logging.INFO) and self.local_rank == 0:
  46. self._log(logging.INFO, msg, args, **kwargs)
  47. def save_args(self, args):
  48. self.info('Args:')
  49. args_dict = vars(args)
  50. for key in args_dict.keys():
  51. self.info('--> %s: %s', key, args_dict[key])
  52. self.info('')
  53. def important_info(self, msg, *args, **kwargs):
  54. if self.isEnabledFor(logging.INFO) and self.local_rank == 0:
  55. line_width = 2
  56. important_msg = '\n'
  57. important_msg += ('*'*70 + '\n')*line_width
  58. important_msg += ('*'*line_width + '\n')*2
  59. important_msg += '*'*line_width + ' '*8 + msg + '\n'
  60. important_msg += ('*'*line_width + '\n')*2
  61. important_msg += ('*'*70 + '\n')*line_width
  62. self.info(important_msg, *args, **kwargs)
  63. def get_logger(path, rank):
  64. logger = LOGGER(logger_name_1)
  65. logger.setup_logging_file(path, rank)
  66. return logger
  67. class AverageMeter():
  68. """Computes and stores the average and current value"""
  69. def __init__(self, name, fmt=':f', tb_writer=None):
  70. self.name = name
  71. self.fmt = fmt
  72. self.reset()
  73. self.tb_writer = tb_writer
  74. self.cur_step = 1
  75. def reset(self):
  76. self.val = 0
  77. self.avg = 0
  78. self.sum = 0
  79. self.count = 0
  80. def update(self, val, n=1):
  81. self.val = val
  82. self.sum += val * n
  83. self.count += n
  84. self.avg = self.sum / self.count
  85. if self.tb_writer is not None:
  86. self.tb_writer.add_scalar(self.name, self.val, self.cur_step)
  87. self.cur_step += 1
  88. def __str__(self):
  89. fmtstr = '{name}:{avg' + self.fmt + '}'
  90. return fmtstr.format(**self.__dict__)