Beispiel #1
0
    def test_border_mode(mode, skimage_mode):
        img = theano.shared(astronaut_stacked[np.newaxis])
        sigma = 3
        theano_blur = gaussian_filter_2d(img, sigma, border_mode=mode)
        blur = theano_blur.eval()
        assert blur.shape == (1, 2, 64, 64)
        blur = blur[0]
        expected = skimage.filters.gaussian_filter(
            astronaut, sigma, mode=skimage_mode)
        expected_transposed = skimage.filters.gaussian_filter(
            astronaut.T, sigma, mode=skimage_mode)
        np.testing.assert_allclose(blur[0], expected, rtol=0.001, atol=0.002)
        np.testing.assert_allclose(blur[1], expected_transposed,
                                   rtol=0.001, atol=0.002)
        plt.subplot(221)
        plt.imshow(blur[0], cmap='gray')
        plt.subplot(222)
        plt.imshow(expected, cmap='gray')

        plt.subplot(223)
        plt.imshow(blur[1], cmap='gray')
        plt.subplot(224)
        plt.imshow(expected_transposed, cmap='gray')

        plt_save_and_maybe_show("test_gaussian_blur_2d_{}.png".format(mode))
Beispiel #2
0
def test_gaussian_filter_2d_variable_sigma(astronaut):
    astronaut = astronaut[::2, ::2]
    astronaut_stacked = np.stack([astronaut, astronaut.T])
    astronaut_stacked = np.stack([astronaut,
                                  astronaut[:, ::-1],
                                  astronaut[::-1, :],
                                  astronaut[::-1, ::-1]])
    bs = len(astronaut_stacked)
    img = theano.shared(astronaut_stacked[:, np.newaxis])
    sigmas = np.array([3, 1, 2, 0.5], dtype=np.float32)
    sigmas_shared = theano.shared(sigmas)
    theano_blur = gaussian_filter_2d_variable_sigma(img, sigmas_shared,
                                                    border_mode='zero')
    blur = theano_blur.eval()
    assert blur.shape == (bs, 1, 64, 64)
    blur = blur.reshape(bs, 64, 64)
    r, c = 4, 2
    for i, (sigma, astro) in enumerate(zip(sigmas, astronaut_stacked)):
        expected = skimage.filters.gaussian_filter(astro,
                                                   float(sigma), mode='constant')

        np.testing.assert_allclose(blur[i], expected, rtol=0.01, atol=0.02)

        plt.subplot(r, c, 2*i+1)
        plt.imshow(blur[0], cmap='gray')
        plt.subplot(r, c, 2*(i+1))
        plt.imshow(expected, cmap='gray')

    plt_save_and_maybe_show("test_gaussian_blur_2d_variable_sigmas.png")
Beispiel #3
0
def test_gaussian_filter_2d_variable_sigma(astronaut):
    astronaut = astronaut[::2, ::2]
    astronaut_stacked = np.stack([astronaut, astronaut.T])
    astronaut_stacked = np.stack([
        astronaut, astronaut[:, ::-1], astronaut[::-1, :],
        astronaut[::-1, ::-1]
    ])
    bs = len(astronaut_stacked)
    img = theano.shared(astronaut_stacked[:, np.newaxis])
    sigmas = np.array([3, 1, 2, 0.5], dtype=np.float32)
    sigmas_shared = theano.shared(sigmas)
    theano_blur = gaussian_filter_2d_variable_sigma(img,
                                                    sigmas_shared,
                                                    border_mode='zero')
    blur = theano_blur.eval()
    assert blur.shape == (bs, 1, 64, 64)
    blur = blur.reshape(bs, 64, 64)
    r, c = 4, 2
    for i, (sigma, astro) in enumerate(zip(sigmas, astronaut_stacked)):
        expected = skimage.filters.gaussian_filter(astro,
                                                   float(sigma),
                                                   mode='constant')

        np.testing.assert_allclose(blur[i], expected, rtol=0.01, atol=0.02)

        plt.subplot(r, c, 2 * i + 1)
        plt.imshow(blur[0], cmap='gray')
        plt.subplot(r, c, 2 * (i + 1))
        plt.imshow(expected, cmap='gray')

    plt_save_and_maybe_show("test_gaussian_blur_2d_variable_sigmas.png")
Beispiel #4
0
    def test_border_mode(mode, skimage_mode):
        img = theano.shared(astronaut_stacked[np.newaxis])
        sigma = 3
        theano_blur = gaussian_filter_2d(img, sigma, border_mode=mode)
        blur = theano_blur.eval()
        assert blur.shape == (1, 2, 64, 64)
        blur = blur[0]
        expected = skimage.filters.gaussian_filter(astronaut,
                                                   sigma,
                                                   mode=skimage_mode)
        expected_transposed = skimage.filters.gaussian_filter(
            astronaut.T, sigma, mode=skimage_mode)
        np.testing.assert_allclose(blur[0], expected, rtol=0.001, atol=0.002)
        np.testing.assert_allclose(blur[1],
                                   expected_transposed,
                                   rtol=0.001,
                                   atol=0.002)
        plt.subplot(221)
        plt.imshow(blur[0], cmap='gray')
        plt.subplot(222)
        plt.imshow(expected, cmap='gray')

        plt.subplot(223)
        plt.imshow(blur[1], cmap='gray')
        plt.subplot(224)
        plt.imshow(expected_transposed, cmap='gray')

        plt_save_and_maybe_show("test_gaussian_blur_2d_{}.png".format(mode))
Beispiel #5
0
def test_tile():
    n = 128
    images = []

    height, width = 16, 16
    for i in range(n):
        color = hsv_to_rgb(random.random(), 1, 1)
        image = np.zeros((3, 16, 16))
        for c in range(len(color)):
            image[c] = color[c]
        images.append(image)

    number = 20
    tiled = tile(images, columns_must_be_multiple_of=number)

    rows = tiled.shape[1] // height
    cols = tiled.shape[2] // width
    assert cols % number == 0
    for r in range(rows):
        for c in range(cols):
            idx = cols*r + c
            ri = r*height
            ci = c*width
            subimage = tiled[:, ri:ri+height, ci:ci+height]
            if idx < len(images):
                np.testing.assert_allclose(subimage, images[idx])

    fig = plt.figure()
    h_w_rgb = np.zeros((tiled.shape[1], tiled.shape[2], tiled.shape[0]))
    for i in range(3):
        h_w_rgb[:, :, i] = tiled[i]
    plt.imshow(h_w_rgb)
    plt_save_and_maybe_show("test_tile.png")
    plt.close(fig)
Beispiel #6
0
def test_upsample(astronaut):
    x = theano.shared(astronaut[np.newaxis, np.newaxis])
    x_up = upsample(resize_interpolate(x, scale=0.5)).eval()
    plt.subplot(121)
    plt.imshow(x.get_value()[0, 0, :])
    plt.subplot(122)
    plt.imshow(x_up[0, 0, :])
    plt_save_and_maybe_show("test_upsample.png")
Beispiel #7
0
def test_transform_high_frequencies(astronaut):
    layer = HighFrequencies(sigma=2, nb_steps=5)
    layer.build(astronaut.shape)
    high = layer(theano.shared(astronaut)).eval()
    plt.subplot(121)
    plt.imshow(astronaut[0, 0], cmap='gray')
    plt.subplot(122)
    plt.imshow(high[0, 0], cmap='gray')
    plt_save_and_maybe_show("transfrom/high_frequencies.png")
Beispiel #8
0
def test_sobel(astronaut):
    x = theano.shared(astronaut[np.newaxis, np.newaxis].astype(np.float32))
    sobel_x, sobel_y = [s.eval() for s in sobel(x)]
    plt.subplot(131)
    plt.imshow(x.get_value()[0, 0, :])
    plt.subplot(132)
    plt.imshow(sobel_x[0, 0, :])
    plt.subplot(133)
    plt.imshow(sobel_y[0, 0, :])
    plt_save_and_maybe_show("test_sobel.png")
    plt.show()
Beispiel #9
0
def test_sobel(astronaut):
    x = theano.shared(astronaut[np.newaxis, np.newaxis].astype(np.float32))
    sobel_x, sobel_y = [s.eval() for s in sobel(x)]
    plt.subplot(131)
    plt.imshow(x.get_value()[0, 0, :])
    plt.subplot(132)
    plt.imshow(sobel_x[0, 0, :])
    plt.subplot(133)
    plt.imshow(sobel_y[0, 0, :])
    plt_save_and_maybe_show("test_sobel.png")
    plt.show()
Beispiel #10
0
def test_util_pyramid_gaussian(astronaut):
    max_layer = np.log2(astronaut.shape[-1])
    astronauts = theano.shared(np.repeat(astronaut, 64, axis=0))
    pyr_gaus = list(pyramid_gaussian(astronauts, max_layer))
    assert len(pyr_gaus) == max_layer
    gauss_fn = theano.function([], pyr_gaus)
    for i, gaus in enumerate(gauss_fn()):
        plt.imshow(gaus[3, 0], cmap='gray')
        plt_save_and_maybe_show("utils/gaussian/gauss_{}.png".format(i))

    assert pyr_gaus[-1].shape.eval()[-1] == \
        astronaut.shape[-1] // 2**(max_layer-1)
Beispiel #11
0
def test_util_rotate_by_multiple_of_90(batch):
    n = len(batch)
    th_batch = K.variable(batch)
    rots = K.variable(np.array([0, 1, 2, 3]))
    rotated = rotate_by_multiple_of_90(th_batch, rots).eval()
    for i in range(n):
        plt.subplot(131)
        plt.imshow(batch[i, 0])
        plt.subplot(132)
        plt.imshow(rotated[i, 0])
        plt.subplot(133)
        plt.imshow(np.rot90(batch[i, 0], k=i))
        plt_save_and_maybe_show("utils/rotate_{}.png".format(i))

    assert rotated.shape == batch.shape
    for i in range(n):
        assert (rotated[i, 0] == np.rot90(batch[i, 0], k=i)).all(), i
Beispiel #12
0
def test_util_blending_pyramid(blend_images, blend_pyramid_fn):
    orange, apple, mask = blend_images
    print(orange.shape)
    print(mask.shape)
    img = blend_pyramid_fn([orange, apple, mask])[0]
    print(img.shape)
    w = orange.shape[-1]
    hard_add = np.concatenate([apple[:, :, :, :w/2],
                               orange[:, :, :, w/2:]], axis=3)
    plt.subplot(221)
    plt.imshow(orange[0, 0], cmap='gray')
    plt.subplot(222)
    plt.imshow(apple[0, 0], cmap='gray')
    plt.subplot(223)
    plt.imshow(hard_add[0, 0], cmap='gray')
    plt.subplot(224)
    plt.imshow(img[0, 0], cmap='gray')
    plt_save_and_maybe_show("utils/blending.png")