pascal_context.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 PIL import Image
  16. from paddlex.paddleseg.datasets import Dataset
  17. from paddlex.paddleseg.cvlibs import manager
  18. from paddlex.paddleseg.transforms import Compose
  19. @manager.DATASETS.add_component
  20. class PascalContext(Dataset):
  21. """
  22. PascalVOC2010 dataset `http://host.robots.ox.ac.uk/pascal/VOC/`.
  23. If you want to use pascal context dataset, please run the convert_voc2010.py in tools firstly.
  24. Args:
  25. transforms (list): Transforms for image.
  26. dataset_root (str): The dataset directory. Default: None
  27. mode (str): Which part of dataset to use. it is one of ('train', 'trainval', 'context', 'val').
  28. If you want to set mode to 'context', please make sure the dataset have been augmented. Default: 'train'.
  29. """
  30. NUM_CLASSES = 59
  31. def __init__(self, transforms=None, dataset_root=None, mode='train'):
  32. self.dataset_root = dataset_root
  33. self.transforms = Compose(transforms)
  34. mode = mode.lower()
  35. self.mode = mode
  36. self.file_list = list()
  37. self.num_classes = self.NUM_CLASSES
  38. self.ignore_index = 255
  39. if mode not in ['train', 'trainval', 'val']:
  40. raise ValueError(
  41. "`mode` should be one of ('train', 'trainval', 'val') in PascalContext dataset, but got {}."
  42. .format(mode))
  43. if self.transforms is None:
  44. raise ValueError("`transforms` is necessary, but it is None.")
  45. if self.dataset_root is None:
  46. raise ValueError(
  47. "The dataset is not Found or the folder structure is nonconfoumance."
  48. )
  49. image_set_dir = os.path.join(self.dataset_root, 'ImageSets',
  50. 'Segmentation')
  51. if mode == 'train':
  52. file_path = os.path.join(image_set_dir, 'train_context.txt')
  53. elif mode == 'val':
  54. file_path = os.path.join(image_set_dir, 'val_context.txt')
  55. elif mode == 'trainval':
  56. file_path = os.path.join(image_set_dir, 'trainval_context.txt')
  57. if not os.path.exists(file_path):
  58. raise RuntimeError(
  59. "PASCAL-Context annotations are not ready, "
  60. "Please make sure voc_context.py has been properly run.")
  61. img_dir = os.path.join(self.dataset_root, 'JPEGImages')
  62. label_dir = os.path.join(self.dataset_root, 'Context')
  63. with open(file_path, 'r') as f:
  64. for line in f:
  65. line = line.strip()
  66. image_path = os.path.join(img_dir, ''.join([line, '.jpg']))
  67. label_path = os.path.join(label_dir, ''.join([line, '.png']))
  68. self.file_list.append([image_path, label_path])