Guest User

videoify.py

a guest
Jul 18th, 2015
896
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # imports and basic notebook setup
  2. from cStringIO import StringIO
  3. import numpy as np
  4. import scipy.ndimage as nd
  5. import PIL.Image
  6. from IPython.display import clear_output, Image, display
  7. from google.protobuf import text_format
  8.  
  9. import caffe
  10.  
  11. import sys
  12.  
  13. def savearray(a, filename, fmt='png'):
  14.     a = np.uint8(np.clip(a, 0, 255))
  15.     with open(filename, 'wb') as f:
  16.         PIL.Image.fromarray(a).save(f, fmt)
  17.        
  18.  
  19. model_path = '/home/vagrant/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. frame = np.float32(PIL.Image.open('image.jpg'))
  96. frame_i = 0
  97.  
  98. h, w = frame.shape[:2]
  99. s = 0.05 # scale coefficient
  100.  
  101. for i in xrange(1000):
  102.     frame = deepdream(net, frame)
  103.     PIL.Image.fromarray(np.uint8(frame)).save("image-%04d.png"%frame_i)
  104.     frame = nd.affine_transform(frame, [1-s,1-s,1], [h*s/2,w*s/2,0], order=1)
  105.     frame_i += 1
RAW Paste Data