外行学python有用吗_图像风格迁移也有框架了:使用Python编写,与PyTorch完美兼容,外行也能用...

来源:机器之心

作者:Philip Meier

机器之心编译

编辑:陈萍

易于使用的神经风格迁移框架 pystiche。2530c56ce465e1b4bf08ed22b9823674.png

2530c56ce465e1b4bf08ed22b9823674.png

将内容图片与艺术风格图片进行融合,生成一张具有特定风格的新图,这种想法并不新鲜。早在 2015 年,Gatys、 Ecker 以及 Bethge 开创性地提出了神经风格迁移(Neural Style Transfer ,NST)。

不同于深度学习,目前 NST 还没有现成的库或框架。因此,新的 NST 技术要么从头开始实现所有内容,要么基于现有的方法实现。但这两种方法都有各自的缺点:前者由于可重用部分的冗长实现,限制了技术创新;后者继承了 DL 硬件和软件快速发展导致的技术债务。

最近,新项目 pystiche 很好地解决了这些问题,虽然它的核心受众是研究人员,但其易于使用的用户界面为非专业人员使用 NST 提供了可能。

pystiche 是一个用 Python 编写的 NST 框架,基于 PyTorch 构建,并与之完全兼容。相关研究由 pyOpenSci 进行同行评审,并发表在 JOSS 期刊 (Journal of Open Source Software) 上。2290f6dbbfbf03146aecc9ebe6176c7f.png

2290f6dbbfbf03146aecc9ebe6176c7f.png

论文地址:https://joss.theoj.org/papers/10.21105/joss.02761

项目地址:https://github.com/pmeier/pystiche

在深入实现之前,我们先来回顾一下 NST 的原理。它有两种优化方式:基于图像的优化和基于模型的优化。虽然 pystiche 能够很好地处理后者,但更为复杂,因此本文只讨论基于图像的优化方法。

在基于图像的方法中,将图像的像素迭代调整训练,来拟合感知损失函数(perceptual loss)。感知损失是 NST 的核心部分,分为内容损失(content loss)和风格损失(style loss),这些损失评估输出图像与目标图像的匹配程度。与传统的风格迁移算法不同,感知损失包含一个称为编码器的多层模型,这就是 pystiche 基于 PyTorch 构建的原因。

如何使用 pystiche

让我们用一个例子介绍怎么使用 pystiche 生成神经风格迁移图片。首先导入所需模块,选择处理设备。虽然 pystiche 的设计与设备无关,但使用 GPU 可以将 NST 的速度提高几个数量级。

模块导入与设备选择:

import torch

import pystiche

from pystiche import demo, enc, loss, ops, optim

print(f"pystiche=={pystiche.__version__}")

device = torch.device("cuda"if torch.cuda.is_available() else"cpu")

输出:

pystiche==0.7.0

多层编码器

content_loss 和 style_loss 是对图像编码进行操作而不是图像本身,这些编码是由在不同层级的预训练编码器生成的。pystiche 定义了 enc.MultiLayerEncoder 类,该类在单个前向传递中可以有效地处理编码问题。该示例使用基于 VGG19 架构的 vgg19_multi_layer_encoder。默认情况下,它将加载 torchvision 提供的权重。

多层编码器:

multi_layer_encoder = enc.vgg19_multi_layer_encoder()

print(multi_layer_encoder)

输出:

