Neural Style论文笔记+源码解析
来源:互联网 发布:二维正态分布 知乎 编辑:程序博客网 时间:2024/06/06 01:06
引言
前面在Ubuntu16.04+GTX1080配置TensorFlow并实现图像风格转换中介绍了TensorFlow的配置过程,以及运用TensorFlow实现图像风格转换,主要是使用了文章A Neural Algorithm of Artistic Style中的方法,今天,我将主要对这篇文章进行解读,并对基于TensorFlow版本的Neural Style开源代码进行解析。
论文地址:A Neural Algorithm of Artistic Style
源码地址:Neural Style in TensorFlow
A Neural Algorithm of Artistic Style
任意一幅画,在人的眼中可以分为两个部分,一是画的具体内容(Content),如人物、山水、建筑等等,这是很实在的东西;二是绘画的风格(Style),这却是很抽象的东西,这和艺术家的手法、使用的绘画材料以及绘画习惯都有很大的关系。而在计算机中,这仅仅是一些像素点(pixel)而已。这篇文章的key finding是the representations of content and style in the Convolutional Neural Network are separable,即,我们不仅仅可以用深度学习来提取图像的特征,同时还可以将图像的Content和Style分隔开来。
上图即代表了这篇文章的整体框架。
这里的CNN模型使用的是pre-trained的VGG-16模型。
主要包含两个部分:
Content Reconstruction:将图像输入到CNN模型中,使用不同的layer提取到的feature maps来对输入图像进行reconstruct。上图中使用了VGG的‘conv1_1’(a), ‘conv2_1’(b), ‘conv3_1’(c), ‘conv4_1’(d) 和‘conv5_1’(e)的feature maps来重构原始图像。作者在文中说,使用低层的特征能够很好的对原始图像的像素值进行重构(a,b,c),而使用高层的特征来重构的时候detailed pixel出现了丢失,但是图像中的high-level content却保留了下来(d,e),因此认为高层的特征能够更好的对图像的content进行重构。
Style Reconstruction:Style 的重构比Content复杂的多,作者提出使用某一层不同filter的响应之间的correlations来代表该层提取到的texture information,然后来对Style进行重构。上图中使用了VGG的
‘conv1_1’(a), ‘conv1_1’+‘conv2_1’(b), ‘conv1_1’+‘conv2_1’ +‘conv3_1’(c),‘conv1_1’+‘conv2_1’+‘conv3_1’+‘conv4_1’(d), ‘conv1_1’+‘conv2_1’+‘conv3_1’+‘conv4_1’+‘conv5_1’(e)的texture information来进行重构。作者表示使用多层的feature correlations来重构的Style会在各个不同的尺度上更加匹配图像本身的style,忽略场景的全局信息。
基于以上,作者提出了本文的主要工作:finding an image that simultaneously matches the content representation of the photograph and the style representation of the artwork,即将一幅画的Style和另一幅话的Content进行融合,来证明作者的观点。
假设某一层得到的响应是
上面我们提到了,某一层的Style可以用
文章中作者使用了多层来表达Style,所以总的Style Loss为:
定义好了两个Loss之后,就利用优化方法来最小化总的Loss:
其中的
最终迭代出来的
Neural Style in TensorFlow
TensorFlow版本的源码主要包含了三个文件:neural_style.py, stylize.py和 vgg.py。
neural_style.py:外部接口函数,定义了函数的主要参数以及部分参数的默认值,包含对图像的读取和存贮,对输入图像进行resize,权值分配等操作,并将参数以及resize的图片传入stylize.py中。
stylize.py:核心代码,包含了训练、优化等过程。
vgg.py:定义了网络模型以及相关的运算。
以下主要对stylize.py中的相关代码进行解析。
shape = (1,) + content.shapestyle_shapes = [(1,) + style.shape for style in styles]
content.shape是三维(height, width, channel),这里将维度变成(1, height, width, channel)为了与后面保持一致。
# compute content features in feedforward modeg = tf.Graph() #创建图 with g.as_default(), g.device('/cpu:0'), tf.Session() as sess: image = tf.placeholder('float', shape=shape) net, mean_pixel = vgg.net(network, image) content_pre = np.array([vgg.preprocess(content, mean_pixel)]) content_features[CONTENT_LAYER] = net[CONTENT_LAYER].eval(feed_dict={image: content_pre})
首先创建一个image的占位符,然后通过eval()的feed_dict将content_pre传给image,启动net的运算过程,得到了content的feature maps。
# compute style features in feedforward mode for i in range(len(styles)): g = tf.Graph() with g.as_default(), g.device('/cpu:0'), tf.Session() as sess: image = tf.placeholder('float', shape=style_shapes[i]) net, _ = vgg.net(network, image) style_pre = np.array([vgg.preprocess(styles[i], mean_pixel)]) for layer in STYLE_LAYERS: features = net[layer].eval(feed_dict={image: style_pre}) features = np.reshape(features, (-1, features.shape[3])) gram = np.matmul(features.T, features) / features.size style_features[i][layer] = gram
由于style图像可以输入多幅,这里使用for循环。同样的,将style_pre传给image占位符,启动net运算,得到了style的feature maps,由于style为不同filter响应的内积,因此在这里增加了一步:gram = np.matmul(features.T, features) / features.size,即为style的feature。
# make stylized image using backpropogation with tf.Graph().as_default(): if initial is None: noise = np.random.normal(size=shape, scale=np.std(content) * 0.1) initial = tf.random_normal(shape) * 0.256 else: initial = np.array([vgg.preprocess(initial, mean_pixel)]) initial = initial.astype('float32') image = tf.Variable(initial) net, _ = vgg.net(network, image) # content loss content_loss = content_weight * (2 * tf.nn.l2_loss( net[CONTENT_LAYER] - content_features[CONTENT_LAYER]) / content_features[CONTENT_LAYER].size) # style loss style_loss = 0 for i in range(len(styles)): style_losses = [] for style_layer in STYLE_LAYERS: layer = net[style_layer] _, height, width, number = map(lambda i: i.value, layer.get_shape()) size = height * width * number feats = tf.reshape(layer, (-1, number)) gram = tf.matmul(tf.transpose(feats), feats) / size style_gram = style_features[i][style_layer] style_losses.append(2 * tf.nn.l2_loss(gram - style_gram) / style_gram.size) style_loss += style_weight * style_blend_weights[i] * reduce(tf.add, style_losses) # total variation denoising tv_y_size = _tensor_size(image[:,1:,:,:]) tv_x_size = _tensor_size(image[:,:,1:,:]) tv_loss = tv_weight * 2 * ( (tf.nn.l2_loss(image[:,1:,:,:] - image[:,:shape[1]-1,:,:]) / tv_y_size) + (tf.nn.l2_loss(image[:,:,1:,:] - image[:,:,:shape[2]-1,:]) / tv_x_size)) # overall loss loss = content_loss + style_loss + tv_loss
image = tf.Variable(initial)初始化了一个TensorFlow的变量,即为我们需要训练的对象。注意这里我们训练的对象是一张图像,而不是weight和bias。接下来定义了Content Loss和Style Loss,结合文中的公式很容易看懂,在代码中,还增加了total variation denoising,因此总的loss = content_loss + style_loss + tv_loss。
# optimizer setup train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss)
创建train_step,使用Adam优化器,优化对象是上面的loss。
# optimization best_loss = float('inf') best = None with tf.Session() as sess: sess.run(tf.initialize_all_variables()) for i in range(iterations): last_step = (i == iterations - 1) print_progress(i, last=last_step) train_step.run() if (checkpoint_iterations and i % checkpoint_iterations == 0) or last_step: this_loss = loss.eval() if this_loss < best_loss: best_loss = this_loss best = image.eval() yield ( (None if last_step else i), vgg.unprocess(best.reshape(shape[1:]), mean_pixel) )
优化过程,通过迭代使用train_step来最小化loss,最终得到一个best,即为训练优化的结果。
总的来说,TensorFlow是一个效率很高、功能很强大的工具,代码也并不是很难看懂,本人对TensorFlow并不是很熟悉,还需要对它的内部函数以及运行机制更深入的了解,因此这也当做是一个学习的过程,上述如果有任何不对的地方,也希望大家批评指正。
- Neural Style论文笔记+源码解析
- 论文笔记:A neural algorithm of artistic style
- 《image Style Transfer Using Convolutional Neural Networks》论文笔记
- [深度学习论文笔记][Neural Arts] A Neural Algorithm of Artistic Style
- DCGAN论文笔记+源码解析
- InfoGAN论文笔记+源码解析
- DCGAN论文笔记+源码解析
- DCGAN论文笔记+源码解析
- DCGAN论文笔记+源码解析
- A Neural Algorithm of Artistic Style 论文理解
- 《A Neural Algorithm of Artistic Style》笔记
- neural-style
- Neural Style
- 【论文笔记】Deep Neural Decision Forests
- 【论文笔记】residual neural network-kaiming he
- 【论文笔记】Deep Neural Decision Forests
- 【论文笔记】Recurrent Neural Network Regularization
- 论文笔记之Diffusion-Convolutional Neural Networks
- SRAM、DRAM、SDRAM等存储器的区别?
- 架构师的成功之路。。。
- iOS-创建自己的Signal工具类(一)
- 关于正则表达式表示浮点数记录
- nginx|apache|tomcat 配置htttps
- Neural Style论文笔记+源码解析
- ZCMU-1670-和费马开个玩笑(数据范围)
- iOS 16进制颜色使用
- SharedPreference的封装
- React入门到精通(二)——React基础实例
- Swift 面试题
- 水果编曲软件的安装教程
- HTTP网络协议(三)
- 通过一个小例子理解java回调机制