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_gpu_summary.py 3.6 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. import os
  16. import random
  17. import shutil
  18. import pytest
  19. import numpy as np
  20. import mindspore.context as context
  21. import mindspore.nn as nn
  22. from mindspore.common.tensor import Tensor
  23. from mindspore.ops import operations as P
  24. from mindspore.train.summary.summary_record import SummaryRecord
  25. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  26. CUR_DIR = os.getcwd()
  27. SUMMARY_DIR_ME = CUR_DIR + "/test_me_summary_event_file/"
  28. SUMMARY_DIR_ME_TEMP = CUR_DIR + "/test_me_temp_summary_event_file/"
  29. def clean_environment_file(srcDir):
  30. if os.path.exists(srcDir):
  31. ls = os.listdir(srcDir)
  32. for line in ls:
  33. filePath = os.path.join(srcDir, line)
  34. os.remove(filePath)
  35. os.removedirs(srcDir)
  36. def save_summary_events_file(srcDir, desDir):
  37. if not os.path.exists(desDir):
  38. print("-- create desDir")
  39. os.makedirs(desDir)
  40. ls = os.listdir(srcDir)
  41. for line in ls:
  42. filePath = os.path.join(srcDir, line)
  43. if os.path.isfile(filePath):
  44. print("-- move events file : {}".format(filePath))
  45. shutil.copy(filePath, desDir)
  46. os.remove(filePath)
  47. os.removedirs(srcDir)
  48. class SummaryNet(nn.Cell):
  49. def __init__(self, tag_tuple=None, scalar=1):
  50. super(SummaryNet, self).__init__()
  51. self.summary_s = P.ScalarSummary()
  52. self.summary_i = P.ImageSummary()
  53. self.summary_t = P.TensorSummary()
  54. self.histogram_summary = P.HistogramSummary()
  55. self.add = P.TensorAdd()
  56. self.tag_tuple = tag_tuple
  57. self.scalar = scalar
  58. def construct(self, x, y):
  59. self.summary_i("image", x)
  60. self.summary_s("x1", x)
  61. z = self.add(x, y)
  62. self.summary_t("z1", z)
  63. self.histogram_summary("histogram", z)
  64. return z
  65. def train_summary_record_scalar_for_1(test_writer, steps):
  66. net = SummaryNet()
  67. out_me_dict = {}
  68. for i in range(0, steps):
  69. x = Tensor(np.array([1.1 + random.uniform(1, 10)]).astype(np.float32))
  70. y = Tensor(np.array([1.2 + random.uniform(1, 10)]).astype(np.float32))
  71. out_put = net(x, y)
  72. test_writer.record(i)
  73. print("-----------------output: %s-------------\n", out_put.asnumpy())
  74. out_me_dict[i] = out_put.asnumpy()
  75. return out_me_dict
  76. def me_scalar_summary(steps):
  77. with SummaryRecord(SUMMARY_DIR_ME_TEMP) as test_writer:
  78. out_me_dict = train_summary_record_scalar_for_1(test_writer, steps)
  79. return out_me_dict
  80. @pytest.mark.level0
  81. @pytest.mark.platform_x86_gpu_training
  82. @pytest.mark.env_onecard
  83. def test_scalarsummary_scalar1_step10_summaryrecord1():
  84. clean_environment_file(SUMMARY_DIR_ME_TEMP)
  85. output_dict = me_scalar_summary(10)
  86. print("test_scalarsummary_scalar1_step10_summaryrecord1 \n", output_dict)
  87. save_summary_events_file(SUMMARY_DIR_ME_TEMP, SUMMARY_DIR_ME)
  88. clean_environment_file(SUMMARY_DIR_ME)