tnn1t1s / colorpunx

punk rock colors
Apache License 2.0
5 stars 4 forks source link

Walk The Line #16

Closed jmsdnns closed 2 years ago

jmsdnns commented 2 years ago

Could be cool to use some of the animation code in ar.py to explore the network by creating a gif stream of some algorithmic walk.

To start, maybe a straight line through.

[-1, -1, -1, -1]
[0, 0, 0, 0]
[1, 1, 1, 1]

Or, walk down a list of all permutations, with some kind of step.

[-1, -1, -1, -1],
[-1, -1, -1, 1],
[-1, -1, 0, -1],
[-1, -1, 1, -1],
...
[1, 1, 1, -1],
[1, 1, 1, 1]
jmsdnns commented 2 years ago

First experiement, is a function that walks in a straight line between two positions.

def dim_walk(start, end, step=1):
    steps = [s + start[0] for s in np.arange(0, end[0] - start[0] + 1, step)]
    dims = len(start)
    for i in steps:
        coords = [i]
        for j in range(1, dims):
            r = random.random()
            coords = coords + [float(i) + r]
        yield coords

this snippet requires the definition of a dims value, which I'll discuss in a moment

im = plt.imshow(decoder.predict([[-1, 1, -1, 1]])[0])

def update_dims(j):
    dim = dims.__next__()
    z_s = np.array([dim])
    reconst_img = decoder.predict(z_s)
    im.set_array(reconst_img[0])
    return [im]

ani = animation.FuncAnimation(
    fig, update_dims, frames=range(40), interval=48
)

writer = animation.PillowWriter(fps=5)
ani.save("ani.gif", writer=writer) 

Here is one way to create that dims value

dims = dim_walk([-20, -20, -20, -20], [20, 20, 20, 20])

That walking function produces this image dim_walk(-20, 20)

I modified the number of frames and the fps to accomodate a lot more values, using the following dims

dims = dim_walk([-20, -20, -20, -20], [20, 20, 20, 20], 0.05)

That produced this gif dim_walk (-20, 20, 0 05) 400 frames

jmsdnns commented 2 years ago

I also tried creating a function that would function like a multi-dimensional range function. Or, it created all the permutations between two values.

def dim_range(start, end, z_dim, step=1):
    if z_dim == 0:
        raise StopIteration
    dims = []
    for f in np.arange(start, end, step):
        if z_dim == 1:
            yield [f]
        else:
            for ld in dim_range(start, end, (z_dim - 1), step=step):
                yield [f] + ld
    return dims

This uses the same animation code as the post above.

dims = dim_range(-13, 13, 4, 7)

dim_range (13, 13,4, 7)

dims = dim_range(-10, 10, 4, 5)

dim_range (-10, 10,  4, 5)

dims = dim_range(-20, 20, 4)

dim_range (-20, 20, 4)

jmsdnns commented 2 years ago

And finally, I tried just generating a bunch of random pictures, but I then sort them before putting them in a gif

n_to_show = 50
example_idxs = np.random.choice(range(len(X_test)), n_to_show)
example_images = X_test[example_idxs]
example_zs = encoder.predict(example_images)
example_images = decoder.predict(example_zs)
sorted_images = np.sort(example_images, axis=0)

The animation code for this batch is slightly different.

def update_random(j):
    im.set_array(sorted_images[j])
    return [im]

ani = animation.FuncAnimation(
    fig, update_random, frames=range(n_to_show), interval=48
)

writer = animation.PillowWriter(fps=5)
ani.save("ani.gif", writer=writer) 

sorted_random

I have generated this several times and the path generally looks the same each time.

sorted_random 1