def test_random_transform_generator(): """ Test random_transform_generator by confirming that it generates appropriate solutions and output sizes for seeded examples. """ # Check shapes are correct Batch Size = 1 - Pass batch_size = 1 transforms = layer_util.random_transform_generator(batch_size, 0) assert transforms.shape == (batch_size, 4, 3) # Check numerical outputs are correct for a given seed - Pass batch_size = 1 scale = 0.1 seed = 0 expected = tf.constant( np.array( [[ [9.4661278e-01, -3.8267835e-03, 3.6934228e-03], [5.5613145e-03, 9.8034811e-01, -1.8044969e-02], [1.9651605e-04, 1.4576728e-02, 9.6243286e-01], [-2.5107686e-03, 1.9579126e-02, -1.2195010e-02], ]], dtype=np.float32, )) # shape = (1, 4, 3) got = layer_util.random_transform_generator(batch_size=batch_size, scale=scale, seed=seed) assert check_equal(got, expected)
def _gen_transforms(self): """ Function that generates a random 3D transformation parameters for a batch of data. :return: shape = (batch, 4, 3) """ return layer_util.random_transform_generator( batch_size=self._batch_size, scale=self._scale)
## load image if not os.path.exists(DATA_PATH): raise ("Download the data using demo_data.py script") if not os.path.exists(FILE_PATH): raise ("Download the data using demo_data.py script") fid = h5py.File(FILE_PATH, "r") fixed_image = tf.cast(tf.expand_dims(fid["image"], axis=0), dtype=tf.float32) fixed_image = (fixed_image - tf.reduce_min(fixed_image)) / ( tf.reduce_max(fixed_image) - tf.reduce_min(fixed_image) ) # normalisation to [0,1] # generate a radomly-affine-transformed moving image fixed_image_size = fixed_image.shape transform_random = layer_util.random_transform_generator(batch_size=1, scale=0.2) grid_ref = layer_util.get_reference_grid(grid_size=fixed_image_size[1:4]) grid_random = layer_util.warp_grid(grid_ref, transform_random) moving_image = layer_util.resample(vol=fixed_image, loc=grid_random) # warp the labels to get ground-truth using the same random affine, for validation fixed_labels = tf.cast(tf.expand_dims(fid["label"], axis=0), dtype=tf.float32) moving_labels = tf.stack( [ layer_util.resample(vol=fixed_labels[..., idx], loc=grid_random) for idx in range(fixed_labels.shape[4]) ], axis=4, ) ## optimisation
def _gen_transforms(self): return layer_util.random_transform_generator( batch_size=self._batch_size, scale=self._scale )