def test_processed_uniform_probabilities(self): sampler = UniformSampler(5) probabilities = sampler.get_probability_map(self.sample) probabilities = sampler.process_probability_map(probabilities) fixtures = np.zeros_like(probabilities) # Other positions cannot be patch centers fixtures[2:-2, 2:-2, 2:-2] = probabilities[2, 2, 2] self.assertAlmostEqual(probabilities.sum(), 1) assert np.equal(probabilities, fixtures).all()
def test_incosistent_shape(self): # https://github.com/fepegar/torchio/issues/234#issuecomment-675029767 sample = torchio.Subject( im1=torchio.ScalarImage(tensor=torch.rand(1, 4, 5, 6)), im2=torchio.ScalarImage(tensor=torch.rand(2, 4, 5, 6)), ) patch_size = 2 sampler = UniformSampler(patch_size) next(sampler(sample))
def main(): # Define training and patches sampling parameters num_epochs = 20 patch_size = 128 queue_length = 100 samples_per_volume = 5 batch_size = 2 # Populate a list with images one_subject = Subject( T1=Image('../BRATS2018_crop_renamed/LGG75_T1.nii.gz', torchio.INTENSITY), T2=Image('../BRATS2018_crop_renamed/LGG75_T2.nii.gz', torchio.INTENSITY), label=Image('../BRATS2018_crop_renamed/LGG75_Label.nii.gz', torchio.LABEL), ) # This subject doesn't have a T2 MRI! another_subject = Subject( T1=Image('../BRATS2018_crop_renamed/LGG74_T1.nii.gz', torchio.INTENSITY), label=Image('../BRATS2018_crop_renamed/LGG74_Label.nii.gz', torchio.LABEL), ) subjects = [ one_subject, another_subject, ] subjects_dataset = ImagesDataset(subjects) queue_dataset = Queue( subjects_dataset, queue_length, samples_per_volume, UniformSampler(patch_size), ) # This collate_fn is needed in the case of missing modalities # In this case, the batch will be composed by a *list* of samples instead of # the typical Python dictionary that is collated by default in Pytorch batch_loader = DataLoader( queue_dataset, batch_size=batch_size, collate_fn=lambda x: x, ) # Mock PyTorch model model = nn.Identity() for epoch_index in range(num_epochs): for batch in batch_loader: # batch is a *list* here, not a dictionary logits = model(batch) print([batch[idx].keys() for idx in range(batch_size)]) print()
def run_queue(self, num_workers, **kwargs): subjects_dataset = SubjectsDataset(self.subjects_list) patch_size = 10 sampler = UniformSampler(patch_size) queue_dataset = Queue( subjects_dataset, max_length=6, samples_per_volume=2, sampler=sampler, **kwargs, ) _ = str(queue_dataset) batch_loader = DataLoader(queue_dataset, batch_size=4) for batch in batch_loader: _ = batch['one_modality'][DATA] _ = batch['segmentation'][DATA]
def test_queue(self): subjects_dataset = ImagesDataset(self.subjects_list) patch_size = 10 sampler = UniformSampler(patch_size) queue_dataset = Queue( subjects_dataset, max_length=6, samples_per_volume=2, sampler=sampler, num_workers=0, verbose=True, ) _ = str(queue_dataset) batch_loader = DataLoader(queue_dataset, batch_size=4) for batch in batch_loader: _ = batch['one_modality'][DATA] _ = batch['segmentation'][DATA]
def test_uniform_probabilities(self): sampler = UniformSampler(5) probabilities = sampler.get_probability_map(self.sample) fixtures = torch.ones_like(probabilities) assert torch.all(probabilities.eq(fixtures))
def __init__(self, images_dir, labels_dir): if hp.mode == '3d': patch_size = hp.patch_size elif hp.mode == '2d': patch_size = (hp.patch_size, hp.patch_size, 1) else: raise Exception('no such kind of mode!') queue_length = 5 samples_per_volume = 5 self.subjects = [] if (hp.in_class == 1) and (hp.out_class == 1): images_dir = Path(images_dir) self.image_paths = sorted(images_dir.glob(hp.fold_arch)) labels_dir = Path(labels_dir) self.label_paths = sorted(labels_dir.glob(hp.fold_arch)) for (image_path, label_path) in zip(self.image_paths, self.label_paths): subject = tio.Subject( source=tio.ScalarImage(image_path), label=tio.LabelMap(label_path), ) self.subjects.append(subject) else: images_dir = Path(images_dir) self.image_paths = sorted(images_dir.glob(hp.fold_arch)) artery_labels_dir = Path(labels_dir + '/artery') self.artery_label_paths = sorted( artery_labels_dir.glob(hp.fold_arch)) lung_labels_dir = Path(labels_dir + '/lung') self.lung_label_paths = sorted(lung_labels_dir.glob(hp.fold_arch)) trachea_labels_dir = Path(labels_dir + '/trachea') self.trachea_label_paths = sorted( trachea_labels_dir.glob(hp.fold_arch)) vein_labels_dir = Path(labels_dir + '/vein') self.vein_label_paths = sorted(vein_labels_dir.glob(hp.fold_arch)) for (image_path, artery_label_path, lung_label_path, trachea_label_path, vein_label_path) in zip( self.image_paths, self.artery_label_paths, self.lung_label_paths, self.trachea_label_paths, self.vein_label_paths): subject = tio.Subject( source=tio.ScalarImage(image_path), atery=tio.LabelMap(artery_label_path), lung=tio.LabelMap(lung_label_path), trachea=tio.LabelMap(trachea_label_path), vein=tio.LabelMap(vein_label_path), ) self.subjects.append(subject) self.transforms = self.transform() self.training_set = tio.SubjectsDataset(self.subjects, transform=self.transforms) self.queue_dataset = Queue( self.training_set, queue_length, samples_per_volume, UniformSampler(patch_size), )