seg_dataset.py 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Copyright (c) 2022 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.path as osp
  15. import copy
  16. from .base import BaseDataset
  17. from paddlers.utils import logging, get_encoding, norm_path, is_pic
  18. class SegDataset(BaseDataset):
  19. """
  20. Dataset for semantic segmentation tasks.
  21. Args:
  22. data_dir (str): Root directory of the dataset.
  23. file_list (str): Path of the file that contains relative paths of images and annotation files.
  24. transforms (paddlers.transforms.Compose): Data preprocessing and data augmentation operators to apply.
  25. label_list (str|None, optional): Path of the file that contains the category names. Defaults to None.
  26. num_workers (int|str, optional): Number of processes used for data loading. If `num_workers` is 'auto',
  27. the number of workers will be automatically determined according to the number of CPU cores: If
  28. there are more than 16 cores,8 workers will be used. Otherwise, the number of workers will be half
  29. the number of CPU cores. Defaults: 'auto'.
  30. shuffle (bool, optional): Whether to shuffle the samples. Defaults to False.
  31. """
  32. def __init__(self,
  33. data_dir,
  34. file_list,
  35. transforms,
  36. label_list=None,
  37. num_workers='auto',
  38. shuffle=False):
  39. super(SegDataset, self).__init__(data_dir, label_list, transforms,
  40. num_workers, shuffle)
  41. # TODO batch padding
  42. self.batch_transforms = None
  43. self.file_list = list()
  44. self.labels = list()
  45. # TODO:非None时,让用户跳转数据集分析生成label_list
  46. # 不要在此处分析label file
  47. if label_list is not None:
  48. with open(label_list, encoding=get_encoding(label_list)) as f:
  49. for line in f:
  50. item = line.strip()
  51. self.labels.append(item)
  52. with open(file_list, encoding=get_encoding(file_list)) as f:
  53. for line in f:
  54. items = line.strip().split()
  55. if len(items) > 2:
  56. raise ValueError(
  57. "A space is defined as the delimiter to separate the image and label path, " \
  58. "so the space cannot be in the image or label path, but the line[{}] of " \
  59. " file_list[{}] has a space in the image or label path.".format(line, file_list))
  60. items[0] = norm_path(items[0])
  61. items[1] = norm_path(items[1])
  62. full_path_im = osp.join(data_dir, items[0])
  63. full_path_label = osp.join(data_dir, items[1])
  64. if not is_pic(full_path_im) or not is_pic(full_path_label):
  65. continue
  66. if not osp.exists(full_path_im):
  67. raise IOError('Image file {} does not exist!'.format(
  68. full_path_im))
  69. if not osp.exists(full_path_label):
  70. raise IOError('Label file {} does not exist!'.format(
  71. full_path_label))
  72. self.file_list.append({
  73. 'image': full_path_im,
  74. 'mask': full_path_label
  75. })
  76. self.num_samples = len(self.file_list)
  77. logging.info("{} samples in file {}".format(
  78. len(self.file_list), file_list))
  79. def __len__(self):
  80. return len(self.file_list)