Exemple #1
0
def main():
    nb_rows = 8
    nb_cols = 8
    h, w = (128, 128)
    sample_size = 128

    noise_gens = [
        iap.SimplexNoise(),
        iap.FrequencyNoise(exponent=-4, size_px_max=sample_size, upscale_method="cubic"),
        iap.FrequencyNoise(exponent=-2, size_px_max=sample_size, upscale_method="cubic"),
        iap.FrequencyNoise(exponent=0, size_px_max=sample_size, upscale_method="cubic"),
        iap.FrequencyNoise(exponent=2, size_px_max=sample_size, upscale_method="cubic"),
        iap.FrequencyNoise(exponent=4, size_px_max=sample_size, upscale_method="cubic"),
        iap.FrequencyNoise(exponent=(-4, 4), size_px_max=(4, sample_size), upscale_method=["nearest", "linear", "cubic"]),
        iap.IterativeNoiseAggregator(
            other_param=iap.FrequencyNoise(exponent=(-4, 4), size_px_max=(4, sample_size), upscale_method=["nearest", "linear", "cubic"]),
            iterations=(1, 3),
            aggregation_method=["max", "avg"]
        ),
        iap.IterativeNoiseAggregator(
            other_param=iap.Sigmoid(
                iap.FrequencyNoise(exponent=(-4, 4), size_px_max=(4, sample_size), upscale_method=["nearest", "linear", "cubic"]),
                threshold=(-10, 10),
                activated=0.33,
                mul=20,
                add=-10
            ),
            iterations=(1, 3),
            aggregation_method=["max", "avg"]
        )
    ]

    samples = [[] for _ in range(len(noise_gens))]
    for _ in range(nb_rows * nb_cols):
        for i, noise_gen in enumerate(noise_gens):
            samples[i].append(noise_gen.draw_samples((h, w)))

    rows = [np.hstack(row) for row in samples]
    grid = np.vstack(rows)
    misc.imshow((grid*255).astype(np.uint8))

    images = [ia.quokka_square(size=(128, 128)) for _ in range(16)]
    seqs = [
        iaa.SimplexNoiseAlpha(first=iaa.EdgeDetect(1.0)),
        iaa.SimplexNoiseAlpha(first=iaa.EdgeDetect(1.0), per_channel=True),
        iaa.FrequencyNoiseAlpha(first=iaa.EdgeDetect(1.0)),
        iaa.FrequencyNoiseAlpha(first=iaa.EdgeDetect(1.0), per_channel=True)
    ]
    images_aug = []

    for seq in seqs:
        images_aug.append(np.hstack(seq.augment_images(images)))
    images_aug = np.vstack(images_aug)
    misc.imshow(images_aug)
Exemple #2
0
def chapter_alpha_masks_sigmoid():
    # -----------------------------------------
    # Sigmoid varying on/off
    # -----------------------------------------
    import imgaug as ia
    from imgaug import parameters as iap

    seed = 1
    ia.seed(seed)

    masks = []

    for activated in [False, True]:
        noise = iap.Sigmoid.create_for_noise(other_param=iap.FrequencyNoise(
            exponent=(-4.0, 4.0), upscale_method="linear"),
                                             activated=activated)

        row = [
            noise.draw_samples((64, 64),
                               random_state=ia.new_random_state(seed + 1 + i))
            for i in range(16)
        ]
        row = np.hstack(row)
        row = np.tile(row[:, :, np.newaxis], (1, 1, 3))
        row = (row * 255).astype(np.uint8)
        row = np.pad(row, ((0, 0), (90, 0), (0, 0)),
                     mode="constant",
                     constant_values=255)
        row = ia.draw_text(row,
                           y=17,
                           x=2,
                           text="activated=\n%s" % (activated, ),
                           size=14,
                           color=[0, 0, 0])
        masks.append(row)

    # ------------

    save("alpha", "sigmoid_vary_activated.jpg", grid(masks, cols=1, rows=2))

    # -----------------------------------------
    # Sigmoid varying on/off
    # -----------------------------------------
    import imgaug as ia
    from imgaug import parameters as iap

    seed = 1
    ia.seed(seed)

    masks = []
    nb_rows = 3

    class ConstantNoise(iap.StochasticParameter):
        def __init__(self, noise, seed):
            super(ConstantNoise, self).__init__()
            self.noise = noise
            self.seed = seed

        def _draw_samples(self, size, random_state):
            return self.noise.draw_samples(size,
                                           random_state=ia.new_random_state(
                                               self.seed))

    for rowidx in range(nb_rows):
        row = []
        for tidx, threshold in enumerate(np.linspace(-10.0, 10.0, 10)):
            noise = iap.Sigmoid.create_for_noise(other_param=ConstantNoise(
                iap.FrequencyNoise(exponent=(-4.0, 4.0),
                                   upscale_method="linear"),
                seed=seed + 100 + rowidx),
                                                 activated=True,
                                                 threshold=threshold)

            cell = noise.draw_samples(
                (64, 64), random_state=ia.new_random_state(seed + tidx))
            cell = np.tile(cell[:, :, np.newaxis], (1, 1, 3))
            cell = (cell * 255).astype(np.uint8)
            if rowidx == 0:
                cell = np.pad(cell, ((20, 0), (0, 0), (0, 0)),
                              mode="constant",
                              constant_values=255)
                cell = ia.draw_text(cell,
                                    y=2,
                                    x=15,
                                    text="%.1f" % (threshold, ),
                                    size=14,
                                    color=[0, 0, 0])
            row.append(cell)
        row = np.hstack(row)
        masks.append(row)

    gridarr = np.vstack(masks)
    # ------------

    save("alpha", "sigmoid_vary_threshold.jpg", gridarr)
Exemple #3
0
def chapter_alpha_masks_iterative():
    # -----------------------------------------
    # IterativeNoiseAggregator varying number of iterations
    # -----------------------------------------
    import imgaug as ia
    from imgaug import parameters as iap

    seed = 1
    ia.seed(seed)

    masks = []
    iterations_all = [1, 2, 3, 4]

    for iterations in iterations_all:
        noise = iap.IterativeNoiseAggregator(other_param=iap.FrequencyNoise(
            exponent=(-4.0, 4.0), upscale_method=["linear", "nearest"]),
                                             iterations=iterations,
                                             aggregation_method="max")

        row = [
            noise.draw_samples((64, 64),
                               random_state=ia.new_random_state(seed + 1 + i))
            for i in range(16)
        ]
        row = np.hstack(row)
        row = np.tile(row[:, :, np.newaxis], (1, 1, 3))
        row = (row * 255).astype(np.uint8)
        row = np.pad(row, ((0, 0), (50, 0), (0, 0)),
                     mode="constant",
                     constant_values=255)
        row = ia.draw_text(row,
                           y=24,
                           x=2,
                           text="%d iter." % (iterations, ),
                           size=14,
                           color=[0, 0, 0])
        masks.append(row)

    # ------------

    save("alpha", "iterative_vary_iterations.jpg",
         grid(masks, cols=1, rows=len(iterations_all)))

    # -----------------------------------------
    # IterativeNoiseAggregator varying methods
    # -----------------------------------------
    import imgaug as ia
    from imgaug import parameters as iap

    seed = 1
    ia.seed(seed)

    iterations_all = [1, 2, 3, 4, 5, 6]
    methods = ["min", "avg", "max"]
    cell_idx = 0
    rows = []

    for method_idx, method in enumerate(methods):
        row = []
        for iterations in iterations_all:
            noise = iap.IterativeNoiseAggregator(
                other_param=iap.FrequencyNoise(
                    exponent=-2.0,
                    size_px_max=32,
                    upscale_method=["linear", "nearest"]),
                iterations=iterations,
                aggregation_method=method)

            cell = noise.draw_samples(
                (64, 64),
                random_state=ia.new_random_state(seed + 1 + method_idx))
            cell = np.tile(cell[:, :, np.newaxis], (1, 1, 3))
            cell = (cell * 255).astype(np.uint8)

            if iterations == 1:
                cell = np.pad(cell, ((0, 0), (40, 0), (0, 0)),
                              mode="constant",
                              constant_values=255)
                cell = ia.draw_text(cell,
                                    y=27,
                                    x=2,
                                    text="%s" % (method, ),
                                    size=14,
                                    color=[0, 0, 0])
            if method_idx == 0:
                cell = np.pad(cell, ((20, 0), (0, 0), (0, 0)),
                              mode="constant",
                              constant_values=255)
                cell = ia.draw_text(cell,
                                    y=0,
                                    x=12 + 40 * (iterations == 1),
                                    text="%d iter." % (iterations, ),
                                    size=14,
                                    color=[0, 0, 0])
            cell = np.pad(cell, ((0, 1), (0, 1), (0, 0)),
                          mode="constant",
                          constant_values=255)

            row.append(cell)
            cell_idx += 1
        rows.append(np.hstack(row))
    gridarr = np.vstack(rows)

    # ------------

    save("alpha", "iterative_vary_methods.jpg", gridarr)