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_face_detection_scrfd_trainer.py 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import glob
  3. import os
  4. import shutil
  5. import tempfile
  6. import unittest
  7. import torch
  8. from modelscope.hub.snapshot_download import snapshot_download
  9. from modelscope.metainfo import Trainers
  10. from modelscope.msdatasets import MsDataset
  11. from modelscope.trainers import build_trainer
  12. from modelscope.utils.config import Config
  13. from modelscope.utils.constant import ModelFile
  14. from modelscope.utils.test_utils import DistributedTestCase, test_level
  15. def _setup():
  16. model_id = 'damo/cv_resnet_facedetection_scrfd10gkps'
  17. # mini dataset only for unit test, remove '_mini' for full dataset.
  18. ms_ds_widerface = MsDataset.load('WIDER_FACE_mini', namespace='shaoxuan')
  19. data_path = ms_ds_widerface.config_kwargs['split_config']
  20. train_dir = data_path['train']
  21. val_dir = data_path['validation']
  22. train_root = train_dir + '/' + os.listdir(train_dir)[0] + '/'
  23. val_root = val_dir + '/' + os.listdir(val_dir)[0] + '/'
  24. max_epochs = 1 # run epochs in unit test
  25. cache_path = snapshot_download(model_id)
  26. tmp_dir = tempfile.TemporaryDirectory().name
  27. if not os.path.exists(tmp_dir):
  28. os.makedirs(tmp_dir)
  29. return train_root, val_root, max_epochs, cache_path, tmp_dir
  30. def train_func(**kwargs):
  31. trainer = build_trainer(
  32. name=Trainers.face_detection_scrfd, default_args=kwargs)
  33. trainer.train()
  34. class TestFaceDetectionScrfdTrainerSingleGPU(unittest.TestCase):
  35. def setUp(self):
  36. print(('SingleGPU Testing %s.%s' %
  37. (type(self).__name__, self._testMethodName)))
  38. self.train_root, self.val_root, self.max_epochs, self.cache_path, self.tmp_dir = _setup(
  39. )
  40. def tearDown(self):
  41. shutil.rmtree(self.tmp_dir)
  42. super().tearDown()
  43. def _cfg_modify_fn(self, cfg):
  44. cfg.checkpoint_config.interval = 1
  45. cfg.log_config.interval = 10
  46. cfg.evaluation.interval = 1
  47. cfg.data.workers_per_gpu = 3
  48. cfg.data.samples_per_gpu = 4 # batch size
  49. return cfg
  50. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  51. def test_trainer_from_scratch(self):
  52. kwargs = dict(
  53. cfg_file=os.path.join(self.cache_path, 'mmcv_scrfd.py'),
  54. work_dir=self.tmp_dir,
  55. train_root=self.train_root,
  56. val_root=self.val_root,
  57. total_epochs=self.max_epochs,
  58. cfg_modify_fn=self._cfg_modify_fn)
  59. trainer = build_trainer(
  60. name=Trainers.face_detection_scrfd, default_args=kwargs)
  61. trainer.train()
  62. results_files = os.listdir(self.tmp_dir)
  63. self.assertIn(f'{trainer.timestamp}.log.json', results_files)
  64. for i in range(self.max_epochs):
  65. self.assertIn(f'epoch_{i+1}.pth', results_files)
  66. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  67. def test_trainer_finetune(self):
  68. pretrain_epoch = 640
  69. self.max_epochs += pretrain_epoch
  70. kwargs = dict(
  71. cfg_file=os.path.join(self.cache_path, 'mmcv_scrfd.py'),
  72. work_dir=self.tmp_dir,
  73. train_root=self.train_root,
  74. val_root=self.val_root,
  75. total_epochs=self.max_epochs,
  76. resume_from=os.path.join(self.cache_path,
  77. ModelFile.TORCH_MODEL_BIN_FILE),
  78. cfg_modify_fn=self._cfg_modify_fn)
  79. trainer = build_trainer(
  80. name=Trainers.face_detection_scrfd, default_args=kwargs)
  81. trainer.train()
  82. results_files = os.listdir(self.tmp_dir)
  83. self.assertIn(f'{trainer.timestamp}.log.json', results_files)
  84. for i in range(pretrain_epoch, self.max_epochs):
  85. self.assertIn(f'epoch_{i+1}.pth', results_files)
  86. @unittest.skipIf(not torch.cuda.is_available()
  87. or torch.cuda.device_count() <= 1, 'distributed unittest')
  88. class TestFaceDetectionScrfdTrainerMultiGpus(DistributedTestCase):
  89. def setUp(self):
  90. print(('MultiGPUs Testing %s.%s' %
  91. (type(self).__name__, self._testMethodName)))
  92. self.train_root, self.val_root, self.max_epochs, self.cache_path, self.tmp_dir = _setup(
  93. )
  94. cfg_file_path = os.path.join(self.cache_path, 'mmcv_scrfd.py')
  95. cfg = Config.from_file(cfg_file_path)
  96. cfg.checkpoint_config.interval = 1
  97. cfg.log_config.interval = 10
  98. cfg.evaluation.interval = 1
  99. cfg.data.workers_per_gpu = 3
  100. cfg.data.samples_per_gpu = 4
  101. cfg.dump(cfg_file_path)
  102. def tearDown(self):
  103. shutil.rmtree(self.tmp_dir)
  104. super().tearDown()
  105. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  106. def test_multi_gpus_finetune(self):
  107. pretrain_epoch = 640
  108. self.max_epochs += pretrain_epoch
  109. kwargs = dict(
  110. cfg_file=os.path.join(self.cache_path, 'mmcv_scrfd.py'),
  111. work_dir=self.tmp_dir,
  112. train_root=self.train_root,
  113. val_root=self.val_root,
  114. total_epochs=self.max_epochs,
  115. resume_from=os.path.join(self.cache_path,
  116. ModelFile.TORCH_MODEL_BIN_FILE),
  117. launcher='pytorch')
  118. self.start(train_func, num_gpus=2, **kwargs)
  119. results_files = os.listdir(self.tmp_dir)
  120. json_files = glob.glob(os.path.join(self.tmp_dir, '*.log.json'))
  121. self.assertEqual(len(json_files), 1)
  122. for i in range(pretrain_epoch, self.max_epochs):
  123. self.assertIn(f'epoch_{i+1}.pth', results_files)
  124. if __name__ == '__main__':
  125. unittest.main()