├── van.jpg ├── minsk.jpg ├── results.jpg ├── results-ny.jpg ├── New_York_night.jpg ├── tmp_950_color.jpg ├── New_York_night_picasso.jpg ├── README.md └── run.py /van.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pavelgonchar/color-independent-style-transfer/HEAD/van.jpg -------------------------------------------------------------------------------- /minsk.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pavelgonchar/color-independent-style-transfer/HEAD/minsk.jpg -------------------------------------------------------------------------------- /results.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pavelgonchar/color-independent-style-transfer/HEAD/results.jpg -------------------------------------------------------------------------------- /results-ny.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pavelgonchar/color-independent-style-transfer/HEAD/results-ny.jpg -------------------------------------------------------------------------------- /New_York_night.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pavelgonchar/color-independent-style-transfer/HEAD/New_York_night.jpg -------------------------------------------------------------------------------- /tmp_950_color.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pavelgonchar/color-independent-style-transfer/HEAD/tmp_950_color.jpg -------------------------------------------------------------------------------- /New_York_night_picasso.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pavelgonchar/color-independent-style-transfer/HEAD/New_York_night_picasso.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # color-independent-style-transfer using tensorflow 2 | An attempt to reproduce results from Transfer Style But Not Color http://blog.deepart.io/2016/06/04/color-independent-style-transfer/ 3 | 4 | Transfer style with color 5 | ------------------------------- 6 | ![neural-art](van.jpg?raw=true "neural-art") 7 | 8 | Transfer only style but not color 9 | ---------------------------------- 10 | ![results](results.jpg?raw=true "results") 11 | 12 | ![deepart.io results](results-ny.jpg?raw=true "deepart.io results") 13 | -------------------------------------------------------------------------------- /run.py: -------------------------------------------------------------------------------- 1 | import skimage.io 2 | import tensorflow as tf 3 | from tensorflow.python.framework import ops, dtypes 4 | import numpy as np 5 | from matplotlib import pyplot as plt 6 | 7 | flags = tf.app.flags 8 | FLAGS = flags.FLAGS 9 | flags.DEFINE_string('original', 'minsk.jpg', 'Original Image') 10 | flags.DEFINE_string('styled', 'tmp_950_color.jpg', 'Styled Image') 11 | 12 | original = tf.placeholder("float", [1, 338, 600, 3]) 13 | styled = tf.placeholder("float", [1, 338, 600, 3]) 14 | 15 | def concat_images(imga, imgb): 16 | """ 17 | Combines two color image ndarrays side-by-side. 18 | """ 19 | ha, wa = imga.shape[:2] 20 | hb, wb = imgb.shape[:2] 21 | max_height = np.max([ha, hb]) 22 | total_width = wa + wb 23 | new_img = np.zeros(shape=(max_height, total_width, 3), dtype=np.float32) 24 | new_img[:ha, :wa] = imga 25 | new_img[:hb, wa:wa + wb] = imgb 26 | return new_img 27 | 28 | 29 | def rgb2yuv(rgb): 30 | """ 31 | Convert RGB image into YUV https://en.wikipedia.org/wiki/YUV 32 | """ 33 | rgb2yuv_filter = tf.constant( 34 | [[[[0.299, -0.169, 0.499], 35 | [0.587, -0.331, -0.418], 36 | [0.114, 0.499, -0.0813]]]]) 37 | rgb2yuv_bias = tf.constant([0., 0.5, 0.5]) 38 | 39 | temp = tf.nn.conv2d(rgb, rgb2yuv_filter, [1, 1, 1, 1], 'SAME') 40 | temp = tf.nn.bias_add(temp, rgb2yuv_bias) 41 | 42 | return temp 43 | 44 | 45 | def yuv2rgb(yuv): 46 | """ 47 | Convert YUV image into RGB https://en.wikipedia.org/wiki/YUV 48 | """ 49 | yuv = tf.mul(yuv, 255) 50 | yuv2rgb_filter = tf.constant( 51 | [[[[1., 1., 1.], 52 | [0., -0.34413999, 1.77199996], 53 | [1.40199995, -0.71414, 0.]]]]) 54 | yuv2rgb_bias = tf.constant([-179.45599365, 135.45983887, -226.81599426]) 55 | temp = tf.nn.conv2d(yuv, yuv2rgb_filter, [1, 1, 1, 1], 'SAME') 56 | temp = tf.nn.bias_add(temp, yuv2rgb_bias) 57 | temp = tf.maximum(temp, tf.zeros(temp.get_shape(), dtype=tf.float32)) 58 | temp = tf.minimum(temp, tf.mul( 59 | tf.ones(temp.get_shape(), dtype=tf.float32), 255)) 60 | temp = tf.div(temp, 255) 61 | return temp 62 | 63 | 64 | styled_grayscale = tf.image.rgb_to_grayscale(styled) 65 | styled_grayscale_rgb = tf.image.grayscale_to_rgb(styled_grayscale) 66 | styled_grayscale_yuv = rgb2yuv(styled_grayscale_rgb) 67 | 68 | original_yuv = rgb2yuv(original) 69 | 70 | combined_yuv = tf.concat(3, [tf.split(3, 3, styled_grayscale_yuv)[0], tf.split(3, 3, original_yuv)[1], tf.split(3, 3, original_yuv)[2]]) 71 | combined_rbg = yuv2rgb(combined_yuv) 72 | 73 | init = tf.initialize_all_variables() 74 | 75 | with tf.Session() as sess: 76 | sess.run(tf.initialize_all_variables()) 77 | 78 | original_image = skimage.io.imread(FLAGS.original) / 255.0 79 | original_image = original_image.reshape((1, 338, 600, 3)) 80 | styled_image = skimage.io.imread(FLAGS.styled) / 255.0 81 | styled_image = styled_image.reshape((1, 338, 600, 3)) 82 | 83 | combined_rbg_ = sess.run(combined_rbg, feed_dict={original: original_image, styled: styled_image}) 84 | 85 | summary_image = concat_images(original_image.reshape((338, 600, 3)), styled_image.reshape((338, 600, 3))) 86 | summary_image = concat_images(summary_image, combined_rbg_[0]) 87 | plt.imsave("results.jpg", summary_image) --------------------------------------------------------------------------------