利用神经网络进行艺术风格迁移

本文介绍了如何使用神经网络实现艺术风格迁移的技术原理。通过提取图像的“风格”和“内容”,并利用VGG网络和梯度下降等方法,可以将任意图像转换成具有特定艺术风格的新图像。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文链接:
https://jvns.ca/blog/2017/02/12/neural-style/
How do these “neural network style transfer” tools work?

是否能利用机器学习来自动把我画的画变得更有艺术感?

于是我决定花点时间去理解一下如何实现,重要的是里面的数学原理,而不仅仅是直觉的感受。我从A Neural Algorithm of Artistic Style文章开始,因为它写得简明扼要。

这篇文章的作者另外还建立了一个在线艺术风格迁移的网站,deepart.io

这里写图片描述

“风格”和“内容”
这篇论文的核心思想就是,你能从一副图像中提取出“风格style”(比如梵高的夜空风格)以及“内容content”(比如你能在画中看出河边有匹马)。你可以告诉机器,”把A用B的风格再画一遍“。

这样讲还不够清楚。神经网络干的事都是数学,所以是怎么用数学定义“风格”呢?这就是这篇文章试图做的事,我们在此简单解释一下。

一个用于分类识别的网络
这些有艺术感的神经网络,倾向于直接包含一个特别的神经网络。这个被用于风格迁移的特殊神经网络在这篇文章中有介绍,出自Oxford大学的VGG团队。

这个网络的目的不是去创造什么有艺术感的东西。它的目的是图像识别,能够识别出图像中是猫还是马。

这个特别的网络赢得了ImageNet2014年的图像识别大赛识别定位组的冠军。
定位,就是说你必须在图片中识别出物体,并且指明这个物体在哪。

我们要定义”风格“和”内容“,那这个网络又是怎么帮助我们去定义呢?

”内容“和”风格“的数学定义
你可以在这里看到神经网络的结构

这里写图片描述

当我们把一张图片输入到这个网络中,会在开始处变成一系列向量(每个像素上包含 红/绿/蓝 三值,代表了图像长什么样)。而在网络的每层中,我们能得到中间向量,它们并没有内在的含义。

但是!如果我们想,我们还是能够随意从中间向量中挑出几个说“你看,我觉得这几个就是代表图像’内容‘的。”

在那篇文章中,他们就把“conv4_2”层当作图像的“内容”层。这看起来有点随意,它不就是网络里比较深的一层嘛。

而定义“风格”就有点复杂了。如果我理解正确的话,定义“风格”这件事,正是那篇文章的独特之处——他们不是就挑了一层说“这个就是‘风格’层”。他们把一层中的所有特征图都拿来(针对每个特征,每一层基本上都有大量的向量),然后对那些向量两两作内积,即求Gram矩阵。这个Gram矩阵就是风格。

我还是不能完全理解内积这件事。这篇文章里可能有更好的解释:Texture Synthesis Using Convolution Neural Networks

在文章中,他们对不同的看起来能代表风格的层(conv1_1,conv2_1,conv3_1,以及conv4_1)做了实验,也得到了不同的结果。

所以,我们就把conv3_1的Gram矩阵当作“风格”吧,把conv4_2的向量当作图像的“内容”吧。

用油画的风格画你的房子
我们已经定义好了什么是“风格”,什么是“内容”。

我现在有一副画,画的是在河边有很多房子。这幅画里有我想要的内容(就是你在神经网络中的conv4_2层得到的向量)。
另外还有一副梵高的《星空》。这幅画里有我想要的风格,就是当你把画塞进神经网络后,在conv3_1层作Gram矩阵得到的向量。

所以!是否能得到一副画,同时包含“河边有很多房子”的内容,和《星空》的风格呢?开篇提到的文章就做到了。

我们先从一些白噪声开始,把它们当作我们生成图像的基础,然后定义一个损失函数(“how different is the style from the painting, and how different is the content from the photo?”损失函数描述生成图像与风格图像及内容图像之间的差异)。在文章中有公式7如下:
这里写图片描述

然后我们利用梯度下降原理去改变我们的白噪声图像,直到我们能使上面的损失函数值达到最小值。我的感受是,梯度下降速度是很快的,所以我们的确能够快速得到一副图像,它既有“河边房子”的内容,也有《星空》的风格。

或许你奇怪的直觉是对的

令我感到激动的是,这件事我本以为是不可行的。所以或许我对神经网络和艺术之间的其他奇怪想法,都是有可能实现的。当然我得尽可能地去尝试,直到我找到一个合乎逻辑的数学方法去实现他们!我其实没多少时间去做神经网络的实验,但是我或许能找到些别人做的。The Unreasonable Effectiveness of Recurrent Neural Networks 这篇文章值得读一读。

如果你想了解更多数学上的细节,还是要看看开篇提到的文章。它讲的清晰明了,尽管我还是有点不太理解为何那样定义风格。(取决于你对偏导数的理解程度)。我可能在这方面理解会有些问题,毕竟我刚接触神经网络,但我觉得是对的。如果有任何错误情联系我。

这个针对生成音乐和艺术的讲座值得看一下。

补充待续…

A free, fully-featured, and extensible tool for automating any web or desktop application. Automation software that drives user interface like a human. How does it work? With UiPath you can create windows automation software that manipulates applications, akin to how a human uses the computer. You can create an automation robot as easy as you train a human user, by simply indicating on the screen what the steps of the automation are and assembling them into a visual flowchart diagram. Anyone can understand a flowchart, and automation is just as intuitive. A free, fully-featured, and extensible tool for automating any web or desktop application. UiPath Studio Community is free for individual developers, small professional teams, education and training purposes UiPath enables organizations to configure software robots that automate manual, repetitive rules-based tasks at a fraction of the cost of their human equivalent, and integrate without disruption the legacy system. Desktop Automation. UiPath Studio introduces a visual, declarative way of describing how to automate a process, and you can use it in the same way you use a Visio diagram. When working with the presentation layer of other apps, you simply indicate on the screen what operation you need to perform. UiPath understands the UI at the logical control level and does not rely on the position of elements on screen. This makes automation much more reliable and independent of screen-size and resolution. Most advanced Screen Scraping Technology. UiPath features an innovative technique for extracting text from running apps, even if they are hidden or covered by another app. Web scraping is a premier feature of the screen-scraping landscape, as there are dedicated methods for extracting pattern-based data that span multiple web pages. Macro Recorder - The world's most advanced windows macro recorder. You create a Windows macro with the UiPath recorder in the same way that you would train a human user to use an application. Indicate directly on the screen where to click, type, select, copy, paste, or perform any other common action and UiPath will generate a visual script like in the image below. UiPath's macro recorder allows you to record mouse events and keyboard activities to generate automation scripts. The arrangement of the activities is on the sequence of actions being performed in the screen. This sequence is saved in your workflow and you can use later playback the recorded actions.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值