[docs]classSBDataset(VisionDataset):"""`Semantic Boundaries Dataset <http://home.bharathh.info/pubs/codes/SBD/download.html>`_ The SBD currently contains annotations from 11355 images taken from the PASCAL VOC 2011 dataset. .. note :: Please note that the train and val splits included with this dataset are different from the splits in the PASCAL VOC dataset. In particular some "train" images might be part of VOC2012 val. If you are interested in testing on VOC 2012 val, then use `image_set='train_noval'`, which excludes all val images. .. warning:: This class needs `scipy <https://docs.scipy.org/doc/>`_ to load target files from `.mat` format. Args: root (str or ``pathlib.Path``): Root directory of the Semantic Boundaries Dataset image_set (string, optional): Select the image_set to use, ``train``, ``val`` or ``train_noval``. Image set ``train_noval`` excludes VOC 2012 val images. mode (string, optional): Select target type. Possible values 'boundaries' or 'segmentation'. In case of 'boundaries', the target is an array of shape `[num_classes, H, W]`, where `num_classes=20`. download (bool, optional): If true, downloads the dataset from the internet and puts it in root directory. If dataset is already downloaded, it is not downloaded again. transforms (callable, optional): A function/transform that takes input sample and its target as entry and returns a transformed version. Input sample is PIL image and target is a numpy array if `mode='boundaries'` or PIL image if `mode='segmentation'`. """url="https://www2.eecs.berkeley.edu/Research/Projects/CS/vision/grouping/semantic_contours/benchmark.tgz"md5="82b4d87ceb2ed10f6038a1cba92111cb"filename="benchmark.tgz"voc_train_url="https://www.cs.cornell.edu/~bharathh/train_noval.txt"voc_split_filename="train_noval.txt"voc_split_md5="79bff800c5f0b1ec6b21080a3c066722"def__init__(self,root:Union[str,Path],image_set:str="train",mode:str="boundaries",download:bool=False,transforms:Optional[Callable]=None,)->None:try:fromscipy.ioimportloadmatself._loadmat=loadmatexceptImportError:raiseRuntimeError("Scipy is not found. This dataset needs to have scipy installed: pip install scipy")super().__init__(root,transforms)self.image_set=verify_str_arg(image_set,"image_set",("train","val","train_noval"))self.mode=verify_str_arg(mode,"mode",("segmentation","boundaries"))self.num_classes=20sbd_root=self.rootimage_dir=os.path.join(sbd_root,"img")mask_dir=os.path.join(sbd_root,"cls")ifdownload:download_and_extract_archive(self.url,self.root,filename=self.filename,md5=self.md5)extracted_ds_root=os.path.join(self.root,"benchmark_RELEASE","dataset")forfin["cls","img","inst","train.txt","val.txt"]:old_path=os.path.join(extracted_ds_root,f)shutil.move(old_path,sbd_root)ifself.image_set=="train_noval":# Note: this is failing as of June 2024 https://github.com/pytorch/vision/issues/8471download_url(self.voc_train_url,sbd_root,self.voc_split_filename,self.voc_split_md5)ifnotos.path.isdir(sbd_root):raiseRuntimeError("Dataset not found or corrupted. You can use download=True to download it")split_f=os.path.join(sbd_root,image_set.rstrip("\n")+".txt")withopen(os.path.join(split_f))asfh:file_names=[x.strip()forxinfh.readlines()]self.images=[os.path.join(image_dir,x+".jpg")forxinfile_names]self.masks=[os.path.join(mask_dir,x+".mat")forxinfile_names]self._get_target=self._get_segmentation_targetifself.mode=="segmentation"elseself._get_boundaries_targetdef_get_segmentation_target(self,filepath:str)->Image.Image:mat=self._loadmat(filepath)returnImage.fromarray(mat["GTcls"][0]["Segmentation"][0])def_get_boundaries_target(self,filepath:str)->np.ndarray:mat=self._loadmat(filepath)returnnp.concatenate([np.expand_dims(mat["GTcls"][0]["Boundaries"][0][i][0].toarray(),axis=0)foriinrange(self.num_classes)],axis=0,)
To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.