Example #1
0
def test_random_transforms():
    x = np.random.random((2, 28, 28))
    assert affine_transformations.random_rotation(x, 45).shape == (2, 28, 28)
    assert affine_transformations.random_shift(x, 1, 1).shape == (2, 28, 28)
    assert affine_transformations.random_shear(x, 20).shape == (2, 28, 28)
    assert affine_transformations.random_channel_shift(x,
                                                       20).shape == (2, 28, 28)
Example #2
0
def transfo_imgs(
        image, args,
        mode):  # Types of transformations and range inspired by Sujit 2019
    """
    Takes as input an image and transforms it (currently rotation and translation available)
    """
    img = image
    if len(img.shape) == 3:
        if np.random.rand(1)[0] < args[0]:
            angle = 10
            img = at.random_rotation(img,
                                     angle,
                                     row_axis=0,
                                     col_axis=1,
                                     channel_axis=2)
        if np.random.rand(1)[0] < args[1]:
            axs_0 = 21
            axs_1 = 6
            img = at.random_shift(img,
                                  axs_0,
                                  axs_1,
                                  row_axis=0,
                                  col_axis=1,
                                  channel_axis=2)
    elif len(img.shape) == 4:
        img2 = np.zeros((*img.shape[:-1], 0))
        if np.random.rand(1)[0] < args[0]:
            angle = 10
            axes = tuple(np.random.choice(range(3), 2))
            for k in range(img.shape[3]):
                img2 = np.concatenate(
                    (img2, ndi.rotate(img[k], angle, axes=axes,
                                      reshape=False)),
                    axis=3)
        img = img2
        if np.random.rand(1)[0] < args[1]:
            axs_0 = np.random.randint(0, 21)
            axs_1 = np.random.randint(-5, 6)
            axs_2 = np.random.randint(-5, 5)
            img = shift(img, [axs_0, axs_1, axs_2])
    return img
Example #3
0
 def _transfo_img(
         self, image,
         args):  # Types of transformations and range inspired by Sujit 2019
     """
     Takes as input an image and transforms it (currently rotation and translation available)
     """
     img = image
     if np.random.rand(1)[0] < args[0]:
         angle = 10
         img = at.random_rotation(img,
                                  angle,
                                  row_axis=0,
                                  col_axis=1,
                                  channel_axis=2)
     if np.random.rand(1)[0] < args[1]:
         axs_0 = 21
         axs_1 = 6
         img = at.random_shift(img,
                               axs_0,
                               axs_1,
                               row_axis=0,
                               col_axis=1,
                               channel_axis=2)
     return img