Python imgaug.HooksImages() Examples
The following are 8
code examples of imgaug.HooksImages().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
imgaug
, or try the search function
.
Example #1
Source File: test_blend.py From imgaug with MIT License | 6 votes |
def test_hooks_limiting_propagation(self): aug = iaa.BlendAlphaElementwise( 0.5, iaa.Add(100), iaa.Add(50), name="AlphaElementwiseTest") def propagator(images, augmenter, parents, default): if "AlphaElementwise" in augmenter.name: return False else: return default hooks = ia.HooksImages(propagator=propagator) image = np.zeros((10, 10, 3), dtype=np.uint8) + 10 observed = aug.augment_image(image, hooks=hooks) assert np.array_equal(observed, image)
Example #2
Source File: utils.py From Brats2019 with MIT License | 5 votes |
def data_augment(self, image, mask, augmentation): # Augmentation # This requires the imgaug lib (https://github.com/aleju/imgaug) if augmentation: import imgaug # Augmenters that are safe to apply to masks # Some, such as Affine, have settings that make them unsafe, so always # test your augmentation on masks MASK_AUGMENTERS = ["Sequential", "SomeOf", "OneOf", "Sometimes", "Fliplr", "Flipud", "CropAndPad", "Affine", "PiecewiseAffine"] def hook(images, augmenter, parents, default): """Determines which augmenters to apply to masks.""" return augmenter.__class__.__name__ in MASK_AUGMENTERS # Store shapes before augmentation to compare image_shape = image.shape mask_shape = mask.shape # Make augmenters deterministic to apply similarly to images and masks det = augmentation.to_deterministic() # image should be uint8!! images = det.augment_image(image) # Change mask to np.uint8 because imgaug doesn't support np.bool masks = det.augment_image(mask.astype(np.uint8), hooks=imgaug.HooksImages(activator=hook)) # Verify that shapes didn't change assert images.shape == image_shape, "Augmentation shouldn't change image size" assert masks.shape == mask_shape, "Augmentation shouldn't change mask size" # Change mask back to bool # masks = masks.astype(np.bool) return image, mask
Example #3
Source File: test_batches.py From imgaug with MIT License | 5 votes |
def test_propagation_hooks_ctx(self): def propagator(images, augmenter, parents, default): if ia.is_np_array(images): return False else: return True hooks = ia.HooksImages(propagator=propagator) batch = _BatchInAugmentation( images=np.zeros((3, 3, 4, 1), dtype=np.uint8), keypoints=[ ia.KeypointsOnImage( [ia.Keypoint(0, 0)], shape=(3, 4, 1) ), ia.KeypointsOnImage( [ia.Keypoint(1, 1)], shape=(3, 4, 1) ), ia.KeypointsOnImage( [ia.Keypoint(2, 2)], shape=(3, 4, 1) ) ] ) with batch.propagation_hooks_ctx(iaa.Identity(), hooks, []) \ as batch_prop: assert batch_prop.images is None assert batch_prop.keypoints is not None assert len(batch_prop.keypoints) == 3 batch_prop.keypoints[0].keypoints[0].x = 10 assert batch.images is not None assert batch.keypoints is not None assert batch.keypoints[0].keypoints[0].x == 10
Example #4
Source File: test_blend.py From imgaug with MIT License | 5 votes |
def test_hooks_limiting_propagation(self): aug = iaa.BlendAlpha(0.5, iaa.Add(100), iaa.Add(50), name="AlphaTest") def propagator(images, augmenter, parents, default): if "Alpha" in augmenter.name: return False else: return default hooks = ia.HooksImages(propagator=propagator) image = np.zeros((10, 10, 3), dtype=np.uint8) + 1 observed = aug.augment_image(image, hooks=hooks) assert np.array_equal(observed, image)
Example #5
Source File: test_color.py From imgaug with MIT License | 5 votes |
def test_using_hooks_to_deactivate_propagation(self): def _propagator(images, augmenter, parents, default): return False if augmenter.name == "foo" else default aug = iaa.WithBrightnessChannels( children=iaa.Add(100), to_colorspace=iaa.CSPACE_HSV, name="foo") image = np.arange(6*6*3).astype(np.uint8).reshape((6, 6, 3)) image_aug = aug(image=image, hooks=ia.HooksImages(propagator=_propagator)) assert np.array_equal(image_aug, image)
Example #6
Source File: test_readme_examples.py From ViolenceDetection with Apache License 2.0 | 5 votes |
def example_hooks(): print("Example: Hooks") import imgaug as ia from imgaug import augmenters as iaa import numpy as np # images and heatmaps, just arrays filled with value 30 images = np.ones((16, 128, 128, 3), dtype=np.uint8) * 30 heatmaps = np.ones((16, 128, 128, 21), dtype=np.uint8) * 30 # add vertical lines to see the effect of flip images[:, 16:128-16, 120:124, :] = 120 heatmaps[:, 16:128-16, 120:124, :] = 120 seq = iaa.Sequential([ iaa.Fliplr(0.5, name="Flipper"), iaa.GaussianBlur((0, 3.0), name="GaussianBlur"), iaa.Dropout(0.02, name="Dropout"), iaa.AdditiveGaussianNoise(scale=0.01*255, name="MyLittleNoise"), iaa.AdditiveGaussianNoise(loc=32, scale=0.0001*255, name="SomeOtherNoise"), iaa.Affine(translate_px={"x": (-40, 40)}, name="Affine") ]) # change the activated augmenters for heatmaps def activator_heatmaps(images, augmenter, parents, default): if augmenter.name in ["GaussianBlur", "Dropout", "MyLittleNoise"]: return False else: # default value for all other augmenters return default hooks_heatmaps = ia.HooksImages(activator=activator_heatmaps) seq_det = seq.to_deterministic() # call this for each batch again, NOT only once at the start images_aug = seq_det.augment_images(images) heatmaps_aug = seq_det.augment_images(heatmaps, hooks=hooks_heatmaps) # ----------- ia.show_grid(images_aug) ia.show_grid(heatmaps_aug[..., 0:3])
Example #7
Source File: utils.py From Brats2019 with MIT License | 4 votes |
def data_augment_volume(self, *datalist , augmentation): # first get the volume data from the data list image1, image2, image3, mask1, mask2, mask3 = datalist # Augmentation # This requires the imgaug lib (https://github.com/aleju/imgaug) if augmentation: import imgaug # Augmenters that are safe to apply to masks # Some, such as Affine, have settings that make them unsafe, so always # test your augmentation on masks MASK_AUGMENTERS = ["Sequential", "SomeOf", "OneOf", "Sometimes", "Fliplr", "Flipud", "CropAndPad", "Affine", "PiecewiseAffine"] def hook(images, augmenter, parents, default): """Determines which augmenters to apply to masks.""" return augmenter.__class__.__name__ in MASK_AUGMENTERS # Store shapes before augmentation to compare image1_shape = image1.shape mask1_shape = mask1.shape image2_shape = image2.shape mask2_shape = mask2.shape image3_shape = image3.shape mask3_shape = mask3.shape # Make augmenters deterministic to apply similarly to images and masks det = augmentation.to_deterministic() # image should be uint8!! image1 = det.augment_image(image1) image2 = det.augment_image(image2) image3 = det.augment_image(image3) # Change mask to np.uint8 because imgaug doesn't support np.bool mask1 = det.augment_image(mask1.astype(np.uint8), hooks=imgaug.HooksImages(activator=hook)) mask2 = det.augment_image(mask2.astype(np.uint8), hooks=imgaug.HooksImages(activator=hook)) mask3 = det.augment_image(mask3.astype(np.uint8), hooks=imgaug.HooksImages(activator=hook)) # Verify that shapes didn't change assert image1.shape == image1_shape, "Augmentation shouldn't change image size" assert mask1.shape == mask1_shape, "Augmentation shouldn't change mask size" assert image2.shape == image2_shape, "Augmentation shouldn't change image size" assert mask2.shape == mask2_shape, "Augmentation shouldn't change mask size" assert image3.shape == image3_shape, "Augmentation shouldn't change image size" assert mask3.shape == mask3_shape, "Augmentation shouldn't change mask size" # Change mask back to bool # masks = masks.astype(np.bool) return image1,image2, image3, mask1, mask2, mask3
Example #8
Source File: check_readme_examples.py From imgaug with MIT License | 4 votes |
def example_hooks(): print("Example: Hooks") import numpy as np import imgaug as ia import imgaug.augmenters as iaa # Images and heatmaps, just arrays filled with value 30. # We define the heatmaps here as uint8 arrays as we are going to feed them # through the pipeline similar to normal images. In that way, every # augmenter is applied to them. images = np.full((16, 128, 128, 3), 30, dtype=np.uint8) heatmaps = np.full((16, 128, 128, 21), 30, dtype=np.uint8) # add vertical lines to see the effect of flip images[:, 16:128-16, 120:124, :] = 120 heatmaps[:, 16:128-16, 120:124, :] = 120 seq = iaa.Sequential([ iaa.Fliplr(0.5, name="Flipper"), iaa.GaussianBlur((0, 3.0), name="GaussianBlur"), iaa.Dropout(0.02, name="Dropout"), iaa.AdditiveGaussianNoise(scale=0.01*255, name="MyLittleNoise"), iaa.AdditiveGaussianNoise(loc=32, scale=0.0001*255, name="SomeOtherNoise"), iaa.Affine(translate_px={"x": (-40, 40)}, name="Affine") ]) # change the activated augmenters for heatmaps, # we only want to execute horizontal flip, affine transformation and one of # the gaussian noises def activator_heatmaps(images, augmenter, parents, default): if augmenter.name in ["GaussianBlur", "Dropout", "MyLittleNoise"]: return False else: # default value for all other augmenters return default hooks_heatmaps = ia.HooksImages(activator=activator_heatmaps) # call to_deterministic() once per batch, NOT only once at the start seq_det = seq.to_deterministic() images_aug = seq_det(images=images) heatmaps_aug = seq_det(images=heatmaps, hooks=hooks_heatmaps) # ----------- ia.show_grid(images_aug) ia.show_grid(heatmaps_aug[..., 0:3])