Created
July 20, 2018 23:04
-
-
Save llSourcell/90425d08e8c712f05c99a98964e4927f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from __future__ import print_function | |
| import sys | |
| import vgg, pdb, time | |
| import tensorflow as tf, numpy as np, os | |
| import transform | |
| from utils import get_img | |
| STYLE_LAYERS = ('relu1_1', 'relu2_1', 'relu3_1', 'relu4_1', 'relu5_1') | |
| CONTENT_LAYER = 'relu4_2' | |
| DEVICES = 'CUDA_VISIBLE_DEVICES' | |
| # np arr, np arr | |
| def optimize(content_targets, style_target, content_weight, style_weight, | |
| tv_weight, vgg_path, epochs=2, print_iterations=1000, | |
| batch_size=4, save_path='saver/fns.ckpt', slow=False, | |
| learning_rate=1e-3, device='/cpu:0', debug=False, total_iterations=-1, | |
| base_model_path=None): | |
| if slow: | |
| batch_size = 1 | |
| mod = len(content_targets) % batch_size | |
| if mod > 0: | |
| print("Train set has been trimmed slightly..") | |
| content_targets = content_targets[:-mod] | |
| style_features = {} | |
| batch_shape = (batch_size,256,256,3) | |
| style_shape = (1,) + style_target.shape | |
| print(style_shape) | |
| # precompute style features | |
| print("Precomputing style features") | |
| sys.stdout.flush() | |
| with tf.Graph().as_default(), tf.device(device), tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess: | |
| style_image = tf.placeholder(tf.float32, shape=style_shape, name='style_image') | |
| style_image_pre = vgg.preprocess(style_image) | |
| net = vgg.net(vgg_path, style_image_pre) | |
| style_pre = np.array([style_target]) | |
| for layer in STYLE_LAYERS: | |
| features = net[layer].eval(feed_dict={style_image:style_pre}) | |
| features = np.reshape(features, (-1, features.shape[3])) | |
| gram = np.matmul(features.T, features) / features.size | |
| style_features[layer] = gram | |
| with tf.Graph().as_default(), tf.Session() as sess: | |
| X_content = tf.placeholder(tf.float32, shape=batch_shape, name="X_content") | |
| X_pre = vgg.preprocess(X_content) | |
| print("Precomputing content features") | |
| sys.stdout.flush() | |
| # precompute content features | |
| content_features = {} | |
| content_net = vgg.net(vgg_path, X_pre) | |
| content_features[CONTENT_LAYER] = content_net[CONTENT_LAYER] | |
| if slow: | |
| preds = tf.Variable( | |
| tf.random_normal(X_content.get_shape()) * 0.256 | |
| ) | |
| preds_pre = preds | |
| else: | |
| preds = transform.net(X_content/255.0) | |
| preds_pre = vgg.preprocess(preds) | |
| print("Building VGG net") | |
| sys.stdout.flush() | |
| net = vgg.net(vgg_path, preds_pre) | |
| content_size = _tensor_size(content_features[CONTENT_LAYER])*batch_size | |
| assert _tensor_size(content_features[CONTENT_LAYER]) == _tensor_size(net[CONTENT_LAYER]) | |
| content_loss = content_weight * (2 * tf.nn.l2_loss( | |
| net[CONTENT_LAYER] - content_features[CONTENT_LAYER]) / content_size | |
| ) | |
| style_losses = [] | |
| for style_layer in STYLE_LAYERS: | |
| layer = net[style_layer] | |
| bs, height, width, filters = map(lambda i:i.value,layer.get_shape()) | |
| size = height * width * filters | |
| feats = tf.reshape(layer, (bs, height * width, filters)) | |
| feats_T = tf.transpose(feats, perm=[0,2,1]) | |
| grams = tf.batch_matmul(feats_T, feats) / size | |
| style_gram = style_features[style_layer] | |
| style_losses.append(2 * tf.nn.l2_loss(grams - style_gram)/style_gram.size) | |
| style_loss = style_weight * reduce(tf.add, style_losses) / batch_size | |
| # total variation denoising | |
| tv_y_size = _tensor_size(preds[:,1:,:,:]) | |
| tv_x_size = _tensor_size(preds[:,:,1:,:]) | |
| y_tv = tf.nn.l2_loss(preds[:,1:,:,:] - preds[:,:batch_shape[1]-1,:,:]) | |
| x_tv = tf.nn.l2_loss(preds[:,:,1:,:] - preds[:,:,:batch_shape[2]-1,:]) | |
| tv_loss = tv_weight*2*(x_tv/tv_x_size + y_tv/tv_y_size)/batch_size | |
| loss = content_loss + style_loss + tv_loss | |
| # overall loss | |
| train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss) | |
| sess.run(tf.initialize_all_variables()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment