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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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': 'SoftmaxCrossEntropyExpand',
  48. 'optimizer': 'Momentum',
  49. 'dataset': 'Cifar10'},
  50. 'dataset_loader_name': 'Cifar10Dataset'
  51. }, {
  52. 'config': {'loss': 'SoftmaxCrossEntropyExpand',
  53. 'optimizer': 'Adam',
  54. 'dataset': 'Cifar10'},
  55. 'dataset_loader_name': 'Cifar10Dataset'
  56. }, {
  57. 'config': {'loss': 'SoftmaxCrossEntropyExpand',
  58. 'optimizer': 'SGD',
  59. 'dataset': 'Cifar10'},
  60. 'dataset_loader_name': 'Cifar10Dataset'
  61. }, {
  62. 'config': {'loss': 'SoftmaxCrossEntropyWithLogits',
  63. 'optimizer': 'Momentum',
  64. 'dataset': 'ImageNet'},
  65. 'dataset_loader_name': 'ImageFolderDatasetV2'
  66. }, {
  67. 'config': {'loss': 'SoftmaxCrossEntropyWithLogits',
  68. 'optimizer': 'Adam',
  69. 'dataset': 'ImageNet'},
  70. 'dataset_loader_name': 'ImageFolderDatasetV2'
  71. }, {
  72. 'config': {'loss': 'SoftmaxCrossEntropyWithLogits',
  73. 'optimizer': 'SGD',
  74. 'dataset': 'ImageNet'},
  75. 'dataset_loader_name': 'ImageFolderDatasetV2'
  76. }, {
  77. 'config': {'loss': 'SoftmaxCrossEntropyExpand',
  78. 'optimizer': 'Momentum',
  79. 'dataset': 'ImageNet'},
  80. 'dataset_loader_name': 'ImageFolderDatasetV2'
  81. }, {
  82. 'config': {'loss': 'SoftmaxCrossEntropyExpand',
  83. 'optimizer': 'Adam',
  84. 'dataset': 'ImageNet'},
  85. 'dataset_loader_name': 'ImageFolderDatasetV2'
  86. }, {
  87. 'config': {'loss': 'SoftmaxCrossEntropyExpand',
  88. 'optimizer': 'SGD',
  89. 'dataset': 'ImageNet'},
  90. 'dataset_loader_name': 'ImageFolderDatasetV2'
  91. }])
  92. def test_combinations(self, params):
  93. """Do testing"""
  94. network_maker_name = NETWORK_NAME
  95. config = params['config']
  96. dataset_loader_name = params['dataset_loader_name']
  97. network_maker = load_network_maker(network_maker_name)
  98. network_maker.configure(config)
  99. self.source_files = network_maker.generate(**config)
  100. self.check_scripts()
  101. self.check_src(dataset_loader_name, config)
  102. self.check_train_eval_readme(config['dataset'], config['loss'], config['optimizer'])
  103. def check_src(self, dataset_name, config):
  104. """Check src file"""
  105. dataset_is_right = False
  106. config_dataset_is_right = False
  107. config_optimizer_is_right = False
  108. network_is_right = False
  109. cross_entorpy_smooth_is_right = False
  110. generator_lr_is_right = False
  111. for source_file in self.source_files:
  112. if source_file.file_relative_path == 'src/dataset.py':
  113. if dataset_name in source_file.content:
  114. dataset_is_right = True
  115. if source_file.file_relative_path == os.path.join('src', NETWORK_NAME.lower() + '.py'):
  116. network_is_right = True
  117. if source_file.file_relative_path == 'src/CrossEntropySmooth.py':
  118. cross_entorpy_smooth_is_right = True
  119. if source_file.file_relative_path == 'src/lr_generator.py':
  120. generator_lr_is_right = True
  121. if source_file.file_relative_path == 'src/config.py':
  122. content = source_file.content
  123. config_dataset_is_right = self._check_config_dataset(config, content)
  124. config_optimizer_is_right = self._check_config_optimizer(config, content)
  125. assert dataset_is_right
  126. assert config_dataset_is_right
  127. assert config_optimizer_is_right
  128. assert network_is_right
  129. assert cross_entorpy_smooth_is_right
  130. assert generator_lr_is_right
  131. @staticmethod
  132. def _check_config_dataset(config, content):
  133. """Check dataset in config"""
  134. config_dataset_is_right = False
  135. if config['dataset'] == 'Cifar10':
  136. if "'num_classes': 10" in content \
  137. and "'warmup_epochs': 5" in content \
  138. and "'lr_decay_mode': 'poly'" in content:
  139. config_dataset_is_right = True
  140. elif config['dataset'] == 'ImageNet':
  141. if "'num_classes': 1001" in content \
  142. and "'warmup_epochs': 0" in content \
  143. and "'lr_decay_mode': 'cosine'":
  144. config_dataset_is_right = True
  145. return config_dataset_is_right
  146. @staticmethod
  147. def _check_config_optimizer(config, content):
  148. """Check optimizer in config"""
  149. config_optimizer_is_right = False
  150. if config['optimizer'] == 'Momentum':
  151. if "'lr': 0.01" in content and \
  152. "'momentum': 0.9" in content:
  153. config_optimizer_is_right = True
  154. elif config['optimizer'] == 'SGD':
  155. if "'lr': 0.01" in content:
  156. config_optimizer_is_right = True
  157. else:
  158. if "'lr': 0.001" in content:
  159. config_optimizer_is_right = True
  160. return config_optimizer_is_right
  161. def check_train_eval_readme(self, dataset_name, loss_name, optimizer_name):
  162. """Check train and eval"""
  163. train_is_right = False
  164. eval_is_right = False
  165. readme_is_right = False
  166. for source_file in self.source_files:
  167. if source_file.file_relative_path == 'train.py':
  168. content = source_file.content
  169. if 'resnet50' in content and optimizer_name in content:
  170. if dataset_name == 'ImageNet' and loss_name == 'SoftmaxCrossEntropyWithLogits' \
  171. and 'loss = CrossEntropySmooth' in content:
  172. train_is_right = True
  173. elif loss_name in content:
  174. train_is_right = True
  175. if source_file.file_relative_path == 'eval.py':
  176. content = source_file.content
  177. if 'resnet50' in content:
  178. if dataset_name == 'ImageNet' and loss_name == 'SoftmaxCrossEntropyWithLogits' \
  179. and 'loss = CrossEntropySmooth' in content:
  180. eval_is_right = True
  181. elif loss_name in content:
  182. eval_is_right = True
  183. if source_file.file_relative_path == 'README.md':
  184. content = source_file.content
  185. if 'ResNet50' in content and dataset_name in content:
  186. readme_is_right = True
  187. assert train_is_right
  188. assert eval_is_right
  189. assert readme_is_right
  190. def check_scripts(self):
  191. """Check scripts"""
  192. exist_run_distribute_train = False
  193. exist_run_distribute_train_gpu = False
  194. exist_run_eval = False
  195. exist_run_eval_gpu = False
  196. exist_run_standalone_train = False
  197. exist_run_standalone_train_gpu = False
  198. for source_file in self.source_files:
  199. if source_file.file_relative_path == 'scripts/run_distribute_train.sh':
  200. exist_run_distribute_train = True
  201. if source_file.file_relative_path == 'scripts/run_distribute_train_gpu.sh':
  202. exist_run_distribute_train_gpu = True
  203. if source_file.file_relative_path == 'scripts/run_eval.sh':
  204. exist_run_eval = True
  205. if source_file.file_relative_path == 'scripts/run_eval_gpu.sh':
  206. exist_run_eval_gpu = True
  207. if source_file.file_relative_path == 'scripts/run_standalone_train.sh':
  208. exist_run_standalone_train = True
  209. if source_file.file_relative_path == 'scripts/run_standalone_train_gpu.sh':
  210. exist_run_standalone_train_gpu = True
  211. assert exist_run_distribute_train
  212. assert exist_run_distribute_train_gpu
  213. assert exist_run_eval
  214. assert exist_run_eval_gpu
  215. assert exist_run_standalone_train
  216. assert exist_run_standalone_train_gpu