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 2.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. """
  2. Copyright 2020 Tianshu AI Platform. All Rights Reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. =============================================================
  13. """
  14. from .base import Callback
  15. import numbers
  16. from tqdm import tqdm
  17. class MetricsLogging(Callback):
  18. def __init__(self, keys):
  19. super(MetricsLogging, self).__init__()
  20. self._keys = keys
  21. def __call__(self, engine):
  22. if engine.logger==None:
  23. return
  24. state = engine.state
  25. content = "Iter %d/%d (Epoch %d/%d, Batch %d/%d)"%(
  26. state.iter, state.max_iter,
  27. state.current_epoch, state.max_epoch,
  28. state.current_batch_index, state.max_batch_index
  29. )
  30. for key in self._keys:
  31. value = state.metrics.get(key, None)
  32. if value is not None:
  33. if isinstance(value, numbers.Number):
  34. content += " %s=%.4f"%(key, value)
  35. if engine.tb_writer is not None:
  36. engine.tb_writer.add_scalar(key, value, global_step=state.iter)
  37. elif isinstance(value, (list, tuple)):
  38. content += " %s=%s"%(key, value)
  39. engine.logger.info(content)
  40. class ProgressCallback(Callback):
  41. def __init__(self, max_iter=100, tag=None):
  42. self._max_iter = max_iter
  43. self._tag = tag
  44. #self._pbar = tqdm(total=self._max_iter, desc=self._tag)
  45. def __call__(self, engine):
  46. self._pbar.update(1)
  47. if self._pbar.n==self._max_iter:
  48. self._pbar.close()
  49. def reset(self):
  50. self._pbar = tqdm(total=self._max_iter, desc=self._tag)

一站式算法开发平台、高性能分布式深度学习框架、先进算法模型库、视觉模型炼知平台、数据可视化分析平台等一系列平台及工具,在模型高效分布式训练、数据处理和可视分析、模型炼知和轻量化等技术上形成独特优势,目前已在产学研等各领域近千家单位及个人提供AI应用赋能

Contributors (1)