test_lvis.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import os
  3. import unittest
  4. from mmengine.fileio import dump
  5. from mmdet.datasets import LVISV1Dataset, LVISV05Dataset
  6. try:
  7. import lvis
  8. except ImportError:
  9. lvis = None
  10. class TestLVISDataset(unittest.TestCase):
  11. def setUp(self) -> None:
  12. image1 = {
  13. # ``coco_url`` for v1 only.
  14. 'coco_url': 'http://images.cocodataset.org/train2017/0.jpg',
  15. # ``file_name`` for v0.5 only.
  16. 'file_name': '0.jpg',
  17. 'height': 1024,
  18. 'width': 2048,
  19. 'neg_category_ids': [],
  20. 'not_exhaustive_category_ids': [],
  21. 'id': 0
  22. }
  23. image2 = {
  24. 'coco_url': 'http://images.cocodataset.org/train2017/1.jpg',
  25. 'file_name': '1.jpg',
  26. 'height': 1024,
  27. 'width': 2048,
  28. 'neg_category_ids': [],
  29. 'not_exhaustive_category_ids': [],
  30. 'id': 1
  31. }
  32. image3 = {
  33. 'coco_url': 'http://images.cocodataset.org/train2017/2.jpg',
  34. 'file_name': '2.jpg',
  35. 'height': 1024,
  36. 'width': 2048,
  37. 'neg_category_ids': [],
  38. 'not_exhaustive_category_ids': [],
  39. 'id': 2
  40. }
  41. image4 = {
  42. 'coco_url': 'http://images.cocodataset.org/train2017/3.jpg',
  43. 'file_name': '3.jpg',
  44. 'height': 31,
  45. 'width': 15,
  46. 'neg_category_ids': [],
  47. 'not_exhaustive_category_ids': [],
  48. 'id': 3
  49. }
  50. images = [image1, image2, image3, image4]
  51. categories = [{
  52. 'id': 1,
  53. 'name': 'aerosol_can',
  54. 'frequency': 'c',
  55. 'image_count': 64
  56. }, {
  57. 'id': 2,
  58. 'name': 'air_conditioner',
  59. 'frequency': 'f',
  60. 'image_count': 364
  61. }, {
  62. 'id': 3,
  63. 'name': 'airplane',
  64. 'frequency': 'f',
  65. 'image_count': 1911
  66. }]
  67. annotations = [
  68. {
  69. 'category_id': 1,
  70. 'bbox': [379.0, 435.0, 52.0, 124.0],
  71. 'area': 2595,
  72. 'segmentation': [[0.0, 0.0]],
  73. 'image_id': 0,
  74. 'id': 0
  75. },
  76. {
  77. 'category_id': 2,
  78. 'bbox': [379.0, 435.0, 52.0, 124.0],
  79. 'area': -1,
  80. 'segmentation': [[0.0, 0.0]],
  81. 'image_id': 0,
  82. 'id': 1
  83. },
  84. {
  85. 'category_id': 3,
  86. 'bbox': [379.0, 435.0, -1, 124.0],
  87. 'area': 2,
  88. 'segmentation': [[0.0, 0.0]],
  89. 'image_id': 0,
  90. 'id': 2
  91. },
  92. {
  93. 'category_id': 1,
  94. 'bbox': [379.0, 435.0, 52.0, -1],
  95. 'area': 2,
  96. 'segmentation': [[0.0, 0.0]],
  97. 'image_id': 0,
  98. 'id': 3
  99. },
  100. {
  101. 'category_id': 1,
  102. 'bbox': [379.0, 435.0, 52.0, 124.0],
  103. 'area': 2595,
  104. 'segmentation': [[0.0, 0.0]],
  105. 'image_id': 0,
  106. 'id': 4
  107. },
  108. {
  109. 'category_id': 3,
  110. 'bbox': [379.0, 435.0, 52.0, 124.0],
  111. 'area': 2595,
  112. 'segmentation': [[0.0, 0.0]],
  113. 'image_id': 1,
  114. 'id': 5
  115. },
  116. {
  117. 'category_id': 3,
  118. 'bbox': [379.0, 435.0, 10, 2],
  119. 'area': 2595,
  120. 'segmentation': [[0.0, 0.0]],
  121. 'image_id': 3,
  122. 'id': 6
  123. },
  124. ]
  125. fake_json = {
  126. 'images': images,
  127. 'annotations': annotations,
  128. 'categories': categories
  129. }
  130. self.json_name = 'lvis.json'
  131. dump(fake_json, self.json_name)
  132. self.metainfo = dict(
  133. classes=('aerosol_can', 'air_conditioner', 'airplane'))
  134. def tearDown(self):
  135. os.remove(self.json_name)
  136. @unittest.skipIf(lvis is None, 'lvis is not installed.')
  137. def test_lvis05_dataset(self):
  138. dataset = LVISV05Dataset(
  139. ann_file=self.json_name,
  140. data_prefix=dict(img='imgs'),
  141. metainfo=self.metainfo,
  142. filter_cfg=dict(filter_empty_gt=True, min_size=32),
  143. pipeline=[])
  144. self.assertEqual(dataset.metainfo['classes'], self.metainfo['classes'])
  145. dataset.full_init()
  146. # filter images of small size and images
  147. # with all illegal annotations
  148. self.assertEqual(len(dataset), 2)
  149. self.assertEqual(len(dataset.load_data_list()), 4)
  150. dataset = LVISV05Dataset(
  151. ann_file=self.json_name,
  152. data_prefix=dict(img='imgs'),
  153. metainfo=self.metainfo,
  154. test_mode=True,
  155. filter_cfg=dict(filter_empty_gt=True, min_size=32),
  156. pipeline=[])
  157. dataset.full_init()
  158. # filter images of small size and images
  159. # with all illegal annotations
  160. self.assertEqual(len(dataset), 4)
  161. self.assertEqual(len(dataset.load_data_list()), 4)
  162. @unittest.skipIf(lvis is None, 'lvis is not installed.')
  163. def test_lvis1_dataset(self):
  164. dataset = LVISV1Dataset(
  165. ann_file=self.json_name,
  166. data_prefix=dict(img='imgs'),
  167. metainfo=self.metainfo,
  168. filter_cfg=dict(filter_empty_gt=True, min_size=32),
  169. pipeline=[])
  170. self.assertEqual(dataset.metainfo['classes'], self.metainfo['classes'])
  171. dataset.full_init()
  172. # filter images of small size and images
  173. # with all illegal annotations
  174. self.assertEqual(len(dataset), 2)
  175. self.assertEqual(len(dataset.load_data_list()), 4)
  176. dataset = LVISV1Dataset(
  177. ann_file=self.json_name,
  178. data_prefix=dict(img='imgs'),
  179. metainfo=self.metainfo,
  180. test_mode=True,
  181. filter_cfg=dict(filter_empty_gt=True, min_size=32),
  182. pipeline=[])
  183. dataset.full_init()
  184. # filter images of small size and images
  185. # with all illegal annotations
  186. self.assertEqual(len(dataset), 4)
  187. self.assertEqual(len(dataset.load_data_list()), 4)
  188. @unittest.skipIf(lvis is None, 'lvis is not installed.')
  189. def test_lvis1_dataset_without_filter_cfg(self):
  190. dataset = LVISV1Dataset(
  191. ann_file=self.json_name,
  192. data_prefix=dict(img='imgs'),
  193. metainfo=self.metainfo,
  194. filter_cfg=None,
  195. pipeline=[])
  196. self.assertEqual(dataset.metainfo['classes'], self.metainfo['classes'])
  197. dataset.full_init()
  198. # filter images of small size and images
  199. # with all illegal annotations
  200. self.assertEqual(len(dataset), 4)
  201. self.assertEqual(len(dataset.load_data_list()), 4)
  202. dataset = LVISV1Dataset(
  203. ann_file=self.json_name,
  204. data_prefix=dict(img='imgs'),
  205. metainfo=self.metainfo,
  206. test_mode=True,
  207. filter_cfg=None,
  208. pipeline=[])
  209. dataset.full_init()
  210. # filter images of small size and images
  211. # with all illegal annotations
  212. self.assertEqual(len(dataset), 4)
  213. self.assertEqual(len(dataset.load_data_list()), 4)