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 6.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. 'config': {'loss': 'SoftmaxCrossEntropyExpand',
  48. 'optimizer': 'Momentum',
  49. 'dataset': 'MNIST'},
  50. 'dataset_loader_name': 'MnistDataset'
  51. }, {
  52. 'config': {'loss': 'SoftmaxCrossEntropyExpand',
  53. 'optimizer': 'Adam',
  54. 'dataset': 'MNIST'},
  55. 'dataset_loader_name': 'MnistDataset'
  56. }, {
  57. 'config': {'loss': 'SoftmaxCrossEntropyWithLogits',
  58. 'optimizer': 'SGD',
  59. 'dataset': 'MNIST'},
  60. 'dataset_loader_name': 'MnistDataset'
  61. }])
  62. def test_combinations(self, params):
  63. """Do testing"""
  64. network_maker_name = NETWORK_NAME
  65. config = params['config']
  66. dataset_loader_name = params['dataset_loader_name']
  67. network_maker = load_network_maker(network_maker_name)
  68. network_maker.configure(config)
  69. self.source_files = network_maker.generate(**config)
  70. self.check_scripts()
  71. self.check_src(dataset_loader_name, config)
  72. self.check_train_eval_readme(config['loss'], config['optimizer'])
  73. def check_src(self, dataset_name, config):
  74. """Check src file"""
  75. dataset_is_right = False
  76. config_optimizer_is_right = False
  77. network_is_right = False
  78. for source_file in self.source_files:
  79. if source_file.file_relative_path == 'src/dataset.py':
  80. if dataset_name in source_file.content:
  81. dataset_is_right = True
  82. if source_file.file_relative_path == os.path.join('src', NETWORK_NAME.lower() + '.py'):
  83. network_is_right = True
  84. if source_file.file_relative_path == 'src/config.py':
  85. content = source_file.content
  86. if config['optimizer'] == 'Momentum':
  87. if "'lr': 0.01" in content and \
  88. "'momentum': 0.9" in content:
  89. config_optimizer_is_right = True
  90. elif config['optimizer'] == 'SGD':
  91. if "'lr': 0.01" in content:
  92. config_optimizer_is_right = True
  93. else:
  94. if "'lr': 0.001" in content:
  95. config_optimizer_is_right = True
  96. assert dataset_is_right
  97. assert config_optimizer_is_right
  98. assert network_is_right
  99. def check_train_eval_readme(self, loss_name, optimizer_name):
  100. """Check train and eval"""
  101. train_is_right = False
  102. eval_is_right = False
  103. readme_is_right = False
  104. for source_file in self.source_files:
  105. if source_file.file_relative_path == 'train.py':
  106. content = source_file.content
  107. if 'LeNet5' in content and loss_name in content and optimizer_name in content:
  108. train_is_right = True
  109. if source_file.file_relative_path == 'eval.py':
  110. content = source_file.content
  111. if 'LeNet5' in content and loss_name in content:
  112. eval_is_right = True
  113. if source_file.file_relative_path == 'README.md':
  114. content = source_file.content
  115. if 'LeNet' in content:
  116. readme_is_right = True
  117. assert train_is_right
  118. assert eval_is_right
  119. assert readme_is_right
  120. def check_scripts(self):
  121. """Check scripts"""
  122. exist_run_distribute_train = False
  123. exist_run_distribute_train_gpu = False
  124. exist_run_eval = False
  125. exist_run_eval_gpu = False
  126. exist_run_standalone_train = False
  127. exist_run_standalone_train_gpu = False
  128. for source_file in self.source_files:
  129. if source_file.file_relative_path == 'scripts/run_distribute_train.sh':
  130. exist_run_distribute_train = True
  131. if source_file.file_relative_path == 'scripts/run_distribute_train_gpu.sh':
  132. exist_run_distribute_train_gpu = True
  133. if source_file.file_relative_path == 'scripts/run_eval.sh':
  134. exist_run_eval = True
  135. if source_file.file_relative_path == 'scripts/run_eval_gpu.sh':
  136. exist_run_eval_gpu = True
  137. if source_file.file_relative_path == 'scripts/run_standalone_train.sh':
  138. exist_run_standalone_train = True
  139. if source_file.file_relative_path == 'scripts/run_standalone_train_gpu.sh':
  140. exist_run_standalone_train_gpu = True
  141. assert exist_run_distribute_train
  142. assert exist_run_distribute_train_gpu
  143. assert exist_run_eval
  144. assert exist_run_eval_gpu
  145. assert exist_run_standalone_train
  146. assert exist_run_standalone_train_gpu