test_cityscapes.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import os
  3. import unittest
  4. from mmengine.fileio import dump
  5. from mmdet.datasets import CityscapesDataset
  6. class TestCityscapesDataset(unittest.TestCase):
  7. def setUp(self) -> None:
  8. image1 = {
  9. 'file_name': 'munster/munster_000102_000019_leftImg8bit.png',
  10. 'height': 1024,
  11. 'width': 2048,
  12. 'segm_file': 'munster/munster_000102_000019_gtFine_labelIds.png',
  13. 'id': 0
  14. }
  15. image2 = {
  16. 'file_name': 'munster/munster_000157_000019_leftImg8bit.png',
  17. 'height': 1024,
  18. 'width': 2048,
  19. 'segm_file': 'munster/munster_000157_000019_gtFine_labelIds.png',
  20. 'id': 1
  21. }
  22. image3 = {
  23. 'file_name': 'munster/munster_000139_000019_leftImg8bit.png',
  24. 'height': 1024,
  25. 'width': 2048,
  26. 'segm_file': 'munster/munster_000139_000019_gtFine_labelIds.png',
  27. 'id': 2
  28. }
  29. image4 = {
  30. 'file_name': 'munster/munster_000034_000019_leftImg8bit.png',
  31. 'height': 31,
  32. 'width': 15,
  33. 'segm_file': 'munster/munster_000034_000019_gtFine_labelIds.png',
  34. 'id': 3
  35. }
  36. images = [image1, image2, image3, image4]
  37. categories = [{
  38. 'id': 24,
  39. 'name': 'person'
  40. }, {
  41. 'id': 25,
  42. 'name': 'rider'
  43. }, {
  44. 'id': 26,
  45. 'name': 'car'
  46. }]
  47. annotations = [
  48. {
  49. 'iscrowd': 0,
  50. 'category_id': 24,
  51. 'bbox': [379.0, 435.0, 52.0, 124.0],
  52. 'area': 2595,
  53. 'segmentation': {
  54. 'size': [1024, 2048],
  55. 'counts': 'xxx'
  56. },
  57. 'image_id': 0,
  58. 'id': 0
  59. },
  60. {
  61. 'iscrowd': 0,
  62. 'category_id': 25,
  63. 'bbox': [379.0, 435.0, 52.0, 124.0],
  64. 'area': -1,
  65. 'segmentation': {
  66. 'size': [1024, 2048],
  67. 'counts': 'xxx'
  68. },
  69. 'image_id': 0,
  70. 'id': 1
  71. },
  72. {
  73. 'iscrowd': 0,
  74. 'category_id': 26,
  75. 'bbox': [379.0, 435.0, -1, 124.0],
  76. 'area': 2,
  77. 'segmentation': {
  78. 'size': [1024, 2048],
  79. 'counts': 'xxx'
  80. },
  81. 'image_id': 0,
  82. 'id': 2
  83. },
  84. {
  85. 'iscrowd': 0,
  86. 'category_id': 24,
  87. 'bbox': [379.0, 435.0, 52.0, -1],
  88. 'area': 2,
  89. 'segmentation': {
  90. 'size': [1024, 2048],
  91. 'counts': 'xxx'
  92. },
  93. 'image_id': 0,
  94. 'id': 3
  95. },
  96. {
  97. 'iscrowd': 0,
  98. 'category_id': 1,
  99. 'bbox': [379.0, 435.0, 52.0, 124.0],
  100. 'area': 2595,
  101. 'segmentation': {
  102. 'size': [1024, 2048],
  103. 'counts': 'xxx'
  104. },
  105. 'image_id': 0,
  106. 'id': 4
  107. },
  108. {
  109. 'iscrowd': 1,
  110. 'category_id': 26,
  111. 'bbox': [379.0, 435.0, 52.0, 124.0],
  112. 'area': 2595,
  113. 'segmentation': {
  114. 'size': [1024, 2048],
  115. 'counts': 'xxx'
  116. },
  117. 'image_id': 1,
  118. 'id': 5
  119. },
  120. {
  121. 'iscrowd': 0,
  122. 'category_id': 26,
  123. 'bbox': [379.0, 435.0, 10, 2],
  124. 'area': 2595,
  125. 'segmentation': {
  126. 'size': [1024, 2048],
  127. 'counts': 'xxx'
  128. },
  129. 'image_id': 3,
  130. 'id': 6
  131. },
  132. ]
  133. fake_json = {
  134. 'images': images,
  135. 'annotations': annotations,
  136. 'categories': categories
  137. }
  138. self.json_name = 'cityscapes.json'
  139. dump(fake_json, self.json_name)
  140. self.metainfo = dict(classes=('person', 'rider', 'car'))
  141. def tearDown(self):
  142. os.remove(self.json_name)
  143. def test_cityscapes_dataset(self):
  144. dataset = CityscapesDataset(
  145. ann_file=self.json_name,
  146. data_prefix=dict(img='imgs'),
  147. metainfo=self.metainfo,
  148. filter_cfg=dict(filter_empty_gt=True, min_size=32),
  149. pipeline=[])
  150. self.assertEqual(dataset.metainfo['classes'], self.metainfo['classes'])
  151. dataset.full_init()
  152. # filter images of small size and images
  153. # with all illegal annotations
  154. self.assertEqual(len(dataset), 1)
  155. self.assertEqual(len(dataset.load_data_list()), 4)
  156. dataset = CityscapesDataset(
  157. ann_file=self.json_name,
  158. data_prefix=dict(img='imgs'),
  159. metainfo=self.metainfo,
  160. test_mode=True,
  161. filter_cfg=dict(filter_empty_gt=True, min_size=32),
  162. pipeline=[])
  163. dataset.full_init()
  164. # filter images of small size and images
  165. # with all illegal annotations
  166. self.assertEqual(len(dataset), 4)
  167. self.assertEqual(len(dataset.load_data_list()), 4)
  168. def test_cityscapes_dataset_without_filter_cfg(self):
  169. dataset = CityscapesDataset(
  170. ann_file=self.json_name,
  171. data_prefix=dict(img='imgs'),
  172. metainfo=self.metainfo,
  173. filter_cfg=None,
  174. pipeline=[])
  175. self.assertEqual(dataset.metainfo['classes'], self.metainfo['classes'])
  176. dataset.full_init()
  177. # filter images of small size and images
  178. # with all illegal annotations
  179. self.assertEqual(len(dataset), 4)
  180. self.assertEqual(len(dataset.load_data_list()), 4)
  181. dataset = CityscapesDataset(
  182. ann_file=self.json_name,
  183. data_prefix=dict(img='imgs'),
  184. metainfo=self.metainfo,
  185. test_mode=True,
  186. filter_cfg=None,
  187. pipeline=[])
  188. dataset.full_init()
  189. # filter images of small size and images
  190. # with all illegal annotations
  191. self.assertEqual(len(dataset), 4)
  192. self.assertEqual(len(dataset.load_data_list()), 4)