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

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 logging
  22. import numpy as np
  23. import os
  24. import pytest
  25. import random
  26. import mindspore.nn as nn
  27. from mindspore.common.tensor import Tensor
  28. from mindspore.ops import operations as P
  29. from mindspore.train.callback import SummaryStep
  30. from mindspore.train.summary.summary_record import SummaryRecord, _cache_summary_tensor_data
  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. with SummaryRecord(SUMMARY_DIR, file_suffix="_MS_SCALAR") as test_writer:
  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. log.debug("finished test_scalar_summary_sample")
  66. def get_test_data_shape_1(step):
  67. """ get_test_data_shape_1 """
  68. test_data_list = []
  69. tag1 = "x1[:Scalar]"
  70. tag2 = "x2[:Scalar]"
  71. np1 = np.array([step + 1]).astype(np.float32)
  72. np2 = np.array([step + 2]).astype(np.float32)
  73. dict1 = {}
  74. dict1["name"] = tag1
  75. dict1["data"] = Tensor(np1)
  76. dict2 = {}
  77. dict2["name"] = tag2
  78. dict2["data"] = Tensor(np2)
  79. test_data_list.append(dict1)
  80. test_data_list.append(dict2)
  81. return test_data_list
  82. # Test: shape = (1,)
  83. def test_scalar_summary_sample_with_shape_1():
  84. """ test_scalar_summary_sample_with_shape_1 """
  85. log.debug("begin test_scalar_summary_sample_with_shape_1")
  86. # step 0: create the thread
  87. with SummaryRecord(SUMMARY_DIR, file_suffix="_MS_SCALAR") as test_writer:
  88. # step 1: create the test data for summary
  89. # step 2: create the Event
  90. for i in range(1, 100):
  91. test_data = get_test_data_shape_1(i)
  92. _cache_summary_tensor_data(test_data)
  93. test_writer.record(i)
  94. # step 3: send the event to mq
  95. # step 4: accept the event and write the file
  96. log.debug("finished test_scalar_summary_sample")
  97. # Test: test with ge
  98. class SummaryDemo(nn.Cell):
  99. """ SummaryDemo definition """
  100. def __init__(self, ):
  101. super(SummaryDemo, self).__init__()
  102. self.s = P.ScalarSummary()
  103. self.histogram_summary = P.HistogramSummary()
  104. self.add = P.TensorAdd()
  105. def construct(self, x, y):
  106. self.s("x1", x)
  107. z = self.add(x, y)
  108. self.s("z1", z)
  109. self.s("y1", y)
  110. self.histogram_summary("histogram", z)
  111. return z
  112. def test_scalar_summary_with_ge():
  113. """ test_scalar_summary_with_ge """
  114. log.debug("begin test_scalar_summary_with_ge")
  115. # step 0: create the thread
  116. with SummaryRecord(SUMMARY_DIR, file_suffix="_MS_SCALAR") as test_writer:
  117. # step 1: create the network for summary
  118. x = Tensor(np.array([1.1]).astype(np.float32))
  119. y = Tensor(np.array([1.2]).astype(np.float32))
  120. net = SummaryDemo()
  121. net.set_train()
  122. # step 2: create the Event
  123. steps = 100
  124. for i in range(1, steps):
  125. x = Tensor(np.array([1.1 + random.uniform(1, 10)]).astype(np.float32))
  126. y = Tensor(np.array([1.2 + random.uniform(1, 10)]).astype(np.float32))
  127. net(x, y)
  128. test_writer.record(i)
  129. log.debug("finished test_scalar_summary_with_ge")
  130. # test the problem of two consecutive use cases going wrong
  131. def test_scalar_summary_with_ge_2():
  132. """ test_scalar_summary_with_ge_2 """
  133. log.debug("begin test_scalar_summary_with_ge_2")
  134. # step 0: create the thread
  135. with SummaryRecord(SUMMARY_DIR, file_suffix="_MS_SCALAR") as test_writer:
  136. # step 1: create the network for summary
  137. x = Tensor(np.array([1.1]).astype(np.float32))
  138. y = Tensor(np.array([1.2]).astype(np.float32))
  139. net = SummaryDemo()
  140. net.set_train()
  141. # step 2: create the Event
  142. steps = 100
  143. for i in range(1, steps):
  144. x = Tensor(np.array([1.1]).astype(np.float32))
  145. y = Tensor(np.array([1.2]).astype(np.float32))
  146. net(x, y)
  147. test_writer.record(i)
  148. log.debug("finished test_scalar_summary_with_ge_2")
  149. def test_validate():
  150. with SummaryRecord(SUMMARY_DIR) as sr:
  151. with pytest.raises(ValueError):
  152. SummaryStep(sr, 0)
  153. with pytest.raises(ValueError):
  154. SummaryStep(sr, -1)
  155. with pytest.raises(ValueError):
  156. SummaryStep(sr, 1.2)
  157. with pytest.raises(ValueError):
  158. SummaryStep(sr, True)
  159. with pytest.raises(ValueError):
  160. SummaryStep(sr, "str")
  161. sr.record(1)
  162. with pytest.raises(ValueError):
  163. sr.record(False)
  164. with pytest.raises(ValueError):
  165. sr.record(2.0)
  166. with pytest.raises(ValueError):
  167. sr.record((1, 3))
  168. with pytest.raises(ValueError):
  169. sr.record([2, 3])
  170. with pytest.raises(ValueError):
  171. sr.record("str")
  172. with pytest.raises(ValueError):
  173. sr.record(sr)
  174. SummaryStep(sr, 1)
  175. with pytest.raises(ValueError):
  176. SummaryStep(sr, 1.2)
  177. with pytest.raises(ValueError):
  178. SummaryStep(sr, False)
  179. with pytest.raises(ValueError):
  180. SummaryStep(sr, "str")
  181. with pytest.raises(ValueError):
  182. SummaryStep(sr, (1, 2))
  183. with pytest.raises(ValueError):
  184. SummaryStep(sr, [3, 4])
  185. with pytest.raises(ValueError):
  186. SummaryStep(sr, sr)