Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

sbd.py 5.3 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  1. import os
  2. import shutil
  3. from pathlib import Path
  4. from typing import Any, Callable, Optional, Tuple, Union
  5. import numpy as np
  6. from PIL import Image
  7. from .utils import download_and_extract_archive, download_url, verify_str_arg
  8. from .vision import VisionDataset
  9. class SBDataset(VisionDataset):
  10. """`Semantic Boundaries Dataset <http://home.bharathh.info/pubs/codes/SBD/download.html>`_
  11. The SBD currently contains annotations from 11355 images taken from the PASCAL VOC 2011 dataset.
  12. .. note ::
  13. Please note that the train and val splits included with this dataset are different from
  14. the splits in the PASCAL VOC dataset. In particular some "train" images might be part of
  15. VOC2012 val.
  16. If you are interested in testing on VOC 2012 val, then use `image_set='train_noval'`,
  17. which excludes all val images.
  18. .. warning::
  19. This class needs `scipy <https://docs.scipy.org/doc/>`_ to load target files from `.mat` format.
  20. Args:
  21. root (str or ``pathlib.Path``): Root directory of the Semantic Boundaries Dataset
  22. image_set (string, optional): Select the image_set to use, ``train``, ``val`` or ``train_noval``.
  23. Image set ``train_noval`` excludes VOC 2012 val images.
  24. mode (string, optional): Select target type. Possible values 'boundaries' or 'segmentation'.
  25. In case of 'boundaries', the target is an array of shape `[num_classes, H, W]`,
  26. where `num_classes=20`.
  27. download (bool, optional): If true, downloads the dataset from the internet and
  28. puts it in root directory. If dataset is already downloaded, it is not
  29. downloaded again.
  30. transforms (callable, optional): A function/transform that takes input sample and its target as entry
  31. and returns a transformed version. Input sample is PIL image and target is a numpy array
  32. if `mode='boundaries'` or PIL image if `mode='segmentation'`.
  33. """
  34. url = "https://www2.eecs.berkeley.edu/Research/Projects/CS/vision/grouping/semantic_contours/benchmark.tgz"
  35. md5 = "82b4d87ceb2ed10f6038a1cba92111cb"
  36. filename = "benchmark.tgz"
  37. voc_train_url = "https://www.cs.cornell.edu/~bharathh/train_noval.txt"
  38. voc_split_filename = "train_noval.txt"
  39. voc_split_md5 = "79bff800c5f0b1ec6b21080a3c066722"
  40. def __init__(
  41. self,
  42. root: Union[str, Path],
  43. image_set: str = "train",
  44. mode: str = "boundaries",
  45. download: bool = False,
  46. transforms: Optional[Callable] = None,
  47. ) -> None:
  48. try:
  49. from scipy.io import loadmat
  50. self._loadmat = loadmat
  51. except ImportError:
  52. raise RuntimeError("Scipy is not found. This dataset needs to have scipy installed: pip install scipy")
  53. super().__init__(root, transforms)
  54. self.image_set = verify_str_arg(image_set, "image_set", ("train", "val", "train_noval"))
  55. self.mode = verify_str_arg(mode, "mode", ("segmentation", "boundaries"))
  56. self.num_classes = 20
  57. sbd_root = self.root
  58. image_dir = os.path.join(sbd_root, "img")
  59. mask_dir = os.path.join(sbd_root, "cls")
  60. if download:
  61. download_and_extract_archive(self.url, self.root, filename=self.filename, md5=self.md5)
  62. extracted_ds_root = os.path.join(self.root, "benchmark_RELEASE", "dataset")
  63. for f in ["cls", "img", "inst", "train.txt", "val.txt"]:
  64. old_path = os.path.join(extracted_ds_root, f)
  65. shutil.move(old_path, sbd_root)
  66. if self.image_set == "train_noval":
  67. # Note: this is failing as of June 2024 https://github.com/pytorch/vision/issues/8471
  68. download_url(self.voc_train_url, sbd_root, self.voc_split_filename, self.voc_split_md5)
  69. if not os.path.isdir(sbd_root):
  70. raise RuntimeError("Dataset not found or corrupted. You can use download=True to download it")
  71. split_f = os.path.join(sbd_root, image_set.rstrip("\n") + ".txt")
  72. with open(os.path.join(split_f)) as fh:
  73. file_names = [x.strip() for x in fh.readlines()]
  74. self.images = [os.path.join(image_dir, x + ".jpg") for x in file_names]
  75. self.masks = [os.path.join(mask_dir, x + ".mat") for x in file_names]
  76. self._get_target = self._get_segmentation_target if self.mode == "segmentation" else self._get_boundaries_target
  77. def _get_segmentation_target(self, filepath: str) -> Image.Image:
  78. mat = self._loadmat(filepath)
  79. return Image.fromarray(mat["GTcls"][0]["Segmentation"][0])
  80. def _get_boundaries_target(self, filepath: str) -> np.ndarray:
  81. mat = self._loadmat(filepath)
  82. return np.concatenate(
  83. [np.expand_dims(mat["GTcls"][0]["Boundaries"][0][i][0].toarray(), axis=0) for i in range(self.num_classes)],
  84. axis=0,
  85. )
  86. def __getitem__(self, index: int) -> Tuple[Any, Any]:
  87. img = Image.open(self.images[index]).convert("RGB")
  88. target = self._get_target(self.masks[index])
  89. if self.transforms is not None:
  90. img, target = self.transforms(img, target)
  91. return img, target
  92. def __len__(self) -> int:
  93. return len(self.images)
  94. def extra_repr(self) -> str:
  95. lines = ["Image set: {image_set}", "Mode: {mode}"]
  96. return "\n".join(lines).format(**self.__dict__)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...