test_coco_panoptic.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import os
  3. import unittest
  4. from mmengine.fileio import dump
  5. from mmdet.datasets import CocoPanopticDataset
  6. class TestCocoPanopticDataset(unittest.TestCase):
  7. def setUp(self):
  8. image1 = {
  9. 'id': 0,
  10. 'width': 640,
  11. 'height': 640,
  12. 'file_name': 'fake_name1.jpg',
  13. }
  14. image2 = {
  15. 'id': 1,
  16. 'width': 640,
  17. 'height': 800,
  18. 'file_name': 'fake_name2.jpg',
  19. }
  20. image3 = {
  21. 'id': 2,
  22. 'width': 31,
  23. 'height': 40,
  24. 'file_name': 'fake_name3.jpg',
  25. }
  26. image4 = {
  27. 'id': 3,
  28. 'width': 400,
  29. 'height': 400,
  30. 'file_name': 'fake_name4.jpg',
  31. }
  32. images = [image1, image2, image3, image4]
  33. annotations = [
  34. {
  35. 'segments_info': [{
  36. 'id': 1,
  37. 'category_id': 0,
  38. 'area': 400,
  39. 'bbox': [50, 60, 20, 20],
  40. 'iscrowd': 0
  41. }, {
  42. 'id': 2,
  43. 'category_id': 1,
  44. 'area': 900,
  45. 'bbox': [100, 120, 30, 30],
  46. 'iscrowd': 0
  47. }, {
  48. 'id': 3,
  49. 'category_id': 2,
  50. 'iscrowd': 0,
  51. 'bbox': [1, 189, 612, 285],
  52. 'area': 70036
  53. }],
  54. 'file_name':
  55. 'fake_name1.jpg',
  56. 'image_id':
  57. 0
  58. },
  59. {
  60. 'segments_info': [
  61. {
  62. # Different to instance style json, there
  63. # are duplicate ids in panoptic style json
  64. 'id': 1,
  65. 'category_id': 0,
  66. 'area': 400,
  67. 'bbox': [50, 60, 20, 20],
  68. 'iscrowd': 0
  69. },
  70. {
  71. 'id': 4,
  72. 'category_id': 1,
  73. 'area': 900,
  74. 'bbox': [100, 120, 30, 30],
  75. 'iscrowd': 1
  76. },
  77. {
  78. 'id': 5,
  79. 'category_id': 2,
  80. 'iscrowd': 0,
  81. 'bbox': [100, 200, 200, 300],
  82. 'area': 66666
  83. },
  84. {
  85. 'id': 6,
  86. 'category_id': 0,
  87. 'iscrowd': 0,
  88. 'bbox': [1, 189, -10, 285],
  89. 'area': -2
  90. },
  91. {
  92. 'id': 10,
  93. 'category_id': 0,
  94. 'iscrowd': 0,
  95. 'bbox': [1, 189, 10, -285],
  96. 'area': 100
  97. }
  98. ],
  99. 'file_name':
  100. 'fake_name2.jpg',
  101. 'image_id':
  102. 1
  103. },
  104. {
  105. 'segments_info': [{
  106. 'id': 7,
  107. 'category_id': 0,
  108. 'area': 25,
  109. 'bbox': [0, 0, 5, 5],
  110. 'iscrowd': 0
  111. }],
  112. 'file_name':
  113. 'fake_name3.jpg',
  114. 'image_id':
  115. 2
  116. },
  117. {
  118. 'segments_info': [{
  119. 'id': 8,
  120. 'category_id': 0,
  121. 'area': 25,
  122. 'bbox': [0, 0, 400, 400],
  123. 'iscrowd': 1
  124. }],
  125. 'file_name':
  126. 'fake_name4.jpg',
  127. 'image_id':
  128. 3
  129. }
  130. ]
  131. categories = [{
  132. 'id': 0,
  133. 'name': 'car',
  134. 'supercategory': 'car',
  135. 'isthing': 1
  136. }, {
  137. 'id': 1,
  138. 'name': 'person',
  139. 'supercategory': 'person',
  140. 'isthing': 1
  141. }, {
  142. 'id': 2,
  143. 'name': 'wall',
  144. 'supercategory': 'wall',
  145. 'isthing': 0
  146. }]
  147. fake_json = {
  148. 'images': images,
  149. 'annotations': annotations,
  150. 'categories': categories
  151. }
  152. self.json_name = 'coco_panoptic.json'
  153. dump(fake_json, self.json_name)
  154. self.metainfo = dict(
  155. classes=('person', 'car', 'wall'),
  156. thing_classes=('person', 'car'),
  157. stuff_classes=('wall', ))
  158. def tearDown(self):
  159. os.remove(self.json_name)
  160. def test_coco_panoptic_dataset(self):
  161. dataset = CocoPanopticDataset(
  162. data_root='./',
  163. ann_file=self.json_name,
  164. data_prefix=dict(img='imgs', seg='seg'),
  165. metainfo=self.metainfo,
  166. filter_cfg=dict(filter_empty_gt=True, min_size=32),
  167. pipeline=[])
  168. self.assertEqual(dataset.metainfo['classes'], self.metainfo['classes'])
  169. self.assertEqual(dataset.metainfo['thing_classes'],
  170. self.metainfo['thing_classes'])
  171. self.assertEqual(dataset.metainfo['stuff_classes'],
  172. self.metainfo['stuff_classes'])
  173. dataset.full_init()
  174. # filter images of small size and images
  175. # with all illegal annotations
  176. self.assertEqual(len(dataset), 2)
  177. self.assertEqual(len(dataset.load_data_list()), 4)
  178. # test mode
  179. dataset = CocoPanopticDataset(
  180. data_root='./',
  181. ann_file=self.json_name,
  182. data_prefix=dict(img='imgs', seg='seg'),
  183. metainfo=self.metainfo,
  184. test_mode=True,
  185. filter_cfg=dict(filter_empty_gt=True, min_size=32),
  186. pipeline=[])
  187. self.assertEqual(dataset.metainfo['classes'], self.metainfo['classes'])
  188. self.assertEqual(dataset.metainfo['thing_classes'],
  189. self.metainfo['thing_classes'])
  190. self.assertEqual(dataset.metainfo['stuff_classes'],
  191. self.metainfo['stuff_classes'])
  192. dataset.full_init()
  193. # filter images of small size and images
  194. # with all illegal annotations
  195. self.assertEqual(len(dataset), 4)
  196. self.assertEqual(len(dataset.load_data_list()), 4)
  197. def test_coco_panoptic_dataset_without_filter_cfg(self):
  198. dataset = CocoPanopticDataset(
  199. data_root='./',
  200. ann_file=self.json_name,
  201. data_prefix=dict(img='imgs', seg='seg'),
  202. metainfo=self.metainfo,
  203. filter_cfg=None,
  204. pipeline=[])
  205. self.assertEqual(dataset.metainfo['classes'], self.metainfo['classes'])
  206. self.assertEqual(dataset.metainfo['thing_classes'],
  207. self.metainfo['thing_classes'])
  208. self.assertEqual(dataset.metainfo['stuff_classes'],
  209. self.metainfo['stuff_classes'])
  210. dataset.full_init()
  211. # filter images of small size and images
  212. # with all illegal annotations
  213. self.assertEqual(len(dataset), 4)
  214. self.assertEqual(len(dataset.load_data_list()), 4)
  215. # test mode
  216. dataset = CocoPanopticDataset(
  217. data_root='./',
  218. ann_file=self.json_name,
  219. data_prefix=dict(img='imgs', seg='seg'),
  220. metainfo=self.metainfo,
  221. filter_cfg=None,
  222. test_mode=True,
  223. pipeline=[])
  224. self.assertEqual(dataset.metainfo['classes'], self.metainfo['classes'])
  225. self.assertEqual(dataset.metainfo['thing_classes'],
  226. self.metainfo['thing_classes'])
  227. self.assertEqual(dataset.metainfo['stuff_classes'],
  228. self.metainfo['stuff_classes'])
  229. dataset.full_init()
  230. # filter images of small size and images
  231. # with all illegal annotations
  232. self.assertEqual(len(dataset), 4)
  233. self.assertEqual(len(dataset.load_data_list()), 4)