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.

summary_reader.py 1.5 kB

5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. """Summary reader."""
  16. import struct
  17. import mindspore.train.summary_pb2 as summary_pb2
  18. _HEADER_SIZE = 8
  19. _HEADER_CRC_SIZE = 4
  20. _DATA_CRC_SIZE = 4
  21. class SummaryReader:
  22. """Read events from summary file."""
  23. def __init__(self, file_name):
  24. self._file_name = file_name
  25. self._file_handler = open(self._file_name, "rb")
  26. # skip version event
  27. self.read_event()
  28. def read_event(self):
  29. """Read next event."""
  30. file_handler = self._file_handler
  31. header = file_handler.read(_HEADER_SIZE)
  32. data_len = struct.unpack('Q', header)[0]
  33. file_handler.read(_HEADER_CRC_SIZE)
  34. event_str = file_handler.read(data_len)
  35. file_handler.read(_DATA_CRC_SIZE)
  36. summary_event = summary_pb2.Event.FromString(event_str)
  37. return summary_event