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_cpu_summary.py 2.8 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright 2019 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. """Summary cpu st."""
  16. import os
  17. import platform
  18. import tempfile
  19. import numpy as np
  20. import pytest
  21. import mindspore.context as context
  22. import mindspore.nn as nn
  23. from mindspore import Tensor
  24. from mindspore.ops import operations as P
  25. from mindspore.train.summary.summary_record import SummaryRecord
  26. from tests.summary_utils import SummaryReader
  27. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  28. class SummaryNet(nn.Cell):
  29. def __init__(self):
  30. super().__init__()
  31. self.scalar_summary = P.ScalarSummary()
  32. self.image_summary = P.ImageSummary()
  33. self.tensor_summary = P.TensorSummary()
  34. self.histogram_summary = P.HistogramSummary()
  35. def construct(self, image_tensor):
  36. self.image_summary("image", image_tensor)
  37. self.tensor_summary("tensor", image_tensor)
  38. self.histogram_summary("histogram", image_tensor)
  39. scalar = image_tensor[0][0][0][0]
  40. self.scalar_summary("scalar", scalar)
  41. return scalar
  42. def train_summary_record(test_writer, steps):
  43. """Train and record summary."""
  44. net = SummaryNet()
  45. out_me_dict = {}
  46. for i in range(0, steps):
  47. image_tensor = Tensor(np.array([[[[i]]]]).astype(np.float32))
  48. out_put = net(image_tensor)
  49. test_writer.record(i)
  50. out_me_dict[i] = out_put.asnumpy()
  51. return out_me_dict
  52. @pytest.mark.level0
  53. @pytest.mark.platform_x86_cpu
  54. @pytest.mark.env_onecard
  55. def test_summary_step2_summary_record1():
  56. """Test record 10 step summary."""
  57. if platform.system() == "Windows":
  58. # Summary does not support windows currently.
  59. return
  60. with tempfile.TemporaryDirectory() as tmp_dir:
  61. steps = 2
  62. with SummaryRecord(tmp_dir) as test_writer:
  63. train_summary_record(test_writer, steps=steps)
  64. file_name = os.path.realpath(test_writer.full_file_name)
  65. with SummaryReader(file_name) as summary_writer:
  66. for _ in range(steps):
  67. event = summary_writer.read_event()
  68. tags = set(value.tag for value in event.summary.value)
  69. assert tags == {'tensor', 'histogram', 'scalar', 'image'}