def test___init___deterministic_settinga(self): colorizer = iaa.RandomColorsBinaryImageColorizer(color_true=1, color_false=2) assert isinstance(colorizer.color_true, iap.Deterministic) assert isinstance(colorizer.color_false, iap.Deterministic) assert colorizer.color_true.value == 1 assert colorizer.color_false.value == 2
def test_augment_images__alpha_is_one(self): colorizer = iaa.RandomColorsBinaryImageColorizer(color_true=254, color_false=1) aug = iaa.Canny(alpha=1.0, hysteresis_thresholds=100, sobel_kernel_size=3, colorizer=colorizer, random_state=1) image_single_chan = np.uint8([[0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 1, 1, 1, 0, 0, 0]]) image = np.tile(image_single_chan[:, :, np.newaxis] * 128, (1, 1, 3)) # canny image, looks a bit unintuitive, but is what OpenCV returns # can be checked via something like # print("canny\n", cv2.Canny(image_single_chan*255, threshold1=100, # threshold2=200, # apertureSize=3, # L2gradient=True)) image_canny = np.array([[0, 0, 1, 0, 1, 0, 0], [0, 0, 1, 0, 1, 0, 0], [0, 0, 1, 0, 1, 0, 0], [0, 0, 1, 0, 1, 0, 0], [0, 1, 0, 0, 1, 0, 0]], dtype=bool) image_aug_expected = np.copy(image) image_aug_expected[image_canny] = 254 image_aug_expected[~image_canny] = 1 image_aug = aug.augment_image(image) assert np.array_equal(image_aug, image_aug_expected)
def rgbmore(self, im): return_im = [] add_return_im = lambda im: return_im.extend(im) grey = np.array(im.convert(mode='L')) im = np.array(im) rgb_grey = np.dstack((im, grey)) edge = iaa.EdgeDetect(alpha=1)(images=rgb_grey) dir_edge = lambda d: iaa.DirectedEdgeDetect(alpha=1, direction=d)(images= grey) dir_edges = np.array( [dir_edge(d) for d in np.linspace(0, 1, num=3, endpoint=False)]) dir_edges = np.transpose(dir_edges, (1, 2, 0)) canny = iaa.Canny(alpha=1.0, hysteresis_thresholds=128, sobel_kernel_size=4, deterministic=True, colorizer=iaa.RandomColorsBinaryImageColorizer( color_true=255, color_false=0))(images=grey) avg_pool = iaa.AveragePooling(2)(images=grey) max_pool = iaa.MaxPooling(2)(images=grey) min_pool = iaa.MinPooling(2)(images=grey) add_return_im([im, grey]) add_return_im([edge, dir_edges, canny]) add_return_im([avg_pool, max_pool, min_pool]) return np.dstack(return_im)
def test___str___tuple_as_hysteresis(self): alpha = iap.Deterministic(0.2) hysteresis_thresholds = ( iap.Deterministic(10), iap.Deterministic(11) ) sobel_kernel_size = iap.Deterministic(3) colorizer = iaa.RandomColorsBinaryImageColorizer( color_true=10, color_false=20) aug = iaa.Canny( alpha=alpha, hysteresis_thresholds=hysteresis_thresholds, sobel_kernel_size=sobel_kernel_size, colorizer=colorizer ) observed = aug.__str__() expected = ("Canny(alpha=%s, hysteresis_thresholds=(%s, %s), " "sobel_kernel_size=%s, colorizer=%s, name=UnnamedCanny, " "deterministic=False)") % ( str(aug.alpha), str(aug.hysteresis_thresholds[0]), str(aug.hysteresis_thresholds[1]), str(aug.sobel_kernel_size), colorizer) assert observed == expected
def main(): black_and_white = iaa.RandomColorsBinaryImageColorizer( color_true=255, color_false=0) print("alpha=1.0, black and white") image = ia.quokka_square((128, 128)) aug = iaa.Canny(alpha=1.0, colorizer=black_and_white) ia.imshow(ia.draw_grid(aug(images=[image] * (5*5)))) print("alpha=1.0, random color") image = ia.quokka_square((128, 128)) aug = iaa.Canny(alpha=1.0) ia.imshow(ia.draw_grid(aug(images=[image] * (5*5)))) print("alpha=1.0, sobel ksize=[3, 13], black and white") image = ia.quokka_square((128, 128)) aug = iaa.Canny(alpha=1.0, sobel_kernel_size=[3, 7], colorizer=black_and_white) ia.imshow(ia.draw_grid(aug(images=[image] * (5*5)))) print("alpha=1.0, sobel ksize=3, black and white") image = ia.quokka_square((128, 128)) aug = iaa.Canny(alpha=1.0, sobel_kernel_size=3, colorizer=black_and_white) ia.imshow(ia.draw_grid(aug(images=[image] * (5*5)))) print("fully random") image = ia.quokka_square((128, 128)) aug = iaa.Canny() ia.imshow(ia.draw_grid(aug(images=[image] * (5*5))))
def test___init___custom_settings(self): aug = iaa.Canny( alpha=0.2, hysteresis_thresholds=([0, 1, 2], iap.DiscreteUniform(1, 10)), sobel_kernel_size=[3, 5], colorizer=iaa.RandomColorsBinaryImageColorizer( color_true=10, color_false=20) ) assert is_parameter_instance(aug.alpha, iap.Deterministic) assert isinstance(aug.hysteresis_thresholds, tuple) assert is_parameter_instance(aug.sobel_kernel_size, iap.Choice) assert isinstance(aug.colorizer, iaa.RandomColorsBinaryImageColorizer) assert np.isclose(aug.alpha.value, 0.2) assert len(aug.hysteresis_thresholds) == 2 assert is_parameter_instance(aug.hysteresis_thresholds[0], iap.Choice) assert aug.hysteresis_thresholds[0].a == [0, 1, 2] assert is_parameter_instance(aug.hysteresis_thresholds[1], iap.DiscreteUniform) assert np.isclose(aug.hysteresis_thresholds[1].a.value, 1) assert np.isclose(aug.hysteresis_thresholds[1].b.value, 10) assert is_parameter_instance(aug.sobel_kernel_size, iap.Choice) assert aug.sobel_kernel_size.a == [3, 5] assert is_parameter_instance(aug.colorizer.color_true, iap.Deterministic) assert is_parameter_instance(aug.colorizer.color_false, iap.Deterministic) assert aug.colorizer.color_true.value == 10 assert aug.colorizer.color_false.value == 20
def test___init___default_settings(self): colorizer = iaa.RandomColorsBinaryImageColorizer() assert isinstance(colorizer.color_true, iap.DiscreteUniform) assert isinstance(colorizer.color_false, iap.DiscreteUniform) assert colorizer.color_true.a.value == 0 assert colorizer.color_true.b.value == 255 assert colorizer.color_false.a.value == 0 assert colorizer.color_false.b.value == 255
def test_augment_images__random_values(self): colorizer = iaa.RandomColorsBinaryImageColorizer( color_true=255, color_false=0 ) image_single_chan = iarandom.RNG(1).integers( 0, 255, size=(100, 100), dtype="uint8") image = np.tile(image_single_chan[:, :, np.newaxis], (1, 1, 3)) images_canny_uint8 = {} for thresh1, thresh2, ksize in itertools.product([100], [200], [3, 5]): if thresh1 > thresh2: continue image_canny = cv2.Canny( image, threshold1=thresh1, threshold2=thresh2, apertureSize=ksize, L2gradient=True) image_canny_uint8 = np.tile( image_canny[:, :, np.newaxis], (1, 1, 3)) similar = 0 for key, image_expected in images_canny_uint8.items(): if np.array_equal(image_canny_uint8, image_expected): similar += 1 assert similar == 0 images_canny_uint8[(thresh1, thresh2, ksize)] = image_canny_uint8 seen = {key: False for key in images_canny_uint8.keys()} for i in range(500): aug = iaa.Canny( alpha=1.0, hysteresis_thresholds=(iap.Deterministic(100), iap.Deterministic(200)), sobel_kernel_size=[3, 5], colorizer=colorizer, seed=i) image_aug = aug.augment_image(image) match_index = None for key, image_expected in images_canny_uint8.items(): if np.array_equal(image_aug, image_expected): match_index = key break assert match_index is not None seen[match_index] = True assert len(seen.keys()) == len(images_canny_uint8.keys()) if all(seen.values()): break assert np.all(seen.values())
def test_augment_images__random_color(self): class _Color(iap.StochasticParameter): def __init__(self, values): super(_Color, self).__init__() self.values = values def _draw_samples(self, size, random_state): v = random_state.choice(self.values) return np.full(size, v, dtype=np.uint8) colorizer = iaa.RandomColorsBinaryImageColorizer( color_true=_Color([253, 254]), color_false=_Color([1, 2])) image_single_chan = np.uint8([[0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 1, 1, 1, 0, 0, 0]]) image = np.tile(image_single_chan[:, :, np.newaxis] * 128, (1, 1, 3)) # canny image, looks a bit unintuitive, but is what OpenCV returns # can be checked via something like # print("canny\n", cv2.Canny(image_single_chan*255, threshold1=100, # threshold2=200, # apertureSize=3, # L2gradient=True)) image_canny = np.array([[0, 0, 1, 0, 1, 0, 0], [0, 0, 1, 0, 1, 0, 0], [0, 0, 1, 0, 1, 0, 0], [0, 0, 1, 0, 1, 0, 0], [0, 1, 0, 0, 1, 0, 0]], dtype=bool) seen = { (253, 1): False, (253, 2): False, (254, 1): False, (254, 2): False } for i in range(100): aug = iaa.Canny(alpha=1.0, hysteresis_thresholds=100, sobel_kernel_size=3, colorizer=colorizer, random_state=i) image_aug = aug.augment_image(image) color_true = np.unique(image_aug[image_canny]) color_false = np.unique(image_aug[~image_canny]) assert len(color_true) == 1 assert len(color_false) == 1 color_true = int(color_true[0]) color_false = int(color_false[0]) seen[(int(color_true), int(color_false))] = True assert len(seen.keys()) == 4 if all(seen.values()): break assert np.all(seen.values())
def test___init___tuple_and_list(self): colorizer = iaa.RandomColorsBinaryImageColorizer( color_true=(0, 100), color_false=[200, 201, 202]) assert isinstance(colorizer.color_true, iap.DiscreteUniform) assert isinstance(colorizer.color_false, iap.Choice) assert colorizer.color_true.a.value == 0 assert colorizer.color_true.b.value == 100 assert colorizer.color_false.a[0] == 200 assert colorizer.color_false.a[1] == 201 assert colorizer.color_false.a[2] == 202
def test___init___stochastic_parameters(self): colorizer = iaa.RandomColorsBinaryImageColorizer( color_true=iap.DiscreteUniform(0, 100), color_false=iap.Choice([200, 201, 202])) assert isinstance(colorizer.color_true, iap.DiscreteUniform) assert isinstance(colorizer.color_false, iap.Choice) assert colorizer.color_true.a.value == 0 assert colorizer.color_true.b.value == 100 assert colorizer.color_false.a[0] == 200 assert colorizer.color_false.a[1] == 201 assert colorizer.color_false.a[2] == 202
def test_pickleable(self): colorizer = iaa.RandomColorsBinaryImageColorizer(color_true=(50, 100), color_false=(10, 50)) colorizer_pkl = pickle.loads(pickle.dumps(colorizer)) random_state = iarandom.RNG(1) color_true, color_false = colorizer._draw_samples(random_state.copy()) color_true_pkl, color_false_pkl = colorizer_pkl._draw_samples( random_state.copy()) assert np.array_equal(color_true, color_true_pkl) assert np.array_equal(color_false, color_false_pkl)
def test_get_parameters(self): alpha = iap.Deterministic(0.2) hysteresis_thresholds = iap.Deterministic(10) sobel_kernel_size = iap.Deterministic(3) colorizer = iaa.RandomColorsBinaryImageColorizer(color_true=10, color_false=20) aug = iaa.Canny(alpha=alpha, hysteresis_thresholds=hysteresis_thresholds, sobel_kernel_size=sobel_kernel_size, colorizer=colorizer) params = aug.get_parameters() assert params[0] is alpha assert params[1] is hysteresis_thresholds assert params[2] is sobel_kernel_size assert params[3] is colorizer
def test___str___single_value_hysteresis(self): alpha = iap.Deterministic(0.2) hysteresis_thresholds = iap.Deterministic(10) sobel_kernel_size = iap.Deterministic(3) colorizer = iaa.RandomColorsBinaryImageColorizer(color_true=10, color_false=20) aug = iaa.Canny(alpha=alpha, hysteresis_thresholds=hysteresis_thresholds, sobel_kernel_size=sobel_kernel_size, colorizer=colorizer) observed = aug.__str__() expected = ("Canny(alpha=%s, hysteresis_thresholds=%s, " "sobel_kernel_size=%s, colorizer=%s, name=UnnamedCanny, " "deterministic=False)") % (alpha, hysteresis_thresholds, sobel_kernel_size, colorizer) assert observed == expected
def test___init___single_value_hysteresis(self): aug = iaa.Canny(alpha=0.2, hysteresis_thresholds=[0, 1, 2], sobel_kernel_size=[3, 5], colorizer=iaa.RandomColorsBinaryImageColorizer( color_true=10, color_false=20)) assert isinstance(aug.alpha, iap.Deterministic) assert isinstance(aug.hysteresis_thresholds, iap.Choice) assert isinstance(aug.sobel_kernel_size, iap.Choice) assert isinstance(aug.colorizer, iaa.RandomColorsBinaryImageColorizer) assert np.isclose(aug.alpha.value, 0.2) assert aug.hysteresis_thresholds.a == [0, 1, 2] assert isinstance(aug.sobel_kernel_size, iap.Choice) assert aug.sobel_kernel_size.a == [3, 5] assert isinstance(aug.colorizer.color_true, iap.Deterministic) assert isinstance(aug.colorizer.color_false, iap.Deterministic) assert aug.colorizer.color_true.value == 10 assert aug.colorizer.color_false.value == 20
def test_colorize__one_channel(self): colorizer = iaa.RandomColorsBinaryImageColorizer( color_true=100, color_false=10) random_state = iarandom.RNG(42) # input image has shape (H,W,1) image = np.zeros((5, 5, 1), dtype=np.uint8) image[:, 0:3, :] = 255 image_binary = np.zeros((5, 5), dtype=bool) image_binary[:, 0:3] = True image_color = colorizer.colorize( image_binary, image, nth_image=0, random_state=random_state) assert image_color.ndim == 3 assert image_color.shape[-1] == 1 assert np.all(image_color[image_binary] == 100) assert np.all(image_color[~image_binary] == 10)
def test__draw_samples(self): class _ListSampler(iap.StochasticParameter): def __init__(self, offset): super(_ListSampler, self).__init__() self.offset = offset self.last_random_state = None def _draw_samples(self, size, random_state=None): assert size == (3, ) self.last_random_state = random_state return np.uint8([0, 1, 2]) + self.offset colorizer = iaa.RandomColorsBinaryImageColorizer( color_true=_ListSampler(0), color_false=_ListSampler(1)) random_state = iarandom.RNG(42) color_true, color_false = colorizer._draw_samples(random_state) assert np.array_equal(color_true, [0, 1, 2]) assert np.array_equal(color_false, [1, 2, 3]) assert colorizer.color_true.last_random_state.equals(random_state) assert colorizer.color_false.last_random_state.equals(random_state)
def chapter_augmenters_canny(): fn_start = "edges/canny" aug = iaa.Canny() run_and_save_augseq(fn_start + ".jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(4 * 2)], cols=4, rows=2) aug = iaa.Canny(alpha=(0.0, 0.5)) run_and_save_augseq(fn_start + "_alpha.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(4 * 2)], cols=4, rows=2) aug = iaa.Canny(alpha=(0.0, 0.5), colorizer=iaa.RandomColorsBinaryImageColorizer( color_true=255, color_false=0)) run_and_save_augseq(fn_start + "_alpha_white_on_black.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(4 * 2)], cols=4, rows=2) aug = iaa.Canny(alpha=(0.5, 1.0), sobel_kernel_size=[3, 7]) run_and_save_augseq(fn_start + "_sobel_kernel_size.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(4 * 2)], cols=4, rows=2) aug = iaa.Alpha((0.0, 1.0), iaa.Canny(alpha=1), iaa.MedianBlur(13)) run_and_save_augseq(fn_start + "_alpha_median_blur.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(4 * 2)], cols=4, rows=2)
def test_colorize__four_channels(self): colorizer = iaa.RandomColorsBinaryImageColorizer( color_true=100, color_false=10) random_state = iarandom.RNG(42) # input image has shape (H,W,4) image = np.zeros((5, 5, 4), dtype=np.uint8) image[:, 0:3, 0:3] = 255 image[:, 1:4, 3] = 123 # set some content for alpha channel image_binary = np.zeros((5, 5), dtype=bool) image_binary[:, 0:3] = True image_color = colorizer.colorize( image_binary, image, nth_image=0, random_state=random_state) assert image_color.ndim == 3 assert image_color.shape[-1] == 4 assert np.all(image_color[image_binary, 0:3] == 100) assert np.all(image_color[~image_binary, 0:3] == 10) # alpha channel must have been kept untouched assert np.all(image_color[:, :, 3:4] == image[:, :, 3:4])
aug50 = iaa.UniformColorQuantizationToNBits() aug51 = iaa.GammaContrast((0.5, 2.0), per_channel=True) aug52 = iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6), per_channel=True) aug53 = iaa.LogContrast(gain=(0.6, 1.4), per_channel=True) aug54 = iaa.LinearContrast((0.4, 1.6), per_channel=True) # aug55 = iaa.AllChannelsCLAHE(clip_limit=(1, 10), per_channel=True) aug56 = iaa.Alpha((0.0, 1.0), iaa.AllChannelsHistogramEqualization()) aug57 = iaa.HistogramEqualization( from_colorspace=iaa.HistogramEqualization.BGR, to_colorspace=iaa.HistogramEqualization.HSV) aug58 = iaa.DirectedEdgeDetect(alpha=(0.0, 0.5), direction=(0.0, 0.5)) aug59 = iaa.Canny( alpha=(0.0, 0.3), colorizer=iaa.RandomColorsBinaryImageColorizer( color_true=255, color_false=0 ) ) def aug_imgaug(aug, image): image2 = image.copy() image2 = np.expand_dims(image2, axis=0) images_aug = aug(images = image2) return images_aug class FaceEmbeddings(): """Class to load model and run inference.""" INPUT_TENSOR_NAME = 'input_image:0'