[docs]classRawDataLoader(BaseDataLoader):"""A specialized raw-data-loader for the cxr8 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)))self.idiap_file_organisation=load_rc().get(CONFIGURATION_KEY_IDIAP_FILESTRUCTURE,False,)
[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. """file_path=pathlib.Path(sample[0])label_path=pathlib.Path(sample[1])ifself.idiap_file_organisation:sample_parts=sample[0].split("/",1)file_path=pathlib.Path(sample_parts[0]+"/"+sample_parts[1][:5]+"/"+sample_parts[1])label_parts=sample[1].split("/",1)label_path=pathlib.Path(pathlib.Path(label_parts[0]+"/"+label_parts[1][:5]+"/"+label_parts[1]))image=PIL.Image.open(self.datadir/file_path).convert(mode="RGB")image=to_dtype(to_image(image),torch.float32,scale=True)target=np.array(PIL.Image.open(self.datadir/label_path))target=np.where(target==255,0,target)target=PIL.Image.fromarray(np.array(target>0))target=to_dtype(to_image(target),torch.float32,scale=True)image=tv_tensors.Image(image)target=tv_tensors.Mask(target)mask=tv_tensors.Mask(torch.ones_like(target))returndict(image=image,target=target,mask=mask,name=sample[0])
[docs]classDataModule(CachingDataModule):"""ChestX-ray8: Hospital-scale Chest X-ray Database. 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",)