Esempio n. 1
0
def init_mnist_cifar_transforms(n_channels=1, stage='train'):
    if n_channels == 1:
        norm_mean_std = Normalize((0.1307, ), (0.3081, ))
    elif n_channels == 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(n_channels))

    train_trf = Compose([
        wrap2solt,
        slc.Stream([
            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=(-5, 5), p=0.5),
            slt.PadTransform(pad_to=34),
            slt.CropTransform(crop_size=32, crop_mode='r')
        ]), unpack_solt,
        ApplyTransform(norm_mean_std)
    ])

    if stage == 'train':
        return train_trf

    test_trf = Compose([
        wrap2solt,
        slt.PadTransform(pad_to=32), unpack_solt,
        ApplyTransform(norm_mean_std)
    ])

    return test_trf
Esempio n. 2
0
def init_mnist_transforms():
    train_trf = Compose([
        wrap2solt,
        slc.Stream([
            slt.ResizeTransform(resize_to=(64, 64), 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=70),
            slt.CropTransform(crop_size=64, crop_mode='r'),
            slt.ImageAdditiveGaussianNoise(p=1.0)
        ]),
        unpack_solt,
        ApplyTransform(Normalize((0.5, ), (0.5, )))
    ])

    test_trf = Compose([
        wrap2solt,
        slt.ResizeTransform(resize_to=(64, 64), interpolation='bilinear'),
        # slt.PadTransform(pad_to=64),
        unpack_solt,
        ApplyTransform(Normalize((0.5, ), (0.5, ))),
    ])

    return train_trf, test_trf
Esempio n. 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
Esempio n. 4
0
def init_mnist_transforms():
    norm_mean_std = Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    train_trf = Compose([
        wrap2solt,
        slt.PadTransform(pad_to=32), unpack_solt,
        ApplyTransform(norm_mean_std, [0, 1])
    ])

    test_trf = Compose([
        wrap2solt,
        slt.PadTransform(pad_to=32), unpack_solt,
        ApplyTransform(norm_mean_std, [0, 1])
    ])

    return train_trf, test_trf
Esempio n. 5
0
def custom_augment(img):
    if len(img.shape) == 3:
        imgs = img.expand_dims(img, axis=0)
    else:
        imgs = img

    out_imgs = []
    for b in range(img.shape[0]):
        img1 = imgs[b, :, :, 0:1].astype(np.uint8)
        img2 = imgs[b, :, :, 1:2].astype(np.uint8)
        tr = Compose([
            wrap2solt,
            slc.Stream([
                slt.ImageAdditiveGaussianNoise(p=0.5, gain_range=0.3),
                slt.RandomRotate(p=1, rotation_range=(-10, 10)),
                slt.PadTransform(pad_to=int(STD_SZ[0] * 1.05)),
                slt.CropTransform(crop_size=STD_SZ[0], crop_mode='r'),
                slt.ImageGammaCorrection(p=0.5, gamma_range=(0.5, 1.5)),
            ]), unpack_solt,
            ApplyTransform(Normalize((0.5, ), (0.5, )))
        ])

        img1, _ = tr((img1, 0))
        img2, _ = tr((img2, 0))

        out_img = torch.cat((img1, img2), dim=0)
        out_imgs.append(out_img)
    out_imgs = torch.stack(out_imgs, dim=0)
    return out_imgs
Esempio n. 6
0
    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.RandomFlip(axis=1, 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)
        ])

        if len(img.shape) == 3:
            imgs = np.expand_dims(img, axis=0)
        elif len(img.shape) == 4:
            imgs = img
        else:
            raise ValueError('Expect num of dims 3 or 4, but got {}'.format(
                len(img.shape)))

        out_imgs = []
        for b in range(imgs.shape[0]):
            _img = imgs[b, :].astype(np.uint8)
            _img, _ = tr((_img, 0))
            out_imgs.append(_img)

        return torch.stack(out_imgs, dim=0)
Esempio n. 7
0
def init_mnist_transforms():
    return Compose([
        wrap2solt,
        slt.ResizeTransform(resize_to=(64, 64), interpolation='bilinear'),
        unpack_solt,
        ApplyTransform(Normalize((0.5, ), (0.5, )))
    ])
Esempio n. 8
0
def init_transform_wo_aug(nc=1):
    if nc == 1:
        norm_mean_std = Normalize((0.5, ), (0.5, ))
    elif nc == 3:
        norm_mean_std = Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    else:
        raise ValueError("Not support channels of {}".format(nc))

    trf = Compose([wrap2solt, unpack_solt, ApplyTransform(norm_mean_std)])
    return trf
Esempio n. 9
0
def init_transforms():
    train_trf = Compose([
        wrap2solt,
        slc.Stream([
            slt.ImageAdditiveGaussianNoise(p=0.5, gain_range=0.3),
            slt.RandomRotate(p=1, rotation_range=(-10, 10)),
            slt.PadTransform(pad_to=int(STD_SZ[0] * 1.05)),
            slt.CropTransform(crop_size=STD_SZ[0], crop_mode='r'),
            slt.ImageGammaCorrection(p=0.5, gamma_range=(0.5, 1.5)),
        ]),
        unpack_solt,
        ApplyTransform(Normalize((0.5,), (0.5,)))
    ])

    test_trf = Compose([
        wrap2solt,
        unpack_solt,
        ApplyTransform(Normalize((0.5,), (0.5,)))
    ])

    return {"train": train_trf, "eval": test_trf}
Esempio n. 10
0
def init_transforms(nc=1):
    if nc == 1:
        norm_mean_std = Normalize((0.5, ), (0.5, ))
    elif nc == 3:
        norm_mean_std = Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    else:
        raise ValueError("Not support channels of {}".format(nc))

    train_trf = Compose([
        wrap2solt,
        slc.Stream([
            slt.ImageAdditiveGaussianNoise(p=0.5, gain_range=0.3),
            slt.RandomRotate(p=1, rotation_range=(-10, 10)),
            slt.PadTransform(pad_to=int(STD_SZ[0] * 1.05)),
            slt.CropTransform(crop_size=STD_SZ[0], crop_mode='r'),
            slt.ImageGammaCorrection(p=0.5, gamma_range=(0.5, 1.5)),
        ]), unpack_solt,
        ApplyTransform(norm_mean_std)
    ])

    test_trf = Compose([wrap2solt, unpack_solt, ApplyTransform(norm_mean_std)])

    return train_trf, test_trf, custom_augment
Esempio n. 11
0
    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
Esempio n. 12
0
def custom_augment(img):
    img1 = img[:, :, 0:1].astype(np.uint8)
    img2 = img[:, :, 1:2].astype(np.uint8)
    tr = Compose([
        wrap2solt,
        slc.Stream([
            slt.ImageAdditiveGaussianNoise(p=0.5, gain_range=0.3),
            slt.RandomRotate(p=1, rotation_range=(-10, 10)),
            slt.PadTransform(pad_to=int(STD_SZ[0] * 1.05)),
            slt.CropTransform(crop_size=STD_SZ[0], crop_mode='r'),
            slt.ImageGammaCorrection(p=0.5, gamma_range=(0.5, 1.5)),
        ]), unpack_solt,
        ApplyTransform(Normalize((0.5, ), (0.5, )))
    ])

    img1, _ = tr((img1, 0))
    img2, _ = tr((img2, 0))

    out_img = torch.cat((img1, img2), dim=0)
    return out_img
Esempio n. 13
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
    }
Esempio n. 14
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
    }