browse_dataset.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import argparse
  3. import os
  4. import os.path as osp
  5. import mmcv
  6. import mmengine
  7. import mmengine.fileio as fileio
  8. import numpy as np
  9. from mmengine import Config, DictAction
  10. from mmengine.registry import build_from_cfg, init_default_scope
  11. from mmengine.structures import InstanceData
  12. from mmpose.registry import DATASETS, VISUALIZERS
  13. from mmpose.structures import PoseDataSample
  14. def parse_args():
  15. parser = argparse.ArgumentParser(description='Browse a dataset')
  16. parser.add_argument('config', help='train config file path')
  17. parser.add_argument(
  18. '--output-dir',
  19. default=None,
  20. type=str,
  21. help='If there is no display interface, you can save it.')
  22. parser.add_argument('--not-show', default=False, action='store_true')
  23. parser.add_argument(
  24. '--phase',
  25. default='train',
  26. type=str,
  27. choices=['train', 'test', 'val'],
  28. help='phase of dataset to visualize, accept "train" "test" and "val".'
  29. ' Defaults to "train".')
  30. parser.add_argument(
  31. '--show-interval',
  32. type=float,
  33. default=2,
  34. help='the interval of show (s)')
  35. parser.add_argument(
  36. '--mode',
  37. default='transformed',
  38. type=str,
  39. choices=['original', 'transformed'],
  40. help='display mode; display original pictures or transformed '
  41. 'pictures. "original" means to show images load from disk'
  42. '; "transformed" means to show images after transformed;'
  43. 'Defaults to "transformed".')
  44. parser.add_argument(
  45. '--cfg-options',
  46. nargs='+',
  47. action=DictAction,
  48. help='override some settings in the used config, the key-value pair '
  49. 'in xxx=yyy format will be merged into config file. If the value to '
  50. 'be overwritten is a list, it should be like key="[a,b]" or key=a,b '
  51. 'It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" '
  52. 'Note that the quotation marks are necessary and that no white space '
  53. 'is allowed.')
  54. args = parser.parse_args()
  55. return args
  56. def generate_dup_file_name(out_file):
  57. """Automatically rename out_file when duplicated file exists.
  58. This case occurs when there is multiple instances on one image.
  59. """
  60. if out_file and osp.exists(out_file):
  61. img_name, postfix = osp.basename(out_file).rsplit('.', 1)
  62. exist_files = tuple(
  63. filter(lambda f: f.startswith(img_name),
  64. os.listdir(osp.dirname(out_file))))
  65. if len(exist_files) > 0:
  66. img_path = f'{img_name}({len(exist_files)}).{postfix}'
  67. out_file = osp.join(osp.dirname(out_file), img_path)
  68. return out_file
  69. def main():
  70. args = parse_args()
  71. cfg = Config.fromfile(args.config)
  72. if args.cfg_options is not None:
  73. cfg.merge_from_dict(args.cfg_options)
  74. backend_args = cfg.get('backend_args', dict(backend='local'))
  75. # register all modules in mmpose into the registries
  76. init_default_scope(cfg.get('default_scope', 'mmpose'))
  77. if args.mode == 'original':
  78. cfg[f'{args.phase}_dataloader'].dataset.pipeline = []
  79. else:
  80. # pack transformed keypoints for visualization
  81. cfg[f'{args.phase}_dataloader'].dataset.pipeline[
  82. -1].pack_transformed = True
  83. dataset = build_from_cfg(cfg[f'{args.phase}_dataloader'].dataset, DATASETS)
  84. visualizer = VISUALIZERS.build(cfg.visualizer)
  85. visualizer.set_dataset_meta(dataset.metainfo)
  86. progress_bar = mmengine.ProgressBar(len(dataset))
  87. idx = 0
  88. item = dataset[0]
  89. while idx < len(dataset):
  90. idx += 1
  91. next_item = None if idx >= len(dataset) else dataset[idx]
  92. if args.mode == 'original':
  93. if next_item is not None and item['img_path'] == next_item[
  94. 'img_path']:
  95. # merge annotations for one image
  96. item['keypoints'] = np.concatenate(
  97. (item['keypoints'], next_item['keypoints']))
  98. item['keypoints_visible'] = np.concatenate(
  99. (item['keypoints_visible'],
  100. next_item['keypoints_visible']))
  101. item['bbox'] = np.concatenate(
  102. (item['bbox'], next_item['bbox']))
  103. progress_bar.update()
  104. continue
  105. else:
  106. img_path = item['img_path']
  107. img_bytes = fileio.get(img_path, backend_args=backend_args)
  108. img = mmcv.imfrombytes(img_bytes, channel_order='bgr')
  109. # forge pseudo data_sample
  110. gt_instances = InstanceData()
  111. gt_instances.keypoints = item['keypoints']
  112. gt_instances.keypoints_visible = item['keypoints_visible']
  113. gt_instances.bboxes = item['bbox']
  114. data_sample = PoseDataSample()
  115. data_sample.gt_instances = gt_instances
  116. item = next_item
  117. else:
  118. img = item['inputs'].permute(1, 2, 0).numpy()
  119. data_sample = item['data_samples']
  120. img_path = data_sample.img_path
  121. item = next_item
  122. out_file = osp.join(
  123. args.output_dir,
  124. osp.basename(img_path)) if args.output_dir is not None else None
  125. out_file = generate_dup_file_name(out_file)
  126. img = mmcv.bgr2rgb(img)
  127. visualizer.add_datasample(
  128. osp.basename(img_path),
  129. img,
  130. data_sample,
  131. draw_pred=False,
  132. draw_bbox=(args.mode == 'original'),
  133. draw_heatmap=True,
  134. show=not args.not_show,
  135. wait_time=args.show_interval,
  136. out_file=out_file)
  137. progress_bar.update()
  138. if __name__ == '__main__':
  139. main()