test_loader.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
  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. import unittest
  15. import os
  16. import sys
  17. # add python path of PadleDetection to sys.path
  18. parent_path = os.path.abspath(os.path.join(__file__, *(['..'] * 4)))
  19. if parent_path not in sys.path:
  20. sys.path.append(parent_path)
  21. from ppdet.data.source.coco import COCODataSet
  22. from ppdet.data.reader import Reader
  23. from ppdet.utils.download import get_path
  24. from ppdet.utils.download import DATASET_HOME
  25. from ppdet.data.transform.operators import DecodeImage, ResizeImage, Permute
  26. from ppdet.data.transform.batch_operators import PadBatch
  27. from ppdet.utils.check import enable_static_mode
  28. COCO_VAL_URL = 'http://images.cocodataset.org/zips/val2017.zip'
  29. COCO_VAL_MD5SUM = '442b8da7639aecaf257c1dceb8ba8c80'
  30. COCO_ANNO_URL = 'http://images.cocodataset.org/annotations/annotations_trainval2017.zip'
  31. COCO_ANNO_MD5SUM = 'f4bbac642086de4f52a3fdda2de5fa2c'
  32. class TestReader(unittest.TestCase):
  33. @classmethod
  34. def setUpClass(cls):
  35. """ setup
  36. """
  37. root_path = os.path.join(DATASET_HOME, 'coco')
  38. _, _ = get_path(COCO_VAL_URL, root_path, COCO_VAL_MD5SUM)
  39. _, _ = get_path(COCO_ANNO_URL, root_path, COCO_ANNO_MD5SUM)
  40. cls.anno_path = 'annotations/instances_val2017.json'
  41. cls.image_dir = 'val2017'
  42. cls.root_path = root_path
  43. @classmethod
  44. def tearDownClass(cls):
  45. """ tearDownClass """
  46. pass
  47. def test_loader(self):
  48. coco_loader = COCODataSet(
  49. dataset_dir=self.root_path,
  50. image_dir=self.image_dir,
  51. anno_path=self.anno_path,
  52. sample_num=10)
  53. sample_trans = [
  54. DecodeImage(to_rgb=True), ResizeImage(
  55. target_size=800, max_size=1333, interp=1), Permute(to_bgr=False)
  56. ]
  57. batch_trans = [PadBatch(pad_to_stride=32, use_padded_im_info=True), ]
  58. inputs_def = {
  59. 'fields': [
  60. 'image', 'im_info', 'im_id', 'gt_bbox', 'gt_class', 'is_crowd',
  61. 'gt_mask'
  62. ],
  63. }
  64. data_loader = Reader(
  65. coco_loader,
  66. sample_transforms=sample_trans,
  67. batch_transforms=batch_trans,
  68. batch_size=2,
  69. shuffle=True,
  70. drop_empty=True,
  71. inputs_def=inputs_def)()
  72. for i in range(2):
  73. for samples in data_loader:
  74. for sample in samples:
  75. im_shape = sample[0].shape
  76. self.assertEqual(im_shape[0], 3)
  77. self.assertEqual(im_shape[1] % 32, 0)
  78. self.assertEqual(im_shape[2] % 32, 0)
  79. im_info_shape = sample[1].shape
  80. self.assertEqual(im_info_shape[-1], 3)
  81. im_id_shape = sample[2].shape
  82. self.assertEqual(im_id_shape[-1], 1)
  83. gt_bbox_shape = sample[3].shape
  84. self.assertEqual(gt_bbox_shape[-1], 4)
  85. gt_class_shape = sample[4].shape
  86. self.assertEqual(gt_class_shape[-1], 1)
  87. self.assertEqual(gt_class_shape[0], gt_bbox_shape[0])
  88. is_crowd_shape = sample[5].shape
  89. self.assertEqual(is_crowd_shape[-1], 1)
  90. self.assertEqual(is_crowd_shape[0], gt_bbox_shape[0])
  91. mask = sample[6]
  92. self.assertEqual(len(mask), gt_bbox_shape[0])
  93. self.assertEqual(mask[0][0].shape[-1], 2)
  94. data_loader.reset()
  95. def test_loader_multi_threads(self):
  96. coco_loader = COCODataSet(
  97. dataset_dir=self.root_path,
  98. image_dir=self.image_dir,
  99. anno_path=self.anno_path,
  100. sample_num=10)
  101. sample_trans = [
  102. DecodeImage(to_rgb=True), ResizeImage(
  103. target_size=800, max_size=1333, interp=1), Permute(to_bgr=False)
  104. ]
  105. batch_trans = [PadBatch(pad_to_stride=32, use_padded_im_info=True), ]
  106. inputs_def = {
  107. 'fields': [
  108. 'image', 'im_info', 'im_id', 'gt_bbox', 'gt_class', 'is_crowd',
  109. 'gt_mask'
  110. ],
  111. }
  112. data_loader = Reader(
  113. coco_loader,
  114. sample_transforms=sample_trans,
  115. batch_transforms=batch_trans,
  116. batch_size=2,
  117. shuffle=True,
  118. drop_empty=True,
  119. worker_num=2,
  120. use_process=False,
  121. bufsize=8,
  122. inputs_def=inputs_def)()
  123. for i in range(2):
  124. for samples in data_loader:
  125. for sample in samples:
  126. im_shape = sample[0].shape
  127. self.assertEqual(im_shape[0], 3)
  128. self.assertEqual(im_shape[1] % 32, 0)
  129. self.assertEqual(im_shape[2] % 32, 0)
  130. im_info_shape = sample[1].shape
  131. self.assertEqual(im_info_shape[-1], 3)
  132. im_id_shape = sample[2].shape
  133. self.assertEqual(im_id_shape[-1], 1)
  134. gt_bbox_shape = sample[3].shape
  135. self.assertEqual(gt_bbox_shape[-1], 4)
  136. gt_class_shape = sample[4].shape
  137. self.assertEqual(gt_class_shape[-1], 1)
  138. self.assertEqual(gt_class_shape[0], gt_bbox_shape[0])
  139. is_crowd_shape = sample[5].shape
  140. self.assertEqual(is_crowd_shape[-1], 1)
  141. self.assertEqual(is_crowd_shape[0], gt_bbox_shape[0])
  142. mask = sample[6]
  143. self.assertEqual(len(mask), gt_bbox_shape[0])
  144. self.assertEqual(mask[0][0].shape[-1], 2)
  145. data_loader.reset()
  146. if __name__ == '__main__':
  147. enable_static_mode()
  148. unittest.main()