browse_dataset.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import argparse
  3. import os.path as osp
  4. from mmengine import Config, DictAction
  5. from mmengine.registry import init_default_scope
  6. from mmengine.utils import ProgressBar
  7. from mmseg.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('--not-show', default=False, 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. # register all modules in mmseg into the registries
  40. init_default_scope('mmseg')
  41. dataset = DATASETS.build(cfg.train_dataloader.dataset)
  42. cfg.visualizer['save_dir'] = args.output_dir
  43. visualizer = VISUALIZERS.build(cfg.visualizer)
  44. visualizer.dataset_meta = dataset.METAINFO
  45. progress_bar = ProgressBar(len(dataset))
  46. for item in dataset:
  47. img = item['inputs'].permute(1, 2, 0).numpy()
  48. data_sample = item['data_samples'].numpy()
  49. img_path = osp.basename(item['data_samples'].img_path)
  50. img = img[..., [2, 1, 0]] # bgr to rgb
  51. visualizer.add_datasample(
  52. osp.basename(img_path),
  53. img,
  54. data_sample,
  55. show=not args.not_show,
  56. wait_time=args.show_interval)
  57. progress_bar.update()
  58. if __name__ == '__main__':
  59. main()