chase_db1.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. from paddlers.models.ppseg.utils.download import download_file_and_uncompress
  16. from paddlers.models.ppseg.utils import seg_env
  17. from paddlers.models.ppseg.cvlibs import manager
  18. from paddlers.models.ppseg.transforms import Compose
  19. from paddlers.models.ppseg.datasets import Dataset
  20. URL = 'https://bj.bcebos.com/paddleseg/dataset/chase_db1/chase_db1.zip'
  21. @manager.DATASETS.add_component
  22. class CHASEDB1(Dataset):
  23. """
  24. CHASE_DB1 dataset is a dataset for retinal vessel segmentation
  25. which contains 28 color retina images with the size of 999×960 pixels.
  26. It is collected from both left and right eyes of 14 school children.
  27. Each image is annotated by two independent human experts, and we choose the labels from 1st expert.
  28. (https://blogs.kingston.ac.uk/retinal/chasedb1/)
  29. Args:
  30. transforms (list): Transforms for image.
  31. dataset_root (str): The dataset directory. Default: None
  32. edge (bool): whether extract edge infor in the output
  33. mode (str, optional): Which part of dataset to use. it is one of ('train', 'val', 'test'). Default: 'train'.
  34. """
  35. NUM_CLASSES = 2
  36. def __init__(self,
  37. dataset_root=None,
  38. transforms=None,
  39. edge=False,
  40. mode='train'):
  41. self.dataset_root = dataset_root
  42. self.transforms = Compose(transforms)
  43. mode = mode.lower()
  44. self.mode = mode
  45. self.edge = edge
  46. self.file_list = list()
  47. self.num_classes = self.NUM_CLASSES
  48. self.ignore_index = 255 # labels only have 1/0, thus ignore_index is not necessary
  49. if mode not in ['train', 'val', 'test']:
  50. raise ValueError(
  51. "`mode` should be 'train', 'val' or 'test', but got {}.".format(
  52. mode))
  53. if self.transforms is None:
  54. raise ValueError("`transforms` is necessary, but it is None.")
  55. if self.dataset_root is None:
  56. self.dataset_root = download_file_and_uncompress(
  57. url=URL,
  58. savepath=seg_env.DATA_HOME,
  59. extrapath=seg_env.DATA_HOME)
  60. elif not os.path.exists(self.dataset_root):
  61. self.dataset_root = os.path.normpath(self.dataset_root)
  62. savepath, extraname = self.dataset_root.rsplit(
  63. sep=os.path.sep, maxsplit=1)
  64. self.dataset_root = download_file_and_uncompress(
  65. url=URL,
  66. savepath=savepath,
  67. extrapath=savepath,
  68. extraname=extraname)
  69. if mode == 'train':
  70. file_path = os.path.join(self.dataset_root, 'train_list.txt')
  71. elif mode == 'val':
  72. file_path = os.path.join(self.dataset_root, 'val_list.txt')
  73. with open(file_path, 'r') as f:
  74. for line in f:
  75. items = line.strip().split()
  76. if len(items) != 2:
  77. if mode == 'train' or mode == 'val':
  78. raise Exception(
  79. "File list format incorrect! It should be"
  80. " image_name label_name\\n")
  81. image_path = os.path.join(self.dataset_root, items[0])
  82. grt_path = None
  83. else:
  84. image_path = os.path.join(self.dataset_root, items[0])
  85. grt_path = os.path.join(self.dataset_root, items[1])
  86. self.file_list.append([image_path, grt_path])