def setUp(self) -> None: paths = extract_file_paths(self.PATH) self._dataset = MRBrainSSegmentationFactory.create( natural_sort(paths), None, modalities=Modality.T1, dataset_id=0) self._reconstructor = ImageReconstructor([256, 256, 192], [1, 32, 32, 32], [1, 8, 8, 8]) transforms = Compose( [ToNumpyArray(), PadToPatchShape([1, 32, 32, 32], [1, 8, 8, 8])]) self._full_image = transforms(self.FULL_IMAGE_PATH)
class ImageReconstructorABIDETest(unittest.TestCase): PATH = "/home/pierre-luc-delisle/ABIDE/5.1/Stanford_0051160/mri/patches/image" # TARGET_PATH = "/home/pierre-luc-delisle/ABIDE/5.1/Stanford_0051160/mri/patches/labels" FULL_IMAGE_PATH = "/home/pierre-luc-delisle/ABIDE/5.1/Stanford_0051160/mri/real_brainmask.nii.gz" def setUp(self) -> None: paths = extract_file_paths(self.PATH) self._dataset = ABIDESegmentationFactory.create(natural_sort(paths), None, modalities=Modality.T1, dataset_id=0) self._reconstructor = ImageReconstructor([224, 224, 192], [1, 32, 32, 32], [1, 8, 8, 8]) transforms = Compose( [ToNumpyArray(), PadToPatchShape([1, 32, 32, 32], [1, 8, 8, 8])]) self._full_image = transforms(self.FULL_IMAGE_PATH) def test_should_output_reconstructed_image(self): all_patches = [] all_labels = [] for current_batch, input in enumerate(self._dataset): all_patches.append(input.x) all_labels.append(input.y) img = self._reconstructor.reconstruct_from_patches_3d(all_patches) plt.imshow(img[112, :, :], cmap="gray") plt.show() np.testing.assert_array_almost_equal(img, self._full_image.squeeze(0), 6) plt.imshow(self._full_image.squeeze(0)[112, :, :], cmap="gray") plt.show()
class ImageReconstructorMRBrainSTest(unittest.TestCase): PATH = "/mnt/md0/Data/Preprocessed_4/MRBrainS/DataNii/TrainingData/1/T1" # TARGET_PATH = "/mnt/md0/Data/Preprocessed/iSEG/Patches/Aligned/label/6" FULL_IMAGE_PATH = "/mnt/md0/Data/Preprocessed/MRBrainS/DataNii/TrainingData/1/T1/T1.nii.gz" def setUp(self) -> None: paths = extract_file_paths(self.PATH) self._dataset = MRBrainSSegmentationFactory.create( natural_sort(paths), None, modalities=Modality.T1, dataset_id=0) self._reconstructor = ImageReconstructor([256, 256, 192], [1, 32, 32, 32], [1, 8, 8, 8]) transforms = Compose( [ToNumpyArray(), PadToPatchShape([1, 32, 32, 32], [1, 8, 8, 8])]) self._full_image = transforms(self.FULL_IMAGE_PATH) def test_should_output_reconstructed_image(self): all_patches = [] all_labels = [] for current_batch, input in enumerate(self._dataset): all_patches.append(input.x) all_labels.append(input.y) img = self._reconstructor.reconstruct_from_patches_3d(all_patches) plt.imshow(img[64, :, :], cmap="gray") plt.show() np.testing.assert_array_almost_equal(img, self._full_image.squeeze(0), 6)
class SlicedImageReconstructorTest(unittest.TestCase): FULL_IMAGE_PATH = "/mnt/md0/Data/Preprocessed/iSEG/Training/1/T1/T1.nii.gz" TARGET_PATH = "/mnt/md0/Data/Preprocessed/iSEG/Training/1/Labels/Labels.nii.gz" def setUp(self) -> None: transforms = Compose( [ToNumpyArray(), PadToPatchShape((1, 32, 32, 32), (1, 8, 8, 8))]) self._image = transforms(self.FULL_IMAGE_PATH) self._target = transforms(self.TARGET_PATH) patches = iSEGSliceDatasetFactory.get_patches([self._image], [self._target], (1, 32, 32, 32), (1, 16, 16, 16)) self._dataset = iSEGSliceDatasetFactory.create( [self._image], [self._target], patches, Modality.T1, 0, transforms=[ToNDTensor()]) self._reconstructor = ImageReconstructor([256, 192, 160], [1, 32, 32, 32], [1, 16, 16, 16], models=None, dataset=self._dataset, test_image=self._image) def test_should_output_reconstructed_image(self): all_patches = list( map(lambda dataset: [patch.slice for patch in dataset._patches], [self._dataset])) img = self._reconstructor.reconstruct_from_patches_3d(all_patches[0]) plt.imshow(img[150, :, :], cmap="gray") plt.show() np.testing.assert_array_almost_equal(img, self._image.squeeze(0), 6)
def setUp(self) -> None: transforms = Compose( [ToNumpyArray(), PadToPatchShape((1, 32, 32, 32), (1, 8, 8, 8))]) self._image = transforms(self.FULL_IMAGE_PATH) self._target = transforms(self.TARGET_PATH) patches = iSEGSliceDatasetFactory.get_patches([self._image], [self._target], (1, 32, 32, 32), (1, 16, 16, 16)) self._dataset = iSEGSliceDatasetFactory.create( [self._image], [self._target], patches, Modality.T1, 0, transforms=[ToNDTensor()]) self._reconstructor = ImageReconstructor([256, 192, 160], [1, 32, 32, 32], [1, 16, 16, 16], models=None, test_image=self._image)
step=dataset_configs["ABIDE"].step, test_patch_size=dataset_configs["ABIDE"].test_patch_size, test_step=dataset_configs["ABIDE"].test_step, data_augmentation_config=data_augmentation_config) train_datasets.append(ABIDE_train) valid_datasets.append(ABIDE_valid) test_datasets.append(ABIDE_test) reconstruction_datasets.append(ABIDE_reconstruction) if len(list(dataset_configs.keys())) == 2: normalized_reconstructor = ImageReconstructor( [ iSEG_reconstruction._source_images[0], MRBrainS_reconstruction._source_images[0] ], patch_size=dataset_configs["iSEG"].test_patch_size, reconstructed_image_size=(1, 256, 256, 192), step=dataset_configs["iSEG"].test_step, models=[model_trainers[GENERATOR]], normalize=True, batch_size=5) segmentation_reconstructor = ImageReconstructor( [ iSEG_reconstruction._source_images[0], MRBrainS_reconstruction._source_images[0] ], patch_size=dataset_configs["iSEG"].test_patch_size, reconstructed_image_size=(1, 256, 256, 192), step=dataset_configs["iSEG"].test_step, models=[model_trainers[GENERATOR], model_trainers[SEGMENTER]], normalize_and_segment=True,
max_subjects=dataset_configs["iSEG"].max_subjects, max_num_patches=dataset_configs["iSEG"].max_num_patches, augmentation_strategy=iSEG_augmentation_strategy, patch_size=dataset_configs["iSEG"].patch_size, step=dataset_configs["iSEG"].step, augmented_path=dataset_configs["iSEG"].path_augmented, test_patch_size=dataset_configs["iSEG"].test_patch_size, test_step=dataset_configs["iSEG"].test_step) train_datasets.append(iSEG_train) valid_datasets.append(iSEG_valid) test_datasets.append(iSEG_test) reconstruction_datasets.append(iSEG_reconstruction) normalized_reconstructors.append(ImageReconstructor(dataset_configs["iSEG"].reconstruction_size, dataset_configs['iSEG'].test_patch_size, dataset_configs["iSEG"].test_step, [model_trainers[GENERATOR]], normalize=True, test_image=iSEG_reconstruction._augmented_images[ 0] if iSEG_reconstruction._augmented_images is not None else iSEG_reconstruction._source_images[0])) segmentation_reconstructors.append( ImageReconstructor(dataset_configs["iSEG"].reconstruction_size, dataset_configs['iSEG'].test_patch_size, dataset_configs["iSEG"].test_step, [model_trainers[GENERATOR], model_trainers[SEGMENTER]], normalize_and_segment=True, test_image=iSEG_reconstruction._augmented_images[ 0] if iSEG_reconstruction._augmented_images is not None else iSEG_reconstruction._source_images[0])) input_reconstructors.append(ImageReconstructor(dataset_configs["iSEG"].reconstruction_size, dataset_configs['iSEG'].test_patch_size,
patch_size=dataset_configs["iSEG"].patch_size, step=dataset_configs["iSEG"].step, augmented_path=dataset_configs["iSEG"].path_augmented, test_patch_size=dataset_configs["iSEG"].test_patch_size, test_step=dataset_configs["iSEG"].test_step) train_datasets.append(iSEG_train) valid_datasets.append(iSEG_valid) test_datasets.append(iSEG_test) reconstruction_datasets.append(iSEG_reconstruction) segmentation_reconstructors.append( ImageReconstructor(dataset_configs["iSEG"].reconstruction_size, dataset_configs['iSEG'].test_patch_size, dataset_configs["iSEG"].test_step, [model_trainers[0]], segment=True, test_image=iSEG_augmentation_strategy( iSEG_reconstruction._source_images[0]) if iSEG_augmentation_strategy is not None else iSEG_reconstruction._source_images[0])) input_reconstructors.append( ImageReconstructor(dataset_configs["iSEG"].reconstruction_size, dataset_configs['iSEG'].test_patch_size, dataset_configs["iSEG"].test_step, test_image=iSEG_augmentation_strategy( iSEG_reconstruction._source_images[0]) if iSEG_augmentation_strategy is not None else iSEG_reconstruction._source_images[0])) gt_reconstructors.append(