[docs]classRawDataLoader(BaseDataLoader):"""A specialized raw-data-loader for the drionsdb dataset."""datadir:pathlib.Path"""This variable contains the base directory where the database raw data is stored."""def__init__(self):self.datadir=pathlib.Path(load_rc().get(CONFIGURATION_KEY_DATADIR,os.path.realpath(os.curdir)))def_txt_to_pil_1(self,fname:pathlib.Path,size:tuple[int,int])->PIL.Image.Image:"""Convert DRIONS-DB annotations to image format. Parameters ---------- fname Path to a file containing annotations. size The size of the mask (width, height). Returns ------- The binary mask. """withfname.open("r")asf:rows=csv.reader(f,delimiter=",",quoting=csv.QUOTE_NONNUMERIC)data=list(map(tuple,rows))retval=PIL.Image.new("1",size)draw=PIL.ImageDraw.ImageDraw(retval)draw.polygon(data,fill="white")deldrawreturnretval
[docs]defsample(self,sample:typing.Any)->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=PIL.Image.open(self.datadir/sample[0]).convert(mode="RGB")target=self._txt_to_pil_1(self.datadir/sample[1],image.size)image=to_dtype(to_image(image),torch.float32,scale=True)target=to_dtype(to_image(target),torch.float32,scale=True)assertsample[2]isnotNonemask_path=(importlib.resources.files(__package__)/"masks"/DATABASE_SLUG/sample[2])withimportlib.resources.as_file(mask_path)aspath:mask=PIL.Image.open(path).convert(mode="1",dither=None)mask=to_dtype(to_image(mask),torch.float32,scale=True)image=tv_tensors.Image(crop_image_to_mask(image,mask))target=tv_tensors.Mask(crop_image_to_mask(target,mask))mask=tv_tensors.Mask(mask)returndict(image=image,target=target,mask=mask,name=sample[0])
[docs]classDataModule(CachingDataModule):"""DRIONS-DB (training set) for Optic Disc Segmentation. Parameters ---------- split_path Path or traversable (resource) with the JSON split description to load. """def__init__(self,split_path:pathlib.Path|importlib.resources.abc.Traversable):super().__init__(database_split=JSONDatabaseSplit(split_path),raw_data_loader=RawDataLoader(),database_name=DATABASE_SLUG,split_name=split_path.name.rsplit(".",2)[0],task="segmentation",)