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

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # Copyright 2020-2021 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 summary."""
  16. import os
  17. import random
  18. import numpy as np
  19. import mindspore.nn as nn
  20. from mindspore.common.tensor import Tensor
  21. from mindspore.ops import operations as P
  22. from mindspore.train.summary.summary_record import SummaryRecord, _cache_summary_tensor_data
  23. CUR_DIR = os.getcwd()
  24. SUMMARY_DIR = CUR_DIR + "/test_temp_summary_event_file/"
  25. def get_test_data(step):
  26. """ get_test_data """
  27. test_data_list = []
  28. tag1 = "x1[:Scalar]"
  29. tag2 = "x2[:Scalar]"
  30. np1 = np.array(step + 1).astype(np.float32)
  31. np2 = np.array(step + 2).astype(np.float32)
  32. dict1 = {}
  33. dict1["name"] = tag1
  34. dict1["data"] = Tensor(np1)
  35. dict2 = {}
  36. dict2["name"] = tag2
  37. dict2["data"] = Tensor(np2)
  38. test_data_list.append(dict1)
  39. test_data_list.append(dict2)
  40. return test_data_list
  41. def test_scalar_summary_sample():
  42. """ test_scalar_summary_sample """
  43. with SummaryRecord(SUMMARY_DIR, file_suffix="_MS_SCALAR") as test_writer:
  44. for i in range(1, 5):
  45. test_data = get_test_data(i)
  46. _cache_summary_tensor_data(test_data)
  47. test_writer.record(i)
  48. def get_test_data_shape_1(step):
  49. """ get_test_data_shape_1 """
  50. test_data_list = []
  51. tag1 = "x1[:Scalar]"
  52. tag2 = "x2[:Scalar]"
  53. np1 = np.array([step + 1]).astype(np.float32)
  54. np2 = np.array([step + 2]).astype(np.float32)
  55. dict1 = {}
  56. dict1["name"] = tag1
  57. dict1["data"] = Tensor(np1)
  58. dict2 = {}
  59. dict2["name"] = tag2
  60. dict2["data"] = Tensor(np2)
  61. test_data_list.append(dict1)
  62. test_data_list.append(dict2)
  63. return test_data_list
  64. # Test: shape = (1,)
  65. def test_scalar_summary_sample_with_shape_1():
  66. """ test_scalar_summary_sample_with_shape_1 """
  67. with SummaryRecord(SUMMARY_DIR, file_suffix="_MS_SCALAR") as test_writer:
  68. for i in range(1, 100):
  69. test_data = get_test_data_shape_1(i)
  70. _cache_summary_tensor_data(test_data)
  71. test_writer.record(i)
  72. # Test: test with ge
  73. class SummaryDemo(nn.Cell):
  74. """ SummaryDemo definition """
  75. def __init__(self,):
  76. super(SummaryDemo, self).__init__()
  77. self.s = P.ScalarSummary()
  78. self.histogram_summary = P.HistogramSummary()
  79. self.add = P.TensorAdd()
  80. def construct(self, x, y):
  81. self.s("x1", x)
  82. z = self.add(x, y)
  83. self.s("z1", z)
  84. self.s("y1", y)
  85. self.histogram_summary("histogram", z)
  86. return z
  87. def test_scalar_summary_with_ge():
  88. """ test_scalar_summary_with_ge """
  89. with SummaryRecord(SUMMARY_DIR, file_suffix="_MS_SCALAR") as test_writer:
  90. net = SummaryDemo()
  91. net.set_train()
  92. # step 2: create the Event
  93. steps = 100
  94. for i in range(1, steps):
  95. x = Tensor(np.array([1.1 + random.uniform(1, 10)]).astype(np.float32))
  96. y = Tensor(np.array([1.2 + random.uniform(1, 10)]).astype(np.float32))
  97. net(x, y)
  98. test_writer.record(i)
  99. # test the problem of two consecutive use cases going wrong
  100. def test_scalar_summary_with_ge_2():
  101. """ test_scalar_summary_with_ge_2 """
  102. with SummaryRecord(SUMMARY_DIR, file_suffix="_MS_SCALAR") as test_writer:
  103. net = SummaryDemo()
  104. net.set_train()
  105. steps = 100
  106. for i in range(1, steps):
  107. x = Tensor(np.array([1.1]).astype(np.float32))
  108. y = Tensor(np.array([1.2]).astype(np.float32))
  109. net(x, y)
  110. test_writer.record(i)