def __call__(self, img):
        self.img = img

        min_val = torch.min(torch.flatten(self.img))

        if self.img.shape[0] == 1:  # 2D images
            self.img = torch.squeeze(self.img)
            pipeline = A.Compose([
                A.Flip(p=0.5),
                A.Rotate(self.params["rot"],
                         interpolation=1,
                         border_mode=0,
                         value=min_val.item(),
                         p=0.5),
                A.RandomBrightnessContrast(
                    brightness_limit=self.params["bright"],
                    contrast_limit=self.params["contrast"],
                    p=0.5)
            ],
                                 p=0.5)

            data = {"image": np.float32(self.img)}
            out = pipeline(**data)
            out = np.expand_dims(out["image"], 0)

        elif self.img.shape[0] > 1:  # 2.5D images
            pipeline = A.Compose([
                A.Flip(p=0.5),
                A.RandomBrightnessContrast(
                    brightness_limit=self.params["bright"],
                    contrast_limit=self.params["contrast"],
                    p=0.5)
            ],
                                 p=0.75)

            data = {"image": np.float32(self.img)}
            out = pipeline(**data)
            out = out["image"]

        # Elastic deformations with library elasticdeform
        if random.uniform(0, 1) < 0.5:
            elasticdeform.deform_random_grid(
                out,
                self.params["sigma"],
                self.params["points"],
                order=self.params["resample_order"])

        return torch.from_numpy(out).float()
Esempio n. 2
0
    def __getitem__(self, index):

        # Load intensity image
        img_path = self.img_paths[index]
        img = np.load(img_path)
        seg_exists = len(self.seg_paths) > 0

        # Use elastic deformation https://pypi.org/project/elasticdeform/

        # Load segmentation image if exists
        if seg_exists:
            seg_path = self.seg_paths[index]
            seg = np.load(seg_path)

            if self.sigma != 0:

                # Form stack of image and segmentation and deform
                def_input = np.concatenate(
                    (img, seg.reshape(1, seg.shape[0], seg.shape[1])), axis=0)
                def_output = elasticdeform.deform_random_grid(def_input,
                                                              self.sigma,
                                                              self.points,
                                                              order=1,
                                                              axis=(1, 2))

                # Separate image and segmentation in deformed stack
                img = def_output[:img.shape[0], :, :]
                seg = np.around(def_output[img.shape[0], :, :])

        elif self.sigma != 0:
            img = elasticdeform.deform_random_grid(img,
                                                   self.sigma,
                                                   self.points,
                                                   order=1,
                                                   axis=(1, 2))

        # Convert images to pytorch tensors
        X = Variable(torch.from_numpy(img)).float()

        if seg_exists:
            Y = Variable(torch.from_numpy(seg)).long()

        else:
            Y = torch.zeros(1)  # dummy segmentation

        name = os.path.basename(self.img_paths[index])

        return X, Y, name
Esempio n. 3
0
    def elastic_deform(self,X,Y):
        X = X.numpy()
        Y = Y.numpy()
        brain_region = (X > 0).astype('float') * 10 # Multiplying by 10 so there would be a bigger difference between foreground and background pixels to avoid voxels
        # being assigned the wrong label after the elastic deformation

        #Split the labels and deform them separately, as if done together together they'll get mixed up. 10 times multiplication again.
        lbl1 = (Y == 1) * 10.0
        lbl2 = (Y == 2) * 10.0
        lbl3 = (Y == 3) * 10.0
        sigma_nr = random.uniform(0, 7) # Random factor by which to deform - not too high
        deform_axes = tuple(sorted(random.sample([1,2,3], k = 2))) # Random two axes in which to deform. Two to save on computational time. Need to sort the array as axis
        #variable requires sorted input

        [X, brain_region, lbl1, lbl2, lbl3] = elasticdeform.deform_random_grid([X, brain_region, lbl1, lbl2, lbl3], axis=deform_axes, sigma=sigma_nr, points=3)

        brain_region = brain_region.astype('int') > 0
        X = X * brain_region # To make sure background pixels remain 0 in the scans
        X[X<0] = 0 # Remove any negative values - background values close 0

        lbl1[lbl1 < 3] = -1
        lbl2[lbl2 < 3] = -1
        lbl3[lbl3 < 3] = -1
        Y = np.argmax([np.zeros((1, 128, 128, 128)), lbl1, lbl2, lbl3], axis=0).astype('long')
        X = torch.from_numpy(X)
        Y = torch.from_numpy(Y)

        return X,Y
