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_datasets_coco.py 20 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. import numpy as np
  16. import mindspore.dataset as ds
  17. import mindspore.dataset.vision.c_transforms as vision
  18. DATA_DIR = "../data/dataset/testCOCO/train/"
  19. DATA_DIR_2 = "../data/dataset/testCOCO/train"
  20. ANNOTATION_FILE = "../data/dataset/testCOCO/annotations/train.json"
  21. KEYPOINT_FILE = "../data/dataset/testCOCO/annotations/key_point.json"
  22. PANOPTIC_FILE = "../data/dataset/testCOCO/annotations/panoptic.json"
  23. INVALID_FILE = "../data/dataset/testCOCO/annotations/invalid.json"
  24. LACKOFIMAGE_FILE = "../data/dataset/testCOCO/annotations/lack_of_images.json"
  25. INVALID_CATEGORY_ID_FILE = "../data/dataset/testCOCO/annotations/invalid_category_id.json"
  26. def test_coco_detection():
  27. data1 = ds.CocoDataset(DATA_DIR, annotation_file=ANNOTATION_FILE, task="Detection",
  28. decode=True, shuffle=False)
  29. num_iter = 0
  30. image_shape = []
  31. bbox = []
  32. category_id = []
  33. for data in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
  34. image_shape.append(data["image"].shape)
  35. bbox.append(data["bbox"])
  36. category_id.append(data["category_id"])
  37. num_iter += 1
  38. assert num_iter == 6
  39. assert image_shape[0] == (2268, 4032, 3)
  40. assert image_shape[1] == (561, 595, 3)
  41. assert image_shape[2] == (607, 585, 3)
  42. assert image_shape[3] == (642, 675, 3)
  43. assert image_shape[4] == (2268, 4032, 3)
  44. assert image_shape[5] == (2268, 4032, 3)
  45. np.testing.assert_array_equal(np.array([[10., 10., 10., 10.], [70., 70., 70., 70.]]), bbox[0])
  46. np.testing.assert_array_equal(np.array([[20., 20., 20., 20.], [80., 80., 80.0, 80.]]), bbox[1])
  47. np.testing.assert_array_equal(np.array([[30.0, 30.0, 30.0, 30.]]), bbox[2])
  48. np.testing.assert_array_equal(np.array([[40., 40., 40., 40.]]), bbox[3])
  49. np.testing.assert_array_equal(np.array([[50., 50., 50., 50.]]), bbox[4])
  50. np.testing.assert_array_equal(np.array([[60., 60., 60., 60.]]), bbox[5])
  51. np.testing.assert_array_equal(np.array([[1], [7]]), category_id[0])
  52. np.testing.assert_array_equal(np.array([[2], [8]]), category_id[1])
  53. np.testing.assert_array_equal(np.array([[3]]), category_id[2])
  54. np.testing.assert_array_equal(np.array([[4]]), category_id[3])
  55. np.testing.assert_array_equal(np.array([[5]]), category_id[4])
  56. np.testing.assert_array_equal(np.array([[6]]), category_id[5])
  57. def test_coco_stuff():
  58. data1 = ds.CocoDataset(DATA_DIR, annotation_file=ANNOTATION_FILE, task="Stuff",
  59. decode=True, shuffle=False)
  60. num_iter = 0
  61. image_shape = []
  62. segmentation = []
  63. iscrowd = []
  64. for data in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
  65. image_shape.append(data["image"].shape)
  66. segmentation.append(data["segmentation"])
  67. iscrowd.append(data["iscrowd"])
  68. num_iter += 1
  69. assert num_iter == 6
  70. assert image_shape[0] == (2268, 4032, 3)
  71. assert image_shape[1] == (561, 595, 3)
  72. assert image_shape[2] == (607, 585, 3)
  73. assert image_shape[3] == (642, 675, 3)
  74. assert image_shape[4] == (2268, 4032, 3)
  75. assert image_shape[5] == (2268, 4032, 3)
  76. np.testing.assert_array_equal(np.array([[10., 12., 13., 14., 15., 16., 17., 18., 19., 20.],
  77. [70., 72., 73., 74., 75., -1., -1., -1., -1., -1.]]),
  78. segmentation[0])
  79. np.testing.assert_array_equal(np.array([[0], [0]]), iscrowd[0])
  80. np.testing.assert_array_equal(np.array([[20.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0],
  81. [10.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, -1.0]]),
  82. segmentation[1])
  83. np.testing.assert_array_equal(np.array([[0], [1]]), iscrowd[1])
  84. np.testing.assert_array_equal(np.array([[40., 42., 43., 44., 45., 46., 47., 48., 49., 40., 41., 42.]]),
  85. segmentation[2])
  86. np.testing.assert_array_equal(np.array([[0]]), iscrowd[2])
  87. np.testing.assert_array_equal(np.array([[50., 52., 53., 54., 55., 56., 57., 58., 59., 60., 61., 62., 63.]]),
  88. segmentation[3])
  89. np.testing.assert_array_equal(np.array([[0]]), iscrowd[3])
  90. np.testing.assert_array_equal(np.array([[60., 62., 63., 64., 65., 66., 67., 68., 69., 70., 71., 72., 73., 74.]]),
  91. segmentation[4])
  92. np.testing.assert_array_equal(np.array([[0]]), iscrowd[4])
  93. np.testing.assert_array_equal(np.array([[60., 62., 63., 64., 65., 66., 67.], [68., 69., 70., 71., 72., 73., 74.]]),
  94. segmentation[5])
  95. np.testing.assert_array_equal(np.array([[0]]), iscrowd[5])
  96. def test_coco_keypoint():
  97. data1 = ds.CocoDataset(DATA_DIR, annotation_file=KEYPOINT_FILE, task="Keypoint",
  98. decode=True, shuffle=False)
  99. num_iter = 0
  100. image_shape = []
  101. keypoints = []
  102. num_keypoints = []
  103. for data in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
  104. image_shape.append(data["image"].shape)
  105. keypoints.append(data["keypoints"])
  106. num_keypoints.append(data["num_keypoints"])
  107. num_iter += 1
  108. assert num_iter == 2
  109. assert image_shape[0] == (2268, 4032, 3)
  110. assert image_shape[1] == (561, 595, 3)
  111. np.testing.assert_array_equal(np.array([[368., 61., 1., 369., 52., 2., 0., 0., 0., 382., 48., 2., 0., 0., 0., 368.,
  112. 84., 2., 435., 81., 2., 362., 125., 2., 446., 125., 2., 360., 153., 2., 0.,
  113. 0., 0., 397., 167., 1., 439., 166., 1., 369., 193., 2., 461., 234., 2.,
  114. 361., 246., 2., 474., 287., 2.]]), keypoints[0])
  115. np.testing.assert_array_equal(np.array([[14]]), num_keypoints[0])
  116. np.testing.assert_array_equal(np.array([[244., 139., 2., 0., 0., 0., 226., 118., 2., 0., 0., 0., 154., 159., 2.,
  117. 143., 261., 2., 135., 312., 2., 271., 423., 2., 184., 530., 2., 261., 280.,
  118. 2., 347., 592., 2., 0., 0., 0., 123., 596., 2., 0., 0., 0., 0., 0., 0., 0.,
  119. 0., 0., 0., 0., 0.]]),
  120. keypoints[1])
  121. np.testing.assert_array_equal(np.array([[10]]), num_keypoints[1])
  122. def test_coco_panoptic():
  123. data1 = ds.CocoDataset(DATA_DIR, annotation_file=PANOPTIC_FILE, task="Panoptic", decode=True, shuffle=False)
  124. num_iter = 0
  125. image_shape = []
  126. bbox = []
  127. category_id = []
  128. iscrowd = []
  129. area = []
  130. for data in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
  131. image_shape.append(data["image"].shape)
  132. bbox.append(data["bbox"])
  133. category_id.append(data["category_id"])
  134. iscrowd.append(data["iscrowd"])
  135. area.append(data["area"])
  136. num_iter += 1
  137. assert num_iter == 2
  138. assert image_shape[0] == (2268, 4032, 3)
  139. np.testing.assert_array_equal(np.array([[472, 173, 36, 48], [340, 22, 154, 301], [486, 183, 30, 35]]), bbox[0])
  140. np.testing.assert_array_equal(np.array([[1], [1], [2]]), category_id[0])
  141. np.testing.assert_array_equal(np.array([[0], [0], [0]]), iscrowd[0])
  142. np.testing.assert_array_equal(np.array([[705], [14062], [626]]), area[0])
  143. assert image_shape[1] == (642, 675, 3)
  144. np.testing.assert_array_equal(np.array([[103, 133, 229, 422], [243, 175, 93, 164]]), bbox[1])
  145. np.testing.assert_array_equal(np.array([[1], [3]]), category_id[1])
  146. np.testing.assert_array_equal(np.array([[0], [0]]), iscrowd[1])
  147. np.testing.assert_array_equal(np.array([[43102], [6079]]), area[1])
  148. def test_coco_detection_classindex():
  149. data1 = ds.CocoDataset(DATA_DIR, annotation_file=ANNOTATION_FILE, task="Detection", decode=True)
  150. class_index = data1.get_class_indexing()
  151. assert class_index == {'person': [1], 'bicycle': [2], 'car': [3], 'cat': [4], 'dog': [5], 'monkey': [6],
  152. 'bag': [7], 'orange': [8]}
  153. num_iter = 0
  154. for _ in data1.__iter__():
  155. num_iter += 1
  156. assert num_iter == 6
  157. def test_coco_panootic_classindex():
  158. data1 = ds.CocoDataset(DATA_DIR, annotation_file=PANOPTIC_FILE, task="Panoptic", decode=True)
  159. class_index = data1.get_class_indexing()
  160. assert class_index == {'person': [1, 1], 'bicycle': [2, 1], 'car': [3, 1]}
  161. num_iter = 0
  162. for _ in data1.__iter__():
  163. num_iter += 1
  164. assert num_iter == 2
  165. def test_coco_case_0():
  166. data1 = ds.CocoDataset(DATA_DIR, annotation_file=ANNOTATION_FILE, task="Detection", decode=True)
  167. data1 = data1.shuffle(10)
  168. data1 = data1.batch(3, pad_info={})
  169. num_iter = 0
  170. for _ in data1.create_dict_iterator(num_epochs=1):
  171. num_iter += 1
  172. assert num_iter == 2
  173. def test_coco_case_1():
  174. data1 = ds.CocoDataset(DATA_DIR, annotation_file=ANNOTATION_FILE, task="Detection", decode=True)
  175. sizes = [0.5, 0.5]
  176. randomize = False
  177. dataset1, dataset2 = data1.split(sizes=sizes, randomize=randomize)
  178. num_iter = 0
  179. for _ in dataset1.create_dict_iterator(num_epochs=1):
  180. num_iter += 1
  181. assert num_iter == 3
  182. num_iter = 0
  183. for _ in dataset2.create_dict_iterator(num_epochs=1):
  184. num_iter += 1
  185. assert num_iter == 3
  186. def test_coco_case_2():
  187. data1 = ds.CocoDataset(DATA_DIR, annotation_file=ANNOTATION_FILE, task="Detection", decode=True)
  188. resize_op = vision.Resize((224, 224))
  189. data1 = data1.map(operations=resize_op, input_columns=["image"])
  190. data1 = data1.repeat(4)
  191. num_iter = 0
  192. for _ in data1.__iter__():
  193. num_iter += 1
  194. assert num_iter == 24
  195. def test_coco_case_3():
  196. data1 = ds.CocoDataset(DATA_DIR_2, annotation_file=ANNOTATION_FILE, task="Detection", decode=True)
  197. resize_op = vision.Resize((224, 224))
  198. data1 = data1.map(operations=resize_op, input_columns=["image"])
  199. data1 = data1.repeat(4)
  200. num_iter = 0
  201. for _ in data1.__iter__():
  202. num_iter += 1
  203. assert num_iter == 24
  204. def test_coco_case_exception():
  205. try:
  206. data1 = ds.CocoDataset("path_not_exist/", annotation_file=ANNOTATION_FILE, task="Detection")
  207. for _ in data1.__iter__():
  208. pass
  209. assert False
  210. except ValueError as e:
  211. assert "does not exist or permission denied" in str(e)
  212. try:
  213. data1 = ds.CocoDataset(DATA_DIR, annotation_file="./file_not_exist", task="Detection")
  214. for _ in data1.__iter__():
  215. pass
  216. assert False
  217. except ValueError as e:
  218. assert "does not exist or permission denied" in str(e)
  219. try:
  220. data1 = ds.CocoDataset(DATA_DIR, annotation_file=ANNOTATION_FILE, task="Invalid task")
  221. for _ in data1.__iter__():
  222. pass
  223. assert False
  224. except ValueError as e:
  225. assert "Invalid task type" in str(e)
  226. try:
  227. data1 = ds.CocoDataset(DATA_DIR, annotation_file=LACKOFIMAGE_FILE, task="Detection")
  228. for _ in data1.__iter__():
  229. pass
  230. assert False
  231. except RuntimeError as e:
  232. assert "invalid node found in json" in str(e)
  233. try:
  234. data1 = ds.CocoDataset(DATA_DIR, annotation_file=INVALID_CATEGORY_ID_FILE, task="Detection")
  235. for _ in data1.__iter__():
  236. pass
  237. assert False
  238. except RuntimeError as e:
  239. assert "category_id can't find in categories" in str(e)
  240. try:
  241. data1 = ds.CocoDataset(DATA_DIR, annotation_file=INVALID_FILE, task="Detection")
  242. for _ in data1.__iter__():
  243. pass
  244. assert False
  245. except RuntimeError as e:
  246. assert "failed to open json file" in str(e)
  247. try:
  248. sampler = ds.PKSampler(3)
  249. data1 = ds.CocoDataset(DATA_DIR, annotation_file=INVALID_FILE, task="Detection", sampler=sampler)
  250. for _ in data1.__iter__():
  251. pass
  252. assert False
  253. except ValueError as e:
  254. assert "CocoDataset doesn't support PKSampler" in str(e)
  255. def exception_func(item):
  256. raise Exception("Error occur!")
  257. try:
  258. data1 = ds.CocoDataset(DATA_DIR, annotation_file=ANNOTATION_FILE, task="Detection")
  259. data1 = data1.map(operations=exception_func, input_columns=["image"], num_parallel_workers=1)
  260. for _ in data1.__iter__():
  261. pass
  262. assert False
  263. except RuntimeError as e:
  264. assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
  265. try:
  266. data1 = ds.CocoDataset(DATA_DIR, annotation_file=ANNOTATION_FILE, task="Detection")
  267. data1 = data1.map(operations=vision.Decode(), input_columns=["image"], num_parallel_workers=1)
  268. data1 = data1.map(operations=exception_func, input_columns=["image"], num_parallel_workers=1)
  269. for _ in data1.__iter__():
  270. pass
  271. assert False
  272. except RuntimeError as e:
  273. assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
  274. try:
  275. data1 = ds.CocoDataset(DATA_DIR, annotation_file=ANNOTATION_FILE, task="Detection")
  276. data1 = data1.map(operations=exception_func, input_columns=["bbox"], num_parallel_workers=1)
  277. for _ in data1.__iter__():
  278. pass
  279. assert False
  280. except RuntimeError as e:
  281. assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
  282. try:
  283. data1 = ds.CocoDataset(DATA_DIR, annotation_file=ANNOTATION_FILE, task="Detection")
  284. data1 = data1.map(operations=exception_func, input_columns=["category_id"], num_parallel_workers=1)
  285. for _ in data1.__iter__():
  286. pass
  287. assert False
  288. except RuntimeError as e:
  289. assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
  290. try:
  291. data1 = ds.CocoDataset(DATA_DIR, annotation_file=ANNOTATION_FILE, task="Stuff")
  292. data1 = data1.map(operations=exception_func, input_columns=["image"], num_parallel_workers=1)
  293. for _ in data1.__iter__():
  294. pass
  295. assert False
  296. except RuntimeError as e:
  297. assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
  298. try:
  299. data1 = ds.CocoDataset(DATA_DIR, annotation_file=ANNOTATION_FILE, task="Stuff")
  300. data1 = data1.map(operations=vision.Decode(), input_columns=["image"], num_parallel_workers=1)
  301. data1 = data1.map(operations=exception_func, input_columns=["image"], num_parallel_workers=1)
  302. for _ in data1.__iter__():
  303. pass
  304. assert False
  305. except RuntimeError as e:
  306. assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
  307. try:
  308. data1 = ds.CocoDataset(DATA_DIR, annotation_file=ANNOTATION_FILE, task="Stuff")
  309. data1 = data1.map(operations=exception_func, input_columns=["segmentation"], num_parallel_workers=1)
  310. for _ in data1.__iter__():
  311. pass
  312. assert False
  313. except RuntimeError as e:
  314. assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
  315. try:
  316. data1 = ds.CocoDataset(DATA_DIR, annotation_file=ANNOTATION_FILE, task="Stuff")
  317. data1 = data1.map(operations=exception_func, input_columns=["iscrowd"], num_parallel_workers=1)
  318. for _ in data1.__iter__():
  319. pass
  320. assert False
  321. except RuntimeError as e:
  322. assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
  323. try:
  324. data1 = ds.CocoDataset(DATA_DIR, annotation_file=KEYPOINT_FILE, task="Keypoint")
  325. data1 = data1.map(operations=exception_func, input_columns=["image"], num_parallel_workers=1)
  326. for _ in data1.__iter__():
  327. pass
  328. assert False
  329. except RuntimeError as e:
  330. assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
  331. try:
  332. data1 = ds.CocoDataset(DATA_DIR, annotation_file=KEYPOINT_FILE, task="Keypoint")
  333. data1 = data1.map(operations=vision.Decode(), input_columns=["image"], num_parallel_workers=1)
  334. data1 = data1.map(operations=exception_func, input_columns=["image"], num_parallel_workers=1)
  335. for _ in data1.__iter__():
  336. pass
  337. assert False
  338. except RuntimeError as e:
  339. assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
  340. try:
  341. data1 = ds.CocoDataset(DATA_DIR, annotation_file=KEYPOINT_FILE, task="Keypoint")
  342. data1 = data1.map(operations=exception_func, input_columns=["keypoints"], num_parallel_workers=1)
  343. for _ in data1.__iter__():
  344. pass
  345. assert False
  346. except RuntimeError as e:
  347. assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
  348. try:
  349. data1 = ds.CocoDataset(DATA_DIR, annotation_file=KEYPOINT_FILE, task="Keypoint")
  350. data1 = data1.map(operations=exception_func, input_columns=["num_keypoints"], num_parallel_workers=1)
  351. for _ in data1.__iter__():
  352. pass
  353. assert False
  354. except RuntimeError as e:
  355. assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
  356. try:
  357. data1 = ds.CocoDataset(DATA_DIR, annotation_file=PANOPTIC_FILE, task="Panoptic")
  358. data1 = data1.map(operations=exception_func, input_columns=["image"], num_parallel_workers=1)
  359. for _ in data1.__iter__():
  360. pass
  361. assert False
  362. except RuntimeError as e:
  363. assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
  364. try:
  365. data1 = ds.CocoDataset(DATA_DIR, annotation_file=PANOPTIC_FILE, task="Panoptic")
  366. data1 = data1.map(operations=vision.Decode(), input_columns=["image"], num_parallel_workers=1)
  367. data1 = data1.map(operations=exception_func, input_columns=["image"], num_parallel_workers=1)
  368. for _ in data1.__iter__():
  369. pass
  370. assert False
  371. except RuntimeError as e:
  372. assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
  373. try:
  374. data1 = ds.CocoDataset(DATA_DIR, annotation_file=PANOPTIC_FILE, task="Panoptic")
  375. data1 = data1.map(operations=exception_func, input_columns=["bbox"], num_parallel_workers=1)
  376. for _ in data1.__iter__():
  377. pass
  378. assert False
  379. except RuntimeError as e:
  380. assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
  381. try:
  382. data1 = ds.CocoDataset(DATA_DIR, annotation_file=PANOPTIC_FILE, task="Panoptic")
  383. data1 = data1.map(operations=exception_func, input_columns=["category_id"], num_parallel_workers=1)
  384. for _ in data1.__iter__():
  385. pass
  386. assert False
  387. except RuntimeError as e:
  388. assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
  389. try:
  390. data1 = ds.CocoDataset(DATA_DIR, annotation_file=PANOPTIC_FILE, task="Panoptic")
  391. data1 = data1.map(operations=exception_func, input_columns=["area"], num_parallel_workers=1)
  392. for _ in data1.__iter__():
  393. pass
  394. assert False
  395. except RuntimeError as e:
  396. assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
  397. if __name__ == '__main__':
  398. test_coco_detection()
  399. test_coco_stuff()
  400. test_coco_keypoint()
  401. test_coco_panoptic()
  402. test_coco_detection_classindex()
  403. test_coco_panootic_classindex()
  404. test_coco_case_0()
  405. test_coco_case_1()
  406. test_coco_case_2()
  407. test_coco_case_3()
  408. test_coco_case_exception()