赞
踩
风格转化是一个很流行的app应用,虽然现在过去风头了,但是自己实现一下也是好的。paper需要自己去解读,下面是图解。
中间是一个空白图片或者噪音图片。然后将空白图片和S表示style、C表示content进行最小损失函数,但是这样训练和验证会加大时间,测试太慢。然后使用如下的网络:
将网络分成左边Image Transform Net和右侧的Loss Network,左面生成图像的转换,右面进行损失函数的计算,每个特征值的对比。其中,左边的先进性下采样,中间是残差网络,最后上采样是反卷积。其中x和yc是一个。
其中style.py文件如下:
- from __future__ import print_function
- import sys, os, pdb
- sys.path.insert(0, 'src')
- import numpy as np, scipy.misc
- from optimize import optimize
- from argparse import ArgumentParser
- from utils import save_img, get_img, exists, list_files
- import evaluate
-
- CONTENT_WEIGHT = 7.5e0
- STYLE_WEIGHT = 1e2
- TV_WEIGHT = 2e2
-
- LEARNING_RATE = 1e-3
- NUM_EPOCHS = 2
- CHECKPOINT_DIR = 'checkpoints'
- CHECKPOINT_ITERATIONS = 2000
- VGG_PATH = 'data/imagenet-vgg-verydeep-19.mat'
- TRAIN_PATH = 'data/train2014'
- BATCH_SIZE = 4
- DEVICE = '/gpu:0'
- FRAC_GPU = 1
-
- def build_parser():
- parser = ArgumentParser()
- parser.add_argument('--checkpoint-dir', type=str,
- dest='checkpoint_dir', help='dir to save checkpoint in',
- metavar='CHECKPOINT_DIR', required=True)
-
- parser.add_argument('--style', type=str,
- dest='style', help='style image path',
- metavar='STYLE', required=True)
-
- parser.add_argument('--train-path', type=str,
- dest='train_path', help='path to training images folder',
- metavar='TRAIN_PATH', default=TRAIN_PATH)
-
- parser.add_argument('--test', type=str,
- dest='test', help='test image path',
- metavar='TEST', default=False)
-
- parser.add_argument('--test-dir', type=str,
- dest='test_dir', help='test image save dir',
- metavar='TEST_DIR', default=False)
-
- parser.add_argument('--slow', dest='slow', action='store_true',
- help='gatys\' approach (for debugging, not supported)',
- default=False)
-
- parser.add_argument('--epochs', type=int,
- dest='epochs', help='num epochs',
- metavar='EPOCHS', default=NUM_EPOCHS)
-
- parser.add_argument('--batch-size', type=int,
- dest='batch_size', help='batch size',
- metavar='BATCH_SIZE', default=BATCH_SIZE)
-
- parser.add_argument('--checkpoint-iterations', type=int,
- dest='checkpoint_iterations', help='checkpoint frequency',
- metavar='CHECKPOINT_ITERATIONS',
- default=CHECKPOINT_ITERATIONS)
-
- parser.add_argument('--vgg-path', type=str,
- dest='vgg_path',
- help='path to VGG19 network (default %(default)s)',
- metavar='VGG_PATH', default=VGG_PATH)
-
- parser.add_argument('--content-weight', type=float,
- dest='content_weight',
- help='content weight (default %(default)s)',
- metavar='CONTENT_WEIGHT', default=CONTENT_WEIGHT)
-
- parser.add_argument('--style-weight', type=float,
- dest='style_weight',
- help='style weight (default %(default)s)',
- metavar='STYLE_WEIGHT', default=STYLE_WEIGHT)
-
- parser.add_argument('--tv-weight', type=float,
- dest='tv_weight',
- help='total variation regularization weight (default %(default)s)',
- metavar='TV_WEIGHT', default=TV_WEIGHT)
-
- parser.add_argument('--learning-rate', type=float,
- dest='learning_rate',
- help='learning rate (default %(default)s)',
- metavar='LEARNING_RATE', default=LEARNING_RATE)
-
- return parser
-
- def check_opts(opts):
- exists(opts.checkpoint_dir, "checkpoint dir not found!")
- exists(opts.style, "style path not found!")
- exists(opts.train_path, "train path not found!")
- if opts.test or opts.test_dir:
- exists(opts.test, "test img not found!")
- exists(opts.test_dir, "test directory not found!")
- exists(opts.vgg_path, "vgg network data not found!")
- assert opts.epochs > 0
- assert opts.batch_size > 0
- assert opts.checkpoint_iterations > 0
- assert os.path.exists(opts.vgg_path)
- assert opts.content_weight >= 0
- assert opts.style_weight >= 0
- assert opts.tv_weight >= 0
- assert opts.learning_rate >= 0
-
- def _get_files(img_dir):
- files = list_files(img_dir)
- return [os.path.join(img_dir,x) for x in files]
-
-
- def main():
- parser = build_parser()
- options = parser.parse_args()
- check_opts(options)
-
- style_target = get_img(options.style)
- if not options.slow:
- content_targets = _get_files(options.train_path)
- elif options.test:
- content_targets = [options.test]
-
- kwargs = {
- "slow":options.slow,
- "epochs":options.epochs,
- "print_iterations":options.checkpoint_iterations,
- "batch_size":options.batch_size,
- "save_path":os.path.join(options.checkpoint_dir,'fns.ckpt'),
- "learning_rate":options.learning_rate
- }
-
- if options.slow:
- if options.epochs < 10:
- kwargs['epochs'] = 1000
- if options.learning_rate < 1:
- kwargs['learning_rate'] = 1e1
-
- args = [
- content_targets,
- style_target,
- options.content_weight,
- options.style_weight,
- options.tv_weight,
- options.vgg_path
- ]
-
- for preds, losses, i, epoch in optimize(*args, **kwargs):
- style_loss, content_loss, tv_loss, loss = losses
-
- print('Epoch %d, Iteration: %d, Loss: %s' % (epoch, i, loss))
- to_print = (style_loss, content_loss, tv_loss)
- print('style: %s, content:%s, tv: %s' % to_print)
- if options.test:
- assert options.test_dir != False
- preds_path = '%s/%s_%s.png' % (options.test_dir,epoch,i)
- if not options.slow:
- ckpt_dir = os.path.dirname(options.checkpoint_dir)
- evaluate.ffwd_to_img(options.test,preds_path,
- options.checkpoint_dir)
- else:
- save_img(preds_path, img)
- ckpt_dir = options.checkpoint_dir
- cmd_text = 'python evaluate.py --checkpoint %s ...' % ckpt_dir
- print("Training complete. For evaluation:\n `%s`" % cmd_text)
-
- if __name__ == '__main__':
- main()
在里面实现获取图片,缩放图片,保存图片等操作
- import scipy.misc, numpy as np, os, sys
-
- def save_img(out_path, img):
- img = np.clip(img, 0, 255).astype(np.uint8)
- scipy.misc.imsave(out_path, img)
-
- def scale_img(style_path, style_scale):
- scale = float(style_scale)
- o0, o1, o2 = scipy.misc.imread(style_path, mode='RGB').shape
- scale = float(style_scale)
- new_shape = (int(o0 * scale), int(o1 * scale), o2)
- style_target = _get_img(style_path, img_size=new_shape)
- return style_target
-
- def get_img(src, img_size=False):
- img = scipy.misc.imread(src, mode='RGB') # misc.imresize(, (256, 256, 3))
- if not (len(img.shape) == 3 and img.shape[2] == 3):
- img = np.dstack((img,img,img))
- print (img.shape)
- if img_size != False:
- img = scipy.misc.imresize(img, img_size)
- return img
-
- def exists(p, msg):
- assert os.path.exists(p), msg
-
- def list_files(in_path):
- files = []
- for (dirpath, dirnames, filenames) in os.walk(in_path):
- files.extend(filenames)
- break
-
- return files
下面是模型优化的函数,最为重要的函数optimize.py
- from __future__ import print_function
- import functools
- 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, debug=False):
- 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
- with tf.Graph().as_default(), tf.device('/cpu:0'), tf.Session() 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]))
- #print (features.shape)
- 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)
-
- # 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)
-
- 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.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 * functools.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.global_variables_initializer())
- import random
- uid = random.randint(1, 100)
- print("UID: %s" % uid)
- for epoch in range(epochs):
- num_examples = len(content_targets)
- iterations = 0
- while iterations * batch_size < num_examples:
- start_time = time.time()
- curr = iterations * batch_size
- step = curr + batch_size
- X_batch = np.zeros(batch_shape, dtype=np.float32)
- for j, img_p in enumerate(content_targets[curr:step]):
- X_batch[j] = get_img(img_p, (256,256,3)).astype(np.float32)
-
- iterations += 1
- assert X_batch.shape[0] == batch_size
-
- feed_dict = {
- X_content:X_batch
- }
-
- train_step.run(feed_dict=feed_dict)
- end_time = time.time()
- delta_time = end_time - start_time
- if debug:
- print("UID: %s, batch time: %s" % (uid, delta_time))
- is_print_iter = int(iterations) % print_iterations == 0
- if slow:
- is_print_iter = epoch % print_iterations == 0
- is_last = epoch == epochs - 1 and iterations * batch_size >= num_examples
- should_print = is_print_iter or is_last
- if should_print:
- to_get = [style_loss, content_loss, tv_loss, loss, preds]
- test_feed_dict = {
- X_content:X_batch
- }
-
- tup = sess.run(to_get, feed_dict = test_feed_dict)
- _style_loss,_content_loss,_tv_loss,_loss,_preds = tup
- losses = (_style_loss, _content_loss, _tv_loss, _loss)
- if slow:
- _preds = vgg.unprocess(_preds)
- else:
- saver = tf.train.Saver()
- res = saver.save(sess, save_path)
- yield(_preds, losses, iterations, epoch)
-
- def _tensor_size(tensor):
- from operator import mul
- return functools.reduce(mul, (d.value for d in tensor.get_shape()[1:]), 1)
- import tensorflow as tf
- import numpy as np
- import scipy.io
- import pdb
-
- MEAN_PIXEL = np.array([ 123.68 , 116.779, 103.939])
-
- def net(data_path, input_image):
- layers = (
- 'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1',
-
- 'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2',
-
- 'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'conv3_3',
- 'relu3_3', 'conv3_4', 'relu3_4', 'pool3',
-
- 'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'conv4_3',
- 'relu4_3', 'conv4_4', 'relu4_4', 'pool4',
-
- 'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'conv5_3',
- 'relu5_3', 'conv5_4', 'relu5_4'
- )
-
- data = scipy.io.loadmat(data_path)
- mean = data['normalization'][0][0][0]
- mean_pixel = np.mean(mean, axis=(0, 1))
- weights = data['layers'][0]
-
- net = {}
- current = input_image
- for i, name in enumerate(layers):
- kind = name[:4]
- if kind == 'conv':
- kernels, bias = weights[i][0][0][0][0]
- # matconvnet: weights are [width, height, in_channels, out_channels]
- # tensorflow: weights are [height, width, in_channels, out_channels]
- kernels = np.transpose(kernels, (1, 0, 2, 3))
- bias = bias.reshape(-1)
- current = _conv_layer(current, kernels, bias)
- elif kind == 'relu':
- current = tf.nn.relu(current)
- elif kind == 'pool':
- current = _pool_layer(current)
- net[name] = current
-
- assert len(net) == len(layers)
- return net
-
-
- def _conv_layer(input, weights, bias):
- conv = tf.nn.conv2d(input, tf.constant(weights), strides=(1, 1, 1, 1),
- padding='SAME')
- return tf.nn.bias_add(conv, bias)
-
-
- def _pool_layer(input):
- return tf.nn.max_pool(input, ksize=(1, 2, 2, 1), strides=(1, 2, 2, 1),
- padding='SAME')
-
-
- def preprocess(image):
- return image - MEAN_PIXEL
-
-
- def unprocess(image):
- return image + MEAN_PIXEL
然后就是transform.py的转换网络,即生成网络:
- import tensorflow as tf, pdb
-
- WEIGHTS_INIT_STDEV = .1
-
- def net(image):
- conv1 = _conv_layer(image, 32, 9, 1)
- conv2 = _conv_layer(conv1, 64, 3, 2)
- conv3 = _conv_layer(conv2, 128, 3, 2)
- resid1 = _residual_block(conv3, 3)
- resid2 = _residual_block(resid1, 3)
- resid3 = _residual_block(resid2, 3)
- resid4 = _residual_block(resid3, 3)
- resid5 = _residual_block(resid4, 3)
- conv_t1 = _conv_tranpose_layer(resid5, 64, 3, 2)
- conv_t2 = _conv_tranpose_layer(conv_t1, 32, 3, 2)
- conv_t3 = _conv_layer(conv_t2, 3, 9, 1, relu=False)
- preds = tf.nn.tanh(conv_t3) * 150 + 255./2
- return preds
-
- def _conv_layer(net, num_filters, filter_size, strides, relu=True):
- weights_init = _conv_init_vars(net, num_filters, filter_size)
- strides_shape = [1, strides, strides, 1]
- net = tf.nn.conv2d(net, weights_init, strides_shape, padding='SAME')
- net = _instance_norm(net)
- if relu:
- net = tf.nn.relu(net)
-
- return net
-
- def _conv_tranpose_layer(net, num_filters, filter_size, strides):
- weights_init = _conv_init_vars(net, num_filters, filter_size, transpose=True)
-
- batch_size, rows, cols, in_channels = [i.value for i in net.get_shape()]
- new_rows, new_cols = int(rows * strides), int(cols * strides)
- # new_shape = #tf.pack([tf.shape(net)[0], new_rows, new_cols, num_filters])
-
- new_shape = [batch_size, new_rows, new_cols, num_filters]
- tf_shape = tf.stack(new_shape)
- strides_shape = [1,strides,strides,1]
-
- net = tf.nn.conv2d_transpose(net, weights_init, tf_shape, strides_shape, padding='SAME')
- net = _instance_norm(net)
- return tf.nn.relu(net)
-
- def _residual_block(net, filter_size=3):
- tmp = _conv_layer(net, 128, filter_size, 1)
- return net + _conv_layer(tmp, 128, filter_size, 1, relu=False)
-
- def _instance_norm(net, train=True):
- batch, rows, cols, channels = [i.value for i in net.get_shape()]
- var_shape = [channels]
- mu, sigma_sq = tf.nn.moments(net, [1,2], keep_dims=True)
- shift = tf.Variable(tf.zeros(var_shape))
- scale = tf.Variable(tf.ones(var_shape))
- epsilon = 1e-3
- normalized = (net-mu)/(sigma_sq + epsilon)**(.5)
- return scale * normalized + shift
-
- def _conv_init_vars(net, out_channels, filter_size, transpose=False):
- _, rows, cols, in_channels = [i.value for i in net.get_shape()]
- if not transpose:
- weights_shape = [filter_size, filter_size, in_channels, out_channels]
- else:
- weights_shape = [filter_size, filter_size, out_channels, in_channels]
-
- weights_init = tf.Variable(tf.truncated_normal(weights_shape, stddev=WEIGHTS_INIT_STDEV, seed=1), dtype=tf.float32)
- return weights_init
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。