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_summary.py 6.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. """
  16. @File : test_summary.py
  17. @Author:
  18. @Date : 2019-07-4
  19. @Desc : test summary function
  20. """
  21. import os
  22. import logging
  23. import random
  24. import numpy as np
  25. import pytest
  26. from mindspore.train.summary.summary_record import SummaryRecord, _cache_summary_tensor_data
  27. from mindspore.train.callback import SummaryStep
  28. from mindspore.common.tensor import Tensor
  29. import mindspore.nn as nn
  30. from mindspore.ops import operations as P
  31. CUR_DIR = os.getcwd()
  32. SUMMARY_DIR = CUR_DIR + "/test_temp_summary_event_file/"
  33. log = logging.getLogger("test")
  34. log.setLevel(level=logging.ERROR)
  35. def get_test_data(step):
  36. """ get_test_data """
  37. test_data_list = []
  38. tag1 = "x1[:Scalar]"
  39. tag2 = "x2[:Scalar]"
  40. np1 = np.array(step + 1).astype(np.float32)
  41. np2 = np.array(step + 2).astype(np.float32)
  42. dict1 = {}
  43. dict1["name"] = tag1
  44. dict1["data"] = Tensor(np1)
  45. dict2 = {}
  46. dict2["name"] = tag2
  47. dict2["data"] = Tensor(np2)
  48. test_data_list.append(dict1)
  49. test_data_list.append(dict2)
  50. return test_data_list
  51. # Test 1: summary sample of scalar
  52. def test_scalar_summary_sample():
  53. """ test_scalar_summary_sample """
  54. log.debug("begin test_scalar_summary_sample")
  55. # step 0: create the thread
  56. test_writer = SummaryRecord(SUMMARY_DIR, file_suffix="_MS_SCALAR")
  57. # step 1: create the test data for summary
  58. # step 2: create the Event
  59. for i in range(1, 500):
  60. test_data = get_test_data(i)
  61. _cache_summary_tensor_data(test_data)
  62. test_writer.record(i)
  63. # step 3: send the event to mq
  64. # step 4: accept the event and write the file
  65. test_writer.close()
  66. log.debug("finished test_scalar_summary_sample")
  67. def get_test_data_shape_1(step):
  68. """ get_test_data_shape_1 """
  69. test_data_list = []
  70. tag1 = "x1[:Scalar]"
  71. tag2 = "x2[:Scalar]"
  72. np1 = np.array([step + 1]).astype(np.float32)
  73. np2 = np.array([step + 2]).astype(np.float32)
  74. dict1 = {}
  75. dict1["name"] = tag1
  76. dict1["data"] = Tensor(np1)
  77. dict2 = {}
  78. dict2["name"] = tag2
  79. dict2["data"] = Tensor(np2)
  80. test_data_list.append(dict1)
  81. test_data_list.append(dict2)
  82. return test_data_list
  83. # Test: shape = (1,)
  84. def test_scalar_summary_sample_with_shape_1():
  85. """ test_scalar_summary_sample_with_shape_1 """
  86. log.debug("begin test_scalar_summary_sample_with_shape_1")
  87. # step 0: create the thread
  88. test_writer = SummaryRecord(SUMMARY_DIR, file_suffix="_MS_SCALAR")
  89. # step 1: create the test data for summary
  90. # step 2: create the Event
  91. for i in range(1, 100):
  92. test_data = get_test_data_shape_1(i)
  93. _cache_summary_tensor_data(test_data)
  94. test_writer.record(i)
  95. # step 3: send the event to mq
  96. # step 4: accept the event and write the file
  97. test_writer.close()
  98. log.debug("finished test_scalar_summary_sample")
  99. # Test: test with ge
  100. class SummaryDemo(nn.Cell):
  101. """ SummaryDemo definition """
  102. def __init__(self,):
  103. super(SummaryDemo, self).__init__()
  104. self.s = P.ScalarSummary()
  105. self.histogram_summary = P.HistogramSummary()
  106. self.add = P.TensorAdd()
  107. def construct(self, x, y):
  108. self.s("x1", x)
  109. z = self.add(x, y)
  110. self.s("z1", z)
  111. self.s("y1", y)
  112. self.histogram_summary("histogram", z)
  113. return z
  114. def test_scalar_summary_with_ge():
  115. """ test_scalar_summary_with_ge """
  116. log.debug("begin test_scalar_summary_with_ge")
  117. # step 0: create the thread
  118. test_writer = SummaryRecord(SUMMARY_DIR, file_suffix="_MS_SCALAR")
  119. # step 1: create the network for summary
  120. x = Tensor(np.array([1.1]).astype(np.float32))
  121. y = Tensor(np.array([1.2]).astype(np.float32))
  122. net = SummaryDemo()
  123. net.set_train()
  124. # step 2: create the Event
  125. steps = 100
  126. for i in range(1, steps):
  127. x = Tensor(np.array([1.1 + random.uniform(1, 10)]).astype(np.float32))
  128. y = Tensor(np.array([1.2 + random.uniform(1, 10)]).astype(np.float32))
  129. net(x, y)
  130. test_writer.record(i)
  131. # step 3: close the writer
  132. test_writer.close()
  133. log.debug("finished test_scalar_summary_with_ge")
  134. # test the problem of two consecutive use cases going wrong
  135. def test_scalar_summary_with_ge_2():
  136. """ test_scalar_summary_with_ge_2 """
  137. log.debug("begin test_scalar_summary_with_ge_2")
  138. # step 0: create the thread
  139. test_writer = SummaryRecord(SUMMARY_DIR, file_suffix="_MS_SCALAR")
  140. # step 1: create the network for summary
  141. x = Tensor(np.array([1.1]).astype(np.float32))
  142. y = Tensor(np.array([1.2]).astype(np.float32))
  143. net = SummaryDemo()
  144. net.set_train()
  145. # step 2: create the Event
  146. steps = 100
  147. for i in range(1, steps):
  148. x = Tensor(np.array([1.1]).astype(np.float32))
  149. y = Tensor(np.array([1.2]).astype(np.float32))
  150. net(x, y)
  151. test_writer.record(i)
  152. # step 3: close the writer
  153. test_writer.close()
  154. log.debug("finished test_scalar_summary_with_ge_2")
  155. def test_validate():
  156. sr = SummaryRecord(SUMMARY_DIR)
  157. with pytest.raises(ValueError):
  158. SummaryStep(sr, 0)
  159. with pytest.raises(ValueError):
  160. SummaryStep(sr, -1)
  161. with pytest.raises(ValueError):
  162. SummaryStep(sr, 1.2)
  163. with pytest.raises(ValueError):
  164. SummaryStep(sr, True)
  165. with pytest.raises(ValueError):
  166. SummaryStep(sr, "str")
  167. sr.record(1)
  168. with pytest.raises(ValueError):
  169. sr.record(False)
  170. with pytest.raises(ValueError):
  171. sr.record(2.0)
  172. with pytest.raises(ValueError):
  173. sr.record((1,3))
  174. with pytest.raises(ValueError):
  175. sr.record([2,3])
  176. with pytest.raises(ValueError):
  177. sr.record("str")
  178. with pytest.raises(ValueError):
  179. sr.record(sr)
  180. sr.close()
  181. SummaryStep(sr, 1)
  182. with pytest.raises(ValueError):
  183. SummaryStep(sr, 1.2)
  184. with pytest.raises(ValueError):
  185. SummaryStep(sr, False)
  186. with pytest.raises(ValueError):
  187. SummaryStep(sr, "str")
  188. with pytest.raises(ValueError):
  189. SummaryStep(sr, (1,2))
  190. with pytest.raises(ValueError):
  191. SummaryStep(sr, [3,4])
  192. with pytest.raises(ValueError):
  193. SummaryStep(sr, sr)