Exemplo n.º 1
0
def test_fusion_rotate_360_flip_rotate_360(img_5x5):
    img = img_5x5
    dc = sld.DataContainer((img, ), 'I')

    ppl = slc.Stream([
        slt.RandomRotate((45, 45), padding='z', p=1),
        slt.RandomRotate((45, 45), padding='z', p=1),
        slt.RandomRotate((45, 45), padding='z', p=1),
        slt.RandomRotate((45, 45), padding='z', p=1),
        slt.RandomRotate((45, 45), padding='z', p=1),
        slt.RandomRotate((45, 45), padding='z', p=1),
        slt.RandomRotate((45, 45), padding='z', p=1),
        slt.RandomRotate((45, 45), padding='z', p=1),
        slt.RandomFlip(p=1, axis=1),
        slc.Stream([
            slt.RandomRotate((45, 45), padding='z', p=1),
            slt.RandomRotate((45, 45), padding='z', p=1),
            slt.RandomRotate((45, 45), padding='z', p=1),
            slt.RandomRotate((45, 45), padding='z', p=1),
            slt.RandomRotate((45, 45), padding='z', p=1),
            slt.RandomRotate((45, 45), padding='z', p=1),
            slt.RandomRotate((45, 45), padding='z', p=1),
            slt.RandomRotate((45, 45), padding='z', p=1),
        ])
    ])

    img_res = ppl(dc)[0][0]

    np.testing.assert_array_almost_equal(
        cv2.flip(img, 1).reshape(5, 5, 1), img_res)
Exemplo n.º 2
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)
Exemplo n.º 3
0
def get_wrist_fracture_transformation(crop_size):
    return transforms.Compose([
        SplitDataToFunction(wrap_img_target_solt),
        slc.Stream([
            slt.RandomFlip(p=1, axis=1),
            slt.RandomProjection(affine_transforms=slc.Stream([
                slt.RandomScale(range_x=(0.8, 1.2), p=1),
                slt.RandomShear(range_x=(-0.1, 0.1), p=0.5),
                slt.RandomShear(range_y=(-0.1, 0.1), p=0.5),
                slt.RandomRotate(rotation_range=(-10, 10), p=1),
            ]),
                                 v_range=(1e-5, 5e-4),
                                 p=0.8),
            slt.PadTransform(pad_to=(256, 256), padding='z'),
            slt.CropTransform(crop_size, crop_mode='r'),
            slc.SelectiveStream([
                slc.SelectiveStream([
                    slt.ImageSaltAndPepper(p=1, gain_range=0.01),
                    slt.ImageBlur(p=0.5, blur_type='m', k_size=(11, )),
                ]),
                slt.ImageAdditiveGaussianNoise(p=1, gain_range=0.5),
            ]),
            slt.ImageGammaCorrection(p=1, gamma_range=(0.5, 1.5)),
        ]),
        DataToFunction(solt_to_img_target),
        ApplyByIndex(transforms.ToTensor(), 0)
    ])
Exemplo n.º 4
0
def test_img_mask_vertical_horizontal_flip(img_3x4, mask_3x4):
    img, mask = img_3x4, mask_3x4
    dc = sld.DataContainer((img, mask), 'IM')

    stream = slc.Stream(
        [slt.RandomFlip(p=1, axis=0),
         slt.RandomFlip(p=1, axis=1)])

    dc = stream(dc)
    img_res, _ = dc[0]
    mask_res, _ = dc[1]

    h, w = mask.shape
    assert np.array_equal(
        cv2.flip(cv2.flip(img, 0), 1).reshape(h, w, 1), img_res)
    assert np.array_equal(cv2.flip(cv2.flip(mask, 0), 1), mask_res)