VGGMultiLayerEncoder(

arch=vgg19, framework=torch, allow_inplace=True

(preprocessing): TorchPreprocessing(

(0): Normalize(

mean=('0.485', '0.456', '0.406'),

std=('0.229', '0.224', '0.225')

)

)

(conv1_1): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

(relu1_1): ReLU(inplace=True)

(conv1_2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

(relu1_2): ReLU(inplace=True)

(pool1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)

(conv2_1): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

(relu2_1): ReLU(inplace=True)

(conv2_2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

(relu2_2): ReLU(inplace=True)

(pool2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)

(conv3_1): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

(relu3_1): ReLU(inplace=True)

(conv3_2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

(relu3_2): ReLU(inplace=True)

(conv3_3): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

(relu3_3): ReLU(inplace=True)

(conv3_4): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

(relu3_4): ReLU(inplace=True)

(pool3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)

(conv4_1): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

(relu4_1): ReLU(inplace=True)

(conv4_2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

(relu4_2): ReLU(inplace=True)

(conv4_3): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

(relu4_3): ReLU(inplace=True)

(conv4_4): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

(relu4_4): ReLU(inplace=True)

(pool4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)

(conv5_1): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

(relu5_1): ReLU(inplace=True)

(conv5_2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

(relu5_2): ReLU(inplace=True)

(conv5_3): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

(relu5_3): ReLU(inplace=True)

(conv5_4): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

(relu5_4): ReLU(inplace=True)

(pool5): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)

)

感知损失

pystiche 将内容损失和风格损失定义为操作符。使用 ops.FeatureReconstructionOperator 作为 content_loss,直接与编码进行对比。如果编码器针对分类任务进行过训练,如该示例中这些编码表示内容。对于content_layer,选择 multi_layer_encoder 的较深层来获取抽象的内容表示,而不是许多不必要的细节。

content_layer = "relu4_2"

encoder = multi_layer_encoder.extract_encoder(content_layer)

content_loss = ops.FeatureReconstructionOperator(encoder)

pystiche 使用 ops.GramOperator 作为 style_loss 的基础,通过比较编码各个通道之间的相关性来丢弃空间信息。这样就可以在输出图像中的任意区域合成风格元素,而不仅仅是风格图像中它们所在的位置。对于 ops.GramOperator,如果它在浅层和深层 style_layers 都能很好地运行,则其性能达到最佳。

style_weight 可以控制模型对输出图像的重点——内容或风格。为了方便起见,pystiche 将所有内容包装在 ops.MultiLayerEncodingOperator 中,该操作处理在同一 multi_layer_encoder 的多个层上进行操作的相同类型操作符的情况。

style_layers = ("relu1_1", "relu2_1", "relu3_1", "relu4_1", "relu5_1")

style_weight = 1e3

def get_encoding_op(encoder, layer_weight):

return ops.GramOperator(encoder, score_weight=layer_weight)

style_loss = ops.MultiLayerEncodingOperator(

multi_layer_encoder, style_layers, get_encoding_op, score_weight=style_weight,

)

loss.PerceptualLoss 结合了 content_loss 与 style_loss,将作为优化的标准。

criterion = loss.PerceptualLoss(content_loss, style_loss).to(device)

print(criterion)

输出:

PerceptualLoss(

(content_loss): FeatureReconstructionOperator(

score_weight=1,

encoder=VGGMultiLayerEncoder(

layer=relu4_2,

arch=vgg19,

framework=torch,

allow_inplace=True

)

)

(style_loss): MultiLayerEncodingOperator(

encoder=VGGMultiLayerEncoder(

arch=vgg19,

framework=torch,

allow_inplace=True

),

score_weight=1000

(relu1_1): GramOperator(score_weight=0.2)

(relu2_1): GramOperator(score_weight=0.2)

(relu3_1): GramOperator(score_weight=0.2)

(relu4_1): GramOperator(score_weight=0.2)

(relu5_1): GramOperator(score_weight=0.2)

)

)

图像加载

首先加载并显在 NST 需要的目标图片。因为 NST 占用内存较多,故将图像大小调整为 500 像素。

size = 500

images = demo.images()

content_image = images["bird1"].read(size=size, device=device)

criterion.set_content_image(content_image)

03ab0ca3431affec66dace2d6d9fc9ea.png

03ab0ca3431affec66dace2d6d9fc9ea.png

内容图片

style_image = images["paint"].read(size=size, device=device)

criterion.set_style_image(style_image)

890d6eae39117198b02f31226874a104.png

890d6eae39117198b02f31226874a104.png

风格图片

神经风格迁移

创建 input_image。从 content_image 开始执行 NST,这样可以实现快速收敛。image_optimization 函数是为了方便,也可以由手动优化循环代替,且不受限制。如果没有指定,则使用 torch.optim.LBFGS 作为优化器。

input_image = content_image.clone()

output_image = optim.image_optimization(input_image, criterion, num_steps=500)92dd4536d51bfc22b0c9a1607ca72d50.png

92dd4536d51bfc22b0c9a1607ca72d50.png

不氪金玩转中文超大规模预训练!

12月22日20:00,百度自然语言处理部资深研发工程师硕环老师将在第二期直播《NLP开发利器解析:中文超大规模预训练模型精讲》中介绍:

语义理解技术简介

基于预训练的语义理解技术

73f0740f8600a59e093d12ad7b35cfbc.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值