seg_dataset.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. with open(file_list, encoding=get_encoding(file_list)) as f:
  59. for line in f:
  60. items = line.strip().split()
  61. if not is_pic(items[0]):
  62. continue
  63. full_path_im = osp.join(data_dir, items[0])
  64. full_path_label = osp.join(data_dir, items[1])
  65. if not osp.exists(full_path_im):
  66. raise IOError('The image file {} is not exist!'.format(
  67. full_path_im))
  68. if not osp.exists(full_path_label):
  69. raise IOError('The image file {} is not exist!'.format(
  70. full_path_label))
  71. self.file_list.append([full_path_im, full_path_label])
  72. self.num_samples = len(self.file_list)
  73. logging.info("{} samples in file {}".format(
  74. len(self.file_list), file_list))
  75. def iterator(self):
  76. self._epoch += 1
  77. self._pos = 0
  78. files = copy.deepcopy(self.file_list)
  79. if self.shuffle:
  80. random.shuffle(files)
  81. files = files[:self.num_samples]
  82. self.num_samples = len(files)
  83. for f in files:
  84. label_path = f[1]
  85. sample = [f[0], None, label_path]
  86. yield sample