Advertisement
Guest User

deepdream

a guest
Jul 6th, 2015
3,357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.11 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # imports and basic notebook setup
  4. from cStringIO import StringIO
  5. import numpy as np
  6. import scipy.ndimage as nd
  7. import PIL.Image
  8. from IPython.display import clear_output, Image, display
  9. from google.protobuf import text_format
  10.  
  11. import caffe
  12.  
  13. def showarray(a, fmt='jpeg'):
  14.     a = np.uint8(np.clip(a, 0, 255))
  15.     f = StringIO()
  16.     PIL.Image.fromarray(a).save(f, fmt)
  17.     display(Image(data=f.getvalue()))
  18.  
  19. model_path = '../caffe/models/bvlc_googlenet/' # substitute your path here
  20. net_fn   = model_path + 'deploy.prototxt'
  21. param_fn = model_path + 'bvlc_googlenet.caffemodel'
  22.  
  23. # Patching model to be able to compute gradients.
  24. # Note that you can also manually add "force_backward: true" line to "deploy.prototxt".
  25. model = caffe.io.caffe_pb2.NetParameter()
  26. text_format.Merge(open(net_fn).read(), model)
  27. model.force_backward = True
  28. open('tmp.prototxt', 'w').write(str(model))
  29.  
  30. net = caffe.Classifier('tmp.prototxt', param_fn,
  31.                        mean = np.float32([104.0, 116.0, 122.0]), # ImageNet mean, training set dependent
  32.                        channel_swap = (2,1,0)) # the reference model has channels in BGR order instead of RGB
  33.  
  34. # a couple of utility functions for converting to and from Caffe's input image layout
  35. def preprocess(net, img):
  36.     return np.float32(np.rollaxis(img, 2)[::-1]) - net.transformer.mean['data']
  37. def deprocess(net, img):
  38.     return np.dstack((img + net.transformer.mean['data'])[::-1])
  39.  
  40. def make_step(net, step_size=1.5, end='inception_4c/output', jitter=32, clip=True):
  41.     '''Basic gradient ascent step.'''
  42.  
  43.     src = net.blobs['data'] # input image is stored in Net's 'data' blob
  44.     dst = net.blobs[end]
  45.  
  46.     ox, oy = np.random.randint(-jitter, jitter+1, 2)
  47.     src.data[0] = np.roll(np.roll(src.data[0], ox, -1), oy, -2) # apply jitter shift
  48.            
  49.     net.forward(end=end)
  50.     dst.diff[:] = dst.data  # specify the optimization objective
  51.     net.backward(start=end)
  52.     g = src.diff[0]
  53.     # apply normalized ascent step to the input image
  54.     src.data[:] += step_size/np.abs(g).mean() * g
  55.  
  56.     src.data[0] = np.roll(np.roll(src.data[0], -ox, -1), -oy, -2) # unshift image
  57.            
  58.     if clip:
  59.         bias = net.transformer.mean['data']
  60.         src.data[:] = np.clip(src.data, -bias, 255-bias)  
  61.  
  62. def deepdream(net, base_img, iter_n=10, octave_n=4, octave_scale=1.4, end='inception_4c/output', clip=True, **step_params):
  63.     # prepare base images for all octaves
  64.     octaves = [preprocess(net, base_img)]
  65.     for i in xrange(octave_n-1):
  66.         octaves.append(nd.zoom(octaves[-1], (1, 1.0/octave_scale,1.0/octave_scale), order=1))
  67.    
  68.     src = net.blobs['data']
  69.     detail = np.zeros_like(octaves[-1]) # allocate image for network-produced details
  70.     for octave, octave_base in enumerate(octaves[::-1]):
  71.         h, w = octave_base.shape[-2:]
  72.         if octave > 0:
  73.             # upscale details from the previous octave
  74.             h1, w1 = detail.shape[-2:]
  75.             detail = nd.zoom(detail, (1, 1.0*h/h1,1.0*w/w1), order=1)
  76.  
  77.         src.reshape(1,3,h,w) # resize the network's input image size
  78.         src.data[0] = octave_base+detail
  79.         for i in xrange(iter_n):
  80.             make_step(net, end=end, clip=clip, **step_params)
  81.            
  82.             # visualization
  83.             vis = deprocess(net, src.data[0])
  84.             if not clip: # adjust image contrast if clipping is disabled
  85.                 vis = vis*(255.0/np.percentile(vis, 99.98))
  86.             showarray(vis)
  87.             print octave, i, end, vis.shape
  88.             clear_output(wait=True)
  89.            
  90.         # extract details produced on the current octave
  91.         detail = src.data[0]-octave_base
  92.     # returning the resulting image
  93.     return deprocess(net, src.data[0])
  94.  
  95.  
  96.  
  97. img = np.float32(PIL.Image.open('sky1024px.jpg'))
  98. #showarray(img)
  99.  
  100.  
  101. frame = img
  102. frame_i = 0
  103. h, w = frame.shape[:2]
  104. s = 0.05 # scale coefficient
  105. for i in xrange(100):
  106.     frame = deepdream(net, frame)
  107.     PIL.Image.fromarray(np.uint8(frame)).save("frames/%04d.jpg"%frame_i)
  108.     frame = nd.affine_transform(frame, [1-s,1-s,1], [h*s/2,w*s/2,0], order=1)
  109.     frame_i += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement