def augmentImages(batch_of_images, batch_of_masks): for i in range(len(batch_of_images)): img_and_mask = np.concatenate((batch_of_images[i, ...], batch_of_masks[i,...]), axis=2) if img_and_mask.ndim == 4: # This assumes single channel data. For multi-channel you'll need # change this to put all channel in slices channel orig_shape = img_and_mask.shape img_and_mask = img_and_mask.reshape((img_and_mask.shape[0:3])) if np.random.randint(0,10) == 7: img_and_mask = random_rotation(img_and_mask, rg=45, row_axis=0, col_axis=1, channel_axis=2, fill_mode='constant', cval=0.) if np.random.randint(0, 5) == 3: img_and_mask = elastic_transform(img_and_mask, alpha=1000, sigma=80, alpha_affine=50) if np.random.randint(0, 10) == 7: img_and_mask = random_shift(img_and_mask, wrg=0.2, hrg=0.2, row_axis=0, col_axis=1, channel_axis=2, fill_mode='constant', cval=0.) if np.random.randint(0, 10) == 7: img_and_mask = random_shear(img_and_mask, intensity=16, row_axis=0, col_axis=1, channel_axis=2, fill_mode='constant', cval=0.) if np.random.randint(0, 10) == 7: img_and_mask = random_zoom(img_and_mask, zoom_range=(0.75, 0.75), row_axis=0, col_axis=1, channel_axis=2, fill_mode='constant', cval=0.) if np.random.randint(0, 10) == 7: img_and_mask = flip_axis(img_and_mask, axis=1) if np.random.randint(0, 10) == 7: img_and_mask = flip_axis(img_and_mask, axis=0) if np.random.randint(0, 10) == 7: salt_pepper_noise(img_and_mask, salt=0.2, amount=0.04) if batch_of_images.ndim == 4: batch_of_images[i, ...] = img_and_mask[...,0:img_and_mask.shape[2]//2] batch_of_masks[i,...] = img_and_mask[...,img_and_mask.shape[2]//2:] if batch_of_images.ndim == 5: img_and_mask = img_and_mask.reshape(orig_shape) batch_of_images[i, ...] = img_and_mask[...,0:img_and_mask.shape[2]//2, :] batch_of_masks[i,...] = img_and_mask[...,img_and_mask.shape[2]//2:, :] # Ensure the masks did not get any non-binary values. batch_of_masks[batch_of_masks > 0.5] = 1 batch_of_masks[batch_of_masks <= 0.5] = 0 return(batch_of_images, batch_of_masks)
def augmentImages(batch_of_images, batch_of_masks): for i in range(len(batch_of_images)): img_and_mask = np.concatenate((batch_of_images[i, ...], batch_of_masks[i,...]), axis=2) if img_and_mask.ndim == 4: # This assumes single channel data. For multi-channel you'll need # change this to put all channel in slices channel orig_shape = img_and_mask.shape img_and_mask = img_and_mask.reshape((img_and_mask.shape[0:3])) if np.random.randint(0,10) == 7: img_and_mask = preprocess.random_rotation(img_and_mask, rg=45, row_axis=0, col_axis=1, channel_axis=2, fill_mode='constant', cval=0.) if np.random.randint(0, 5) == 3: img_and_mask = elastic_transform(img_and_mask, alpha=1000, sigma=80, alpha_affine=50) if np.random.randint(0, 10) == 7: img_and_mask = preprocess.random_shift(img_and_mask, wrg=0.2, hrg=0.2, row_axis=0, col_axis=1, channel_axis=2, fill_mode='constant', cval=0.) if np.random.randint(0, 10) == 7: img_and_mask = preprocess.random_shear(img_and_mask, intensity=16, row_axis=0, col_axis=1, channel_axis=2, fill_mode='constant', cval=0.) if np.random.randint(0, 10) == 7: img_and_mask = preprocess.random_zoom(img_and_mask, zoom_range=(0.75, 0.75), row_axis=0, col_axis=1, channel_axis=2, fill_mode='constant', cval=0.) if np.random.randint(0, 10) == 7: img_and_mask = flip_axis(img_and_mask, axis=1) if np.random.randint(0, 10) == 7: img_and_mask = flip_axis(img_and_mask, axis=0) if np.random.randint(0, 10) == 7: salt_pepper_noise(img_and_mask, salt=0.2, amount=0.04) if batch_of_images.ndim == 4: batch_of_images[i, ...] = img_and_mask[...,0:img_and_mask.shape[2]//2] batch_of_masks[i,...] = img_and_mask[...,img_and_mask.shape[2]//2:] if batch_of_images.ndim == 5: img_and_mask = img_and_mask.reshape(orig_shape) batch_of_images[i, ...] = img_and_mask[...,0:img_and_mask.shape[2]//2, :] batch_of_masks[i,...] = img_and_mask[...,img_and_mask.shape[2]//2:, :] # Ensure the masks did not get any non-binary values. batch_of_masks[batch_of_masks > 0.5] = 1 batch_of_masks[batch_of_masks <= 0.5] = 0 return(batch_of_images, batch_of_masks)