voc.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. import os
  15. from paddlex.paddleseg.datasets import Dataset
  16. from paddlex.paddleseg.utils.download import download_file_and_uncompress
  17. from paddlex.paddleseg.utils import seg_env
  18. from paddlex.paddleseg.cvlibs import manager
  19. from paddlex.paddleseg.transforms import Compose
  20. URL = "http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar"
  21. @manager.DATASETS.add_component
  22. class PascalVOC(Dataset):
  23. """
  24. PascalVOC2012 dataset `http://host.robots.ox.ac.uk/pascal/VOC/`.
  25. If you want to augment the dataset, please run the voc_augment.py in tools.
  26. Args:
  27. transforms (list): Transforms for image.
  28. dataset_root (str): The dataset directory. Default: None
  29. mode (str, optional): Which part of dataset to use. it is one of ('train', 'trainval', 'trainaug', 'val').
  30. If you want to set mode to 'trainaug', please make sure the dataset have been augmented. Default: 'train'.
  31. edge (bool, optional): Whether to compute edge while training. Default: False
  32. """
  33. NUM_CLASSES = 21
  34. def __init__(self, transforms, dataset_root=None, mode='train',
  35. edge=False):
  36. self.dataset_root = dataset_root
  37. self.transforms = Compose(transforms)
  38. mode = mode.lower()
  39. self.mode = mode
  40. self.file_list = list()
  41. self.num_classes = self.NUM_CLASSES
  42. self.ignore_index = 255
  43. self.edge = edge
  44. if mode not in ['train', 'trainval', 'trainaug', 'val']:
  45. raise ValueError(
  46. "`mode` should be one of ('train', 'trainval', 'trainaug', 'val') in PascalVOC dataset, but got {}."
  47. .format(mode))
  48. if self.transforms is None:
  49. raise ValueError("`transforms` is necessary, but it is None.")
  50. if self.dataset_root is None:
  51. self.dataset_root = download_file_and_uncompress(
  52. url=URL,
  53. savepath=seg_env.DATA_HOME,
  54. extrapath=seg_env.DATA_HOME,
  55. extraname='VOCdevkit')
  56. elif not os.path.exists(self.dataset_root):
  57. self.dataset_root = os.path.normpath(self.dataset_root)
  58. savepath, extraname = self.dataset_root.rsplit(
  59. sep=os.path.sep, maxsplit=1)
  60. self.dataset_root = download_file_and_uncompress(
  61. url=URL,
  62. savepath=savepath,
  63. extrapath=savepath,
  64. extraname=extraname)
  65. image_set_dir = os.path.join(self.dataset_root, 'VOC2012', 'ImageSets',
  66. 'Segmentation')
  67. if mode == 'train':
  68. file_path = os.path.join(image_set_dir, 'train.txt')
  69. elif mode == 'val':
  70. file_path = os.path.join(image_set_dir, 'val.txt')
  71. elif mode == 'trainval':
  72. file_path = os.path.join(image_set_dir, 'trainval.txt')
  73. elif mode == 'trainaug':
  74. file_path = os.path.join(image_set_dir, 'train.txt')
  75. file_path_aug = os.path.join(image_set_dir, 'aug.txt')
  76. if not os.path.exists(file_path_aug):
  77. raise RuntimeError(
  78. "When `mode` is 'trainaug', Pascal Voc dataset should be augmented, "
  79. "Please make sure voc_augment.py has been properly run when using this mode."
  80. )
  81. img_dir = os.path.join(self.dataset_root, 'VOC2012', 'JPEGImages')
  82. label_dir = os.path.join(self.dataset_root, 'VOC2012',
  83. 'SegmentationClass')
  84. label_dir_aug = os.path.join(self.dataset_root, 'VOC2012',
  85. 'SegmentationClassAug')
  86. with open(file_path, 'r') as f:
  87. for line in f:
  88. line = line.strip()
  89. image_path = os.path.join(img_dir, ''.join([line, '.jpg']))
  90. label_path = os.path.join(label_dir, ''.join([line, '.png']))
  91. self.file_list.append([image_path, label_path])
  92. if mode == 'trainaug':
  93. with open(file_path_aug, 'r') as f:
  94. for line in f:
  95. line = line.strip()
  96. image_path = os.path.join(img_dir, ''.join([line, '.jpg']))
  97. label_path = os.path.join(label_dir_aug,
  98. ''.join([line, '.png']))
  99. self.file_list.append([image_path, label_path])