Advertisement
Guest User

Untitled

a guest
Feb 13th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 1.40 KB | None | 0 0
  1. # Ref: http://blog.otoro.net/2016/03/25/generating-abstract-patterns-with-tensorflow/
  2.  
  3. module MyCppn
  4.  
  5. using Images
  6. using Flux
  7.  
  8. # set parameters
  9. z_dim = 3
  10. x_dim = 512
  11. y_dim = 512
  12. c_dim = 3
  13. N = 14
  14. hidden = 9
  15. batch_size = 1024
  16. n = x_dim * y_dim
  17.  
  18. # cast 0:x-1 to -0.5:0.5
  19. cast(x) = [range(-0.5, stop=0.5, step=1/(x - 1))...]
  20.  
  21. xs, ys = cast(x_dim), cast(y_dim)
  22. xs = repeat(xs, inner=(y_dim))
  23. ys = repeat(ys, outer=(x_dim))
  24. rs = sqrt.(xs.^2 + ys.^2)
  25.  
  26. # sample weigths from a gaussian distribution
  27. unit(in=N, out=N, f=tanh) = Dense(in, out, f, initW=randn)
  28.  
  29. # input -> [x, y, r, z...]
  30. layers = Any[unit(3 + z_dim)]
  31. for i=1:hidden
  32.     push!(layers, unit())
  33. end
  34. push!(layers, unit(N, c_dim, σ))
  35.  
  36. model = Chain(layers...)
  37. getColorAt(x) = Flux.data(model(x))
  38.  
  39. function batch(arr, s)
  40.     batches = []
  41.     l = size(arr, 2)
  42.     for i=1:s:l
  43.         push!(batches, arr[:, i:min(i+s-1, l)])
  44.     end
  45.     batches
  46. end
  47.  
  48. function getImage(z)
  49.     z = repeat(reshape(z, 1, z_dim), outer=(n, 1))
  50.     coords = hcat(xs, ys, rs, z)
  51.     coords = batch(coords, batch_size)
  52.     colorarr = getColorAt.(coords)
  53.     pvals = reshape(hcat(colorarr...), 3, n)
  54.     prsph = reshape(pvals, c_dim, y_dim, x_dim)
  55.     colorview(RGB, prsph)
  56. end
  57.  
  58. function saveImg(z, image_path="sample.png")
  59.     imgg = getImage(z)
  60.     save(image_path, imgg)
  61.     imgg
  62. end
  63.  
  64. function doStuff()
  65.   saveImg(rand(z_dim))
  66. end
  67.  
  68.  
  69. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement