Exemple #1
0
def test_projection_translate_plus_minus_1():
    trf = slt.RandomProjection(affine_transforms=slc.Stream([
        slt.RandomTranslate(range_x=(1, 1), range_y=(1, 1), p=1),
        slt.RandomTranslate(range_x=(-1, -1), range_y=(-1, -1), p=1),
    ]),
                               p=1)

    trf.sample_transform()
    assert np.array_equal(trf.state_dict['transform_matrix'], np.eye(3))
Exemple #2
0
def test_translate_forward_backward_sampling():
    stream = slc.Stream([
        slt.RandomTranslate(range_x=(1, 1), range_y=(1, 1), p=1),
        slt.RandomTranslate(range_x=(-1, -1), range_y=(-1, -1), p=1),
    ])
    trf = stream.optimize_stack(stream.transforms)[0]
    assert 1 == trf.state_dict[
        'translate_x']  # The settings will be overrided by the first transform
    assert 1 == trf.state_dict[
        'translate_y']  # The settings will be overrided by the first transform
    assert np.array_equal(trf.state_dict['transform_matrix'], np.eye(3))
Exemple #3
0
def init_transforms(nc=1):
    if nc == 1:
        norm_mean_std = Normalize((0.1307, ), (0.3081, ))
    elif nc == 3:
        norm_mean_std = Normalize((0.4914, 0.4822, 0.4465),
                                  (0.247, 0.243, 0.261))
    else:
        raise ValueError("Not support channels of {}".format(nc))

    train_trf = Compose([
        wrap2solt,
        slc.Stream([
            # slt.ResizeTransform(resize_to=(32, 32), interpolation='bilinear'),
            slt.RandomScale(range_x=(0.95, 1.05), same=False, p=0.5),
            # slt.RandomShear(range_x=(-0.05, 0.05), p=0.5),
            slt.RandomRotate(rotation_range=(-10, 10), p=0.5),
            slt.RandomFlip(p=0.5, axis=1),
            # slt.RandomRotate(rotation_range=(-5, 5), p=0.5),
            slt.RandomTranslate(range_x=3, range_y=3),
            # slt.PadTransform(pad_to=34),
            # slt.CropTransform(crop_size=32, crop_mode='r'),
            # slt.ImageAdditiveGaussianNoise(p=1.0)
        ]),
        unpack_solt,
        ApplyTransform(norm_mean_std)
    ])

    test_trf = Compose([
        wrap2solt,
        slt.ResizeTransform(resize_to=(32, 32), interpolation='bilinear'),
        unpack_solt,
        ApplyTransform(norm_mean_std)
    ])

    def custom_augment(img):
        tr = Compose([
            wrap2solt,
            slc.Stream([
                slt.ResizeTransform(resize_to=(32, 32),
                                    interpolation='bilinear'),
                slt.RandomScale(range_x=(0.9, 1.1), same=False, p=0.5),
                slt.RandomShear(range_x=(-0.05, 0.05), p=0.5),
                slt.RandomRotate(rotation_range=(-10, 10), p=0.5),
                # slt.RandomRotate(rotation_range=(-5, 5), p=0.5),
                slt.PadTransform(pad_to=36),
                slt.CropTransform(crop_size=32, crop_mode='r'),
                slt.ImageAdditiveGaussianNoise(p=1.0)
            ]),
            unpack_solt,
            ApplyTransform(norm_mean_std)
        ])

        img_tr, _ = tr((img, 0))
        return img_tr

    return train_trf, test_trf, custom_augment
Exemple #4
0
def test_scale_when_range_x_is_none(translate_x, translate_y, expected):
    trf = slt.RandomTranslate(range_x=translate_x, range_y=translate_y, p=1)
    trf.sample_transform()
    assert (trf.state_dict['translate_x'],
            trf.state_dict['translate_y']) == expected
Exemple #5
0
def test_translate_range_from_number(translate, expected):
    trf = slt.RandomTranslate(range_x=translate, range_y=translate)
    assert trf.translate_range_x == expected
    assert trf.translate_range_y == expected
Exemple #6
0
def train_test_transforms(conf, mean=None, std=None):
    trf = conf['training']
    prob = trf['transform_probability']
    crop_size = tuple(trf['crop_size'])
    # Training transforms
    if trf['uCT']:
        train_transforms = [
            slt.RandomProjection(slc.Stream([
                slt.RandomRotate(rotation_range=tuple(trf['rotation_range']),
                                 p=prob),
                slt.RandomScale(range_x=tuple(trf['scale_range']),
                                range_y=tuple(trf['scale_range']),
                                same=False,
                                p=prob),
                slt.RandomShear(range_x=tuple(trf['shear_range']),
                                range_y=tuple(trf['shear_range']),
                                p=prob),
                slt.RandomTranslate(range_x=trf['translation_range'],
                                    range_y=trf['translation_range'],
                                    p=prob)
            ]),
                                 v_range=tuple(trf['v_range'])),
            # Spatial
            slt.RandomFlip(p=prob),
            slt.PadTransform(pad_to=crop_size),
            slt.CropTransform(crop_mode='r', crop_size=crop_size),
            # Intensity

            #slt.ImageGammaCorrection(gamma_range=tuple(trf['gamma_range']), p=prob),
            #slt.ImageRandomHSV(h_range=tuple(trf['hsv_range']),
            #                   s_range=tuple(trf['hsv_range']),
            #                   v_range=tuple(trf['hsv_range']), p=prob),
            # Brightness/contrast
            slc.SelectiveStream([
                slt.ImageRandomBrightness(brightness_range=tuple(
                    trf['brightness_range']),
                                          p=prob),
                slt.ImageRandomContrast(contrast_range=trf['contrast_range'],
                                        p=prob)
            ]),
            # Noise
            slc.SelectiveStream([
                slt.ImageSaltAndPepper(p=prob,
                                       gain_range=trf['gain_range_sp']),
                slt.ImageAdditiveGaussianNoise(
                    p=prob, gain_range=trf['gain_range_gn']),
                slc.SelectiveStream([
                    slt.ImageBlur(p=prob,
                                  blur_type='g',
                                  k_size=(3, 7, 11),
                                  gaussian_sigma=tuple(trf['sigma'])),
                    slt.ImageBlur(p=prob,
                                  blur_type='m',
                                  k_size=(3, 7, 11),
                                  gaussian_sigma=tuple(trf['sigma']))
                ])
            ])
        ]
    else:
        train_transforms = [
            # Projection
            slt.RandomProjection(
                slc.Stream([
                    slt.RandomRotate(rotation_range=tuple(
                        trf['rotation_range']),
                                     p=prob),
                    slt.RandomScale(range_x=tuple(trf['scale_range']),
                                    range_y=tuple(trf['scale_range']),
                                    same=False,
                                    p=prob),
                    #slt.RandomShear(range_x=tuple(trf['shear_range']),
                    #                range_y=tuple(trf['shear_range']), p=prob),
                    #slt.RandomTranslate(range_x=trf['translation_range'], range_y=trf['translation_range'], p=prob)
                ]),
                v_range=tuple(trf['v_range'])),
            # Spatial
            slt.RandomFlip(p=prob),
            slt.PadTransform(pad_to=crop_size[1]),
            slt.CropTransform(crop_mode='r', crop_size=crop_size),
            # Intensity
            # Add an empty stream
            #slc.SelectiveStream([]),
            slc.SelectiveStream([
                slt.ImageGammaCorrection(gamma_range=tuple(trf['gamma_range']),
                                         p=prob),
                slt.ImageRandomHSV(h_range=tuple(trf['hsv_range']),
                                   s_range=tuple(trf['hsv_range']),
                                   v_range=tuple(trf['hsv_range']),
                                   p=prob)
            ]),
            slc.SelectiveStream([
                slt.ImageRandomBrightness(brightness_range=tuple(
                    trf['brightness_range']),
                                          p=prob),
                slt.ImageRandomContrast(contrast_range=trf['contrast_range'],
                                        p=prob)
            ]),
            slc.SelectiveStream([
                slt.ImageSaltAndPepper(p=prob,
                                       gain_range=trf['gain_range_sp']),
                slt.ImageAdditiveGaussianNoise(
                    p=prob, gain_range=trf['gain_range_gn']),
                slc.SelectiveStream([
                    slt.ImageBlur(p=prob,
                                  blur_type='g',
                                  k_size=(3, 7, 11),
                                  gaussian_sigma=tuple(trf['sigma'])),
                    slt.ImageBlur(p=prob,
                                  blur_type='m',
                                  k_size=(3, 7, 11),
                                  gaussian_sigma=tuple(trf['sigma']))
                ])
            ])
        ]

    train_trf = [
        wrap_solt,
        #slc.Stream(train_transforms),
        slc.Stream([
            slt.PadTransform(pad_to=crop_size[1]),
            slt.CropTransform(crop_mode='r', crop_size=crop_size)
        ]),
        unwrap_solt,
        ApplyTransform(numpy2tens, (0, 1, 2))
    ]
    # Validation transforms
    val_trf = [
        wrap_solt,
        slc.Stream([
            slt.PadTransform(pad_to=crop_size[1]),
            slt.CropTransform(crop_mode='r', crop_size=crop_size)
        ]), unwrap_solt,
        ApplyTransform(numpy2tens, idx=(0, 1, 2))
    ]
    # Test transforms
    test_trf = [unwrap_solt, ApplyTransform(numpy2tens, idx=(0, 1, 2))]

    # Use normalize_channel_wise if mean and std not calculated
    if mean is not None and std is not None:
        train_trf.append(
            ApplyTransform(partial(normalize_channel_wise, mean=mean,
                                   std=std)))

    if mean is not None and std is not None:
        val_trf.append(
            ApplyTransform(partial(normalize_channel_wise, mean=mean,
                                   std=std)))

    # Compose transforms
    train_trf_cmp = Compose(train_trf)
    val_trf_cmp = Compose(val_trf)
    test_trf_cmp = Compose(test_trf)

    return {
        'train': train_trf_cmp,
        'val': val_trf_cmp,
        'test': test_trf_cmp,
        'train_list': train_trf,
        'val_list': val_trf,
        'test_list': test_trf
    }
