[docs]classRawDataLoader(SegmentationRawDataLoader):"""A specialized raw-data-loader for the drishtigs1 dataset. Parameters ---------- target_type Indicate whether to use the "cup" or "disc" target. """datadir:pathlib.Path"""This variable contains the base directory where the database raw data is stored."""def__init__(self,target_type:str):self.datadir=pathlib.Path(load_rc().get(CONFIGURATION_KEY_DATADIR,os.path.realpath(os.curdir)))self.target_type=target_type
[docs]defsample(self,sample:tuple[str,str,str|None])->Sample:"""Load a single image sample from the disk. Parameters ---------- sample A tuple containing path suffixes to the sample image, target, and mask to be loaded, within the dataset root folder. Returns ------- The sample representation. """image=to_tensor(PIL.Image.open(self.datadir/sample[0]).convert(mode="RGB"))ifself.target_type=="disc":target=to_tensor(PIL.Image.open(self.datadir/sample[1]).convert(mode="RGB",dither=None).convert("L").point(lambdap:p<=150,mode="1"))elifself.target_type=="cup":target=to_tensor(PIL.Image.open(self.datadir/sample[1]).convert(mode="RGB",dither=None).convert("L").point(lambdap:p<=100,mode="1"))else:raiseValueError(f"Target type {self.target_type} is not an option. "f"Available options are 'cup' and 'disc'.")assertsample[2]isnotNonemask_path=(importlib.resources.files(__package__)/"masks"/DATABASE_SLUG/sample[2])withimportlib.resources.as_file(mask_path)aspath:mask=to_tensor(PIL.Image.open(path).convert(mode="1",dither=None))image=tv_tensors.Image(crop_image_to_mask(image,mask))target=tv_tensors.Mask(crop_image_to_mask(target,mask))mask=tv_tensors.Mask(crop_image_to_mask(mask,mask))returndict(image=image,target=target,mask=mask),dict(name=sample[0])# type: ignore[arg-type]
[docs]classDataModule(CachingDataModule):"""REFUGE for optic disc and cup segmentation. Parameters ---------- split_path Path or traversable (resource) with the JSON split description to load. target_type Indicate whether to use the "cup" or "disc" target. """def__init__(self,split_path:pathlib.Path|importlib.resources.abc.Traversable,target_type:str,):super().__init__(database_split=JSONDatabaseSplit(split_path),raw_data_loader=RawDataLoader(target_type),database_name=DATABASE_SLUG,split_name=split_path.name.rsplit(".",2)[0],task="segmentation",)