change_det_dataset.py 4.8 KB

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