seg_dataset.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright (c) 2020 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. from __future__ import absolute_import
  15. import os.path as osp
  16. import random
  17. import copy
  18. import paddlex.utils.logging as logging
  19. from .dataset import Dataset
  20. from .dataset import get_encoding
  21. from .dataset import is_pic
  22. class SegDataset(Dataset):
  23. """读取语义分割任务数据集,并对样本进行相应的处理。
  24. Args:
  25. data_dir (str): 数据集所在的目录路径。
  26. file_list (str): 描述数据集图片文件和对应标注文件的文件路径(文本内每行路径为相对data_dir的相对路)。
  27. label_list (str): 描述数据集包含的类别信息文件路径。默认值为None。
  28. transforms (list): 数据集中每个样本的预处理/增强算子。
  29. num_workers (int): 数据集中样本在预处理过程中的线程或进程数。默认为4。
  30. buffer_size (int): 数据集中样本在预处理过程中队列的缓存长度,以样本数为单位。默认为100。
  31. parallel_method (str): 数据集中样本在预处理过程中并行处理的方式,支持'thread'
  32. 线程和'process'进程两种方式。默认为'process'(Windows和Mac下会强制使用thread,该参数无效)。
  33. shuffle (bool): 是否需要对数据集中样本打乱顺序。默认为False。
  34. """
  35. def __init__(self,
  36. data_dir,
  37. file_list,
  38. label_list=None,
  39. transforms=None,
  40. num_workers='auto',
  41. buffer_size=100,
  42. parallel_method='process',
  43. shuffle=False):
  44. super(SegDataset, self).__init__(
  45. transforms=transforms,
  46. num_workers=num_workers,
  47. buffer_size=buffer_size,
  48. parallel_method=parallel_method,
  49. shuffle=shuffle)
  50. self.file_list = list()
  51. self.labels = list()
  52. self._epoch = 0
  53. if label_list is not None:
  54. with open(label_list, encoding=get_encoding(label_list)) as f:
  55. for line in f:
  56. item = line.strip()
  57. self.labels.append(item)
  58. win_sep = "\\"
  59. other_sep = "/"
  60. with open(file_list, encoding=get_encoding(file_list)) as f:
  61. for line in f:
  62. items = line.strip().split()
  63. if platform.system() == "Windows":
  64. items[0] = win_sep.join(items[0].split(other_sep))
  65. items[1] = win_sep.join(items[1].split(other_sep))
  66. else:
  67. items[0] = other_sep.join(items[0].split(win_sep))
  68. items[1] = other_sep.join(items[1].split(win_sep))
  69. if not is_pic(items[0]):
  70. continue
  71. full_path_im = osp.join(data_dir, items[0])
  72. full_path_label = osp.join(data_dir, items[1])
  73. if not osp.exists(full_path_im):
  74. raise IOError('The image file {} is not exist!'.format(
  75. full_path_im))
  76. if not osp.exists(full_path_label):
  77. raise IOError('The image file {} is not exist!'.format(
  78. full_path_label))
  79. self.file_list.append([full_path_im, full_path_label])
  80. self.num_samples = len(self.file_list)
  81. logging.info("{} samples in file {}".format(
  82. len(self.file_list), file_list))
  83. def iterator(self):
  84. self._epoch += 1
  85. self._pos = 0
  86. files = copy.deepcopy(self.file_list)
  87. if self.shuffle:
  88. random.shuffle(files)
  89. files = files[:self.num_samples]
  90. self.num_samples = len(files)
  91. for f in files:
  92. label_path = f[1]
  93. sample = [f[0], None, label_path]
  94. yield sample