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_lineage_api.py 4.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. """Test the module of lineage_api."""
  16. import json
  17. import os
  18. from unittest import TestCase, mock
  19. from flask import Response
  20. from mindinsight.backend.application import APP
  21. from mindinsight.lineagemgr.common.exceptions.exceptions import \
  22. LineageQuerySummaryDataError
  23. LINEAGE_FILTRATION_BASE = {
  24. 'accuracy': None,
  25. 'mae': None,
  26. 'mse': None,
  27. 'loss_function': 'SoftmaxCrossEntropyWithLogits',
  28. 'train_dataset_path': None,
  29. 'train_dataset_count': 64,
  30. 'test_dataset_path': None,
  31. 'test_dataset_count': None,
  32. 'network': 'str',
  33. 'optimizer': 'Momentum',
  34. 'learning_rate': 0.11999999731779099,
  35. 'epoch': 12,
  36. 'batch_size': 32,
  37. 'loss': 0.029999999329447746,
  38. 'model_size': 128
  39. }
  40. LINEAGE_FILTRATION_RUN1 = {
  41. 'accuracy': 0.78,
  42. 'mae': None,
  43. 'mse': None,
  44. 'loss_function': 'SoftmaxCrossEntropyWithLogits',
  45. 'train_dataset_path': None,
  46. 'train_dataset_count': 64,
  47. 'test_dataset_path': None,
  48. 'test_dataset_count': 64,
  49. 'network': 'str',
  50. 'optimizer': 'Momentum',
  51. 'learning_rate': 0.11999999731779099,
  52. 'epoch': 14,
  53. 'batch_size': 32,
  54. 'loss': 0.029999999329447746,
  55. 'model_size': 128
  56. }
  57. class TestSearchModel(TestCase):
  58. """Test the restful api of search_model."""
  59. def setUp(self):
  60. """Test init."""
  61. APP.response_class = Response
  62. self.app_client = APP.test_client()
  63. self.url = '/v1/mindinsight/models/model_lineage'
  64. @mock.patch('mindinsight.backend.lineagemgr.lineage_api.settings')
  65. @mock.patch('mindinsight.backend.lineagemgr.lineage_api.filter_summary_lineage')
  66. def test_search_model_success(self, *args):
  67. """Test the success of model_success."""
  68. base_dir = '/path/to/test_lineage_summary_dir_base'
  69. args[0].return_value = {
  70. 'object': [
  71. {
  72. 'summary_dir': base_dir,
  73. **LINEAGE_FILTRATION_BASE
  74. },
  75. {
  76. 'summary_dir': os.path.join(base_dir, 'run1'),
  77. **LINEAGE_FILTRATION_RUN1
  78. }
  79. ],
  80. 'count': 2
  81. }
  82. args[1].SUMMARY_BASE_DIR = base_dir
  83. body_data = {
  84. 'limit': 10,
  85. 'offset': 0,
  86. 'sorted_name': 'summary_dir',
  87. 'sorted_type': None
  88. }
  89. response = self.app_client.post(self.url, data=json.dumps(body_data))
  90. self.assertEqual(200, response.status_code)
  91. expect_result = {
  92. 'object': [
  93. {
  94. 'summary_dir': './',
  95. **LINEAGE_FILTRATION_BASE
  96. },
  97. {
  98. 'summary_dir': './run1',
  99. **LINEAGE_FILTRATION_RUN1
  100. }
  101. ],
  102. 'count': 2
  103. }
  104. self.assertDictEqual(expect_result, response.get_json())
  105. @mock.patch('mindinsight.backend.lineagemgr.lineage_api.settings')
  106. @mock.patch('mindinsight.backend.lineagemgr.lineage_api.filter_summary_lineage')
  107. def test_search_model_fail(self, *args):
  108. """Test the function of model_lineage with exception."""
  109. response = self.app_client.post(self.url, data='xxx')
  110. self.assertEqual(400, response.status_code)
  111. expect_result = {
  112. 'error_code': '50540002',
  113. 'error_msg': 'Invalid parameter value. Json data parse failed.'
  114. }
  115. self.assertDictEqual(expect_result, response.get_json())
  116. args[0].side_effect = LineageQuerySummaryDataError('xxx')
  117. args[1].SUMMARY_BASE_DIR = '/path/to/test_lineage_summary_dir_base'
  118. body_data = {
  119. 'limit': 10,
  120. 'offset': 0,
  121. 'sorted_name': 'summary_dir',
  122. 'sorted_type': None
  123. }
  124. response = self.app_client.post(self.url, data=json.dumps(body_data))
  125. self.assertEqual(400, response.status_code)
  126. expect_result = {
  127. 'error_code': '50542215',
  128. 'error_msg': 'Query summary data error: xxx'
  129. }
  130. self.assertDictEqual(expect_result, response.get_json())

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