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.

test_histogram_summary.py 7.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. """Test histogram summary."""
  16. import logging
  17. import os
  18. import tempfile
  19. import numpy as np
  20. from mindspore.common.tensor import Tensor
  21. from mindspore.train.summary.summary_record import SummaryRecord, _cache_summary_tensor_data
  22. from .summary_reader import SummaryReader
  23. CUR_DIR = os.getcwd()
  24. SUMMARY_DIR = os.path.join(CUR_DIR, "/test_temp_summary_event_file/")
  25. LOG = logging.getLogger("test")
  26. LOG.setLevel(level=logging.ERROR)
  27. def _wrap_test_data(input_data: Tensor):
  28. """
  29. Wraps test data to summary format.
  30. Args:
  31. input_data (Tensor): Input data.
  32. Returns:
  33. dict, the wrapped data.
  34. """
  35. return [{
  36. "name": "test_data[:Histogram]",
  37. "data": input_data
  38. }]
  39. def test_histogram_summary():
  40. """Test histogram summary."""
  41. with tempfile.TemporaryDirectory() as tmp_dir:
  42. test_writer = SummaryRecord(tmp_dir, file_suffix="_MS_HISTOGRAM")
  43. test_data = _wrap_test_data(Tensor([[1, 2, 3], [4, 5, 6]]))
  44. _cache_summary_tensor_data(test_data)
  45. test_writer.record(step=1)
  46. test_writer.close()
  47. file_name = os.path.join(tmp_dir, test_writer.event_file_name)
  48. reader = SummaryReader(file_name)
  49. event = reader.read_event()
  50. assert event.summary.value[0].histogram.count == 6
  51. def test_histogram_multi_summary():
  52. """Test histogram multiple step."""
  53. with tempfile.TemporaryDirectory() as tmp_dir:
  54. test_writer = SummaryRecord(tmp_dir, file_suffix="_MS_HISTOGRAM")
  55. rng = np.random.RandomState(10)
  56. size = 50
  57. num_step = 5
  58. for i in range(num_step):
  59. arr = rng.normal(size=size)
  60. test_data = _wrap_test_data(Tensor(arr))
  61. _cache_summary_tensor_data(test_data)
  62. test_writer.record(step=i)
  63. test_writer.close()
  64. file_name = os.path.join(tmp_dir, test_writer.event_file_name)
  65. reader = SummaryReader(file_name)
  66. for _ in range(num_step):
  67. event = reader.read_event()
  68. assert event.summary.value[0].histogram.count == size
  69. def test_histogram_summary_scalar_tensor():
  70. """Test histogram summary, input is a scalar tensor."""
  71. with tempfile.TemporaryDirectory() as tmp_dir:
  72. test_writer = SummaryRecord(tmp_dir, file_suffix="_MS_HISTOGRAM")
  73. test_data = _wrap_test_data(Tensor(1))
  74. _cache_summary_tensor_data(test_data)
  75. test_writer.record(step=1)
  76. test_writer.close()
  77. file_name = os.path.join(tmp_dir, test_writer.event_file_name)
  78. reader = SummaryReader(file_name)
  79. event = reader.read_event()
  80. assert event.summary.value[0].histogram.count == 1
  81. def test_histogram_summary_empty_tensor():
  82. """Test histogram summary, input is an empty tensor."""
  83. with tempfile.TemporaryDirectory() as tmp_dir:
  84. test_writer = SummaryRecord(tmp_dir, file_suffix="_MS_HISTOGRAM")
  85. test_data = _wrap_test_data(Tensor([]))
  86. _cache_summary_tensor_data(test_data)
  87. test_writer.record(step=1)
  88. test_writer.close()
  89. file_name = os.path.join(tmp_dir, test_writer.event_file_name)
  90. reader = SummaryReader(file_name)
  91. event = reader.read_event()
  92. assert event.summary.value[0].histogram.count == 0
  93. def test_histogram_summary_same_value():
  94. """Test histogram summary, input is an ones tensor."""
  95. with tempfile.TemporaryDirectory() as tmp_dir:
  96. test_writer = SummaryRecord(tmp_dir, file_suffix="_MS_HISTOGRAM")
  97. dim1 = 100
  98. dim2 = 100
  99. test_data = _wrap_test_data(Tensor(np.ones([dim1, dim2])))
  100. _cache_summary_tensor_data(test_data)
  101. test_writer.record(step=1)
  102. test_writer.close()
  103. file_name = os.path.join(tmp_dir, test_writer.event_file_name)
  104. reader = SummaryReader(file_name)
  105. event = reader.read_event()
  106. LOG.debug(event)
  107. assert len(event.summary.value[0].histogram.buckets) == 1
  108. def test_histogram_summary_high_dims():
  109. """Test histogram summary, input is a 4-dimension tensor."""
  110. with tempfile.TemporaryDirectory() as tmp_dir:
  111. test_writer = SummaryRecord(tmp_dir, file_suffix="_MS_HISTOGRAM")
  112. dim = 10
  113. rng = np.random.RandomState(0)
  114. tensor_data = rng.normal(size=[dim, dim, dim, dim])
  115. test_data = _wrap_test_data(Tensor(tensor_data))
  116. _cache_summary_tensor_data(test_data)
  117. test_writer.record(step=1)
  118. test_writer.close()
  119. file_name = os.path.join(tmp_dir, test_writer.event_file_name)
  120. reader = SummaryReader(file_name)
  121. event = reader.read_event()
  122. LOG.debug(event)
  123. assert event.summary.value[0].histogram.count == tensor_data.size
  124. def test_histogram_summary_nan_inf():
  125. """Test histogram summary, input tensor has nan."""
  126. with tempfile.TemporaryDirectory() as tmp_dir:
  127. test_writer = SummaryRecord(tmp_dir, file_suffix="_MS_HISTOGRAM")
  128. dim1 = 100
  129. dim2 = 100
  130. arr = np.ones([dim1, dim2])
  131. arr[0][0] = np.nan
  132. arr[0][1] = np.inf
  133. arr[0][2] = -np.inf
  134. test_data = _wrap_test_data(Tensor(arr))
  135. _cache_summary_tensor_data(test_data)
  136. test_writer.record(step=1)
  137. test_writer.close()
  138. file_name = os.path.join(tmp_dir, test_writer.event_file_name)
  139. reader = SummaryReader(file_name)
  140. event = reader.read_event()
  141. LOG.debug(event)
  142. assert event.summary.value[0].histogram.nan_count == 1
  143. def test_histogram_summary_all_nan_inf():
  144. """Test histogram summary, input tensor has no valid number."""
  145. with tempfile.TemporaryDirectory() as tmp_dir:
  146. test_writer = SummaryRecord(tmp_dir, file_suffix="_MS_HISTOGRAM")
  147. test_data = _wrap_test_data(Tensor(np.array([np.nan, np.nan, np.nan, np.inf, -np.inf])))
  148. _cache_summary_tensor_data(test_data)
  149. test_writer.record(step=1)
  150. test_writer.close()
  151. file_name = os.path.join(tmp_dir, test_writer.event_file_name)
  152. reader = SummaryReader(file_name)
  153. event = reader.read_event()
  154. LOG.debug(event)
  155. histogram = event.summary.value[0].histogram
  156. assert histogram.nan_count == 3
  157. assert histogram.pos_inf_count == 1
  158. assert histogram.neg_inf_count == 1