Exemplo n.º 5
0
def get_landmark_transform(config):
    return transforms.Compose([
        # WrapImageLandmarksSOLT(),
        slc.Stream([
            slt.RandomFlip(p=0.5, axis=1),
            slt.RandomScale(range_x=(0.8, 1.2), p=1),
            slt.RandomRotate(rotation_range=(-180, 180), p=0.2),
            slt.RandomProjection(affine_transforms=slc.Stream([
                slt.RandomScale(range_x=(0.8, 1.3), p=1),
                slt.RandomRotate(rotation_range=(-180, 180), p=1),
                slt.RandomShear(range_x=(-0.1, 0.1), range_y=(0, 0), p=0.5),
                slt.RandomShear(range_y=(-0.1, 0.1), range_x=(0, 0), p=0.5),
            ]), v_range=(1e-5, 2e-3), p=0.8),
            slt.PadTransform(int(config.dataset.crop_size * 1.4), padding='z'),
            slt.CropTransform(config.dataset.crop_size, crop_mode='r'),
            slc.SelectiveStream([
                slt.ImageSaltAndPepper(p=1, gain_range=0.01),
                slt.ImageBlur(p=1, blur_type='g', k_size=(3, 5)),
                slt.ImageBlur(p=1, blur_type='m', k_size=(3, 5)),
                slt.ImageAdditiveGaussianNoise(p=1, gain_range=0.5),
                slc.Stream([
                    slt.ImageSaltAndPepper(p=1, gain_range=0.05),
                    slt.ImageBlur(p=0.5, blur_type='m', k_size=(3, 5)),
                ]),
                slc.Stream([
                    slt.ImageBlur(p=0.5, blur_type='m', k_size=(3, 5)),
                    slt.ImageSaltAndPepper(p=1, gain_range=0.01),
                ]),
                slc.Stream()
            ]),
            slt.ImageGammaCorrection(p=1, gamma_range=(0.5, 1.5))
        ]),
        SOLTtoHourGlassGSinput(downsample=4, sigma=3),
        ApplyTransformByIndex(transform=dwutils.npg2tens, ids=[0, 1]),
    ])
Exemplo n.º 6
0
def test_image_shape_equal_3_after_nested_flip(img_3x4):
    img = img_3x4
    dc = sld.DataContainer((img, ), 'I')

    stream = slc.Stream([
        slt.RandomFlip(p=1, axis=0),
        slt.RandomFlip(p=1, axis=1),
        slc.Stream([
            slt.RandomFlip(p=1, axis=1),
            slt.RandomFlip(p=1, axis=0),
        ])
    ])

    dc = stream(dc)
    img_res, _, _ = dc[0]

    assert np.array_equal(len(img.shape), 3)
Exemplo n.º 7
0
def get_landmark_transform_kneel(config):
    cutout = slt.ImageCutOut(
        cutout_size=(int(config.dataset.cutout *
                         config.dataset.augs.crop.crop_x),
                     int(config.dataset.cutout *
                         config.dataset.augs.crop.crop_y)),
        p=0.5)
    ppl = transforms.Compose([
        slc.Stream(),
        slc.SelectiveStream(
            [
                slc.Stream([
                    slt.RandomFlip(p=0.5, axis=1),
                    slt.RandomProjection(affine_transforms=slc.Stream([
                        slt.RandomScale(range_x=(0.9, 1.1), p=1),
                        slt.RandomRotate(rotation_range=(-90, 90), p=1),
                        slt.RandomShear(
                            range_x=(-0.1, 0.1), range_y=(-0.1, 0.1), p=0.5),
                        slt.RandomShear(
                            range_x=(-0.1, 0.1), range_y=(-0.1, 0.1), p=0.5),
                    ]),
                                         v_range=(1e-5, 2e-3),
                                         p=0.5),
                    # slt.RandomScale(range_x=(0.5, 2.5), p=0.5),
                ]),
                slc.Stream()
            ],
            probs=[0.7, 0.3]),
        slc.Stream([
            slt.PadTransform(
                (config.dataset.augs.pad.pad_x, config.dataset.augs.pad.pad_y),
                padding='z'),
            slt.CropTransform((config.dataset.augs.crop.crop_x,
                               config.dataset.augs.crop.crop_y),
                              crop_mode='r'),
        ]),
        slc.SelectiveStream([
            slt.ImageSaltAndPepper(p=1, gain_range=0.01),
            slt.ImageBlur(p=1, blur_type='g', k_size=(3, 5)),
            slt.ImageBlur(p=1, blur_type='m', k_size=(3, 5)),
            slt.ImageAdditiveGaussianNoise(p=1, gain_range=0.5),
            slc.Stream([
                slt.ImageSaltAndPepper(p=1, gain_range=0.05),
                slt.ImageBlur(p=0.5, blur_type='m', k_size=(3, 5)),
            ]),
            slc.Stream([
                slt.ImageBlur(p=0.5, blur_type='m', k_size=(3, 5)),
                slt.ImageSaltAndPepper(p=1, gain_range=0.01),
            ]),
            slc.Stream()
        ],
                            n=1),
        slt.ImageGammaCorrection(p=0.5, gamma_range=(0.5, 1.5)),
        cutout if config.dataset.use_cutout else slc.Stream(),
        DataToFunction(solt_to_img_target),
        ApplyByIndex(transforms.ToTensor(), 0)
    ])
    return ppl
