def test_linear_channels_last(self): image = np.array(3 * [[ [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).transpose(1, 2, 0) expected = np.array(3 * [[ [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 1, 1, 1, 1], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0] ]], dtype=DTYPE).transpose(1, 2, 0) intp = gryds.MultiChannelInterpolator(image, gryds.LinearInterpolator, data_format='channels_last', cval=[0, 0, 0]) trf = gryds.AffineTransformation(ndim=2, angles=[np.pi/2.], center=[0.4, 0.4]) new_image = intp.transform(trf).astype(DTYPE) np.testing.assert_almost_equal(expected, new_image, decimal=4) intp = gryds.MultiChannelInterpolator(image, gryds.LinearInterpolator, data_format='channels_last') trf = gryds.AffineTransformation(ndim=2, angles=[np.pi/2.], center=[0.4, 0.4]) new_image = intp.transform(trf).astype(DTYPE) np.testing.assert_almost_equal(expected, new_image, decimal=4)
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 Bspline_and_Affine(img, mask): # Define a scaling transformation object angle = random.randrange(-1,2,2)*np.pi/(random.randint(6,16)) center_point_x = 0.1*random.randint(3,7) center_point_y = 0.1*random.randint(3,7) affine = gryds.AffineTransformation( ndim=2, angles=[angle], # List of angles (for 3D transformations you need a list of 3 angles). center=[center_point_x, center_point_y]) # 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_img = gryds.Interpolator(img) interpolator_mask = gryds.Interpolator(mask) # Transform the image using both transformations. The B-spline is applied to the # sampling grid first, and the affine transformation second. From the # perspective of the image itself, the order will seem reversed (!). transformed_image = interpolator_img.transform(bspline, affine) transformed_mask = interpolator_mask.transform(bspline, affine) return transformed_image, transformed_mask
def test_affine_errors(self): self.assertRaises(ValueError, gryds.AffineTransformation, ndim=2, angles=[1, 2, 3, 4]) # Should raise a ValueError for number of angles not supported self.assertRaises(ValueError, gryds.AffineTransformation, ndim=2, shear_matrix=[[1]]) # Should raise a ValueError for shear_matrix not being ndim x ndim shaped gryds.AffineTransformation(ndim=2, shear_matrix=[[1, 1], [1, 1]]) # Should print a Warning that the shear matrix contains scaling self.assertRaises(ValueError, gryds.AffineTransformation, ndim=2, scaling=[1, 2, 3]) # Should raise a ValueError for number of scaling components not agreeing with ndim self.assertRaises(ValueError, gryds.AffineTransformation, ndim=2, translation=[1, 2, 3])
def test_bspline_channels_first(self): image = np.array(3 * [[ [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.MultiChannelInterpolator(image, data_format='channels_first', cval=[0, 0, 0]) trf = gryds.AffineTransformation(ndim=2, angles=[np.pi/2.], center=[0.4, 0.4]) new_image = intp.transform(trf, mode='mirror').astype(DTYPE) np.testing.assert_almost_equal(image, new_image, decimal=4) intp = gryds.MultiChannelInterpolator(image, data_format='channels_first') trf = gryds.AffineTransformation(ndim=2, angles=[np.pi/2.], center=[0.4, 0.4]) new_image = intp.transform(trf, mode='mirror').astype(DTYPE) np.testing.assert_almost_equal(image, new_image, decimal=4)
def test_2d_bspline_interpolator_90_deg_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) trf = gryds.AffineTransformation(ndim=2, angles=[np.pi / 2.], center=[0.4, 0.4]) new_image = intp.transform(trf, mode='mirror').astype(DTYPE) np.testing.assert_almost_equal(image, new_image, decimal=4)
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_3d_45_deg_rotation_with_center(self): trf = gryds.AffineTransformation( ndim=3, angles=[0, 0.25 * np.pi, 0], center_of=np.zeros((10, 20, 20))) # rotate grid 45° anticlockwise grid = gryds.Grid((10, 20, 20)) # The jacobian of this transformation should be 1 everywhere, i.e. no # scaling should have happened np.testing.assert_almost_equal(grid.jacobian_det(trf), np.array(1, DTYPE), decimal=5)
def gryds_function(images, segmentations): # input: (images): The input is an array of images # input: (segmentations): The segmentations of the corresponding input images # output: (new_image): The output is a geometricly augmented array of images with a randomly defined deformation/ rotation. # output:(new_segmentation): The output the augmented version of the segmentation import numpy as np import gryds new_image = [] new_segmentation = [] for i in range(len(images)): affine = gryds.AffineTransformation( ndim=2, angles=[ np.pi / 4. ], # List of angles (for 3D transformations you need a list of 3 angles). center=[0.5, 0.5] # Center of rotation. ) random_grid = np.random.rand(2, 3, 3) random_grid -= 0.5 random_grid /= 20 bspline = gryds.BSplineTransformation(random_grid) # define interpolator of the input_image interpolator = gryds.MultiChannelInterpolator(images[i], order=0, cval=[.1, .2, .3], mode='nearest') #define interpolator of the segmentation image interpolator_segentation = gryds.Interpolator(segmentations[i][:, :, 0], mode='constant') #transform the input image transformed_image = interpolator.transform(bspline, affine) #transform the segmentation image transformed_segmentation = interpolator_segentation.transform( bspline, affine) #add results into lists new_segmentation.append(np.clip(transformed_segmentation, 0, 1)) new_image.append(np.clip(transformed_image, 0, 1)) return np.array(new_image), np.array(new_segmentation)
def test_3d_linear_interpolator_45_deg_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) expected = np.array( [[0, 0, 0., 0, 0], [0., 1., 0.5, 1., 0.], [0., 0.5, 1., 0.5, 0.], [0., 1., 0.5, 1., 0.], [0., 0., 0., 0., 0.]], dtype=DTYPE ) # Borders will be zero due to being outside of image domain intp = gryds.LinearInterpolator(image) trf = gryds.AffineTransformation(ndim=2, angles=[np.pi / 4.], center=[0.4, 0.4]) new_image = intp.transform(trf, mode='nope').astype(DTYPE) np.testing.assert_almost_equal(expected, new_image, decimal=4)
def test_2d_bspline_interpolator_45_deg_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) expected = np.array( [[1., 0.2929, 0., 0.2929, 1.], [0.2929, 1., 0.5, 1., 0.2929], [0., 0.5, 1., 0.5, 0.], [0.2929, 1., 0.5, 1., 0.2929], [1., 0.2929, 0., 0.2929, 1.]], dtype=DTYPE) intp = gryds.Interpolator(image, order=1, mode='mirror') trf = gryds.AffineTransformation(ndim=2, angles=[np.pi / 4.], center=[0.4, 0.4]) new_image = intp.transform(trf).astype(DTYPE) np.testing.assert_almost_equal(expected, new_image, decimal=4)
def test_2d_cuda_interpolator_90_deg_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) expected = np.array( [[0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 1, 1, 1, 1], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0]], dtype=DTYPE ) # Borders will be zero due to being outside of image domain intp = gryds.BSplineInterpolatorCuda(image) trf = gryds.AffineTransformation(ndim=2, angles=[np.pi / 2.], center=[0.4, 0.4]) new_image = intp.transform(trf).astype(DTYPE) np.testing.assert_almost_equal(expected, new_image, decimal=4)
def test_2d_90_deg_rotation(self): trf = gryds.AffineTransformation(ndim=2, angles=[0.5 * np.pi]) # rotate grid 90 degrees clockwise grid = gryds.Grid((10, 20)) new_grid = grid.transform(trf) # The grid runs from 0 to 0.95 on the j-axis # 90 deg rot means the i-axis will run from 0 to -0.95 np.testing.assert_equal(new_grid.grid[0, 0, 0], np.array(0, DTYPE)) np.testing.assert_equal(new_grid.grid[0, 0, -1], np.array(-0.95, DTYPE)) # The jacobian of this transformation should be 1 everywhere, i.e. no # scaling should have happened np.testing.assert_almost_equal( grid.jacobian_det(trf), np.array(1, DTYPE), decimal=4)
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 Affine(img,mask): # Define a scaling transformation object angle = random.randrange(-1,2,2)*np.pi/(random.randint(6,16)) center_point_x = 0.1*random.randint(3,7) center_point_y = 0.1*random.randint(3,7) affine = gryds.AffineTransformation( ndim=2, angles=[angle], # List of angles (for 3D transformations you need a list of 3 angles). center=[center_point_x, center_point_y]) # Center of rotation. # Define an interpolator object for the image: interpolator_img = gryds.Interpolator(img) interpolator_mask = gryds.Interpolator(mask) # Transform image and mask using Affine transformation transformed_image = interpolator_img.transform(affine) transformed_mask = interpolator_mask.transform(affine) return transformed_image, transformed_mask
def test_3d_linear_interpolator_90_deg_rotation(self): image = np.zeros((2, 5, 5)) image[1] = 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) image[0] = image[1] expected = np.zeros((2, 5, 5)) expected[0] = np.array( [[0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 1, 1, 1, 1], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0]], dtype=DTYPE ) # Borders will be zero due to being outside of image domain intp = gryds.LinearInterpolator(image) trf = gryds.AffineTransformation(ndim=3, angles=[np.pi / 2., 0, 0], center=[0.4, 0.4, 0.4]) new_image = intp.transform(trf).astype(DTYPE) np.testing.assert_almost_equal(expected, new_image, decimal=4)
def Affine(img, mask): center_point_x = 0.1 * random.randint(4, 6) center_point_y = 0.1 * random.randint(4, 6) affine = gryds.AffineTransformation( ndim=2, angles=[ random.randrange(-1, 2, 2) * (np.pi / (random.randint(50, 60))) ], # List of angles (for 3D transformations you need a list of 3 angles). center=[center_point_x, center_point_y] # Center of rotation. ) # Define an interpolator object for the image: interpolator_img = gryds.Interpolator(img) interpolator_mask = gryds.Interpolator(mask) # Transform the image using Affine transformed_image = interpolator_img.transform(affine) transformed_mask = interpolator_mask.transform(affine) return transformed_image, transformed_mask
def test_2d_downscaling(self): trf = gryds.AffineTransformation( ndim=2, scaling=[1.5, 1]) # scale grid by 150% isotropically grid = gryds.Grid((10, 20)) new_grid = grid.transform(trf) # The original grid runs from 0 to 0.9 for the i-coordinates # The transformed grid should run from 0 to 1.35 np.testing.assert_equal(new_grid.grid[0, 0], np.array(0, DTYPE)) np.testing.assert_almost_equal(new_grid.grid[0, 9], np.array(1.35, DTYPE), decimal=6) # The jacobian of this transformation should be 1 everywhere, i.e. no # scaling should have happened np.testing.assert_almost_equal( grid.jacobian_det(trf), np.array(1.5, DTYPE), # i.e. 1.5*1.5 decimal=4)
def Bspline_and_Affine_flipped(img, mask): # Define a scaling transformation object center_point_x = 0.1 * random.randint(4, 6) center_point_y = 0.1 * random.randint(4, 6) affine = gryds.AffineTransformation( ndim=2, angles=[ random.randrange(-1, 2, 2) * (np.pi / (random.randint(50, 60))) ], # List of angles (for 3D transformations you need a list of 3 angles). center=[center_point_x, center_point_y] # 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 /= 10 # Define a B-spline transformation object bspline = gryds.BSplineTransformation(random_grid) # Define an interpolator object for the image: interpolator_img = gryds.Interpolator(img) interpolator_mask = gryds.Interpolator(mask) # Transform the image using both transformations. The B-spline is applied to the # sampling grid first, and the affine transformation second. From the # perspective of the image itself, the order will seem reversed (!). transformed_image = interpolator_img.transform(bspline, affine) transformed_mask = interpolator_mask.transform(bspline, affine) img = torch.from_numpy(transformed_image.copy()) mask = torch.from_numpy(transformed_mask.copy()) flipped_img = torchvision.transforms.functional.hflip( img=img) # change to .vflip for vertical flip flipped_mask = torchvision.transforms.functional.hflip(img=mask) transformed_image = flipped_img.cpu().detach().numpy() transformed_mask = flipped_mask.cpu().detach().numpy() return transformed_image, transformed_mask
def test_repr(self): self.assertEqual(str(gryds.AffineTransformation(2, angles=[0.4])), 'AffineTransformation(2D)')
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)) ]
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)