browse_dataset.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import argparse
  3. import os.path as osp
  4. from mmengine.config import Config, DictAction
  5. from mmengine.utils import ProgressBar
  6. from mmseg.registry import DATASETS, VISUALIZERS
  7. from mmseg.utils import register_all_modules
  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 mmdet into the registries
  40. register_all_modules()
  41. dataset = DATASETS.build(cfg.train_dataloader.dataset)
  42. visualizer = VISUALIZERS.build(cfg.visualizer)
  43. visualizer.dataset_meta = dataset.metainfo
  44. progress_bar = ProgressBar(len(dataset))
  45. for item in dataset:
  46. img = item['inputs'].permute(1, 2, 0).numpy()
  47. img = img[..., [2, 1, 0]] # bgr to rgb
  48. data_sample = item['data_samples'].numpy()
  49. img_path = osp.basename(item['data_samples'].img_path)
  50. out_file = osp.join(
  51. args.output_dir,
  52. osp.basename(img_path)) if args.output_dir is not None else None
  53. visualizer.add_datasample(
  54. name=osp.basename(img_path),
  55. image=img,
  56. data_sample=data_sample,
  57. draw_gt=True,
  58. draw_pred=False,
  59. wait_time=args.show_interval,
  60. out_file=out_file,
  61. show=not args.not_show)
  62. progress_bar.update()
  63. if __name__ == '__main__':
  64. main()