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.

util.py 6.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 json
  16. import os
  17. import numpy as np
  18. #import jsbeautifier
  19. from mindspore import log as logger
  20. COLUMNS = ["col_1d", "col_2d", "col_3d", "col_binary", "col_float",
  21. "col_sint16", "col_sint32", "col_sint64"]
  22. def save_golden(cur_dir, golden_ref_dir, result_dict):
  23. """
  24. Save the dictionary values as the golden result in .npz file
  25. """
  26. logger.info("cur_dir is {}".format(cur_dir))
  27. logger.info("golden_ref_dir is {}".format(golden_ref_dir))
  28. np.savez(golden_ref_dir, np.array(list(result_dict.values())))
  29. def save_golden_dict(cur_dir, golden_ref_dir, result_dict):
  30. """
  31. Save the dictionary (both keys and values) as the golden result in .npz file
  32. """
  33. logger.info("cur_dir is {}".format(cur_dir))
  34. logger.info("golden_ref_dir is {}".format(golden_ref_dir))
  35. np.savez(golden_ref_dir, np.array(list(result_dict.items())))
  36. def compare_to_golden(golden_ref_dir, result_dict):
  37. """
  38. Compare as numpy arrays the test result to the golden result
  39. """
  40. test_array = np.array(list(result_dict.values()))
  41. golden_array = np.load(golden_ref_dir, allow_pickle=True)['arr_0']
  42. assert np.array_equal(test_array, golden_array)
  43. def compare_to_golden_dict(golden_ref_dir, result_dict):
  44. """
  45. Compare as dictionaries the test result to the golden result
  46. """
  47. golden_array = np.load(golden_ref_dir, allow_pickle=True)['arr_0']
  48. assert result_dict == dict(golden_array)
  49. def save_json(filename, parameters, result_dict):
  50. """
  51. Save the result dictionary in json file
  52. """
  53. fout = open(filename[:-3] + "json", "w")
  54. options = jsbeautifier.default_options()
  55. options.indent_size = 2
  56. out_dict = {**parameters, **{"columns": result_dict}}
  57. fout.write(jsbeautifier.beautify(json.dumps(out_dict), options))
  58. def save_and_check(data, parameters, filename, generate_golden=False):
  59. """
  60. Save the dataset dictionary and compare (as numpy array) with golden file.
  61. Use create_dict_iterator to access the dataset.
  62. """
  63. num_iter = 0
  64. result_dict = {}
  65. for column_name in COLUMNS:
  66. result_dict[column_name] = []
  67. for item in data.create_dict_iterator(): # each data is a dictionary
  68. for data_key in list(item.keys()):
  69. if data_key not in result_dict:
  70. result_dict[data_key] = []
  71. result_dict[data_key].append(item[data_key].tolist())
  72. num_iter += 1
  73. logger.info("Number of data in data1: {}".format(num_iter))
  74. cur_dir = os.path.dirname(os.path.realpath(__file__))
  75. golden_ref_dir = os.path.join(cur_dir, "../../data/dataset", 'golden', filename)
  76. if generate_golden:
  77. # Save as the golden result
  78. save_golden(cur_dir, golden_ref_dir, result_dict)
  79. compare_to_golden(golden_ref_dir, result_dict)
  80. # Save to a json file for inspection
  81. # save_json(filename, parameters, result_dict)
  82. def save_and_check_dict(data, parameters, filename, generate_golden=False):
  83. """
  84. Save the dataset dictionary and compare (as dictionary) with golden file.
  85. Use create_dict_iterator to access the dataset.
  86. """
  87. num_iter = 0
  88. result_dict = {}
  89. for item in data.create_dict_iterator(): # each data is a dictionary
  90. for data_key in list(item.keys()):
  91. if data_key not in result_dict:
  92. result_dict[data_key] = []
  93. result_dict[data_key].append(item[data_key].tolist())
  94. num_iter += 1
  95. logger.info("Number of data in ds1: {}".format(num_iter))
  96. cur_dir = os.path.dirname(os.path.realpath(__file__))
  97. golden_ref_dir = os.path.join(cur_dir, "../../data/dataset", 'golden', filename)
  98. if generate_golden:
  99. # Save as the golden result
  100. save_golden_dict(cur_dir, golden_ref_dir, result_dict)
  101. compare_to_golden_dict(golden_ref_dir, result_dict)
  102. # Save to a json file for inspection
  103. # save_json(filename, parameters, result_dict)
  104. def ordered_save_and_check(data, parameters, filename, generate_golden=False):
  105. """
  106. Save the dataset dictionary and compare (as numpy array) with golden file.
  107. Use create_tuple_iterator to access the dataset.
  108. """
  109. num_iter = 0
  110. result_dict = {}
  111. for item in data.create_tuple_iterator(): # each data is a dictionary
  112. for data_key in range(0, len(item)):
  113. if data_key not in result_dict:
  114. result_dict[data_key] = []
  115. result_dict[data_key].append(item[data_key].tolist())
  116. num_iter += 1
  117. logger.info("Number of data in data1: {}".format(num_iter))
  118. cur_dir = os.path.dirname(os.path.realpath(__file__))
  119. golden_ref_dir = os.path.join(cur_dir, "../../data/dataset", 'golden', filename)
  120. if generate_golden:
  121. # Save as the golden result
  122. save_golden(cur_dir, golden_ref_dir, result_dict)
  123. compare_to_golden(golden_ref_dir, result_dict)
  124. # Save to a json file for inspection
  125. # save_json(filename, parameters, result_dict)
  126. def diff_mse(in1, in2):
  127. mse = (np.square(in1.astype(float) / 255 - in2.astype(float) / 255)).mean()
  128. return mse * 100
  129. def diff_me(in1, in2):
  130. mse = (np.abs(in1.astype(float) - in2.astype(float))).mean()
  131. return mse / 255 * 100
  132. def diff_ssim(in1, in2):
  133. from skimage.measure import compare_ssim as ssim
  134. val = ssim(in1, in2, multichannel=True)
  135. return (1 - val) * 100