seg_dataset.py 4.1 KB

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