Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Ref: http://blog.otoro.net/2016/03/25/generating-abstract-patterns-with-tensorflow/
- module MyCppn
- using Images
- using Flux
- # set parameters
- z_dim = 3
- x_dim = 512
- y_dim = 512
- c_dim = 3
- N = 14
- hidden = 9
- batch_size = 1024
- n = x_dim * y_dim
- # cast 0:x-1 to -0.5:0.5
- cast(x) = [range(-0.5, stop=0.5, step=1/(x - 1))...]
- xs, ys = cast(x_dim), cast(y_dim)
- xs = repeat(xs, inner=(y_dim))
- ys = repeat(ys, outer=(x_dim))
- rs = sqrt.(xs.^2 + ys.^2)
- # sample weigths from a gaussian distribution
- unit(in=N, out=N, f=tanh) = Dense(in, out, f, initW=randn)
- # input -> [x, y, r, z...]
- layers = Any[unit(3 + z_dim)]
- for i=1:hidden
- push!(layers, unit())
- end
- push!(layers, unit(N, c_dim, σ))
- model = Chain(layers...)
- getColorAt(x) = Flux.data(model(x))
- function batch(arr, s)
- batches = []
- l = size(arr, 2)
- for i=1:s:l
- push!(batches, arr[:, i:min(i+s-1, l)])
- end
- batches
- end
- function getImage(z)
- z = repeat(reshape(z, 1, z_dim), outer=(n, 1))
- coords = hcat(xs, ys, rs, z)
- coords = batch(coords, batch_size)
- colorarr = getColorAt.(coords)
- pvals = reshape(hcat(colorarr...), 3, n)
- prsph = reshape(pvals, c_dim, y_dim, x_dim)
- colorview(RGB, prsph)
- end
- function saveImg(z, image_path="sample.png")
- imgg = getImage(z)
- save(image_path, imgg)
- imgg
- end
- function doStuff()
- saveImg(rand(z_dim))
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement