Advertisement

【Neural Style Transfer】Deep Photo Style Transfer(含代码详解)

阅读量:

2017CVPR: Deep Photo Style Transfer

      • Summary 总结
    • Approach 方法
      • Developed by Gatys et al., the Neural Style Algorithm represents a significant advancement in the field of generative algorithms.
  • Core idea: Photorealism Regularization

  • Core idea: Enhanced style loss combined with semantic segmentation

  • Through comprehensive analysis, we present a novel approach that combines both total variation loss and our innovative optimization strategy.

    复制代码
    * Results and Comparison 结果和对比
    * Code tf版本的代码

今天与大家分享的这篇论文属于实验室的一位小同学的研究内容。此前也有关于该领域的讨论交流。趁此机会将这篇论文整理出来与大家分享。对于尚未了解Neural Style Transfer(NST) 的读者朋友们,则建议配合我们专栏中的相关内容一起阅读[【专栏——深度学习与艺术

在这里插入图片描述

此外,按照老规矩来感受一下大神作者们⬇️的超高能力!

在这里插入图片描述

个人主页

Torch implementation

Torch implementation

PyTorch-based Implementation


Abstract 摘要

我们介绍了一种深度学习算法用于摄影风格迁移。该算法能够处理多种类型的图像内容,并且能够准确地传递参考风格特征。我们的方法借鉴了最新的绘画风格迁移技术,在其中,在神经网络的不同层级中分离了样式特征与图像内容特征。然而,在默认设置下这种方法难以实现逼真的 style 转换效果。即使输入和参考图片均为自然照片(如 day, weather, season),但输出结果往往呈现出艺术画作般的失真效果(如艺术编辑)。我们将颜色空间中的变换限制在局部仿射模型内,并将其约束条件以自定义的形式融入优化过程(附上原文)。进一步验证表明这种改进算法能够在不同光照条件、天气状况以及艺术风格编辑方面展现出良好的效果)。

This research outlines an innovative deep learning methodology for achieving artistic style transfer in photography by effectively managing diverse image content while preserving the essence of reference styles. The proposed method builds upon recent advancements in painterly transfer techniques, which isolate artistic style from image content through analysis of neural network layers. However, despite its promise, this method falls short when it comes to photorealistic style transfer. Even when both input and reference images are photographs, the output often retains visual artifacts reminiscent of traditional painting techniques. To address these limitations, our key contribution lies in imposing a constraint on the transformation process from input to output by ensuring it remains locally affine within colorspace. This novel approach encapsulates this requirement through a custom energy function tailored for differentiable computation. Our experimental results demonstrate that this method effectively mitigates visual artifacts and produces satisfactory photorealistic style transfers across various scenarios, including adjustments to time-of-day photography, weather conditions, seasonal variations, and incorporating artistic edits.


Method 方法

在引言部分不做过多详细阐述,在此仅做简要介绍

  • 结构保留 。我们希望实现一种强烈的局部效果:例如通过打开摩天大楼某处窗户的日光灯照射室内空间这种方式来达到(见图1)。另一方面这些效果不应导致边缘或规则图案变形:例如在确保窗户网格对齐的情况下维持原有的几何关系。
  • 语义精度和迁移保真度 。现实世界中的复杂场景为迁移带来了新的挑战:为了保持迁移的真实感和一致性"建筑外观"应与"天空背景"相协调一致而不是相互混淆。
    一种有效的方法是通过将风格图像中的最相似区域与每个输入神经元区域进行匹配从而最大限度地减少视觉转移可能带来的不准确结果。

基于这两个关键问题展开讨论会发现

  • 在优化阶段中,在目标函数中我们引入了实则正则化项来限定重构图像由输入的局部仿射颜色变换表示,并尽量避免图像变形。
  • 为了确保内容的匹配性,在样式转换过程中我们增加了可选指南来辅助实现这一目标,并显著提升了结果的真实感。
Background:Neural Style Algorithm by Gatys et al.

此图展示了Neural Style模型在优化过程中所涉及的总损失函数及其组成部分:其中L_{total}代表总损失;而L_c为内容loss(Content Loss),反映不同内容区域之间的差异;同时L_s则代表style loss(Style Loss),衡量不同风格元素间的差异程度)。在此过程中,请注意层别特征矩阵(Feature Matrix)中使用的符号表示为 F_l. 其对应的Gram matrix(Gram Matrix)则由 G_l = ...计算得到。在此过程中,请特别关注基于参数 \alpha_l, \beta_l$, 我们能够实现内容图像与风格图像之间的平衡

