formatting.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import numpy as np
  3. from mmcv.transforms import to_tensor
  4. from mmcv.transforms.base import BaseTransform
  5. from mmengine.structures import InstanceData, PixelData
  6. from mmdet.registry import TRANSFORMS
  7. from mmdet.structures import DetDataSample
  8. from mmdet.structures.bbox import BaseBoxes
  9. @TRANSFORMS.register_module()
  10. class PackDetInputs(BaseTransform):
  11. """Pack the inputs data for the detection / semantic segmentation /
  12. panoptic segmentation.
  13. The ``img_meta`` item is always populated. The contents of the
  14. ``img_meta`` dictionary depends on ``meta_keys``. By default this includes:
  15. - ``img_id``: id of the image
  16. - ``img_path``: path to the image file
  17. - ``ori_shape``: original shape of the image as a tuple (h, w)
  18. - ``img_shape``: shape of the image input to the network as a tuple \
  19. (h, w). Note that images may be zero padded on the \
  20. bottom/right if the batch tensor is larger than this shape.
  21. - ``scale_factor``: a float indicating the preprocessing scale
  22. - ``flip``: a boolean indicating if image flip transform was used
  23. - ``flip_direction``: the flipping direction
  24. Args:
  25. meta_keys (Sequence[str], optional): Meta keys to be converted to
  26. ``mmcv.DataContainer`` and collected in ``data[img_metas]``.
  27. Default: ``('img_id', 'img_path', 'ori_shape', 'img_shape',
  28. 'scale_factor', 'flip', 'flip_direction')``
  29. """
  30. mapping_table = {
  31. 'gt_bboxes': 'bboxes',
  32. 'gt_bboxes_labels': 'labels',
  33. 'gt_masks': 'masks'
  34. }
  35. def __init__(self,
  36. meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape',
  37. 'scale_factor', 'flip', 'flip_direction')):
  38. self.meta_keys = meta_keys
  39. def transform(self, results: dict) -> dict:
  40. """Method to pack the input data.
  41. Args:
  42. results (dict): Result dict from the data pipeline.
  43. Returns:
  44. dict:
  45. - 'inputs' (obj:`torch.Tensor`): The forward data of models.
  46. - 'data_sample' (obj:`DetDataSample`): The annotation info of the
  47. sample.
  48. """
  49. packed_results = dict()
  50. if 'img' in results:
  51. img = results['img']
  52. if len(img.shape) < 3:
  53. img = np.expand_dims(img, -1)
  54. # To improve the computational speed by by 3-5 times, apply:
  55. # If image is not contiguous, use
  56. # `numpy.transpose()` followed by `numpy.ascontiguousarray()`
  57. # If image is already contiguous, use
  58. # `torch.permute()` followed by `torch.contiguous()`
  59. # Refer to https://github.com/open-mmlab/mmdetection/pull/9533
  60. # for more details
  61. if not img.flags.c_contiguous:
  62. img = np.ascontiguousarray(img.transpose(2, 0, 1))
  63. img = to_tensor(img)
  64. else:
  65. img = to_tensor(img).permute(2, 0, 1).contiguous()
  66. packed_results['inputs'] = img
  67. if 'gt_ignore_flags' in results:
  68. valid_idx = np.where(results['gt_ignore_flags'] == 0)[0]
  69. ignore_idx = np.where(results['gt_ignore_flags'] == 1)[0]
  70. data_sample = DetDataSample()
  71. instance_data = InstanceData()
  72. ignore_instance_data = InstanceData()
  73. for key in self.mapping_table.keys():
  74. if key not in results:
  75. continue
  76. if key == 'gt_masks' or isinstance(results[key], BaseBoxes):
  77. if 'gt_ignore_flags' in results:
  78. instance_data[
  79. self.mapping_table[key]] = results[key][valid_idx]
  80. ignore_instance_data[
  81. self.mapping_table[key]] = results[key][ignore_idx]
  82. else:
  83. instance_data[self.mapping_table[key]] = results[key]
  84. else:
  85. if 'gt_ignore_flags' in results:
  86. instance_data[self.mapping_table[key]] = to_tensor(
  87. results[key][valid_idx])
  88. ignore_instance_data[self.mapping_table[key]] = to_tensor(
  89. results[key][ignore_idx])
  90. else:
  91. instance_data[self.mapping_table[key]] = to_tensor(
  92. results[key])
  93. data_sample.gt_instances = instance_data
  94. data_sample.ignored_instances = ignore_instance_data
  95. if 'proposals' in results:
  96. proposals = InstanceData(
  97. bboxes=to_tensor(results['proposals']),
  98. scores=to_tensor(results['proposals_scores']))
  99. data_sample.proposals = proposals
  100. if 'gt_seg_map' in results:
  101. gt_sem_seg_data = dict(
  102. sem_seg=to_tensor(results['gt_seg_map'][None, ...].copy()))
  103. data_sample.gt_sem_seg = PixelData(**gt_sem_seg_data)
  104. img_meta = {}
  105. for key in self.meta_keys:
  106. assert key in results, f'`{key}` is not found in `results`, ' \
  107. f'the valid keys are {list(results)}.'
  108. img_meta[key] = results[key]
  109. data_sample.set_metainfo(img_meta)
  110. packed_results['data_samples'] = data_sample
  111. return packed_results
  112. def __repr__(self) -> str:
  113. repr_str = self.__class__.__name__
  114. repr_str += f'(meta_keys={self.meta_keys})'
  115. return repr_str
  116. @TRANSFORMS.register_module()
  117. class ToTensor:
  118. """Convert some results to :obj:`torch.Tensor` by given keys.
  119. Args:
  120. keys (Sequence[str]): Keys that need to be converted to Tensor.
  121. """
  122. def __init__(self, keys):
  123. self.keys = keys
  124. def __call__(self, results):
  125. """Call function to convert data in results to :obj:`torch.Tensor`.
  126. Args:
  127. results (dict): Result dict contains the data to convert.
  128. Returns:
  129. dict: The result dict contains the data converted
  130. to :obj:`torch.Tensor`.
  131. """
  132. for key in self.keys:
  133. results[key] = to_tensor(results[key])
  134. return results
  135. def __repr__(self):
  136. return self.__class__.__name__ + f'(keys={self.keys})'
  137. @TRANSFORMS.register_module()
  138. class ImageToTensor:
  139. """Convert image to :obj:`torch.Tensor` by given keys.
  140. The dimension order of input image is (H, W, C). The pipeline will convert
  141. it to (C, H, W). If only 2 dimension (H, W) is given, the output would be
  142. (1, H, W).
  143. Args:
  144. keys (Sequence[str]): Key of images to be converted to Tensor.
  145. """
  146. def __init__(self, keys):
  147. self.keys = keys
  148. def __call__(self, results):
  149. """Call function to convert image in results to :obj:`torch.Tensor` and
  150. transpose the channel order.
  151. Args:
  152. results (dict): Result dict contains the image data to convert.
  153. Returns:
  154. dict: The result dict contains the image converted
  155. to :obj:`torch.Tensor` and permuted to (C, H, W) order.
  156. """
  157. for key in self.keys:
  158. img = results[key]
  159. if len(img.shape) < 3:
  160. img = np.expand_dims(img, -1)
  161. results[key] = to_tensor(img).permute(2, 0, 1).contiguous()
  162. return results
  163. def __repr__(self):
  164. return self.__class__.__name__ + f'(keys={self.keys})'
  165. @TRANSFORMS.register_module()
  166. class Transpose:
  167. """Transpose some results by given keys.
  168. Args:
  169. keys (Sequence[str]): Keys of results to be transposed.
  170. order (Sequence[int]): Order of transpose.
  171. """
  172. def __init__(self, keys, order):
  173. self.keys = keys
  174. self.order = order
  175. def __call__(self, results):
  176. """Call function to transpose the channel order of data in results.
  177. Args:
  178. results (dict): Result dict contains the data to transpose.
  179. Returns:
  180. dict: The result dict contains the data transposed to \
  181. ``self.order``.
  182. """
  183. for key in self.keys:
  184. results[key] = results[key].transpose(self.order)
  185. return results
  186. def __repr__(self):
  187. return self.__class__.__name__ + \
  188. f'(keys={self.keys}, order={self.order})'
  189. @TRANSFORMS.register_module()
  190. class WrapFieldsToLists:
  191. """Wrap fields of the data dictionary into lists for evaluation.
  192. This class can be used as a last step of a test or validation
  193. pipeline for single image evaluation or inference.
  194. Example:
  195. >>> test_pipeline = [
  196. >>> dict(type='LoadImageFromFile'),
  197. >>> dict(type='Normalize',
  198. mean=[123.675, 116.28, 103.53],
  199. std=[58.395, 57.12, 57.375],
  200. to_rgb=True),
  201. >>> dict(type='Pad', size_divisor=32),
  202. >>> dict(type='ImageToTensor', keys=['img']),
  203. >>> dict(type='Collect', keys=['img']),
  204. >>> dict(type='WrapFieldsToLists')
  205. >>> ]
  206. """
  207. def __call__(self, results):
  208. """Call function to wrap fields into lists.
  209. Args:
  210. results (dict): Result dict contains the data to wrap.
  211. Returns:
  212. dict: The result dict where value of ``self.keys`` are wrapped \
  213. into list.
  214. """
  215. # Wrap dict fields into lists
  216. for key, val in results.items():
  217. results[key] = [val]
  218. return results
  219. def __repr__(self):
  220. return f'{self.__class__.__name__}()'