Esempio n. 4
0
    def generator_tr():
        for i in range(len(train_img_names)):
            X_data = nib.load(train_img_names[i]).get_fdata()
            ## in case of high variation in the dataset, the data can be normalized
            #X_data_norm = ( X_data - np.min(X_data) ) / ( np.max(X_data) - np.min(X_data) ) 
            
            Y_data = nib.load(train_lbl_names[i]).get_fdata()

            ## apply registration based data augmentation  
            if(flags.registration_augmentation and (np.random.rand(1)[0]>0.667) ):
                j = np.random.randint(0, high=len(augment_names)) 
                fixed = nib.load(augment_names[j]).get_fdata()
                ## Need to change depending upon the input is normalized or not
                ## Here eneuronet does not take normalized input 
                ## but registration model used for data aug takes normalized input
                X_data, fixed = X_data/255, fixed/255 ## should be removed if using X_data_norm
                X_data, Y_data = data_augment.register(X_data, Y_data, fixed, flags.register_modelfile)
                X_data, fixed = X_data*255, fixed*255  ## should be removed if using X_data_norm
                
            #apply elastic deformation
            elif(flags.elastic_augmentation and (np.random.rand(1)[0]>0.5) ):
                [X_data, Y_data] = elasticdeform.deform_random_grid([X_data, Y_data], sigma=flags.std_dev, order=[3, 0], points=flags.control_points)

            #concat_data = np.stack((X_data_norm, Y_data), axis = -1)
            concat_data = np.stack((X_data, Y_data), axis = -1)
            yield concat_data
Esempio n. 5
0
    def random_elastic_deform(self, image, labels, sigma, points):
        cropy, cropx = image.shape[1:]

        image = ndimage.zoom(image, (1, self.zoom_factor, self.zoom_factor),
                             order=1)
        labels = ndimage.zoom(labels, (1, self.zoom_factor, self.zoom_factor),
                              order=1)

        y, x = image.shape[1:]
        startx = x // 2 - (cropx // 2)
        starty = y // 2 - (cropy // 2)

        [image, labels] = elasticdeform.deform_random_grid(
            X=[image, labels],
            axis=(1, 2),
            sigma=sigma,
            points=points,
            order=1,
            crop=[
                slice(starty, (starty + cropy)),
                slice(startx, (startx + cropx))
            ],
            mode="mirror")

        return image, labels
Esempio n. 6
0
    def elastic_deform(self, X, Y):
        # Elastic deformation with a random square deformation grid, where the displacements are sampled from a normal distribution with
        # standard deviation sigma. Applies elastic deformation in all 3 axes, if you wish to speed up training time change the d variable
        # to two axes chosen randomly. Uses elasticdeform package from gvtulder/elasticdeform.

        X = X.numpy()
        Y = Y.numpy()
        brain_region = (X > 0).astype('float') * 10 # Multiplying by 10 so there would be a bigger difference between foreground and background pixels to avoid voxels
        # being assigned the wrong label after the elastic deformation

        #Split the labels and deform them separately, as if done together together they'll get mixed up. 10 times multiplication again.
        lbl1 = (Y == 1) * 10.0
        lbl2 = (Y == 2) * 10.0
        lbl3 = (Y == 3) * 10.0
        sigma_nr = 5 # Random factor by which to deform
        # d = (1,2,3) # Axes in which to deform
        d = tuple(sorted(random.sample([1, 2, 3], k=2))) # Use this instead if you wish to speed up training. Performs elastic transform over 2 axes only

        [X, brain_region, lbl1, lbl2, lbl3] = elasticdeform.deform_random_grid([X, brain_region, lbl1, lbl2, lbl3], axis=[d]*5, sigma=sigma_nr, points=3)

        brain_region = brain_region.astype('int') > 0
        X = X * brain_region  # To make sure background pixels remain 0 in the scans
        X[X < 0] = 0  # Remove any negative values - background values close 0

        lbl1[lbl1 < 3] = -1
        lbl2[lbl2 < 3] = -1
        lbl3[lbl3 < 3] = -1
        Y = np.argmax([np.zeros((1, 128, 128, 128)), lbl1, lbl2, lbl3], axis=0).astype('long')
        X = torch.from_numpy(X)
        Y = torch.from_numpy(Y)

        return X, Y
Esempio n. 7
0
    def imaugment(self, X, Y=None):
        """
        Preprocess the tuple (image,mask) and then apply if selected:
            augmentation techniques adapted from Keras ImageDataGenerator
            elastic deformation
        """
        if self.params["augmentation"][0] == True:
            X, Y = random_transform(X, Y, **self.params["random_deform"])
            
        if self.params["augmentation"][1] == True:
            [X, Y] = elasticdeform.deform_random_grid([X, Y], 
                axis=[(0, 1), (0, 1)], 
                sigma=self.params["e_deform_g"]["sigma"],
                points=self.params["e_deform_g"]["points"])
                
        if self.params["augmentation"][2] == True:
            r = np.random.normal(self.params["noise"]["mean"],
                self.params["noise"]["std"], X.shape)
            X = X + r.reshape(X.shape)

        if self.params["augmentation"][3]:
            g = 2 * self.params["gamma"]["range"][1]
            while ((g < self.params["gamma"]["range"][0]) | 
                (g > self.params["gamma"]["range"][1])):
                    g = np.random.normal(self.params["gamma"]["mean"],
                        self.params["gamma"]["std"])
            if self.params["norm"] is None:
                temp = (X - np.min(X)) / (np.max(X) - np.min(X))
                temp = np.power(temp, 1 / g)
                X = temp * (np.max(X) - np.min(X)) + np.min(X)
            else:
                X = 2 * np.power(X / 2 + 0.5, 1 / g) - 1
        return X, Y
def example_main(what_number_label, tuple_shape, max_data,
                 the_other_class_num):

    randstart = np.random.randint(300)
    randend = np.random.randint(500)
    for i in range(randstart, 2000 + randend, 1):
        if DB_MNIST_checkpunctual.data_sets.train.labels[
                i] == what_number_label:
            break

    pick_a_random_input = i  # AMONGST THE data_sets.train.images (28x28) and labels STORED in THE DB

    im = DB_MNIST_checkpunctual.data_sets.train.images[pick_a_random_input]
    im = np.reshape(im, (28, 28))
    #plt.imshow(im, cmap = plt.cm.binary)
    #plt.show()
    print('LABEL corr to index', pick_a_random_input, 'of the DB is:',
          DB_MNIST_checkpunctual.data_sets.train.labels[pick_a_random_input])

    im_deformed = []
    for i in range(max_data - 1):
        im_d_pre = elasticdeform.deform_random_grid(im,
                                                    sigma=(0.25, 1, -0.5, 2,
                                                           -0.5),
                                                    points=5)
        im_deformed.append(Normalize(im_d_pre, (28, 28)))
        #plt.imshow(im_deformed[-1].reshape(28,28), cmap = plt.cm.binary)
        #plt.show()

    X = []
    X.append(DB_MNIST_checkpunctual.REDUCEPIXELSTOTHEMAX__(im))
    for e in im_deformed:
        red = DB_MNIST_checkpunctual.REDUCEPIXELSTOTHEMAX__(e)
        #norm = Normalize(red, tuple_shape)
        X.append(red)

    # FLATTEN X
    X = np.reshape(X, (max_data, tuple_shape[0] * tuple_shape[1]))

    #===========================================================================
    # CHECK THE SAMPLE BUILT
    #===========================================================================
    #for e in X:
    #    plt.imshow(e.reshape(tuple_shape), cmap = plt.cm.binary)
    #    plt.show()

    # CHECK THE ORIGINAL REDUCEPIZELTOTHEMAX
    #just_to_check = DB_MNIST_checkpunctual.REDUCEPIXELSTOTHEMAX__(im)
    #plt.imshow(just_to_check, cmap = plt.cm.binary)
    #plt.show()
    #plt.imshow(X[0].reshape(tuple_shape), cmap = plt.cm.binary)
    #plt.show()

    FLATTEN_MERG_MATR = stackflatmatrixes(max_data, X[0], X[1:],
                                          the_other_class_num,
                                          tuple_shape[0] * tuple_shape[1])
    print('shape FLATTEN_MERG_MATR', np.shape(FLATTEN_MERG_MATR))

    return FLATTEN_MERG_MATR
Esempio n. 9
0
def augmentation(batch):
    sigma = ran.uniform(0,5)
    aug = edf.deform_random_grid(batch, axis=(1, 2), sigma=sigma, points=4)
    up = [ident, flipud]
    lr = [ident, fliplr]
    aug = ran.choice(up)(aug)
    aug = ran.choice(lr)(aug)
    return aug
Esempio n. 10
0
def test2():
    X = numpy.array(Image.open(r'C:\proj\py\rw\source_images\3.jpg'))
    X[::10, ::10] = 1

    # apply deformation with a random 3 x 3 grid
    X_deformed = elasticdeform.deform_random_grid(X, sigma=25, points=3)

    Image.fromarray(X_deformed).show()
def rotate_and_deform(image, mask, sigma=25):
    axes = sample(range(2,4), random.randint(0,3))

    # Rotate image and deform
    rot_im, rot_ma = torch.flip(image, dims=axes), torch.flip(mask, dims=axes)
    rot_im, rot_ma = rot_im.numpy(), rot_ma.numpy()
    [im_def, mask_def]  = elasticdeform.deform_random_grid([rot_im, rot_ma], axis=(2,3), \
        sigma=sigma, mode='mirror', prefilter=False, order = [3, 0])

    return torch.as_tensor(im_def), torch.as_tensor(mask_def.round())
Esempio n. 12
0
 def _data_augmentation(self, X, y):
     if (self.sigma is not None) and (self.points is not None):
         X, y = elasticdeform.deform_random_grid([X, y],
                                                 sigma=self.sigma,
                                                 points=self.points)
     if self.rotate:
         rotation = np.random.choice(np.linspace(0, 360, 10))
         X, y = ndimage.rotate(X, rotation), ndimage.rotate(y, rotation)
     if self.force_zero:
         y = np.where(y <= 0.5, 0, 1)
     return X, y
Esempio n. 13
0
def elastic(X, y):
    """
    Elastic deformation on a image and its target
    """

    [Xel, yel] = elasticdeform.deform_random_grid([X, y],
                                                  sigma=5,
                                                  axis=[(1, 2, 3), (1, 2, 3)],
                                                  order=[3, 0])

    return Xel, yel
Esempio n. 14
0
def random_elastic_deform(x, y):
    # create a 5x5x5 grid of random displacement vectors
    x, y = elasticdeform.deform_random_grid(
        [x, y],
        sigma=4,
        points=(5, 5, 5),
        order=[2, 0],
        mode="reflect",
        prefilter=False
    )  # use order 0 (nearest neigbour interpolation for mask)
    return x, y
Esempio n. 15
0
def process_MSD(root, task='Heart2', num_surf=5):
    img_list, gt_list = get_MSD_list(root, task)
    save_dir = os.path.join(root, task)

    n = len(img_list)
    for i in range(n):
        print('Process img: {}'.format(img_list[i]))

        img = ants.image_read(img_list[i])
        gt = ants.image_read(gt_list[i])

        # iso-resample
        img_ = iso_resample(img, [1.5, 1.5, 1.5], islabel=False)
        gt_ = iso_resample(gt, [1.5, 1.5, 1.5], islabel=True)

        # crop
        img_np = img_.numpy()
        gt_np = gt_.numpy()
        img_np = crop(img_np)
        gt_np = crop(gt_np)

        # normal
        img_np = normalize(img_np)

        # sample surf init
        for j in tqdm(range(num_surf)):
            gt_dfm = elasticdeform.deform_random_grid(gt_np, 4, 4, 0)
            gt_dfm_smooth = mcubes.smooth_gaussian(gt_dfm, 1)
            v, e = mcubes.marching_cubes(gt_dfm_smooth, 0)
            mcubes.export_obj(
                v, e,
                os.path.join(
                    save_dir, 'surfs_unaligned',
                    '{:0>2d}_{:0>2d}surf_init.obj'.format(i + 1, j + 1)))

        # write image
        img_nii = ants.from_numpy(img_np, img_.origin, img_.spacing,
                                  img_.direction, img_.has_components,
                                  img_.is_rgb)
        gt_nii = ants.from_numpy(gt_np, gt_.origin, gt_.spacing, gt_.direction,
                                 gt_.has_components, gt_.is_rgb)
        ants.image_write(
            img_nii,
            os.path.join(save_dir, 'images', '{:0>2d}img.nii'.format(i + 1)))
        ants.image_write(
            gt_nii,
            os.path.join(save_dir, 'labels', '{:0>2d}gt.nii'.format(i + 1)))

        gt_smooth = mcubes.smooth_gaussian(gt_np, 1)
        v, e = mcubes.marching_cubes(gt_smooth, 0)
        mcubes.export_obj(
            v, e,
            os.path.join(save_dir, 'surfs_unaligned',
                         '{:0>2d}surf.obj'.format(i + 1)))
Esempio n. 16
0
def elastic(X, y):
    """
    Elastic deformation on a image and its target
    """
    [Xel, yel] = elasticdeform.deform_random_grid([X, y],
                                                  sigma=2,
                                                  axis=[(0, 1, 2), (0, 1, 2)],
                                                  order=[1, 0],
                                                  mode='constant')

    return Xel, yel
Esempio n. 17
0
 def apply_deform(img, label):
     """Apply B-spline deformation to an image and label simultaneously - the same transform applies to both."""
     # Note that the image and label must be the same size in order to use this
     assert (
         img.shape == label.shape
     ), f"Image and label must be the same shape but got image: {img.shape}, label: {label.shape}"
     img, label = elasticdeform.deform_random_grid([img, label],
                                                   sigma=img.shape[0] /
                                                   500.0,
                                                   points=10,
                                                   order=[3, 0])
     return img, label
Esempio n. 18
0
 def elastic_deform(self, img, seg):
     i = random.randint(0, 2)
     if i == 0:
         img, seg = elasticdeform.deform_random_grid([img, seg],
                                                     order=[3, 0],
                                                     sigma=7,
                                                     points=3,
                                                     axis=[(0, 1), (0, 1)])
     elif i == 1:
         img, seg = elasticdeform.deform_random_grid([img, seg],
                                                     order=[3, 0],
                                                     sigma=7,
                                                     points=3,
                                                     axis=[(1, 2), (1, 2)])
     else:
         img, seg = elasticdeform.deform_random_grid([img, seg],
                                                     order=[3, 0],
                                                     sigma=7,
                                                     points=3,
                                                     axis=[(0, 2), (0, 2)])
     return img, seg
def augment_and_crop(reps=5):
    '''
        Cropping down each n original images to 4 new (256x256) images, and also augmenting each original
        image to reps new images, resulting in an extended dataset of size n*4 + n*reps.
        The new dataset is saved in data/train and data/label and the original dataset
        is expected to exist in data/train_original and data/label_original
    '''

    if os.path.isdir('data/label'):
        shutil.rmtree('data/label')  # FIY these are gitignored
        shutil.rmtree('data/train')  # FIY these are gitignored

    os.mkdir('data/label')
    os.mkdir('data/train')

    n = 30
    img_len = 512
    border_pad = 256 // 2
    for i in range(n):
        # apply deformation with a random 3 x 3 grid and standard dev=10 pixels
        x = np.asarray(imageio.imread("data/train_original/" + str(i) +
                                      ".jpg")).astype('float64')
        y = np.asarray(imageio.imread("data/label_original/" + str(i) +
                                      ".jpg")).astype('float64')

        print(i)

        for (k, (u, v)) in enumerate(((0, 0), (0, 256), (256, 0), (256, 256))):
            #crop down each original 512x512 image to 4 cropped 256x256 images
            imageio.imsave(
                'data/train/' + str(4 * i + k) + ".jpg",
                x[u:u + 256, v:v + 256].clip(0, 255).astype('uint8'))
            imageio.imsave(
                'data/label/' + str(4 * i + k) + ".jpg",
                y[u:u + 256, v:v + 256].clip(0, 255).astype('uint8'))

        for j in range(reps):
            [x0_ij, x1_ij, x2_ij, y_ij] = elasticdeform.deform_random_grid(
                [x[:, :, 0], x[:, :, 1], x[:, :, 2], y], sigma=10, points=3)
            x_ij = np.zeros((512, 512, 3))
            x_ij[:, :, 0] = x0_ij
            x_ij[:, :, 1] = x1_ij
            x_ij[:, :, 2] = x2_ij
            x_ij = x_ij[border_pad:img_len - border_pad, border_pad:img_len -
                        border_pad]  # crop down to 256x256x3
            y_ij = y_ij[border_pad:img_len - border_pad, border_pad:img_len -
                        border_pad]  # crop down to 256x256
            x_ij = x_ij.clip(0, 255).astype('uint8')
            y_ij = y_ij.clip(0, 255).astype('uint8')
            idx = 30 * 4 + i * reps + j
            imageio.imsave('data/train/' + str(idx) + ".jpg", x_ij)
            imageio.imsave('data/label/' + str(idx) + ".jpg", y_ij)
Esempio n. 20
0
 def generator_tr():
     for i in range(len(train_img_names)):
         X_data = nib.load(train_img_names[i]).get_fdata()
         ## in case of high variation in the dataset, the data can be normalized
         #X_data_norm = ( X_data - np.min(X_data) ) / ( np.max(X_data) - np.min(X_data) ) 
         
         Y_data = nib.load(train_lbl_names[i]).get_fdata()
         
         #apply elastic deformation
         if(flags.data_augmentation and (np.random.rand(1)[0]>0.5) ):
             [X_data, Y_data] = elasticdeform.deform_random_grid([X_data, Y_data], sigma=flags.std_dev, order=[3, 0], points=flags.control_points)
         #concat_data = np.stack((X_data_norm, Y_data), axis = -1)
         concat_data = np.stack((X_data, Y_data), axis = -1)
         yield concat_data
Esempio n. 21
0
def aug_elastic_deform(img, label, wmap):
    assert len(img.shape) == 2 and len(label.shape) == 2 and len(
        wmap.shape) == 2

    # Apply deformation with a random 3 x 3 grid to inputs X and Y,
    # with a different interpolation for each input
    img_distort, label_distort, wmap_distort = elasticdeform.deform_random_grid(
        X=[img, label, wmap],
        sigma=10,
        points=3,
        order=[2, 0, 0],
        mode='mirror')

    return img_distort, label_distort, wmap_distort
