/tests/test_detectors.py

https://github.com/facebookresearch/maskrcnn-benchmark
Python | 143 lines | 88 code | 34 blank | 21 comment | 13 complexity | 5aac3430223d703b10205eadd9f164d3 MD5 | raw file
  1. # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  2. import unittest
  3. import glob
  4. import os
  5. import copy
  6. import torch
  7. from maskrcnn_benchmark.modeling.detector import build_detection_model
  8. from maskrcnn_benchmark.structures.image_list import to_image_list
  9. import utils
  10. CONFIG_FILES = [
  11. # bbox
  12. "e2e_faster_rcnn_R_50_C4_1x.yaml",
  13. "e2e_faster_rcnn_R_50_FPN_1x.yaml",
  14. "e2e_faster_rcnn_fbnet.yaml",
  15. # mask
  16. "e2e_mask_rcnn_R_50_C4_1x.yaml",
  17. "e2e_mask_rcnn_R_50_FPN_1x.yaml",
  18. "e2e_mask_rcnn_fbnet.yaml",
  19. # keypoints
  20. # TODO: fail to run for random model due to empty head input
  21. # "e2e_keypoint_rcnn_R_50_FPN_1x.yaml",
  22. # gn
  23. "gn_baselines/e2e_faster_rcnn_R_50_FPN_1x_gn.yaml",
  24. # TODO: fail to run for random model due to empty head input
  25. # "gn_baselines/e2e_mask_rcnn_R_50_FPN_Xconv1fc_1x_gn.yaml",
  26. # retinanet
  27. "retinanet/retinanet_R-50-FPN_1x.yaml",
  28. # rpn only
  29. "rpn_R_50_C4_1x.yaml",
  30. "rpn_R_50_FPN_1x.yaml",
  31. ]
  32. EXCLUDED_FOLDERS = [
  33. "caffe2",
  34. "quick_schedules",
  35. "pascal_voc",
  36. "cityscapes",
  37. ]
  38. TEST_CUDA = torch.cuda.is_available()
  39. def get_config_files(file_list, exclude_folders):
  40. cfg_root_path = utils.get_config_root_path()
  41. if file_list is not None:
  42. files = [os.path.join(cfg_root_path, x) for x in file_list]
  43. else:
  44. files = glob.glob(
  45. os.path.join(cfg_root_path, "./**/*.yaml"), recursive=True)
  46. def _contains(path, exclude_dirs):
  47. return any(x in path for x in exclude_dirs)
  48. if exclude_folders is not None:
  49. files = [x for x in files if not _contains(x, exclude_folders)]
  50. return files
  51. def create_model(cfg, device):
  52. cfg = copy.deepcopy(cfg)
  53. cfg.freeze()
  54. model = build_detection_model(cfg)
  55. model = model.to(device)
  56. return model
  57. def create_random_input(cfg, device):
  58. ret = []
  59. for x in cfg.INPUT.MIN_SIZE_TRAIN:
  60. ret.append(torch.rand(3, x, int(x * 1.2)))
  61. ret = to_image_list(ret, cfg.DATALOADER.SIZE_DIVISIBILITY)
  62. ret = ret.to(device)
  63. return ret
  64. def _test_build_detectors(self, device):
  65. ''' Make sure models build '''
  66. cfg_files = get_config_files(None, EXCLUDED_FOLDERS)
  67. self.assertGreater(len(cfg_files), 0)
  68. for cfg_file in cfg_files:
  69. with self.subTest(cfg_file=cfg_file):
  70. print('Testing {}...'.format(cfg_file))
  71. cfg = utils.load_config_from_file(cfg_file)
  72. create_model(cfg, device)
  73. def _test_run_selected_detectors(self, cfg_files, device):
  74. ''' Make sure models build and run '''
  75. self.assertGreater(len(cfg_files), 0)
  76. for cfg_file in cfg_files:
  77. with self.subTest(cfg_file=cfg_file):
  78. print('Testing {}...'.format(cfg_file))
  79. cfg = utils.load_config_from_file(cfg_file)
  80. cfg.MODEL.RPN.POST_NMS_TOP_N_TEST = 10
  81. cfg.MODEL.RPN.FPN_POST_NMS_TOP_N_TEST = 10
  82. model = create_model(cfg, device)
  83. inputs = create_random_input(cfg, device)
  84. model.eval()
  85. output = model(inputs)
  86. self.assertEqual(len(output), len(inputs.image_sizes))
  87. class TestDetectors(unittest.TestCase):
  88. def test_build_detectors(self):
  89. ''' Make sure models build '''
  90. _test_build_detectors(self, "cpu")
  91. @unittest.skipIf(not TEST_CUDA, "no CUDA detected")
  92. def test_build_detectors_cuda(self):
  93. ''' Make sure models build on gpu'''
  94. _test_build_detectors(self, "cuda")
  95. def test_run_selected_detectors(self):
  96. ''' Make sure models build and run '''
  97. # run on selected models
  98. cfg_files = get_config_files(CONFIG_FILES, None)
  99. # cfg_files = get_config_files(None, EXCLUDED_FOLDERS)
  100. _test_run_selected_detectors(self, cfg_files, "cpu")
  101. @unittest.skipIf(not TEST_CUDA, "no CUDA detected")
  102. def test_run_selected_detectors_cuda(self):
  103. ''' Make sure models build and run on cuda '''
  104. # run on selected models
  105. cfg_files = get_config_files(CONFIG_FILES, None)
  106. # cfg_files = get_config_files(None, EXCLUDED_FOLDERS)
  107. _test_run_selected_detectors(self, cfg_files, "cuda")
  108. if __name__ == "__main__":
  109. unittest.main()