Exemplo n.º 8
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
Exemplo n.º 9
0
def test_nested_stream(img_3x4, mask_3x4):
    img, mask = img_3x4, mask_3x4
    dc = sld.DataContainer((img, mask), 'IM')

    stream = slc.Stream([
        slt.RandomFlip(p=1, axis=0),
        slt.RandomFlip(p=1, axis=1),
        slc.Stream([
            slt.RandomFlip(p=1, axis=1),
            slt.RandomFlip(p=1, axis=0),
        ])
    ])

    dc = stream(dc)
    img_res, t0, _ = dc[0]
    mask_res, t1, _ = dc[1]

    assert np.array_equal(img, img_res)
    assert np.array_equal(mask, mask_res)
Exemplo n.º 10
0
def test_fusion_happens():
    ppl = slc.Stream([
        slt.RandomScale((0.5, 1.5), (0.5, 1.5), p=1),
        slt.RandomRotate((-50, 50), padding='z', p=1),
        slt.RandomShear((-0.5, 0.5), (-0.5, 0.5), padding='z', p=1),
        slt.RandomFlip(p=1, axis=1),
    ])

    st = ppl.optimize_stack(ppl.transforms)
    assert len(st) == 2
Exemplo n.º 11
0
def test_keypoints_horizontal_flip_within_stream():
    kpts_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]).reshape((4, 2))
    kpts = sld.KeyPoints(kpts_data, 2, 2)
    stream = slc.Stream([slt.RandomFlip(p=1, axis=1)])
    dc = sld.DataContainer((kpts, ), 'P')

    dc_res = stream(dc)

    assert np.array_equal(
        dc_res[0][0].data,
        np.array([[1, 0], [1, 1], [0, 0], [0, 1]]).reshape((4, 2)))
Exemplo n.º 12
0
def init_augs():
    kvs = GlobalKVS()
    args = kvs['args']
    cutout = slt.ImageCutOut(cutout_size=(int(args.cutout * args.crop_x),
                                          int(args.cutout * args.crop_y)),
                             p=0.5)
    # plus-minus 1.3 pixels
    jitter = slt.KeypointsJitter(dx_range=(-0.003, 0.003),
                                 dy_range=(-0.003, 0.003))
    ppl = tvt.Compose([
        jitter if args.use_target_jitter else slc.Stream(),
        slc.SelectiveStream([
            slc.Stream([
                slt.RandomFlip(p=0.5, axis=1),
                slt.RandomProjection(affine_transforms=slc.Stream([
                    slt.RandomScale(range_x=(0.8, 1.3), p=1),
                    slt.RandomRotate(rotation_range=(-90, 90), p=1),
                    slt.RandomShear(
                        range_x=(-0.1, 0.1), range_y=(-0.1, 0.1), p=0.5),
                ]),
                                     v_range=(1e-5, 2e-3),
                                     p=0.5),
                slt.RandomScale(range_x=(0.5, 2.5), p=0.5),
            ]),
            slc.Stream()
        ],
                            probs=[0.7, 0.3]),
        slc.Stream([
            slt.PadTransform((args.pad_x, args.pad_y), padding='z'),
            slt.CropTransform((args.crop_x, args.crop_y), crop_mode='r'),
        ]),
        slc.SelectiveStream([
            slt.ImageSaltAndPepper(p=1, gain_range=0.01),
            slt.ImageBlur(p=1, blur_type='g', k_size=(3, 5)),
            slt.ImageBlur(p=1, blur_type='m', k_size=(3, 5)),
            slt.ImageAdditiveGaussianNoise(p=1, gain_range=0.5),
            slc.Stream([
                slt.ImageSaltAndPepper(p=1, gain_range=0.05),
                slt.ImageBlur(p=0.5, blur_type='m', k_size=(3, 5)),
            ]),
            slc.Stream([
                slt.ImageBlur(p=0.5, blur_type='m', k_size=(3, 5)),
                slt.ImageSaltAndPepper(p=1, gain_range=0.01),
            ]),
            slc.Stream()
        ],
                            n=1),
        slt.ImageGammaCorrection(p=0.5, gamma_range=(0.5, 1.5)),
        cutout if args.use_cutout else slc.Stream(),
        partial(solt2torchhm, downsample=None, sigma=None),
    ])
    kvs.update('train_trf', ppl)
