import numpy as np import tensorflow as tf import vgg16 from scipy.misc import imread, imresize sess = tf.Session() opt_img = tf.Variable( tf.truncated_normal( [1,224,224,3], dtype=tf.float32, stddev=1e-1), name='opt_img' ) tmp_img = tf.clip_by_value( opt_img, 0.0, 255.0 ) vgg = vgg16.vgg16( tmp_img, 'vgg16_weights.npz', sess ) style_img = imread( 'style.png', mode='RGB' ) style_img = imresize( style_img, (224, 224) ) style_img = np.reshape( style_img, [1,224,224,3] ) content_img = imread( 'content.png', mode='RGB' ) content_img = imresize( content_img, (224, 224) ) content_img = np.reshape( content_img, [1,224,224,3] ) layers = [ 'conv1_1', 'conv1_2', 'conv2_1', 'conv2_2', 'conv3_1', 'conv3_2', 'conv3_3', 'conv4_1', 'conv4_2', 'conv4_3', 'conv5_1', 'conv5_2', 'conv5_3' ] ops = [ getattr( vgg, x ) for x in layers ] content_acts = sess.run( ops, feed_dict={vgg.imgs: content_img } ) style_acts = sess.run( ops, feed_dict={vgg.imgs: style_img} ) # # --- construct your cost function here # # Relevant snippets from the paper: # For the images shown in Fig 2 we matched the content representation on layer 'conv4_2' # and the style representations on layers 'conv1_1', 'conv2_1', 'conv3_1', 'conv4_1' and 'conv5_1' # The ratio alpha/beta was 1x10-3 # The factor w_l was always equal to one divided by the number of active layers (ie, 1/5) # --- place your adam optimizer call here # (don't forget to optimize only the opt_img variable) # this clobbers all VGG variables, but we need it to initialize the # adam stuff, so we reload all of the weights... sess.run( tf.initialize_all_variables() ) vgg.load_weights( 'vgg16_weights.npz', sess ) # initialize with the content image sess.run( opt_img.assign( content_img )) # --- place your optimization loop here