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_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_shape_prop(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) intp = gryds.MultiChannelInterpolator(image, gryds.LinearInterpolator, data_format='channels_last', cval=[0, 0, 0]) self.assertEqual(intp.shape, (5, 5, 3))
def datagenerator(images, segmentations, patch_size, patches_per_im, batch_size, augment_brightness=False, augment_bspline=False): """ Simple data-generator to feed patches in batches to the network. To extract different patches each epoch, steps_per_epoch in fit_generator should be equal to nr_batches. :param images: Input images :param segmentations: Corresponding segmentations :param patch_size: Desired patch size :param patches_per_im: Amount of patches to extract per image :param batch_size: Number of patches per batch :return: Batch of patches to feed to the model """ # Total number of patches generated per epoch total_patches = len(images) * patches_per_im # Amount of batches in one epoch nr_batches = int(np.ceil(total_patches / batch_size)) while True: # Each epoch extract different patches from the training images x, y = extract_patches(images, segmentations, patch_size, patches_per_im, seed=np.random.randint(0, 500)) # Feed data in batches to the network for idx in range(nr_batches): x_batch = x[idx * batch_size:(idx + 1) * batch_size] y_batch = y[idx * batch_size:(idx + 1) * batch_size] ## BEGIN EXERCISE if augment_brightness: delta = np.random.rand(1) delta = delta - 0.5 x_batch = x_batch[:] + delta if augment_bspline: for jdx in range(x_batch.shape[0]): b_grid = np.random.rand(2, 3, 3) b_grid -= 0.5 b_grid /= 10 bspline = gryds.BSplineTransformation(b_grid) intpl = gryds.MultiChannelInterpolator(x_batch[jdx]) x_batch[jdx] = intpl.transform(bspline) ## END EXERCISE yield x_batch, y_batch
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_repr(self): self.assertEqual( str(gryds.MultiChannelInterpolator(np.random.rand(3, 20, 20))), 'MultiChannelInterpolator(2D, channels_last)')