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.

lineage_api.py 4.7 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. """Lineage restful api."""
  16. import json
  17. import os
  18. from flask import Blueprint, jsonify, request
  19. from mindinsight.conf import settings
  20. from mindinsight.datavisual.utils.tools import get_train_id
  21. from mindinsight.lineagemgr import filter_summary_lineage, get_summary_lineage
  22. from mindinsight.lineagemgr.common.validator.validate import validate_path
  23. from mindinsight.utils.exceptions import MindInsightException, ParamValueError
  24. BLUEPRINT = Blueprint("lineage", __name__, url_prefix=settings.URL_PREFIX.rstrip("/"))
  25. @BLUEPRINT.route("/lineagemgr/lineages", methods=["POST"])
  26. def get_lineage():
  27. """
  28. Get lineage.
  29. Returns:
  30. str, the lineage information.
  31. Raises:
  32. MindInsightException: If method fails to be called.
  33. ParamValueError: If parsing json data search_condition fails.
  34. Examples:
  35. >>> POST http://xxxx/v1/mindinsight/lineagemgr/lineages
  36. """
  37. search_condition = request.stream.read()
  38. try:
  39. search_condition = json.loads(search_condition if search_condition else "{}")
  40. except Exception:
  41. raise ParamValueError("Json data parse failed.")
  42. lineage_info = _get_lineage_info(search_condition=search_condition)
  43. return jsonify(lineage_info)
  44. def _get_lineage_info(search_condition):
  45. """
  46. Get lineage info for dataset or model.
  47. Args:
  48. search_condition (dict): Search condition.
  49. Returns:
  50. dict, lineage info.
  51. Raises:
  52. MindInsightException: If method fails to be called.
  53. """
  54. summary_base_dir = str(settings.SUMMARY_BASE_DIR)
  55. try:
  56. lineage_info = filter_summary_lineage(
  57. summary_base_dir, search_condition)
  58. lineages = lineage_info['object']
  59. summary_base_dir = os.path.realpath(summary_base_dir)
  60. length = len(summary_base_dir)
  61. for lineage in lineages:
  62. summary_dir = lineage['summary_dir']
  63. summary_dir = os.path.realpath(summary_dir)
  64. if summary_base_dir == summary_dir:
  65. relative_dir = './'
  66. else:
  67. relative_dir = os.path.join(os.curdir, summary_dir[length+1:])
  68. lineage['summary_dir'] = relative_dir
  69. except MindInsightException as exception:
  70. raise MindInsightException(exception.error, exception.message, http_code=400)
  71. return lineage_info
  72. @BLUEPRINT.route("/datasets/dataset_graph", methods=["GET"])
  73. def get_dataset_graph():
  74. """
  75. Get dataset graph.
  76. Returns:
  77. str, the dataset graph information.
  78. Raises:
  79. MindInsightException: If method fails to be called.
  80. ParamValueError: If summary_dir is invalid.
  81. Examples:
  82. >>> GET http://xxxx/v1/mindinsight/datasets/dataset_graph?train_id=xxx
  83. """
  84. summary_base_dir = str(settings.SUMMARY_BASE_DIR)
  85. summary_dir = get_train_id(request)
  86. if summary_dir.startswith('/'):
  87. validate_path(summary_dir)
  88. elif summary_dir.startswith('./'):
  89. summary_dir = os.path.join(summary_base_dir, summary_dir[2:])
  90. summary_dir = validate_path(summary_dir)
  91. else:
  92. raise ParamValueError(
  93. "Summary dir should be absolute path or "
  94. "relative path that relate to summary base dir."
  95. )
  96. try:
  97. dataset_graph = get_summary_lineage(
  98. summary_dir=summary_dir,
  99. keys=['dataset_graph']
  100. )
  101. except MindInsightException as exception:
  102. raise MindInsightException(exception.error, exception.message, http_code=400)
  103. if dataset_graph:
  104. summary_dir_result = dataset_graph.get('summary_dir')
  105. base_dir_len = len(summary_base_dir)
  106. if summary_base_dir == summary_dir_result:
  107. relative_dir = './'
  108. else:
  109. relative_dir = os.path.join(
  110. os.curdir, summary_dir[base_dir_len + 1:]
  111. )
  112. dataset_graph['summary_dir'] = relative_dir
  113. return jsonify(dataset_graph)
  114. def init_module(app):
  115. """
  116. Init module entry.
  117. Args:
  118. app (Flask): The application obj.
  119. """
  120. app.register_blueprint(BLUEPRINT)

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