Tensor Flow to create a useless images

To learn how to encode a simple image and a GIF

Import needed for Tensorflow


In [1]:
import numpy as np
import tensorflow as tf

Import needed for Jupiter


In [2]:
%matplotlib notebook
import matplotlib
import matplotlib.pyplot as plt

from IPython.display import Image

A function to save a picture


In [3]:
def write_png(tensor, name):
    casted_to_uint8 = tf.cast(tensor, tf.uint8)
    converted_to_png = tf.image.encode_png(casted_to_uint8)
    f = open(name, "wb+")
    f.write(converted_to_png.eval())
    f.close()

A function to draw the cost function in Jupyter


In [4]:
class CostTrace:
    """A simple example class"""
    def __init__(self):
        self.cost_array = []
    def log(self, cost):
        self.cost_array.append(cost)
    def draw(self):
        plt.figure(figsize=(12,5))
        plt.plot(range(len(self.cost_array)), self.cost_array, label='cost')
        plt.legend()
        plt.yscale('log')
        plt.show()

Create some random pictures

Encode the input (a number)

This example convert the number to a binary representation


In [5]:
# Init size
width = 100
height = 100
RGB = 3
shape = [height,width, RGB]

# Create the generated tensor as a variable
rand_uniform = tf.random_uniform(shape, minval=0, maxval=255, dtype=tf.float32)
generated = tf.Variable(rand_uniform)

#define the cost function
c_mean = tf.reduce_mean(tf.pow(generated,2)) # we want a low mean
c_max = tf.reduce_max(generated) # we want a low max
c_min = -tf.reduce_min(generated) # we want a high mix

c_diff = 0
for i in range(0,height-1, 1):
    line1 = tf.gather(generated, i,)
    line2 = tf.gather(generated, i+1)
    c_diff += tf.reduce_mean(tf.pow(line1-line2-30, 2)) # to force a gradient


cost = c_mean + c_max + c_min + c_diff
#cost = c_mean + c_diff
print ('cost defined')

train_op = tf.train.GradientDescentOptimizer(0.5).minimize(cost, var_list=[generated])
print ('train_op defined')

# Initializing the variables
init = tf.initialize_all_variables()
print ('variables initialiazed defined')    

# Launch the graph
with tf.Session() as sess:
    sess.run(init)
    print ('init done')
    cost_trace = CostTrace()
    for epoch in range(0,10000):
        sess.run(train_op)
        if (epoch % 100 == 0):
            c = cost.eval()
            print ('epoch', epoch,'cost' ,c, c_mean.eval(), c_min.eval(), c_max.eval(), c_diff.eval())
            cost_trace.log(c)
            write_png(generated, "generated{:06}.png".format(epoch))
print ('all done')


