browse_dataset.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import argparse
  3. import os.path as osp
  4. import mmengine
  5. from mmengine import Config, DictAction
  6. from mmengine.registry import init_default_scope
  7. from mmdet.registry import DATASETS, VISUALIZERS
  8. def parse_args():
  9. parser = argparse.ArgumentParser(description='Browse a dataset')
  10. parser.add_argument('config', help='train config file path')
  11. parser.add_argument(
  12. '--output-dir',
  13. default=None,
  14. type=str,
  15. help='If there is no display interface, you can save it')
  16. parser.add_argument('--show', default=True, action='store_true')
  17. parser.add_argument(
  18. '--show-interval',
  19. type=float,
  20. default=2,
  21. help='the interval of show (s)')
  22. parser.add_argument(
  23. '--cfg-options',
  24. nargs='+',
  25. action=DictAction,
  26. help='override some settings in the used config, the key-value pair '
  27. 'in xxx=yyy format will be merged into config file. If the value to '
  28. 'be overwritten is a list, it should be like key="[a,b]" or key=a,b '
  29. 'It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" '
  30. 'Note that the quotation marks are necessary and that no white space '
  31. 'is allowed.')
  32. args = parser.parse_args()
  33. return args
  34. def main():
  35. args = parse_args()
  36. cfg = Config.fromfile(args.config)
  37. if args.cfg_options is not None:
  38. cfg.merge_from_dict(args.cfg_options)
  39. init_default_scope(cfg.get('default_scope', 'mmdet'))
  40. dataset = DATASETS.build(cfg.train_dataloader.dataset)
  41. visualizer = VISUALIZERS.build(cfg.visualizer)
  42. visualizer.dataset_meta = dataset.metainfo
  43. progress_bar = mmengine.ProgressBar(len(dataset))
  44. for idx, item in enumerate(dataset): # inputs data_samples
  45. data_sample = item['data_samples']
  46. input = item['inputs']
  47. for img_idx in range(len(data_sample)):
  48. img_data_sample = data_sample[img_idx]
  49. img_path = img_data_sample.img_path
  50. img = input[img_idx].permute(1, 2, 0).numpy()
  51. out_file = osp.join(
  52. args.output_dir,
  53. str(idx).zfill(6),
  54. f'img_{img_idx}.jpg') if args.output_dir is not None else None
  55. img = img[..., [2, 1, 0]] # bgr to rgb
  56. visualizer.add_datasample(
  57. osp.basename(img_path),
  58. img,
  59. data_sample=img_data_sample,
  60. draw_pred=False,
  61. show=args.show,
  62. wait_time=args.show_interval,
  63. out_file=out_file)
  64. # Record file path mapping.
  65. if args.output_dir is not None:
  66. with open(
  67. osp.join(args.output_dir,
  68. str(idx).zfill(6), 'info.txt'), 'a') as f:
  69. f.write(f'The source filepath of img_{img_idx}.jpg'
  70. f'is `{img_path}`.\n')
  71. progress_bar.update()
  72. if __name__ == '__main__':
  73. main()