Exemplo n.º 13
0
def test_transform_returns_original_data_if_not_in_specified_indices(img_2x2, img_3x3, img_3x4, img_5x5):
    kpts_data = np.array([[0, 0], [0, 2], [2, 2], [2, 0]]).reshape((4, 2))
    kpts = sld.KeyPoints(kpts_data, 3, 3)
    dc = sld.DataContainer((img_2x2, img_3x3, img_3x4, img_5x5, 1, kpts, 2), 'IIIILPL')
    trf = slt.RandomFlip(p=1, data_indices=(0, 1, 4))
    res = trf(dc)

    assert np.linalg.norm(res.data[0]-img_2x2) > 0
    assert np.linalg.norm(res.data[1]-img_3x3) > 0
    np.testing.assert_array_equal(res.data[2], img_3x4)
    np.testing.assert_array_equal(res.data[3], img_5x5)
    assert res.data[-1] == 2
    np.testing.assert_array_equal(res.data[5].data, kpts_data)
Exemplo n.º 14
0
def get_landmark_transform_kneel(config):
    cutout = slt.ImageCutOut(cutout_size=(int(config.dataset.cutout * config.dataset.augs.crop.crop_x),
                                          int(config.dataset.cutout * config.dataset.augs.crop.crop_y)),
                             p=0.5)
    # plus-minus 1.3 pixels
    jitter = slt.KeypointsJitter(dx_range=(-0.003, 0.003), dy_range=(-0.003, 0.003))
    ppl = transforms.Compose([
        ColorPaddingWithSide(p=0.05, pad_size=10, side=SIDES.RANDOM, color=(50,100)),
        TriangularMask(p=0.025, arm_lengths=(100, 50), side=SIDES.RANDOM, color=(50,100)),
        TriangularMask(p=0.025, arm_lengths=(50, 100), side=SIDES.RANDOM, color=(50,100)),
        LowVisibilityTransform(p=0.05, alpha=0.15, bgcolor=(50,100)),
        SubSampleUpScale(p=0.01),
        jitter if config.dataset.augs.use_target_jitter else slc.Stream(),
        slc.SelectiveStream([
            slc.Stream([
                slt.RandomFlip(p=0.5, axis=1),
                slt.RandomProjection(affine_transforms=slc.Stream([
                    slt.RandomScale(range_x=(0.9, 1.1), p=1),
                    slt.RandomRotate(rotation_range=(-90, 90), p=1),
                    slt.RandomShear(range_x=(-0.1, 0.1), range_y=(-0.1, 0.1), p=0.5),
                ]), v_range=(1e-5, 2e-3), p=0.5),
                # slt.RandomScale(range_x=(0.5, 2.5), p=0.5),
            ]),
            slc.Stream()
        ], probs=[0.7, 0.3]),
        slc.Stream([
            slt.PadTransform((config.dataset.augs.pad.pad_x, config.dataset.augs.pad.pad_y), padding='z'),
            slt.CropTransform((config.dataset.augs.crop.crop_x, config.dataset.augs.crop.crop_y), crop_mode='r'),
        ]),
        slc.SelectiveStream([
            slt.ImageSaltAndPepper(p=1, gain_range=0.01),
            slt.ImageBlur(p=1, blur_type='g', k_size=(3, 5)),
            slt.ImageBlur(p=1, blur_type='m', k_size=(3, 5)),
            slt.ImageAdditiveGaussianNoise(p=1, gain_range=0.5),
            slc.Stream([
                slt.ImageSaltAndPepper(p=1, gain_range=0.05),
                slt.ImageBlur(p=0.5, blur_type='m', k_size=(3, 5)),
            ]),
            slc.Stream([
                slt.ImageBlur(p=0.5, blur_type='m', k_size=(3, 5)),
                slt.ImageSaltAndPepper(p=1, gain_range=0.01),
            ]),
            slc.Stream()
        ], n=1),
        slt.ImageGammaCorrection(p=0.5, gamma_range=(0.5, 1.5)),
        cutout if config.dataset.use_cutout else slc.Stream(),
        partial(solt2torchhm, downsample=None, sigma=None),
    ])
    return ppl
Exemplo n.º 15
0
def init_train_augs(crop_mode='r', pad_mode='r'):
    trf = transforms.Compose([
        img_labels2solt,
        slc.Stream(
            [
                slt.PadTransform(pad_to=(PAD_TO, PAD_TO)),
                slt.RandomFlip(p=0.5, axis=1),  # horizontal flip
                slt.CropTransform(crop_size=(CROP_SIZE, CROP_SIZE),
                                  crop_mode=crop_mode),
            ],
            padding=pad_mode),
        unpack_solt_data,
        partial(apply_by_index, transform=transforms.ToTensor(), idx=0),
    ])
    return trf
