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_lenet.py 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. Function:
  17. Test the various combinations based on LeNet.
  18. """
  19. import os
  20. import pytest
  21. from mindinsight.wizard.base.utility import load_network_maker
  22. NETWORK_NAME = 'lenet'
  23. class TestLeNet:
  24. """Test LeNet Module."""
  25. @pytest.mark.level0
  26. @pytest.mark.env_single
  27. @pytest.mark.platform_x86_cpu
  28. @pytest.mark.platform_arm_ascend_training
  29. @pytest.mark.platform_x86_gpu_training
  30. @pytest.mark.platform_x86_ascend_training
  31. @pytest.mark.parametrize('params', [{
  32. 'config': {'loss': 'SoftmaxCrossEntropyWithLogits',
  33. 'optimizer': 'Momentum',
  34. 'dataset': 'MNIST'},
  35. 'dataset_loader_name': 'MnistDataset'
  36. }, {
  37. 'config': {'loss': 'SoftmaxCrossEntropyWithLogits',
  38. 'optimizer': 'Adam',
  39. 'dataset': 'MNIST'},
  40. 'dataset_loader_name': 'MnistDataset'
  41. }, {
  42. 'config': {'loss': 'SoftmaxCrossEntropyWithLogits',
  43. 'optimizer': 'SGD',
  44. 'dataset': 'MNIST'},
  45. 'dataset_loader_name': 'MnistDataset'
  46. }])
  47. def test_combinations(self, params):
  48. """Do testing."""
  49. network_maker_name = NETWORK_NAME
  50. config = params['config']
  51. dataset_loader_name = params['dataset_loader_name']
  52. network_maker = load_network_maker(network_maker_name)
  53. network_maker.configure(config)
  54. self.source_files = network_maker.generate(**config)
  55. self.check_scripts()
  56. self.check_src(dataset_loader_name, config)
  57. self.check_train_eval_readme(config['loss'], config['optimizer'])
  58. def check_src(self, dataset_name, config):
  59. """Check src file."""
  60. dataset_is_right = False
  61. config_optimizer_is_right = False
  62. network_is_right = False
  63. for source_file in self.source_files:
  64. if source_file.file_relative_path == 'src/dataset.py':
  65. if dataset_name in source_file.content:
  66. dataset_is_right = True
  67. if source_file.file_relative_path == os.path.join('src', NETWORK_NAME.lower() + '.py'):
  68. network_is_right = True
  69. if source_file.file_relative_path == 'src/config.py':
  70. content = source_file.content
  71. if config['optimizer'] == 'Momentum':
  72. if "'lr': 0.01" in content and \
  73. "'momentum': 0.9" in content:
  74. config_optimizer_is_right = True
  75. elif config['optimizer'] == 'SGD':
  76. if "'lr': 0.01" in content:
  77. config_optimizer_is_right = True
  78. else:
  79. if "'lr': 0.001" in content:
  80. config_optimizer_is_right = True
  81. assert dataset_is_right
  82. assert config_optimizer_is_right
  83. assert network_is_right
  84. def check_train_eval_readme(self, loss_name, optimizer_name):
  85. """Check train and eval."""
  86. train_is_right = False
  87. eval_is_right = False
  88. readme_is_right = False
  89. for source_file in self.source_files:
  90. if source_file.file_relative_path == 'train.py':
  91. content = source_file.content
  92. if 'LeNet5' in content and loss_name in content and optimizer_name in content:
  93. train_is_right = True
  94. if source_file.file_relative_path == 'eval.py':
  95. content = source_file.content
  96. if 'LeNet5' in content and loss_name in content:
  97. eval_is_right = True
  98. if source_file.file_relative_path == 'README.md':
  99. content = source_file.content
  100. if 'LeNet' in content:
  101. readme_is_right = True
  102. assert train_is_right
  103. assert eval_is_right
  104. assert readme_is_right
  105. def check_scripts(self):
  106. """Check scripts."""
  107. exist_run_distribute_train = False
  108. exist_run_distribute_train_gpu = False
  109. exist_run_eval = False
  110. exist_run_eval_gpu = False
  111. exist_run_standalone_train = False
  112. exist_run_standalone_train_gpu = False
  113. for source_file in self.source_files:
  114. if source_file.file_relative_path == 'scripts/run_distribute_train.sh':
  115. exist_run_distribute_train = True
  116. if source_file.file_relative_path == 'scripts/run_distribute_train_gpu.sh':
  117. exist_run_distribute_train_gpu = True
  118. if source_file.file_relative_path == 'scripts/run_eval.sh':
  119. exist_run_eval = True
  120. if source_file.file_relative_path == 'scripts/run_eval_gpu.sh':
  121. exist_run_eval_gpu = True
  122. if source_file.file_relative_path == 'scripts/run_standalone_train.sh':
  123. exist_run_standalone_train = True
  124. if source_file.file_relative_path == 'scripts/run_standalone_train_gpu.sh':
  125. exist_run_standalone_train_gpu = True
  126. assert exist_run_distribute_train
  127. assert exist_run_distribute_train_gpu
  128. assert exist_run_eval
  129. assert exist_run_eval_gpu
  130. assert exist_run_standalone_train
  131. assert exist_run_standalone_train_gpu