在这里插入图片描述
Core idea:Photorealism Regularization

解决结构保留问题

解决结构保留问题

在这里插入图片描述
Core idea:Augmented style loss with semantic segmentation

解决语义精度和迁移保真度问题

针对语义精确度与迁移保真性的问题展开探讨

在这里插入图片描述
Final Total Loss and Our Approach

基于所述背景及核心理念的基础上提出了一种名为Deep photo style transfer的方法。该方法的整体损失如下所述:总损失L_total由三部分组成:内容损失L_c^l(Content Loss)反映了图像的内容特征;风格损失L_{s+}^l(Augmented Style Loss)包含了额外引入的风格元素;图像真实感正则化损失L_m(Photorealism regularization)则用于提升生成图像的质量。其中,l代表某一层卷积层,在本研究中选择第4层卷积层(conv4_2)作为内容特征提取的基础;同时,为风格特征提取选择了第1至第5层卷积层(conv1_1, conv2_1, conv3_1, conv4_1, conv5_1)。

在这里插入图片描述

Results and Comparison 结果和对比

在这里插入图片描述
在这里插入图片描述

由于图像文件体积较大的原因,在处理过程中只能截取有限数量的结果作为案例进行初步体验。总体而言表现十分出色,并且在比较先进的算法包括Neural Style和CNNMRF等基准测试中均取得了超越性的效果。该方法在避免语义变形的问题上表现出色,并且基本保持了内容的完整性

在这里插入图片描述

论文中为不同区域分配不同的seg mask以实现风格迁移,并通过实验验证其有效性。


Code tf版本的代码

TensorFLow implementation

TensorFlow实现

复制代码
    python deep_photostyle.py --content_image_path <path_to_content_image> 
    						  --style_image_path <path_to_style_image> 
    						  --content_seg_path <path_to_content_segmentation> 
    						  --style_seg_path <path_to_style_segmentation> 
    						  --style_option 2

主文件名为deep_photostyle.py并包含三种不同的style_option设置。其中第一个选项(option 0)负责生成分割过程中的中间结果;第二个选项(option 1)则利用这些中间结果来完成最终图像的合成;第三个选项(option 2)则能够一次性完成整个图像处理流程。通常情况下,默认使用第三个选项(option 2)即可满足需求。

在这里插入图片描述

photo_style.py文件中有一些关键函数被定义,在机器视觉领域具有核心地位。这些函数包括但不限于content loss(内容损失)、style loss(风格损失)以及gram matrix(格拉姆矩阵)等技术指标。具体而言,content loss是通过计算权重与单个像素值平方差的平均值得出的一种衡量图像特征差异的方法。

复制代码
    tf.reduce_mean(tf.squared_difference(const_layer, var_layer)) * weight

首先,在计算Style loss的过程中,需要依次完成以下步骤:一是计算每个风格层对应的Gram Matrix,并对其平方差取平均;二是将这些平均值与对应的mask取平均相乘以获得各层差异总和diff_style_sum;三是将所有层的diff_style_sum相加得到layer_style_loss;最后将该总和乘以预设权重系数即可获得最终的Style loss值。

首先,在计算Style loss的过程中,需要依次完成以下步骤:一是计算每个风格层对应的Gram Matrix,并对其平方差取平均;二是将这些平均值与对应的mask取平均相乘以获得各层差异总和diff_style_sum;三是将所有层的diff_style_sum相加得到layer_style_loss;最后将该总和乘以预设权重系数即可获得最终的Style loss值。

复制代码
    diff_style_sum = tf.reduce_mean(tf.squared_difference(gram_matrix_const, gram_matrix_var)) * content_mask_mean
    layer_style_loss += diff_style_sum

Gram matrix的具体计算方法如下: activations指的是从卷积某一层中提取出来的特征图。 这个特征图是一个四维数组(batchsize, height, width, channels) 其中 batch size 为 1 因此 在这种情况下生成的 gram matrix 将成为一个二维数组

复制代码
    def gram_matrix(activations):
    height = tf.shape(activations)[1]
    width = tf.shape(activations)[2]
    num_channels = tf.shape(activations)[3]
    gram_matrix = tf.transpose(activations, [0, 3, 1, 2])
    gram_matrix = tf.reshape(gram_matrix, [num_channels, width * height])
    gram_matrix = tf.matmul(gram_matrix, gram_matrix, transpose_b=True)
    return gram_matrix

本次深度照片风格迁移技术分享圆满结束。如有理解偏差之处,请伙伴们多提宝贵意见。感谢一路陪伴和支持的伙伴们!

全部评论 (0)

还没有任何评论哟~