Exemple #7
0
def train_test_transforms(conf, mean=None, std=None, crop_size=(512, 1024)):
    """
    Compiles the different image augmentations that are used for input images.

    :param conf: Transformation parameters
    :param mean: Dataset image mean
    :param std: Dataset image std
    :param crop_size: Image size for the segmentation model
    :return: Compiled transformation objects, and lists of the used transforms
    """
    trf = conf['training']
    prob = trf['transform_probability']
    # Training transforms

    # 3D transforms
    if trf['experiment'] == '3D':
        train_transforms = [
            slc.SelectiveStream([
                slc.Stream([
                    slt.RandomProjection(
                        slc.Stream([
                            slt.RandomRotate(rotation_range=tuple(
                                trf['rotation_range']),
                                             p=prob),
                            slt.RandomScale(range_x=tuple(trf['scale_range']),
                                            range_y=tuple(trf['scale_range']),
                                            same=False,
                                            p=prob),
                            #slt.RandomShear(range_x=tuple(trf['shear_range']),
                            #                range_y=tuple(trf['shear_range']), p=prob),
                            slt.RandomTranslate(
                                range_x=trf['translation_range'],
                                range_y=trf['translation_range'],
                                p=prob)
                        ]),
                        v_range=None  #tuple(trf['v_range'])
                    ),
                    # Spatial
                    slt.RandomFlip(p=prob),
                    slt.PadTransform(pad_to=crop_size),
                    slt.CropTransform(crop_mode='r', crop_size=crop_size),

                    # Intensity
                    # Brightness/contrast
                    slc.SelectiveStream([
                        slt.ImageRandomBrightness(brightness_range=tuple(
                            trf['brightness_range']),
                                                  p=prob),
                        slt.ImageRandomContrast(
                            contrast_range=trf['contrast_range'], p=prob)
                    ]),
                    # Noise
                    slc.SelectiveStream([
                        slt.ImageSaltAndPepper(
                            p=prob, gain_range=trf['gain_range_sp']),
                        slt.ImageAdditiveGaussianNoise(
                            p=prob, gain_range=trf['gain_range_gn']),
                        slc.SelectiveStream([
                            slt.ImageBlur(p=prob,
                                          blur_type='g',
                                          k_size=(3, 7, 11),
                                          gaussian_sigma=tuple(trf['sigma'])),
                            slt.ImageBlur(p=prob,
                                          blur_type='m',
                                          k_size=(3, 7, 11),
                                          gaussian_sigma=tuple(trf['sigma']))
                        ])
                    ])
                ]),

                # Empty stream
                slc.Stream([
                    slt.PadTransform(pad_to=crop_size),
                    slt.CropTransform(crop_mode='r', crop_size=crop_size)
                ])
            ])
        ]

    # 2D transforms
    else:
        train_transforms = [
            slc.SelectiveStream([
                slc.Stream([
                    # Projection
                    slt.RandomProjection(
                        slc.Stream([
                            slt.RandomRotate(rotation_range=tuple(
                                trf['rotation_range']),
                                             p=prob),
                            slt.RandomScale(range_x=tuple(trf['scale_range']),
                                            range_y=tuple(trf['scale_range']),
                                            same=False,
                                            p=prob),
                            # slt.RandomShear(range_x=tuple(trf['shear_range']),
                            #                range_y=tuple(trf['shear_range']), p=prob),
                            slt.RandomTranslate(
                                range_x=trf['translation_range'],
                                range_y=trf['translation_range'],
                                p=prob)
                        ]),
                        v_range=tuple(trf['v_range'])),
                    # Spatial
                    slt.RandomFlip(p=prob),
                    slt.PadTransform(pad_to=crop_size),
                    slt.CropTransform(crop_mode='r', crop_size=crop_size),
                    # Intensity
                    slc.SelectiveStream([
                        slt.ImageGammaCorrection(gamma_range=tuple(
                            trf['gamma_range']),
                                                 p=prob),
                        slt.ImageRandomHSV(h_range=tuple(trf['hsv_range']),
                                           s_range=tuple(trf['hsv_range']),
                                           v_range=tuple(trf['hsv_range']),
                                           p=prob)
                    ]),
                    slc.SelectiveStream([
                        slt.ImageRandomBrightness(brightness_range=tuple(
                            trf['brightness_range']),
                                                  p=prob),
                        slt.ImageRandomContrast(
                            contrast_range=trf['contrast_range'], p=prob)
                    ]),
                    slc.SelectiveStream([
                        slt.ImageSaltAndPepper(
                            p=prob, gain_range=trf['gain_range_sp']),
                        slt.ImageAdditiveGaussianNoise(
                            p=prob, gain_range=trf['gain_range_gn']),
                        slc.SelectiveStream([
                            slt.ImageBlur(p=prob,
                                          blur_type='g',
                                          k_size=(3, 7, 11),
                                          gaussian_sigma=tuple(trf['sigma'])),
                            slt.ImageBlur(p=prob,
                                          blur_type='m',
                                          k_size=(3, 7, 11),
                                          gaussian_sigma=tuple(trf['sigma']))
                        ])
                    ])
                ]),

                # Empty stream
                slc.Stream([
                    slt.PadTransform(pad_to=crop_size),
                    slt.CropTransform(crop_mode='r', crop_size=crop_size)
                ])
            ])
        ]

    # Compile training transforms
    train_trf = [
        # Move to SOLT format
        wrap_solt,
        # Transforms
        slc.Stream(train_transforms),
        # Extract image
        unwrap_solt,
        # Move to tensor
        ApplyTransform(numpy2tens, (0, 1, 2))
    ]

    # Validation transforms
    val_trf = [
        wrap_solt,
        slc.Stream([
            slt.PadTransform(pad_to=crop_size[1]),
            slt.CropTransform(crop_mode='r', crop_size=crop_size)
        ]), unwrap_solt,
        ApplyTransform(numpy2tens, idx=(0, 1, 2))
    ]

    # Test transforms
    test_trf = [unwrap_solt, ApplyTransform(numpy2tens, idx=(0, 1, 2))]

    # Normalize train and val images if mean and std are given
    if mean is not None and std is not None:
        train_trf.append(
            ApplyTransform(partial(normalize_channel_wise, mean=mean,
                                   std=std)))

    if mean is not None and std is not None:
        val_trf.append(
            ApplyTransform(partial(normalize_channel_wise, mean=mean,
                                   std=std)))

    # Compose transforms
    train_trf_cmp = Compose(train_trf)
    val_trf_cmp = Compose(val_trf)
    test_trf_cmp = Compose(test_trf)

    return {
        'train': train_trf_cmp,
        'val': val_trf_cmp,
        'test': test_trf_cmp,
        'train_list': train_trf,
        'val_list': val_trf,
        'test_list': test_trf
    }