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

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