Esempio n. 22
0
def DataAugmentation(images_input, aug_keys):
    '''
    DataAugmentation() applies the data augmentations specified in aug_par
    to input images. It compiles a numpy array of batch_size images, as well as
    concatenating ground truth masks and weight maps into a single numpy array.  
    '''

    output = list(images_input)
    
    # Apply augmentation operations if key word in aug_par:  
    if "horizontal_flip" in aug_keys:
        if aug_keys["horizontal_flip"]:
            if random.randint(0,1): #coin flip
                for index, item in enumerate(output):
                    output[index] = np.fliplr(item)
                    
    if "vertical_flip" in aug_keys:
        if aug_keys["vertical_flip"]:
            if random.randint(0,1): #coin flip
                for index, item in enumerate(output):
                    output[index] = np.flipud(item)
                
    
    if "rotations_360" in aug_keys:
        if aug_keys["rotations_360"]:
            rot = random.randint(0,360)
            if rot>0:
                for index,item in enumerate(output):
                    output[index] = ndimage.rotate(item,rot,order=0,reshape=False)
    
    if "elastic_deformation" in aug_keys:
        if aug_keys['elastic_deformation']:
            if random.randint(0,1): #coin flip 
                   output = elasticdeform.deform_random_grid(output,
                                                      sigma=25,
                                                      points=3,
                                                      order=0,
                                                      axis=(0,1))
                   # Correct for large black patches in deformed raw image
                   img = output[0]
                   output[0] = np.where(img==0,np.mean(img),img)
               
    if "histogram_voodoo" in aug_keys:
        if aug_keys['histogram_voodoo']:
            if random.randint(0,1): #coin flip
                # Only transform the input image, not masks
                output[0] = histogram_voodoo(output[0])

            
    return output
Esempio n. 23
0
    def __call__(self, sample):

        seed = random.randint(1, 9999999)
        np.random.seed(seed + 1)

        elastic_bool = bool(random.getrandbits(1))

        if elastic_bool:
            image, mask = sample['image'], sample['mask']

            [deformed_img, deformed_mask] = elasticdeform.deform_random_grid([image, mask], axis=[(1, 2, 3), (0, 1, 2)],
                                                                             sigma=self.sigma,
                                                                             points=self.points,
                                                                             order=self.order)
            return {'image': deformed_img, 'mask': deformed_mask}

        else:
            return {'image': sample['image'], 'mask': sample['mask']}
Esempio n. 24
0
def generate_offres(N, f=300, rotate=True, deform=True):
    '''
    Off-resonance generator

    Parameters
    ----------
    N : int
        Grid size.
    f : float or array_like
        Off-resonance frequency.
    rotate : bool
        Rotation flag
    deform : bool
        Elastic Deformation flag
    '''
    max_rot = 360
    offres = np.zeros((N, N))
    rot_angle = max_rot * random.uniform(-1,1)
    offres, _ = np.meshgrid(np.linspace(-f, f, N), np.linspace(-f, f, N))
    if rotate == True:
        offres = ndimage.rotate(offres, rot_angle, reshape=False, order=3, mode='nearest')
    if deform == True:
        offres = ed.deform_random_grid(offres, sigma=10, points=3, order=3, mode='nearest')
    return offres
Esempio n. 25
0
def elastic_deformation(image):
    sigma = random.randint(1, 10)
    image[:,:,0] = elasticdeform.deform_random_grid(image[:,:,0], sigma=sigma, points=3)
    return image
Esempio n. 26
0
def toy_voronoi_labels_affinities(
        width_, height_, radius, opening_radius, deform_sigma, deform_points,
        intensity_prob,
        noise_sigma
    ):
    # we will create bigger images such that we can crop out regions,
    # that look good
    
    offset = 2 * radius
    height = height_ + 2 * offset
    width = width_ + 2 * offset
    
    shape = (height, width)
    labels = np.zeros(shape, dtype=np.uint16)
    sketch = np.zeros_like(labels, dtype=float)
    
    # r is the distance between two center points, so we need to multiply by 2
    centers = poisson_disc_samples(width=width, height=height, r=radius * 2)
    
    # append far points to centers to complete voronoi regions
    x_max = width + 1
    y_max = height + 1
    centers = centers + [(-1, -1), (-1, y_max), (x_max, -1), (x_max, y_max)]
    
    vor = Voronoi(centers)
    
    # create selem with provided radius to apply clsoing
    selem = disk(opening_radius)
    for i, region in enumerate(vor.regions):
        if -1 in region or len(region) == 0:
            continue
        
        polygon = [vor.vertices[i] for i in region]
        mask = polygon2mask(shape, polygon)
        
        # close polygon mask with provided selem and radius
        mask = binary_opening(mask, selem)
        
        # enumerate starts at 0, but 0 is background
        labels[mask] = i + 1
        
        edt = distance_transform_edt(mask)
        edt = edt / edt.max()
        
        tmp_intensity = np.random.uniform(*intensity_prob)
        sketch[mask] = edt[mask] * tmp_intensity
        
    sketch = scipy.ndimage.gaussian_filter(sketch, radius / 4)
        
    [labels, sketch] = elasticdeform.deform_random_grid(
        [labels, sketch], sigma=deform_sigma, points=deform_points,
        # labels must be interpolated by nearest neighbor
        order=[0, 3],
        crop=(
            slice(offset, offset + height_ + 1),
            slice(offset, offset + width_ + 1)
        )
    )
    
    # labels = labels[offset:-offset + 1, offset:-offset + 1]
    # sketch = sketch[offset:-offset + 1, offset:-offset + 1]
    
    noise = sketch + np.random.normal(0, noise_sigma, size=sketch.shape)
    
    affinities = get_affinities(labels)
    
    return labels[:-1, :-1], sketch[:-1, :-1], noise[:-1, :-1], affinities
