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

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