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.

tools.py 2.4 kB

5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright 2020 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. """
  16. Description: This file is used for some common util.
  17. """
  18. import io
  19. import os
  20. import shutil
  21. import time
  22. import json
  23. from urllib.parse import urlencode
  24. import numpy as np
  25. from PIL import Image
  26. from mindinsight.datavisual.common.enums import DataManagerStatus
  27. def get_url(url, params):
  28. """
  29. Concatenate the URL and params.
  30. Args:
  31. url (str): A link requested. For example, http://example.com.
  32. params (dict): A dict consists of params. For example, {'offset': 1, 'limit':'100}.
  33. Returns:
  34. str, like http://example.com?offset=1&limit=100
  35. """
  36. return url + '?' + urlencode(params)
  37. def delete_files_or_dirs(path_list):
  38. """Delete files or dirs in path_list."""
  39. for path in path_list:
  40. if os.path.isdir(path):
  41. shutil.rmtree(path)
  42. else:
  43. os.remove(path)
  44. def check_loading_done(data_manager, time_limit=15, first_sleep_time=0):
  45. """If loading data for more than `time_limit` seconds, exit."""
  46. if first_sleep_time > 0:
  47. time.sleep(first_sleep_time)
  48. start_time = time.time()
  49. while data_manager.status != DataManagerStatus.DONE.value:
  50. time_used = time.time() - start_time
  51. if time_used > time_limit:
  52. break
  53. time.sleep(0.1)
  54. continue
  55. def get_image_tensor_from_bytes(image_string):
  56. """Get image tensor from bytes."""
  57. img = Image.open(io.BytesIO(image_string))
  58. image_tensor = np.array(img)
  59. return image_tensor
  60. def compare_result_with_file(result, expected_file_path):
  61. """Compare result with file which contain the expected results."""
  62. with open(expected_file_path, 'r') as file:
  63. expected_results = json.load(file)
  64. assert result == expected_results

MindInsight为MindSpore提供了简单易用的调优调试能力。在训练过程中,可以将标量、张量、图像、计算图、模型超参、训练耗时等数据记录到文件中,通过MindInsight可视化页面进行查看及分析。