def __call__(self, image, label): image = image.reshape(256, 256) label = label.reshape(256, 256) ia = False random_grid = np.random.rand(2, 7, 7) random_grid -= 0.5 random_grid /= 12 # Define a B-spline transformation object bspline_trf = gryds.BSplineTransformation(random_grid) # rotate between -pi/8 and pi/8 rot = np.random.rand() * np.pi / 4 - np.pi / 8 # scale between 0.9 and 1.1 scale_x = np.random.rand() * 0.2 + 0.9 scale_y = np.random.rand() * 0.2 + 0.9 # translate between -10% and 10% trans_x = np.random.rand() * .2 - .1 trans_y = np.random.rand() * .2 - .1 affine_trf = gryds.AffineTransformation( ndim=2, angles=[rot], # the rotation angle scaling=[scale_x, scale_y], # the anisotropic scaling translation=[trans_x, trans_y], # translation center=[0.5, 0.5] # center of rotation ) composed_trf = gryds.ComposedTransformation(bspline_trf, affine_trf) t_ind = np.random.randint(2) interpolator = gryds.Interpolator(image[:, :], mode='reflect') interpolator_label = gryds.Interpolator(label[:, :], order=0, mode='constant') patch = interpolator.transform(composed_trf) patch_label = interpolator_label.transform(composed_trf) if ia: intensity_shift = np.random.rand() * .1 - .05 contrast_shift = np.random.rand() * 0.05 + 0.975 patch += intensity_shift patch = np.sign(patch) * np.power(np.abs(patch), contrast_shift) blur = np.random.uniform() patch = gaussian_filter(patch, sigma=blur) p5, p95 = np.percentile(patch, (5, 95)) patch = (patch - p5) / (p95 - p5) patch = equalize_adapthist(np.clip(patch, 1e-5, 1), kernel_size=24)[..., np.newaxis] patch += np.random.normal(scale=0.025, size=patch.shape) return patch, patch_label
def data_augmentation(images, labels, how_many): augmented_images = np.zeros( (images.shape[0], images.shape[1], images.shape[2] * how_many)) augmented_labels = np.zeros( (labels.shape[0], labels.shape[1], labels.shape[2] * how_many)) for i in range(images.shape[2]): for j in range(how_many): img_sa = images[:, :, i] # normalise data p5 = np.percentile(img_sa, 5) p95 = np.percentile(img_sa, 95) img_sa = (img_sa - p5) / (p95 - p5) # affine transformation affine_transformation = gryds.AffineTransformation( ndim=2, angles=[np.random.uniform(-np.pi / 8., np.pi / 8.)], # the rotation angle scaling=[ np.random.uniform(0.8, 1.2), np.random.uniform(0.8, 1.2) ], # the anisotropic scaling # shear_matrix=[[1, 0.5], [0, 1]], # shearing matrix translation=[ np.random.uniform(-0.2, 0.2), np.random.uniform(-0.2, 0.2) ], # translation center=[0.5, 0.5] # center of rotation ) # Define a random 3x3 B-spline grid for a 2D image: random_grid = np.random.rand(2, 3, 3) random_grid -= 0.5 random_grid /= 5 # Define a B-spline transformation object bspline = gryds.BSplineTransformation(random_grid) # Define an interpolator object for the image: interpolator_sa = gryds.Interpolator(img_sa) interpolator_gt = gryds.Interpolator(labels[:, :, i], order=0) # img_gt composed_trf = gryds.ComposedTransformation( bspline, affine_transformation) augmented_images[:, :, i * how_many + j] = np.clip( interpolator_sa.transform(composed_trf), 0, 1) augmented_labels[:, :, i * how_many + j] = interpolator_gt.transform(composed_trf) augmented_images = augmented_images[np.newaxis, ...] augmented_images = np.transpose(augmented_images, (3, 0, 1, 2)) augmented_labels = np.transpose(augmented_labels, (2, 0, 1)) return augmented_images, augmented_labels
def test_rotation(self): image = np.array([[0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0]], dtype=DTYPE) intp = gryds.Interpolator(image, mode='mirror') trf1 = gryds.AffineTransformation(ndim=2, angles=[0.1]) trf2 = gryds.AffineTransformation(ndim=2, angles=[-0.1]) trf = gryds.ComposedTransformation(trf2, trf1) new_image = intp.transform(trf) np.testing.assert_almost_equal(image, new_image, decimal=6)
def test_translation(self): image = np.array([[0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0]], dtype=DTYPE) intp = gryds.Interpolator(image, mode='mirror') trf1 = gryds.TranslationTransformation([0.1, 0]) trf2 = gryds.TranslationTransformation([-0.1, 0]) trf = gryds.ComposedTransformation(trf2, trf1) new_image = intp.transform(trf) np.testing.assert_almost_equal(image, new_image)
def do_domain_augmentation2(image, sz): random_grid = np.random.rand(2, 7, 7) random_grid -= 0.5 random_grid /= 10 # Define a B-spline transformation object bspline_trf = gryds.BSplineTransformation(random_grid) # rotate between -pi/8 and pi/8 rot = np.random.rand() * np.pi / 4 - np.pi / 8 # scale between 0.9 and 1.1 scale_x = np.random.rand() * 0.2 + 0.9 scale_y = np.random.rand() * 0.2 + 0.9 # translate between -10% and 10% trans_x = np.random.rand() * .2 - .1 trans_y = np.random.rand() * .2 - .1 affine_trf = gryds.AffineTransformation( ndim=2, angles=[rot], # the rotation angle scaling=[scale_x, scale_y], # the anisotropic scaling translation=[trans_x, trans_y], # translation center=[0.5, 0.5] # center of rotation ) composed_trf = gryds.ComposedTransformation(bspline_trf, affine_trf) z_ind = np.random.randint(image.shape[2]) t_ind = np.random.randint(2) interpolator = gryds.Interpolator(image[..., z_ind, t_ind], mode='reflect') patch = interpolator.transform(composed_trf) patch += np.random.normal(scale=0.025, size=patch.shape) blur = np.random.uniform() patch = gaussian_filter(patch, sigma=blur) midx = image.shape[0] // 2 midy = image.shape[1] // 2 patch = patch[midx - (sz[0] // 2):midx + (sz[0] // 2), midy - (sz[1] // 2):midy + (sz[1] // 2)] p5, p95 = np.percentile(patch, (5, 95)) patch = (patch - p5) / (p95 - p5) patch = equalize_adapthist(np.clip(patch, 0, 1))[..., np.newaxis] return patch
def do_augmentation2(image, label, sz, ia=False): random_grid = np.random.rand(2, 7, 7) random_grid -= 0.5 random_grid /= 12 # Define a B-spline transformation object bspline_trf = gryds.BSplineTransformation(random_grid) # rotate between -pi/8 and pi/8 rot = np.random.rand() * np.pi / 4 - np.pi / 8 # scale between 0.9 and 1.1 scale_x = np.random.rand() * 0.2 + 0.9 scale_y = np.random.rand() * 0.2 + 0.9 # translate between -10% and 10% trans_x = np.random.rand() * .2 - .1 trans_y = np.random.rand() * .2 - .1 affine_trf = gryds.AffineTransformation( ndim=2, angles=[rot], # the rotation angle scaling=[scale_x, scale_y], # the anisotropic scaling translation=[trans_x, trans_y], # translation center=[0.5, 0.5] # center of rotation ) composed_trf = gryds.ComposedTransformation(bspline_trf, affine_trf) z_ind = np.random.randint(image.shape[2]) t_ind = np.random.randint(2) interpolator = gryds.Interpolator(image[..., z_ind, t_ind], mode='reflect') interpolator_label = gryds.Interpolator(label[..., z_ind, t_ind], order=0, mode='constant') patch = interpolator.transform(composed_trf) patch_label = interpolator_label.transform(composed_trf) if ia: intensity_shift = np.random.rand() * .1 - .05 contrast_shift = np.random.rand() * 0.05 + 0.975 patch += intensity_shift patch = np.sign(patch) * np.power(np.abs(patch), contrast_shift) blur = np.random.uniform() patch = gaussian_filter(patch, sigma=blur) # midx = image.shape[0] // 2 # midy = image.shape[1] // 2 if patch.shape[0] > sz[0] and patch.shape[1] > sz[1]: all_startx = [ 0, patch.shape[0] // 2 - sz[0] // 2, patch.shape[0] - sz[0] ] all_starty = [ 0, patch.shape[1] // 2 - sz[1] // 2, patch.shape[1] - sz[1] ] xrint = np.random.randint(3) yrint = np.random.randint(3) midx = all_startx[xrint] midy = all_starty[yrint] patch = patch[midx:midx + sz[0], midy:midy + sz[1]] patch_label = patch_label[midx:midx + sz[0], midy:midy + sz[1]] else: patch = patch[:sz[0], :sz[1]] patch_label = patch_label[:sz[0], :sz[1]] new_patch = np.zeros((sz[0], sz[1])) new_patch_label = np.zeros((sz[0], sz[1])) new_patch[:patch.shape[0], :patch.shape[1]] = patch new_patch_label[:patch_label.shape[0], :patch_label. shape[1]] = patch_label patch, patch_label = new_patch, new_patch_label # patch = patch[midx-(sz[0]//2):midx+(sz[0]//2),midy-(sz[1]//2):midy+(sz[1]//2)] p5, p95 = np.percentile(patch, (5, 95)) patch = (patch - p5) / (p95 - p5) patch = equalize_adapthist(np.clip(patch, 1e-5, 1), kernel_size=24)[..., np.newaxis] patch += np.random.normal(scale=0.025, size=patch.shape) # patch = np.clip(patch, 0, 1)[...,np.newaxis] # patch_label = patch_label[midx-(sz[0]//2):midx+(sz[0]//2),midy-(sz[1]//2):midy+(sz[1]//2)] return (patch, patch_label)
def test_repr(self): bsp = gryds.BSplineTransformation(np.random.rand(2, 3, 4)) self.assertEqual( str(gryds.ComposedTransformation(bsp, bsp)), 'ComposedTransformation(2D, BSplineTransformation(2D, 3x4)∘BSplineTransformation(2D, 3x4))' )
def augment(imA, imB): """ Input day0 and day4 image of the same rat An augmented version of both is returned """ #"3D" to 2D imA = imA.reshape((256, 256)) imB = imB.reshape((256, 256)) # Define random deformation matrix random_grid = np.random.rand(2, 3, 3) random_grid -= 0.5 random_grid /= 10 # Define B-Spline transformation matrix bspline = gryds.BSplineTransformation(random_grid) # Define random translation matrix a_translation = gryds.TranslationTransformation( [random.uniform(-0.03, 0.03), random.uniform(-0.03, 0.03)]) # Define random rotation matrix a_rotation = gryds.AffineTransformation( ndim=2, angles=[random.uniform(-np.pi / 24, np.pi / 24)], # ± 7 degrees center=[0.5, 0.5]) # Define an image interpolater an_image_interpolatorA = gryds.Interpolator(imA) an_image_interpolatorB = gryds.Interpolator(imB) # Combine all operations and apply the same augmentation to day0 and day4 composed = gryds.ComposedTransformation(bspline, a_rotation, a_translation) transformed_imageA = an_image_interpolatorA.transform(composed) transformed_imageB = an_image_interpolatorB.transform(composed) # Define noise augmentation mu = 0.0 sigma = random.uniform(0., 0.05) noise_mapA = np.random.normal(mu, sigma, size=np.size(imA)).reshape( (256, 256)) noise_mapB = np.random.normal(mu, sigma, size=np.size(imB)).reshape( (256, 256)) noise_mapA[transformed_imageA < 1e-2] = 0. noise_mapB[transformed_imageB < 1e-2] = 0. # Add noise to augmented image transformed_imageA = transformed_imageA + noise_mapA transformed_imageB = transformed_imageB + noise_mapB # Flip L/R (half of the time) perform_flip = random.choice([False, True]) if perform_flip: transformed_imageA = np.fliplr(transformed_imageA) transformed_imageB = np.fliplr(transformed_imageB) return [ transformed_imageA.reshape((256, 256, 1)), transformed_imageB.reshape((256, 256, 1)) ]