[docs]classRawDataLoader(BaseDataLoader):"""A specialized raw-data-loader for the Montgomery 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)))
[docs]defsample(self,sample:typing.Any)->Sample:"""Load a single image sample from the disk. Parameters ---------- sample A tuple containing the path suffix, within the dataset root folder, where to find the image to be loaded, and an integer, representing the sample label. Returns ------- The sample representation. """image=PIL.Image.open(self.datadir/sample[0]).convert(mode="RGB")image=to_dtype(to_image(image),torch.float32,scale=True)# Combines left and right lung masks into a single tensorleft=PIL.Image.open(self.datadir/sample[1]).convert(mode="1",dither=None)right=PIL.Image.open(self.datadir/sample[2]).convert(mode="1",dither=None)target=np.ma.mask_or(np.asarray(left),np.asarray(right))target=to_dtype(to_image(target),torch.float32,scale=True)mask=torch.ones_like(target)image=tv_tensors.Image(image)target=tv_tensors.Mask(target)mask=tv_tensors.Mask(mask)returndict(image=image,target=target,mask=mask,name=sample[0])
[docs]classDataModule(CachingDataModule):"""Montgomery database for lung 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",)