Esempio n. 27
0
def data_augmentation(images_input, aug_par, order=0):
    '''
    Data augmentation function

    Parameters
    ----------
    images_input : list of 2D numpy arrays of floats
        Images to apply augmentation operations to.
    aug_par : dict
        Augmentation operations parameters. Accepted key-value pairs:
            illumination_voodoo: bool. Whether to apply the illumination 
                voodoo operation.
            histogram_voodoo: bool. Whether to apply the histogram voodoo 
                operation.
            elastic_deformation: dict. If key exists, the elastic deformation
                operation is applied. The parameters are given as key-value 
                pairs. sigma values are given under the sigma key, deformation
                points are given under the points key. See elasticdeform doc.
            gaussian_noise: float. Apply gaussian noise to the image. The 
                sigma value of the gaussian noise is uniformly sampled between
                0 and +gaussian_noise.
            horizontal_flip: bool. Whether to flip the images horizontally. 
                Input images have a 50% chance of being flipped
            vertical_flip: bool. Whether to flip the images vertically. 
                Input images have a 50% chance of being flipped
            rotations90d: bool. Whether to randomly rotate the images in 90°
                increments. Each 90° rotation has a 25% chance of happening
            rotation: int/float. Range of random rotation to apply. The angle 
                is uniformly sampled in the range [-rotation, +rotation]
            zoom: float. Range of random "zoom" to apply. The image
                is randomly zoomed by a factor that is sampled from an 
                exponential distribution with a lamba of 3/zoom. The random
                factor is clipped at +zoom.
            shiftX: int/float. The range of random shifts to apply along X. A
                uniformly sampled shift between [-shiftX, +shiftX] is applied
            shiftY: int/float. The range of random shifts to apply along Y. A
                uniformly sampled shift between [-shiftY, +shiftY] is applied
            Note that the same operations are applied to all inputs.
    order : int or list/tuple of ints, optional
        Interpolation order to use for each image in the input stack. If order
        is a scalar, the same order is applied to all images. If a list of
        orders is provided, each image in the stack will have its own operaiton
        order. See skimage.transform.wrap doc.
        Note that the histogram voodoo operation is only applied to images with
        a non-zero order.
        The default is 0.

    Returns
    -------
    output : list of 2D numpy arrays of floats
        Augmented images array.

    '''

    #processing inputs / initializing variables::
    output = list(images_input)
    if np.isscalar(order):
        orderlist = [order] * len(images_input)
    else:
        orderlist = list(order)

    # Apply augmentation operations:

    if "illumination_voodoo" in aug_par:
        if aug_par["illumination_voodoo"]:
            for index, item in enumerate(output):
                if order[index] > 0:  # Not super elegant, but tells me if binary or grayscale image
                    output[index] = illumination_voodoo(item)

    #TODO: Illumination voodoo 2D

    if "histogram_voodoo" in aug_par:
        if aug_par["histogram_voodoo"]:
            for index, item in enumerate(output):
                if order[index] > 0:  # Not super elegant, but tells me if binary or grayscale image
                    output[index] = histogram_voodoo(item)

    if 'gaussian_noise' in aug_par:
        if aug_par['gaussian_noise']:
            sigma = np.random.rand() * aug_par['gaussian_noise']
            for index, item in enumerate(output):
                if order[index] > 0:  # Not super elegant, but tells me if binary or grayscale image
                    item = item + np.random.normal(
                        0, sigma, item.shape)  # Add Gaussian noise
                    output[index] = (item - np.min(item)) / np.ptp(
                        item)  # Rescale to 0-1

    if "elastic_deformation" in aug_par:
        output = elasticdeform.deform_random_grid(
            output,
            sigma=aug_par["elastic_deformation"]["sigma"],
            points=aug_par["elastic_deformation"]["points"],
            order=[i * 3 for i in orderlist
                   ],  # Using bicubic interpolation instead of bilinear here
            mode='nearest',
            axis=(0, 1),
            prefilter=False)

    if "horizontal_flip" in aug_par:
        if aug_par["horizontal_flip"]:
            if random.randint(0, 1):  #coin flip
                for index, item in enumerate(output):
                    output[index] = np.fliplr(item)

    if "vertical_flip" in aug_par:
        if aug_par["vertical_flip"]:
            if random.randint(0, 1):  #coin flip
                for index, item in enumerate(output):
                    output[index] = np.flipud(item)

    if "rotations_90d" in aug_par:  # Only works with square images right now!
        if aug_par["rotations_90d"]:
            rot = random.randint(0, 3) * 90
            if rot > 0:
                for index, item in enumerate(output):
                    output[index] = trans.rotate(item,
                                                 rot,
                                                 mode='edge',
                                                 order=orderlist[index])

    if "rotation" in aug_par:
        rot = random.uniform(-aug_par["rotation"], aug_par["rotation"])
        for index, item in enumerate(output):
            output[index] = trans.rotate(item,
                                         rot,
                                         mode='edge',
                                         order=orderlist[index])

    if "zoom" in aug_par:
        zoom = random.expovariate(
            3 * 1 /
            aug_par["zoom"])  # I want most of them to not be too zoomed
        zoom = aug_par["zoom"] if zoom > aug_par["zoom"] else zoom
    else:
        zoom = 0

    if "shiftX" in aug_par:
        shiftX = random.uniform(-aug_par["shiftX"], aug_par["shiftX"])
    else:
        shiftX = 0

    if "shiftY" in aug_par:
        shiftY = random.uniform(-aug_par["shiftY"], aug_par["shiftY"])
    else:
        shiftY = 0

    if any(abs(x) > 0 for x in [zoom, shiftX, shiftY]):
        for index, item in enumerate(output):
            output[index] = zoomshift(item,
                                      zoom + 1,
                                      shiftX,
                                      shiftY,
                                      order=orderlist[index])

    return output
