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_resnet50.py 8.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 ResNet50.
  18. """
  19. import os
  20. import pytest
  21. from mindinsight.wizard.base.utility import load_network_maker
  22. NETWORK_NAME = 'resnet50'
  23. class TestResNet50:
  24. """Test ResNet50 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': 'Cifar10'},
  35. 'dataset_loader_name': 'Cifar10Dataset'
  36. }, {
  37. 'config': {'loss': 'SoftmaxCrossEntropyWithLogits',
  38. 'optimizer': 'Adam',
  39. 'dataset': 'Cifar10'},
  40. 'dataset_loader_name': 'Cifar10Dataset'
  41. }, {
  42. 'config': {'loss': 'SoftmaxCrossEntropyWithLogits',
  43. 'optimizer': 'SGD',
  44. 'dataset': 'Cifar10'},
  45. 'dataset_loader_name': 'Cifar10Dataset'
  46. }, {
  47. 'config': {'loss': 'SoftmaxCrossEntropyWithLogits',
  48. 'optimizer': 'Momentum',
  49. 'dataset': 'ImageNet'},
  50. 'dataset_loader_name': 'ImageFolderDataset'
  51. }, {
  52. 'config': {'loss': 'SoftmaxCrossEntropyWithLogits',
  53. 'optimizer': 'Adam',
  54. 'dataset': 'ImageNet'},
  55. 'dataset_loader_name': 'ImageFolderDataset'
  56. }, {
  57. 'config': {'loss': 'SoftmaxCrossEntropyWithLogits',
  58. 'optimizer': 'SGD',
  59. 'dataset': 'ImageNet'},
  60. 'dataset_loader_name': 'ImageFolderDataset'
  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['dataset'], config['loss'], config['optimizer'])
  73. def check_src(self, dataset_name, config):
  74. """Check src file."""
  75. dataset_is_right = False
  76. config_dataset_is_right = False
  77. config_optimizer_is_right = False
  78. network_is_right = False
  79. cross_entorpy_smooth_is_right = False
  80. generator_lr_is_right = False
  81. for source_file in self.source_files:
  82. if source_file.file_relative_path == 'src/dataset.py':
  83. if dataset_name in source_file.content:
  84. dataset_is_right = True
  85. if source_file.file_relative_path == os.path.join('src', NETWORK_NAME.lower() + '.py'):
  86. network_is_right = True
  87. if source_file.file_relative_path == 'src/CrossEntropySmooth.py':
  88. cross_entorpy_smooth_is_right = True
  89. if source_file.file_relative_path == 'src/lr_generator.py':
  90. generator_lr_is_right = True
  91. if source_file.file_relative_path == 'src/config.py':
  92. content = source_file.content
  93. config_dataset_is_right = self._check_config_dataset(config, content)
  94. config_optimizer_is_right = self._check_config_optimizer(config, content)
  95. assert dataset_is_right
  96. assert config_dataset_is_right
  97. assert config_optimizer_is_right
  98. assert network_is_right
  99. assert cross_entorpy_smooth_is_right
  100. assert generator_lr_is_right
  101. @staticmethod
  102. def _check_config_dataset(config, content):
  103. """Check dataset in config."""
  104. config_dataset_is_right = False
  105. if config['dataset'] == 'Cifar10':
  106. if "'num_classes': 10" in content \
  107. and "'warmup_epochs': 5" in content \
  108. and "'lr_decay_mode': 'poly'" in content:
  109. config_dataset_is_right = True
  110. elif config['dataset'] == 'ImageNet':
  111. if "'num_classes': 1001" in content \
  112. and "'warmup_epochs': 0" in content \
  113. and "'lr_decay_mode': 'cosine'":
  114. config_dataset_is_right = True
  115. return config_dataset_is_right
  116. @staticmethod
  117. def _check_config_optimizer(config, content):
  118. """Check optimizer in config."""
  119. config_optimizer_is_right = False
  120. if config['optimizer'] == 'Momentum':
  121. if "'lr': 0.01" in content and \
  122. "'momentum': 0.9" in content:
  123. config_optimizer_is_right = True
  124. elif config['optimizer'] == 'SGD':
  125. if "'lr': 0.01" in content:
  126. config_optimizer_is_right = True
  127. else:
  128. if "'lr': 0.001" in content:
  129. config_optimizer_is_right = True
  130. return config_optimizer_is_right
  131. def check_train_eval_readme(self, dataset_name, loss_name, optimizer_name):
  132. """Check train and eval."""
  133. train_is_right = False
  134. eval_is_right = False
  135. readme_is_right = False
  136. for source_file in self.source_files:
  137. if source_file.file_relative_path == 'train.py':
  138. content = source_file.content
  139. if 'resnet50' in content and optimizer_name in content:
  140. if dataset_name == 'ImageNet' and loss_name == 'SoftmaxCrossEntropyWithLogits' \
  141. and 'loss = CrossEntropySmooth' in content:
  142. train_is_right = True
  143. elif loss_name in content:
  144. train_is_right = True
  145. if source_file.file_relative_path == 'eval.py':
  146. content = source_file.content
  147. if 'resnet50' in content:
  148. if dataset_name == 'ImageNet' and loss_name == 'SoftmaxCrossEntropyWithLogits' \
  149. and 'loss = CrossEntropySmooth' in content:
  150. eval_is_right = True
  151. elif loss_name in content:
  152. eval_is_right = True
  153. if source_file.file_relative_path == 'README.md':
  154. content = source_file.content
  155. if 'ResNet50' in content and dataset_name in content:
  156. readme_is_right = True
  157. assert train_is_right
  158. assert eval_is_right
  159. assert readme_is_right
  160. def check_scripts(self):
  161. """Check scripts."""
  162. exist_run_distribute_train = False
  163. exist_run_distribute_train_gpu = False
  164. exist_run_eval = False
  165. exist_run_eval_gpu = False
  166. exist_run_standalone_train = False
  167. exist_run_standalone_train_gpu = False
  168. for source_file in self.source_files:
  169. if source_file.file_relative_path == 'scripts/run_distribute_train.sh':
  170. exist_run_distribute_train = True
  171. if source_file.file_relative_path == 'scripts/run_distribute_train_gpu.sh':
  172. exist_run_distribute_train_gpu = True
  173. if source_file.file_relative_path == 'scripts/run_eval.sh':
  174. exist_run_eval = True
  175. if source_file.file_relative_path == 'scripts/run_eval_gpu.sh':
  176. exist_run_eval_gpu = True
  177. if source_file.file_relative_path == 'scripts/run_standalone_train.sh':
  178. exist_run_standalone_train = True
  179. if source_file.file_relative_path == 'scripts/run_standalone_train_gpu.sh':
  180. exist_run_standalone_train_gpu = True
  181. assert exist_run_distribute_train
  182. assert exist_run_distribute_train_gpu
  183. assert exist_run_eval
  184. assert exist_run_eval_gpu
  185. assert exist_run_standalone_train
  186. assert exist_run_standalone_train_gpu