コード例 #1
0
def full_image_dataset(default_config: SegmentationModelBase,
                       normalize_fn: Callable) -> FullImageDataset:
    df = default_config.get_dataset_splits()
    return FullImageDataset(
        args=default_config,
        full_image_sample_transforms=Compose3D([normalize_fn]),  # type: ignore
        data_frame=df.train)
コード例 #2
0
 def get_samples_at_index(self, index: int) -> List[Sample]:
     # load the channels into memory
     ds = self.dataset_sources[self.dataset_indices[index]]
     samples = [io_util.load_images_from_dataset_source(dataset_source=ds)
                ]  # type: ignore
     return [
         Compose3D.apply(self.full_image_sample_transforms, x)
         for x in samples
     ]
コード例 #3
0
    def get_full_image_sample_transforms(self) -> ModelTransformsPerExecutionMode:
        """
        Get transforms to perform on full image samples for each model execution mode.
        By default only PhotometricNormalization is performed.
        """
        from InnerEye.ML.utils.transforms import Compose3D
        from InnerEye.ML.photometric_normalization import PhotometricNormalization

        photometric_transformation = Compose3D(transforms=[PhotometricNormalization(self, use_gpu=False)])
        return ModelTransformsPerExecutionMode(train=photometric_transformation,
                                               val=photometric_transformation,
                                               test=photometric_transformation)
コード例 #4
0
 def get_samples_at_index(self, index: int) -> List[Sample]:
     # load the channels into memory
     if not self._is_nifti_dataset():
         raise ValueError(
             "Unknown file extension. Files should be Nifti or HDF5 format but found "
             + self.file_extension)
     ds = self.dataset_sources[self.dataset_indices[index]]
     samples = [io_util.load_images_from_dataset_source(dataset_source=ds)
                ]  # type: ignore
     return [
         Compose3D.apply(self.full_image_sample_transforms, x)
         for x in samples
     ]
コード例 #5
0
    def __getitem__(self, i: int) -> Dict[str, Any]:
        sample = CroppingDataset.create_possibly_padded_sample_for_cropping(
            sample=super().get_samples_at_index(index=i)[0],
            crop_size=self.args.crop_size,
            padding_mode=self.args.padding_mode)

        sample = self.create_random_cropped_sample(
            sample=sample,
            crop_size=self.args.crop_size,
            center_size=self.args.center_size,
            class_weights=self.args.class_weights)

        return Compose3D.apply(self.cropped_sample_transforms,
                               sample).get_dict()
コード例 #6
0
def test_transform_compose(use_gpu: bool = False) -> None:
    class Identity(Transform3D[torch.Tensor]):
        def __call__(self, sample: torch.Tensor) -> torch.Tensor:
            return self.get_gpu_tensor_if_possible(sample)

    class Square(Transform3D[torch.Tensor]):
        def __call__(self, sample: torch.Tensor) -> torch.Tensor:
            return self.get_gpu_tensor_if_possible(sample)**2

    a = torch.randint(low=2, high=4, size=[1])
    if use_gpu:
        a = a.cuda()

    # test that composition of multiple identity operations holds
    identity_compose = Compose3D([Identity(use_gpu=use_gpu)] * 3)
    a_t = identity_compose(a)
    assert torch.equal(Compose3D.apply(identity_compose, a), a_t)
    assert torch.equal(a_t, a)
    assert is_gpu_tensor(a_t) == use_gpu

    # test that composition of multiple square operations holds
    square_compose = Compose3D([Square(use_gpu=use_gpu)] * 3)
    assert torch.equal(square_compose(a), a**8)
    assert torch.equal(Compose3D.apply(square_compose, a), a**8)
コード例 #7
0
    def load_item(self, item: ScalarDataSource) -> ScalarItem:
        """
        Loads the images and/or segmentations as given in the ClassificationDataSource item and
        applying the optional transformation specified by the class.
        :param item: The item to load.
        :return: A ClassificationItem instances with the loaded images, and the labels and non-image features copied
        from the argument.
        """
        sample = item.load_images(
            root_path=self.args.local_dataset,
            file_mapping=self.file_to_full_path,
            load_segmentation=self.args.load_segmentation,
            center_crop_size=self.args.center_crop_size,
            image_size=self.args.image_size)

        return Compose3D.apply(self.transforms, sample)