Advertisement
Guest User

Neural colorization

a guest
Apr 5th, 2016
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. import sys
  2. import numpy
  3. import caffe
  4. import os
  5. import skimage.color
  6. import scipy.ndimage.interpolation
  7. import scipy.misc
  8.  
  9. if (len(sys.argv) != 2):
  10.         sys.exit("Usage: python2 main.py <target_image>")
  11.  
  12. filename = sys.argv[1]
  13. gpu_id = 0
  14. caffe.set_mode_gpu()
  15. caffe.set_device(gpu_id)
  16. net = caffe.Net("colorization_deploy_v0.prototxt", "colorization_release_v0.caffemodel", caffe.TEST)
  17.  
  18. (H_in, W_in) = net.blobs["data_l"].data.shape[2:]
  19. (H_out, W_out) = net.blobs["class8_ab"].data.shape[2:]
  20. net.blobs["Trecip"].data[...] = 6 / numpy.log(10)
  21.  
  22. img_rgb = caffe.io.load_image(filename)
  23. img_lab = skimage.color.rgb2lab(img_rgb)
  24. img_l = img_lab[:,:,0]
  25. (H_orig,W_orig) = img_rgb.shape[:2]
  26.  
  27. img_lab_bw = img_lab.copy()
  28. img_lab_bw[:,:,1:] = 0
  29. img_rgb_bw = skimage.color.lab2rgb(img_lab_bw)
  30.  
  31. img_rs = caffe.io.resize_image(img_rgb,(H_in, W_in))
  32. img_lab_rs = skimage.color.rgb2lab(img_rs)
  33. img_l_rs = img_lab_rs[:,:,0]
  34.  
  35. img_pad = numpy.ones((H_orig, W_orig / 10, 3))
  36.  
  37. net.blobs["data_l"].data[0, 0,:,:] = img_l_rs - 50
  38. net.forward()
  39.  
  40. ab_dec = net.blobs['class8_ab'].data[0,:,:,:].transpose((1, 2, 0))
  41. ab_dec_us = scipy.ndimage.interpolation.zoom(ab_dec, (1.0 * H_orig / H_out, 1.0 * W_orig / W_out, 1))
  42. img_lab_out = numpy.concatenate((img_l[:,:,numpy.newaxis], ab_dec_us), axis = 2)
  43. img_rgb_out = numpy.clip(skimage.color.lab2rgb(img_lab_out), 0, 1)
  44.  
  45. splitted_filename = filename.split("/")
  46. scipy.misc.imsave("./" + "".join(splitted_filename[0:-1]) + "/out_" + splitted_filename[-1], img_rgb_out)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement