Esempio n. 1
0
def grid(images):

    amount = len(images)
    cols = math.ceil(math.sqrt(amount))
    rows = math.ceil(amount / cols)

    # get imgs as array of Image.Image objects
    imgs = utils.images(images)

    strips = []
    for i in range(rows):
        lower = cols * i
        upper = lower + cols
        if upper >= amount:
            upper = amount
            sham = Image.new('RGB', imgs[upper - 1].size)
            dummies = [sham] * (cols * rows - amount)
            strip_imgs = imgs[lower:upper] + dummies
        else:
            strip_imgs = images[lower:upper]

        strips.append(horizontal(strip_imgs))

    grid_img = vertical(strips)

    return grid_img
Esempio n. 2
0
def vertical(images):

    # get imgs as array of Image.Image objects
    imgs = utils.images(images)

    # width of the strip is minimal width of all images
    width = min([i.width for i in imgs])
    resized = [resize.resize(img, width, None) for img in imgs]

    # height of the strip is sum of height of all images
    height = sum([img.height for img in resized])

    # create new image for strip
    strip = Image.new('RGB', (width, height))

    # stack images by offseting them by height of previous
    y_offset = 0
    for i in resized:
        strip.paste(i, (0, y_offset))
        y_offset += i.height

    return strip
Esempio n. 3
0
def horizontal(images):

    # get imgs as array of Image.Image objects
    imgs = utils.images(images)

    # height of the strip is minimal height of all images
    height = min([i.height for i in imgs])
    resized = [resize.resize(img, None, height) for img in imgs]

    # width of the strip is sum of width of all images
    width = sum([img.width for img in resized])

    # create new image for strip
    strip = Image.new('RGB', (width, height))

    # stack images by offseting them by width of previous
    x_offset = 0
    for i in resized:
        strip.paste(i, (x_offset, 0))
        x_offset += i.width

    return strip
        title='Test Set Visualization',
        xlabel='Latent dim 1',
        ylabel='Latent dim 2')

print('\nCorresponding mapping coordinates in the latent space - one image per digit:\n')
encoded_x_test = encoder.predict(x_test)
digits = {}
i = 0
while len(digits) < 10:
    digits[y_test[i]] = encoded_x_test[i]
    i += 1

ut.output('g.c', pd.DataFrame(digits))

# (d)
z_sample = np.array([[-2.5, 0.55]])
decoded_x = generator.predict(z_sample)
ut.image(decoded_x)
ut.plt_save('g.d')

# (e)
source, target = digits[3], digits[5]
(x1, y1), (x2, y2) = source, target
a = (y2 - y1) / (x2 - x1)
b = y1 - (a * x1)
f = lambda _x_: a * _x_ + b
x_samples = np.linspace(x1, x2, num=10)
samples = [np.array([[_x, f(_x)]]) for _x in x_samples]
ut.images(map(generator.predict, samples), map(str, x_samples), n_cols=3)
ut.plt_save('g.e')