Exemple #1
0
 def __init__(self):
     self.solt_stream = slc.Stream(
         [slt.Pad(pad_to=(512, 512), padding="r")])
Exemple #2
0
def test_stream_empty(img, mask):
    dc = slc.DataContainer((img, mask, 1), "IML")
    stream = slc.Stream()
    res = stream(dc, return_torch=False)
    assert dc == res
Exemple #3
0
def test_projection_empty_sampling_low_prob_trf():
    trf = slt.RandomProjection(affine_transforms=slc.Stream(
        [slt.RandomRotate(p=0)]),
                               p=1)
    trf.sample_transform()
    assert np.array_equal(trf.state_dict['transform_matrix'], np.eye(3))
Exemple #4
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.Flip(p=1, axis=0)])
Exemple #5
0
 def __init__(self):
     self.imgaug_transform = iaa.CropToFixedSize(width=64, height=64)
     self.augmentor_op = Operations.Crop(probability=1, width=64, height=64, centre=False)
     self.solt_stream = slc.Stream([slt.CropTransform(crop_size=(64, 64), crop_mode="r")])
Exemple #6
0
 def __init__(self):
     self.augmentor_op = Operations.Greyscale(probability=1)
     self.imgaug_transform = iaa.Grayscale(alpha=1.0)
     self.solt_stream = slc.Stream([slt.ImageColorTransform(mode="rgb2gs")])
Exemple #7
0
def test_projection_empty_sampling_low_prob_trf(img_6x6_rgb):
    dc = slc.DataContainer(img_6x6_rgb, "I")
    trf = slt.Projection(affine_transforms=slc.Stream([slt.Rotate(p=0)]), p=1)

    trf.sample_transform(dc)
    assert np.array_equal(trf.state_dict["transform_matrix"], np.eye(3))
Exemple #8
0
 def __init__(self):
     self.imgaug_transform = iaa.Add((127, 127), per_channel=False)
     self.augmentor_op = Operations.RandomBrightness(probability=1, min_factor=1.5, max_factor=1.5)
     self.solt_stream = slc.Stream([slt.ImageRandomBrightness(p=1, brightness_range=(127, 127))])
        pass

        dc = sld.DataContainer((data, kpts, 0), 'IPL')

        stream = slc.Stream([
            slt.RandomProjection(slc.Stream([
                slt.RandomScale(range_x=(0.8, 1.1), p=1),
                slt.RandomRotate(rotation_range=(-90, 90), p=1),
                slt.RandomShear(range_x=(-0.2, 0.2), range_y=None, p=0.7),
            ]),
                                 v_range=(1e-6, 3e-4),
                                 p=1),
            # Various cropping and padding tricks
            slt.PadTransform(1000, 'z'),
            slt.CropTransform(1000, crop_mode='c'),
            slt.CropTransform(950, crop_mode='r'),
            slt.PadTransform(1000, 'z'),
            # Intensity augmentations
            slt.ImageGammaCorrection(p=1, gamma_range=(0.5, 3)),
            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),
            ]),
        ])

        for i in range(10):
            res = stream(dc)
            img_res, kp_c, lbl_c = res.data