Exemplo n.º 16
0
def init_train_augmentation_pipeline():
    kvs = GlobalKVS()
    ppl = transforms.Compose([
        img_mask2solt,
        slc.Stream([
            slt.RandomFlip(axis=1, p=0.5),
            slt.ImageGammaCorrection(gamma_range=(0.5, 2), p=0.5),
            slt.PadTransform(pad_to=(kvs['args'].crop_x + 1,
                                     kvs['args'].crop_y + 1)),
            slt.CropTransform(crop_size=(kvs['args'].crop_x,
                                         kvs['args'].crop_y),
                              crop_mode='r')
        ]),
        solt2img_mask,
        partial(apply_by_index, transform=gs2tens, idx=[0, 1]),
    ])
    return ppl
Exemplo n.º 17
0
def init_binary_segmentation_augs():
    kvs = GlobalKVS()
    ppl = tvt.Compose([
        img_binary_mask2solt,
        slc.Stream([
            slt.PadTransform(pad_to=(kvs['args'].pad_x, kvs['args'].pad_y)),
            slt.RandomFlip(axis=1, p=0.5),
            slt.CropTransform(crop_size=(kvs['args'].crop_x,
                                         kvs['args'].crop_y),
                              crop_mode='r'),
            slt.ImageGammaCorrection(gamma_range=(kvs['args'].gamma_min,
                                                  kvs['args'].gamma_max),
                                     p=0.5),
        ]),
        solt2img_binary_mask,
        partial(apply_by_index, transform=numpy2tens, idx=[0, 1]),
    ])

    kvs.update('train_trf', ppl)

    return ppl
Exemplo n.º 18
0
def test_img_mask__kptsvertical_horizontal_flip_negative_axes(
        img_3x4, mask_3x4):
    img, mask = img_3x4, mask_3x4

    kpts_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]).reshape((4, 2))
    kpts = sld.KeyPoints(kpts_data.copy(), 3, 4)
    dc = sld.DataContainer((img, mask, kpts), 'IMP')

    stream = slt.RandomFlip(p=1, axis=-1)

    dc = stream(dc)
    img_res, _, _ = dc[0]
    mask_res, _, _ = dc[1]
    kpts_res, _, _ = dc[2]

    h, w = mask.shape
    assert np.array_equal(
        cv2.flip(cv2.flip(img, 0), 1).reshape(h, w, 1), img_res)
    assert np.array_equal(cv2.flip(cv2.flip(mask, 0), 1), mask_res)

    kpts_data[:, 0] = 4 - 1 - kpts_data[:, 0]
    kpts_data[:, 1] = 3 - 1 - kpts_data[:, 1]

    assert np.array_equal(kpts_data, kpts_res.data)
Exemplo n.º 19
0
            trf.state_dict['translate_y']) == expected


@pytest.mark.parametrize('param_set', [
    {
        'affine_transforms': '123'
    },
    {
        'affine_transforms': 123
    },
    {
        'affine_transforms': []
    },
    {
        'affine_transforms': slc.Stream([
            slt.RandomFlip(),
        ])
    },
    {
        'affine_transforms': slc.Stream([
            slt.RandomFlip(),
        ])
    },
    {
        'v_range': '123'
    },
    {
        'v_range': 123
    },
    {
        'v_range': [0, 0]
Exemplo n.º 20
0
def test_transform_returns_original_data_when_not_used_and_applied(img_2x2):
    trf = slt.RandomFlip(p=0)
    dc = sld.DataContainer(img_2x2, 'I')
    dc_res = trf(dc)
    assert dc_res == dc
Exemplo n.º 21
0
def test_base_transform_can_take_none_prop_and_it_becomes_0_5():
    trf = slt.RandomFlip(p=None)
    assert 0.5 == trf.p
Exemplo n.º 22
0
def test_flip_invalid_axis():
    with pytest.raises(ValueError):
        slt.RandomFlip(p=1, axis=100)
Exemplo n.º 23
0
 def __init__(self):
     self.imgaug_transform = iaa.Flipud(p=1)
     self.augmentor_op = Operations.Flip(probability=1,
                                         top_bottom_left_right="TOP_BOTTOM")
     self.solt_stream = slc.Stream([slt.RandomFlip(p=1, axis=0)])
Exemplo n.º 24
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
    }
Exemplo n.º 25
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
    }
Exemplo n.º 26
0
 def __init__(self):
     self.imgaug_transform = iaa.Fliplr(p=1)
     self.augmentor_op = Operations.Flip(probability=1,
                                         top_bottom_left_right='LEFT_RIGHT')
     self.solt_stream = slc.Stream([slt.RandomFlip(p=1, axis=1)])