hrf.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import argparse
  3. import os
  4. import os.path as osp
  5. import tempfile
  6. import zipfile
  7. import mmcv
  8. from mmengine.utils import mkdir_or_exist
  9. HRF_LEN = 15
  10. TRAINING_LEN = 5
  11. def parse_args():
  12. parser = argparse.ArgumentParser(
  13. description='Convert HRF dataset to mmsegmentation format')
  14. parser.add_argument('healthy_path', help='the path of healthy.zip')
  15. parser.add_argument(
  16. 'healthy_manualsegm_path', help='the path of healthy_manualsegm.zip')
  17. parser.add_argument('glaucoma_path', help='the path of glaucoma.zip')
  18. parser.add_argument(
  19. 'glaucoma_manualsegm_path', help='the path of glaucoma_manualsegm.zip')
  20. parser.add_argument(
  21. 'diabetic_retinopathy_path',
  22. help='the path of diabetic_retinopathy.zip')
  23. parser.add_argument(
  24. 'diabetic_retinopathy_manualsegm_path',
  25. help='the path of diabetic_retinopathy_manualsegm.zip')
  26. parser.add_argument('--tmp_dir', help='path of the temporary directory')
  27. parser.add_argument('-o', '--out_dir', help='output path')
  28. args = parser.parse_args()
  29. return args
  30. def main():
  31. args = parse_args()
  32. images_path = [
  33. args.healthy_path, args.glaucoma_path, args.diabetic_retinopathy_path
  34. ]
  35. annotations_path = [
  36. args.healthy_manualsegm_path, args.glaucoma_manualsegm_path,
  37. args.diabetic_retinopathy_manualsegm_path
  38. ]
  39. if args.out_dir is None:
  40. out_dir = osp.join('data', 'HRF')
  41. else:
  42. out_dir = args.out_dir
  43. print('Making directories...')
  44. mkdir_or_exist(out_dir)
  45. mkdir_or_exist(osp.join(out_dir, 'images'))
  46. mkdir_or_exist(osp.join(out_dir, 'images', 'training'))
  47. mkdir_or_exist(osp.join(out_dir, 'images', 'validation'))
  48. mkdir_or_exist(osp.join(out_dir, 'annotations'))
  49. mkdir_or_exist(osp.join(out_dir, 'annotations', 'training'))
  50. mkdir_or_exist(osp.join(out_dir, 'annotations', 'validation'))
  51. print('Generating images...')
  52. for now_path in images_path:
  53. with tempfile.TemporaryDirectory(dir=args.tmp_dir) as tmp_dir:
  54. zip_file = zipfile.ZipFile(now_path)
  55. zip_file.extractall(tmp_dir)
  56. assert len(os.listdir(tmp_dir)) == HRF_LEN, \
  57. f'len(os.listdir(tmp_dir)) != {HRF_LEN}'
  58. for filename in sorted(os.listdir(tmp_dir))[:TRAINING_LEN]:
  59. img = mmcv.imread(osp.join(tmp_dir, filename))
  60. mmcv.imwrite(
  61. img,
  62. osp.join(out_dir, 'images', 'training',
  63. osp.splitext(filename)[0] + '.png'))
  64. for filename in sorted(os.listdir(tmp_dir))[TRAINING_LEN:]:
  65. img = mmcv.imread(osp.join(tmp_dir, filename))
  66. mmcv.imwrite(
  67. img,
  68. osp.join(out_dir, 'images', 'validation',
  69. osp.splitext(filename)[0] + '.png'))
  70. print('Generating annotations...')
  71. for now_path in annotations_path:
  72. with tempfile.TemporaryDirectory(dir=args.tmp_dir) as tmp_dir:
  73. zip_file = zipfile.ZipFile(now_path)
  74. zip_file.extractall(tmp_dir)
  75. assert len(os.listdir(tmp_dir)) == HRF_LEN, \
  76. f'len(os.listdir(tmp_dir)) != {HRF_LEN}'
  77. for filename in sorted(os.listdir(tmp_dir))[:TRAINING_LEN]:
  78. img = mmcv.imread(osp.join(tmp_dir, filename))
  79. # The annotation img should be divided by 128, because some of
  80. # the annotation imgs are not standard. We should set a
  81. # threshold to convert the nonstandard annotation imgs. The
  82. # value divided by 128 is equivalent to '1 if value >= 128
  83. # else 0'
  84. mmcv.imwrite(
  85. img[:, :, 0] // 128,
  86. osp.join(out_dir, 'annotations', 'training',
  87. osp.splitext(filename)[0] + '.png'))
  88. for filename in sorted(os.listdir(tmp_dir))[TRAINING_LEN:]:
  89. img = mmcv.imread(osp.join(tmp_dir, filename))
  90. mmcv.imwrite(
  91. img[:, :, 0] // 128,
  92. osp.join(out_dir, 'annotations', 'validation',
  93. osp.splitext(filename)[0] + '.png'))
  94. print('Done!')
  95. if __name__ == '__main__':
  96. main()