Exemple #10
0
@pytest.mark.parametrize(
    "param_set",
    [
        {
            "affine_transforms": "123"
        },
        {
            "affine_transforms": 123
        },
        {
            "affine_transforms": []
        },
        {
            "affine_transforms": slc.Stream([
                slt.Flip(),
            ])
        },
        {
            "affine_transforms": slc.Stream([
                slt.Flip(),
            ])
        },
        {
            "v_range": "123"
        },
        {
            "v_range": 123
        },
        {
            "v_range": ("123", "456")
Exemple #11
0
def test_reset_ignore_fast_mode_raises_error_for_streams_for_not_bool():
    ppl = slc.Stream()
    with pytest.raises(TypeError):
        ppl.reset_ignore_fast_mode('123')
Exemple #12
0
def test_stream_empty(img_2x2):
    img = img_2x2
    dc = slc.DataContainer((img, ), 'I')
    stream = slc.Stream()
    res, _, _ = stream(dc, return_torch=False)[0]
    assert np.all(res == img)
Exemple #13
0
def test_stream_with_zero_prob_transform(img, mask):
    dc = slc.DataContainer((img, mask, 1), "IML")
    stream = slc.Stream([slt.Rotate(p=0)])
    res = stream(dc, return_torch=False)
    assert dc == res
 def __init__(self):
     self.solt_stream = slc.Stream(
         [slt.PadTransform(pad_to=(512, 512), padding='r')])
Exemple #15
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
    }
Exemple #16
0
 def __init__(self):
     self.imgaug_transform = iaa.Affine(rotate=(45, 45), order=1, mode="reflect")
     self.augmentor_op = Operations.RotateStandard(probability=1, max_left_rotation=45, max_right_rotation=45)
     self.solt_stream = slc.Stream([slt.RandomRotate(p=1, rotation_range=(45, 45))], padding="r")
Exemple #17
0
def test_stream_raises_assertion_error_when_not_basetransform_or_stream_in_the_transforms(
):
    with pytest.raises(TypeError):
        slc.Stream([1, 2, 3])
Exemple #18
0
 def __init__(self):
     self.imgaug_transform = iaa.Multiply((1.5, 1.5), per_channel=False)
     self.augmentor_op = Operations.RandomContrast(probability=1, min_factor=1.5, max_factor=1.5)
     self.solt_stream = slc.Stream([slt.ImageRandomContrast(p=1, contrast_range=(1.5, 1.5))])
Exemple #19
0
def test_stream_empty(img_2x2):
    img = img_2x2
    dc = sld.DataContainer((img, ), 'I')
    stream = slc.Stream()
    res, _, _ = stream(dc)[0]
    assert np.all(res == img)
Exemple #20
0
 def __init__(self):
     self.imgaug_transform = iaa.Scale(size=512, interpolation="linear")
     self.solt_stream = slc.Stream([slt.ResizeTransform(resize_to=(512, 512))])
     self.augmentor_op = Operations.Resize(probability=1, width=512, height=512, resample_filter="BILINEAR")
Exemple #21
0
def init_transforms(mean_vector, std_vector):
    kvs = GlobalKVS()

    if mean_vector is not None:
        mean_vector = torch.from_numpy(mean_vector).float()
        std_vector = torch.from_numpy(std_vector).float()
        norm_trf = partial(normalize_channel_wise,
                           mean=mean_vector,
                           std=std_vector)
        norm_trf = partial(apply_by_index, transform=norm_trf, idx=[0, 1, 2])
    else:
        norm_trf = None

    if kvs['args'].siamese:
        resize_train = slc.Stream()
        crop_train = slt.CropTransform(crop_size=(kvs['args'].imsize,
                                                  kvs['args'].imsize),
                                       crop_mode='c')
    else:
        resize_train = slt.ResizeTransform(
            (kvs['args'].inp_size, kvs['args'].inp_size))
        crop_train = slt.CropTransform(crop_size=(kvs['args'].crop_size,
                                                  kvs['args'].crop_size),
                                       crop_mode='r')

    train_trf = [
        wrap2solt,
        slc.Stream([
            slt.PadTransform(pad_to=(kvs['args'].imsize, kvs['args'].imsize)),
            slt.CropTransform(crop_size=(kvs['args'].imsize,
                                         kvs['args'].imsize),
                              crop_mode='c'),
            resize_train,
            slt.ImageAdditiveGaussianNoise(p=0.5, gain_range=0.3),
            slt.RandomRotate(p=1, rotation_range=(-10, 10)),
            crop_train,
            slt.ImageGammaCorrection(p=0.5, gamma_range=(0.5, 1.5)),
        ]),
        unpack_solt_data,
        partial(pack_tensors, no_kl=kvs['args'].no_kl),
    ]

    if not kvs['args'].siamese:
        resize_val = slc.Stream([
            slt.ResizeTransform((kvs['args'].inp_size, kvs['args'].inp_size)),
            slt.CropTransform(crop_size=(kvs['args'].crop_size,
                                         kvs['args'].crop_size),
                              crop_mode='c'),
        ])
    else:
        resize_val = slc.Stream()

    val_trf = [
        wrap2solt,
        slc.Stream([
            slt.PadTransform(pad_to=(kvs['args'].imsize, kvs['args'].imsize)),
            slt.CropTransform(crop_size=(kvs['args'].imsize,
                                         kvs['args'].imsize),
                              crop_mode='c'),
            resize_val,
        ]),
        unpack_solt_data,
        partial(pack_tensors, no_kl=kvs['args'].no_kl),
    ]

    if norm_trf is not None:
        train_trf.append(norm_trf)
        val_trf.append(norm_trf)

    train_trf = transforms.Compose(train_trf)
    val_trf = transforms.Compose(val_trf)

    return train_trf, val_trf
Exemple #22
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]
Exemple #23
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.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)
    ])

    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.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)

    return train_trf, test_trf, custom_augment
Exemple #24
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.Flip(p=1, axis=1)])
Exemple #25
0
            os.path.join(args.snapshots, 'mean_std.npy'))
    elif args.color_space == 'yuv':
        mean_vector, std_vector = np.load(
            os.path.join(args.snapshots, 'mean_std_yuv.npy'))
    else:
        raise NotImplementedError

    norm_trf = tv_transforms.Normalize(
        torch.from_numpy(mean_vector).float(),
        torch.from_numpy(std_vector).float())

    eval_trf = tv_transforms.Compose([
        img_labels2solt,
        slc.Stream([
            slt.PadTransform(pad_to=(PAD_TO, PAD_TO)),
            slt.CropTransform(crop_size=(CROP_SIZE, CROP_SIZE),
                              crop_mode='c'),  # center crop
        ]),
        unpack_solt_data,
        partial(apply_by_index, transform=tv_transforms.ToTensor(), idx=0),
        partial(apply_by_index, transform=norm_trf, idx=0)
    ])

    dataset, dataset_length = init_dataset(args.dataset_root,
                                           args.dataset_name,
                                           batch='test')

    metadata = pd.read_csv(
        os.path.join(args.metadata_root, args.dataset_name, 'test_meta.csv'))

    eval_dataset = ImageClassificationDataset(dataset, metadata,
Exemple #26
0
 def __init__(self):
     self.imgaug_transform = iaa.AddToHueAndSaturation((20, 20),
                                                       per_channel=False)
     self.solt_stream = slc.Stream([
         slt.HSV(p=1, h_range=(20, 20), s_range=(20, 20), v_range=(20, 20))
     ])
Exemple #27
0
                             {
                                 "cutout": {
                                     "cutout_size": 2,
                                     "p": 0.5
                                 }
                             },
                         ],
                     },
                 },
             ]
         }
     },
     slc.Stream([
         slt.Pad(34),
         slt.Crop(32, "r"),
         slt.CutOut(2),
         slc.Stream([slt.Pad(34),
                     slt.Crop(32, "r"),
                     slt.CutOut(2)]),
     ]),
 ],
 [
     {
         "stream": {
             "transforms": [
                 {
                     "stream": {
                         "interpolation":
                         None,
                         "padding":
                         None,
                         "transforms": [
Exemple #28
0
 def __init__(self):
     self.solt_stream = slc.Stream(
         [slt.GammaCorrection(p=1, gamma_range=(0.5, 0.5))])
Exemple #29
0
def test_projection_empty_sampling_no_trf(img):
    dc = slc.DataContainer(img, "I")
    trf = slt.Projection(affine_transforms=slc.Stream(), p=1)
    trf.sample_transform(dc)
    assert np.array_equal(trf.state_dict["transform_matrix"], np.eye(3))