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.

graph_reader.py 2.0 kB

4 years ago
4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- coding: UTF-8 -*-
  2. """
  3. Copyright 2020 Tianshu AI Platform. All Rights Reserved.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =============================================================
  14. """
  15. from tbparser.events_reader import EventReadingError, EventsFileReader
  16. from tensorboard.compat.proto.graph_pb2 import GraphDef
  17. from pathlib import Path
  18. from typing import Union
  19. class GraphReader:
  20. def __init__(self,
  21. logdir: Union[str, Path],
  22. stop_on_error: bool = False
  23. ):
  24. self._logdir = Path(logdir)
  25. self._stop_on_error = stop_on_error
  26. def read(self):
  27. log_files = sorted(f for f in self._logdir.glob('*') if f.is_file())
  28. for file_path in log_files:
  29. with open(file_path, 'rb') as f:
  30. reader = EventsFileReader(f)
  31. try:
  32. result = self.read_graph_def(reader)
  33. if result is not None:
  34. return result
  35. except EventReadingError:
  36. if self._stop_on_error:
  37. raise
  38. else:
  39. continue
  40. return
  41. def read_graph_def(self, events):
  42. for event in events:
  43. if event.HasField('graph_def'):
  44. # wall_time = event.wall_time
  45. graph = GraphDef()
  46. graph.ParseFromString(event.graph_def)
  47. return graph
  48. return None

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