Esempio n. 28
0
data_shifts_cuda = data_shifts.clone().cuda()
data_shifts_scatter = scatter(data_shifts_cuda)
d_shifts_scatter = torch.mean((data_shifts_scatter - data_scatt)**2)
#print('Distance: {}, shifts std: {}'.format(d_shifts, d_shifts_l2))
print('shift size - INV {}: , scatter: P{}'.format(data_shifts_L2.shape,
                                                   data_shifts_scatter.shape))
print('Distance: {}, shifts (our): {}, scatter: {}'.format(
    d_shifts, d_shifts_l2,
    d_shifts_scatter.detach().cpu()))

# add Deformation
import elasticdeform
σ = 3
data_deformation = torch.tensor(
    elasticdeform.deform_random_grid(data.numpy(), sigma=σ, axis=(2, 3)))
data_deformation_L2 = invariant_L2(model_new, data_deformation)
d_deformation = torch.mean((data_deformation - data)[:, :, 2 * σ:-2 * σ]**2)
d_deformation_L2 = torch.mean((data_deformation_L2 - data_L2)[:, :,
                                                              2 * σ:-2 * σ]**2)

data_deformation_cuda = data_deformation.clone().cuda()
data_deformation_scatter = scatter(data_deformation_cuda)
d_deformation_scatter = torch.mean((data_deformation_scatter - data_scatt)**2)
#print('Distance: {}, deformation std: {}'.format(d_deformation, d_deformation_L2))
print('deformation size - INV {}: , scatter: P{}'.format(
    data_deformation_L2.shape, data_deformation_scatter.shape))
print('Distance: {}, deformation (our): {}, scatter: {} '.format(
    d_deformation, d_deformation_L2,
    d_deformation_scatter.detach().cpu()))
Esempio n. 29
0
 def test_random(self):
     for points in (3, (3, 5)):
         for shape in ((100, 100), (100, 75)):
             for order in (0, 1, 2, 3, 4):
                 X = np.random.rand(*shape)
                 elasticdeform.deform_random_grid(X, points=points)
Esempio n. 30
0
c = segFolder(
    "C:\\Users\\Sharron\\Desktop\\pytorch\\ttt\\Unet-master\\train",
    both_transform=Compose([
        [
            torchvision.transforms.Grayscale(),
            torchvision.transforms.Grayscale()
        ],
        [
            torchvision.transforms.Resize(500),
            torchvision.transforms.Resize(500)
        ],
        [numpy.array, numpy.array],
        lambda X: deform_random_grid(
            X,
            10,
            5,
        ),
        [
            torchvision.transforms.ToPILImage(),
            torchvision.transforms.ToPILImage()
        ],
        RandomCrop(284,
                   padding=(92, 92, 92, 92),
                   padding_mode="symmetric",
                   pad_if_needed=False),
        [None, torchvision.transforms.CenterCrop(100)],
        [torchvision.transforms.ToTensor(),
         torchvision.transforms.ToTensor()],
        #[None,toint64],
        [None, torch.squeeze],