cost defined
train_op defined
variables initialiazed defined
init done
('epoch', 0, 'cost', 1150368.8, 21663.955, -0.36533859, 254.74608, 1128450.4)
('epoch', 100, 'cost', 291412.19, 18368.645, -15.115782, 237.85574, 272820.81)
('epoch', 200, 'cost', 165056.8, 17544.734, -21.837952, 232.34003, 147301.56)
('epoch', 300, 'cost', 134189.97, 17152.645, -26.307716, 228.55885, 116835.08)
('epoch', 400, 'cost', 122183.33, 16887.836, -28.327135, 226.68208, 105097.13)
('epoch', 500, 'cost', 115945.95, 16679.002, -29.263021, 225.22833, 99070.977)
('epoch', 600, 'cost', 112126.48, 16501.117, -29.578547, 224.43327, 95430.516)
('epoch', 700, 'cost', 109534.09, 16342.718, -29.32696, 223.73236, 92996.969)
('epoch', 800, 'cost', 107644.77, 16197.697, -28.789602, 223.4454, 91252.414)
('epoch', 900, 'cost', 106192.88, 16062.438, -28.001347, 223.4292, 89935.016)
('epoch', 1000, 'cost', 105030.01, 15934.678, -27.431665, 223.69884, 88899.062)
('epoch', 1100, 'cost', 104067.64, 15812.794, -26.621958, 223.99297, 88057.477)
('epoch', 1200, 'cost', 103249.95, 15695.761, -25.695034, 224.54492, 87355.336)
('epoch', 1300, 'cost', 102539.81, 15582.763, -24.61548, 225.11725, 86756.547)
('epoch', 1400, 'cost', 101911.8, 15473.235, -23.434679, 225.69951, 86236.297)
('epoch', 1500, 'cost', 101347.98, 15366.707, -22.166765, 226.27254, 85777.164)
('epoch', 1600, 'cost', 100835.41, 15262.867, -20.806252, 226.86876, 85366.484)
('epoch', 1700, 'cost', 100364.26, 15161.412, -19.512392, 227.43944, 84994.922)
('epoch', 1800, 'cost', 99927.562, 15062.138, -18.11471, 228.08459, 84655.453)
('epoch', 1900, 'cost', 99519.438, 14964.866, -16.761795, 228.65022, 84342.68)
('epoch', 2000, 'cost', 99135.828, 14869.39, -15.369679, 229.35928, 84052.445)
('epoch', 2100, 'cost', 98772.875, 14775.653, -13.963433, 229.97066, 83781.211)
('epoch', 2200, 'cost', 98428.086, 14683.482, -12.628812, 230.67967, 83526.555)
('epoch', 2300, 'cost', 98099.062, 14592.864, -11.253049, 231.3035, 83286.148)
('epoch', 2400, 'cost', 97783.914, 14503.649, -9.8661451, 231.93494, 83058.195)
('epoch', 2500, 'cost', 97481.211, 14415.811, -8.4812927, 232.56212, 82841.32)
('epoch', 2600, 'cost', 97189.602, 14329.222, -7.0530643, 233.19951, 82634.234)
('epoch', 2700, 'cost', 96907.992, 14243.912, -5.7030582, 233.83554, 82435.945)
('epoch', 2800, 'cost', 96635.531, 14159.778, -4.2642684, 234.52971, 82245.484)
('epoch', 2900, 'cost', 96371.211, 14076.809, -2.92928, 235.12628, 82062.203)
('epoch', 3000, 'cost', 96114.656, 13994.944, -1.4982566, 235.75494, 81885.453)
('epoch', 3100, 'cost', 95864.828, 13914.136, -0.17798696, 236.37154, 81714.5)
('epoch', 3200, 'cost', 95621.57, 13834.363, 1.1879566, 236.96432, 81549.055)
('epoch', 3300, 'cost', 95384.234, 13755.629, 2.4913745, 237.55823, 81388.555)
('epoch', 3400, 'cost', 95152.531, 13677.86, 3.8178, 238.15701, 81232.695)
('epoch', 3500, 'cost', 94925.984, 13601.029, 5.1785798, 238.6418, 81081.133)
('epoch', 3600, 'cost', 94704.375, 13525.165, 6.4782324, 239.21033, 80933.523)
('epoch', 3700, 'cost', 94487.414, 13450.181, 7.790905, 239.75046, 80789.695)
('epoch', 3800, 'cost', 94274.766, 13376.086, 9.1006842, 240.30164, 80649.273)
('epoch', 3900, 'cost', 94066.234, 13302.894, 10.426006, 240.79126, 80512.125)
('epoch', 4000, 'cost', 93861.594, 13230.516, 11.669244, 241.33521, 80378.078)
('epoch', 4100, 'cost', 93660.703, 13159.001, 12.914554, 241.8109, 80246.977)
('epoch', 4200, 'cost', 93463.383, 13088.286, 14.186662, 242.35774, 80118.555)
('epoch', 4300, 'cost', 93269.344, 13018.374, 15.437316, 242.79968, 79992.734)
('epoch', 4400, 'cost', 93078.641, 12949.28, 16.719786, 243.28366, 79869.359)
('epoch', 4500, 'cost', 92890.953, 12880.897, 17.932627, 243.74199, 79748.383)
('epoch', 4600, 'cost', 92706.297, 12813.32, 19.186958, 244.16904, 79629.625)
('epoch', 4700, 'cost', 92524.406, 12746.473, 20.385881, 244.60582, 79512.945)
('epoch', 4800, 'cost', 92345.461, 12680.382, 21.612112, 245.06868, 79398.398)
('epoch', 4900, 'cost', 92169.031, 12615.004, 22.822504, 245.50629, 79285.703)
('epoch', 5000, 'cost', 91995.211, 12550.343, 24.020807, 245.93062, 79174.914)
('epoch', 5100, 'cost', 91823.812, 12486.374, 25.223736, 246.30487, 79065.906)
('epoch', 5200, 'cost', 91654.852, 12423.097, 26.355972, 246.74725, 78958.648)
('epoch', 5300, 'cost', 91488.156, 12360.511, 27.509201, 247.15604, 78852.984)
('epoch', 5400, 'cost', 91323.75, 12298.599, 28.68071, 247.52623, 78748.945)
('epoch', 5500, 'cost', 91161.539, 12237.346, 29.862875, 247.90227, 78646.43)
('epoch', 5600, 'cost', 91001.422, 12176.73, 30.978376, 248.33514, 78545.375)
('epoch', 5700, 'cost', 90843.352, 12116.776, 32.122364, 248.69653, 78445.758)
('epoch', 5800, 'cost', 90687.352, 12057.466, 33.204342, 249.12915, 78347.555)
('epoch', 5900, 'cost', 90533.219, 11998.775, 34.329216, 249.48636, 78250.625)
('epoch', 6000, 'cost', 90381.062, 11940.698, 35.441059, 249.88995, 78155.031)
('epoch', 6100, 'cost', 90230.695, 11883.233, 36.530693, 250.26273, 78060.672)
('epoch', 6200, 'cost', 90082.18, 11826.368, 37.653908, 250.65392, 77967.5)
('epoch', 6300, 'cost', 89935.32, 11770.112, 38.722553, 250.97285, 77875.516)
('epoch', 6400, 'cost', 89790.328, 11714.453, 39.796543, 251.37904, 77784.695)
('epoch', 6500, 'cost', 89646.945, 11659.339, 40.885002, 251.7234, 77695.0)
('epoch', 6600, 'cost', 89505.258, 11604.822, 41.961563, 252.09854, 77606.375)
('epoch', 6700, 'cost', 89365.125, 11550.89, 43.039661, 252.41971, 77518.773)
('epoch', 6800, 'cost', 89226.586, 11497.508, 44.096024, 252.78455, 77432.195)
('epoch', 6900, 'cost', 89089.578, 11444.661, 45.125507, 253.13783, 77346.656)
('epoch', 7000, 'cost', 88954.094, 11392.401, 46.201717, 253.4518, 77262.039)
('epoch', 7100, 'cost', 88820.094, 11340.646, 47.23938, 253.80269, 77178.406)
('epoch', 7200, 'cost', 88687.5, 11289.444, 48.270275, 254.09996, 77095.688)
('epoch', 7300, 'cost', 88556.398, 11238.786, 49.315536, 254.42761, 77013.867)
('epoch', 7400, 'cost', 88426.695, 11188.644, 50.361012, 254.761, 76932.93)
('epoch', 7500, 'cost', 88298.352, 11139.019, 51.400085, 255.1208, 76852.812)
('epoch', 7600, 'cost', 88171.258, 11089.895, 52.403118, 255.40544, 76773.555)
('epoch', 7700, 'cost', 88045.672, 11041.315, 53.429661, 255.72946, 76695.195)
('epoch', 7800, 'cost', 87921.234, 10993.184, 54.435051, 256.06729, 76617.547)
('epoch', 7900, 'cost', 87798.086, 10945.581, 55.444424, 256.35687, 76540.703)
('epoch', 8000, 'cost', 87676.242, 10898.464, 56.446461, 256.65323, 76464.68)
('epoch', 8100, 'cost', 87555.57, 10851.83, 57.454063, 256.93497, 76389.352)
('epoch', 8200, 'cost', 87436.156, 10805.672, 58.429543, 257.22189, 76314.836)
('epoch', 8300, 'cost', 87317.938, 10760.015, 59.41996, 257.5426, 76240.961)
('epoch', 8400, 'cost', 87200.844, 10714.818, 60.407455, 257.82193, 76167.797)
('epoch', 8500, 'cost', 87084.938, 10670.084, 61.373646, 258.11633, 76095.359)
('epoch', 8600, 'cost', 86970.164, 10625.795, 62.34409, 258.39481, 76023.633)
('epoch', 8700, 'cost', 86856.484, 10581.974, 63.322372, 258.6683, 75952.523)
('epoch', 8800, 'cost', 86743.953, 10538.612, 64.276237, 258.96649, 75882.094)
('epoch', 8900, 'cost', 86632.453, 10495.704, 65.22052, 259.23248, 75812.297)
('epoch', 9000, 'cost', 86521.984, 10453.218, 66.147057, 259.47098, 75743.148)
('epoch', 9100, 'cost', 86412.648, 10411.215, 67.115868, 259.74188, 75674.578)
('epoch', 9200, 'cost', 86304.328, 10369.585, 68.048515, 260.01822, 75606.672)
('epoch', 9300, 'cost', 86196.992, 10328.423, 68.984116, 260.25369, 75539.328)
('epoch', 9400, 'cost', 86090.711, 10287.69, 69.88681, 260.52335, 75472.609)
('epoch', 9500, 'cost', 85985.414, 10247.357, 70.805756, 260.80508, 75406.445)
('epoch', 9600, 'cost', 85881.016, 10207.435, 71.701012, 261.04126, 75340.836)
('epoch', 9700, 'cost', 85777.719, 10167.975, 72.623077, 261.27631, 75275.844)
('epoch', 9800, 'cost', 85675.312, 10128.883, 73.500504, 261.55905, 75211.367)
('epoch', 9900, 'cost', 85573.766, 10090.204, 74.40509, 261.76913, 75147.391)
all done

In [10]:
cost_trace.draw()



In [7]:
Image("generated000000.png")


Out[7]:

To create a GIF


In [8]:
from PIL import Image, ImageSequence
import glob, sys, os
os.chdir(".")
frames = []
for file in glob.glob("gene*.png"):
    print(file)
    im = Image.open(file)
    frames.append(im)

from images2gif import writeGif
writeGif("generated.gif", frames, duration=0.1)


generated000000.png
generated000100.png
generated000200.png
generated000300.png
generated000400.png
generated000500.png
generated000600.png
generated000700.png
generated000800.png
generated000900.png
generated001000.png
generated001100.png
generated001200.png
generated001300.png
generated001400.png
generated001500.png
generated001600.png
generated001700.png
generated001800.png
generated001900.png
generated002000.png
generated002100.png
generated002200.png
generated002300.png
generated002400.png
generated002500.png
generated002600.png
generated002700.png
generated002800.png
generated002900.png
generated003000.png
generated003100.png
generated003200.png
generated003300.png
generated003400.png
generated003500.png
generated003600.png
generated003700.png
generated003800.png
generated003900.png
generated004000.png
generated004100.png
generated004200.png
generated004300.png
generated004400.png
generated004500.png
generated004600.png
generated004700.png
generated004800.png
generated004900.png
generated005000.png
generated005100.png
generated005200.png
generated005300.png
generated005400.png
generated005500.png
generated005600.png
generated005700.png
generated005800.png
generated005900.png
generated006000.png
generated006100.png
generated006200.png
generated006300.png
generated006400.png
generated006500.png
generated006600.png
generated006700.png
generated006800.png
generated006900.png
generated007000.png
generated007100.png
generated007200.png
generated007300.png
generated007400.png
generated007500.png
generated007600.png
generated007700.png
generated007800.png
generated007900.png
generated008000.png
generated008100.png
generated008200.png
generated008300.png
generated008400.png
generated008500.png
generated008600.png
generated008700.png
generated008800.png
generated008900.png
generated009000.png
generated009100.png
generated009200.png
generated009300.png
generated009400.png
generated009500.png
generated009600.png
generated009700.png
generated009800.png
generated009900.png
images2gif.py:325: VisibleDeprecationWarning: converting an array with ndim > 0 to an index will result in an error in the future
  im2 = im[y0:y1,x0:x1]

Feedback wellcome @dh7net