风格迁移(初版)


论文链接

算法简介

本质:定义一个损失函数,然后将这个损失最小化。这里loss可以分为两部分:内容损失和风格损失。

  1. 内容损失:可以被卷积神经网络更靠顶部的层的表示所捕捉到,因此将图像送入预训练的网络模型(本文使用VGG19在ImageNet上的训练参数),从顶部的一层的输出可以表示该图片内容,损失即可用生成的图片和原图片输出之间的差异来表示。
  2. 风格损失:风格需要用图片在多个空间尺度上提取的外观来表示,即在预训练的网络模型的不同层的输出结果的相互关系。在具体的计算中,使用了Gram矩阵。

卷积神经网络提取特征

  1. 每个卷积核提取不同的特征

  2. 每个卷积核对输入进行卷积,生成一个feature map .这个feature map即体现了该卷积核从输入中提取的特征,不同的featuremap显示了图像中不同的特征

层次特征
浅层卷积核提取边缘、颜色、斑块等底层像素特征
中层卷积核提取条纹、纹路、形状等中层纹理特征
高层卷积核提取眼睛、轮胎、文字等高层语义特征,最后的分类输出层输出最抽象的分类结果

在这里插入图片描述
固定VGG19网络参数,选取其的一部分layer构建一个module进行style和content特征提取。content照片和style照片分别在选取的layer进行特征过滤作为基准。目标照片(初始化为噪音生成)随着网络向前传播会在contentloss和styleloss层计算出loss,网络传递到末尾后,根据总的losses对目标照片求梯度,进行像素微调。然后迭代循环epochs次,即可生成目标照片。
缺点:

  1. 一种网络只能对应一种风格
  2. 由于没有预加载风格模型,每次都得循环微调生成目标照片,导致速度慢。

改进:

  1. 将风格打为标签,进行网络预训练
  2. 在1基础上,尝试一个网络对应多个风格(调参数进行风格选择),但这样无疑会添加网络的复杂度

完整代码

from __future__ import print_function #python版本兼容
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from PIL import Image
import matplotlib.pyplot as plt

import torchvision.transforms as transforms
import torchvision.models as models

import copy
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# 所需的输出图像大小
imsize = 512 if torch.cuda.is_available() else 128  # use small size if no gpu

loader = transforms.Compose([
    transforms.Resize(imsize),  # scale imported image
    transforms.ToTensor()])  # transform it into a torch tensor

def image_loader(image_name):
    image = Image.open(image_name)
    # fake batch dimension required to fit network's input dimensions
    image = loader(image).unsqueeze(0)
    return image.to(device, torch.float)

style_img = image_loader("./style.jpg")
#print(style_img.shape)
content_img = image_loader("./content.jpg")
#temp=image_loader("./style.jpg")
#print(content_img.shape)
#print(temp.shape)
#content_img.reshape(1,3,138,128)

#print(style_img.size())


assert style_img.size() == content_img.size(), \
    "we need to import style and content images of the same size"
#现在,让我们创建一个方法,通过重新将图片转换成PIL格式来展示,并使用plt.imshow展示它的拷贝。我们将尝试展示内容和风格图片来确保它们被正确的导入。

unloader = transforms.ToPILImage()  # reconvert into PIL image

plt.ion()

def imshow(tensor, title=None):
    image = tensor.cpu().clone()  # we clone the tensor to not do changes on it
    #tensor转cpu ,array show not tensor
    image = image.squeeze(0)      # remove the fake batch dime
    # nsion
    image = unloader(image) #转PIL格式进行显示
    plt.imshow(image)
    if title is not None:
        plt.title(title)
    plt.pause(0.001) # pause a bit so that plots are updated

plt.figure()#定义一张图片
imshow(style_img, title='Style Image')

plt.figure()
imshow(content_img, title='Content Image')
class ContentLoss(nn.Module):

    def __init__(self, target,):
        super(ContentLoss, self).__init__()
        # 我们从用于动态计算梯度的树中“分离”目标内容:
        # 这是一个声明的值,而不是变量。
        # 否则标准的正向方法将引发错误。
        self.target = target.detach()

    def forward(self, input):
        self.loss = F.mse_loss(input, self.target)
        return input
#这里解释下forward函数为什么返回input:这里添加的contentloss类并不是真正意义上的网络层,它并#没有那些weight、bias等参数对本层输入进行特征提取,只是计算它的上一层网络过滤得到的特征与风格基#准图片之间的loss,就是说这层contengtloss的出现不能改变原本下一层网络应该得到的输入。
def gram_matrix(input):
    a, b, c, d = input.size()  # a=batch size(=1)
    # 特征映射 b=number
    # (c,d)=dimensions of a f. map (N=c*d)

    features = input.view(a * b, c * d)  # resise F_XL into \hat F_XL

    G = torch.mm(features, features.t())  # compute the gram product

    # 我们通过除以每个特征映射中的元素数来“标准化”gram矩阵的值.
    return G.div(a * b * c * d)
class StyleLoss(nn.Module):

    def __init__(self, target_feature):
        super(StyleLoss, self).__init__()
        self.target = gram_matrix(target_feature).detach()

    def forward(self, input):
        G = gram_matrix(input)
        self.loss = F.mse_loss(G, self.target)
        return input
cnn = models.vgg19(pretrained=True).features.to(device).eval()
#mean=[0.485, 0.456, 0.406] and std=[0.229, 0.224, 0.225]来正则化?
# 图像其实是一种平稳的分布,减去数据对应维度的统计平均值,可以消除公共部分。
# 以凸显个体之前的差异和特征。
# 是因为使用了使用ImageNet的均值和标准差。使用Imagenet的均值和标准差是一种常见的做法。它们是根据数百万张图像计算得出的
#。如果要在自己的数据集上从头开始训练,则可以计算新的均值和标准差。否则,建议使用Imagenet预设模型及其平均值和标准差。
cnn_normalization_mean = torch.tensor([0.485, 0.456, 0.406]).to(device)
cnn_normalization_std = torch.tensor([0.229, 0.224, 0.225]).to(device)

# 创建一个模块来规范化输入图像
# 这样我们就可以轻松地将它放入nn.Sequential中
class Normalization(nn.Module):
    def __init__(self, mean, std):
        super(Normalization, self).__init__()
        # .view the mean and std to make them [C x 1 x 1] so that they can
        # directly work with image Tensor of shape [B x C x H x W].
        # B is batch size. C is number of channels. H is height and W is width.
        self.mean = torch.tensor(mean).view(-1, 1, 1)
        self.std = torch.tensor(std).view(-1, 1, 1)

    def forward(self, img):
        # normalize img
        return (img - self.mean) / self.std
# 期望的深度层来计算样式/内容损失:
content_layers_default = ['conv_4']
style_layers_default = ['conv_1', 'conv_2', 'conv_3', 'conv_4', 'conv_5']

def get_style_model_and_losses(cnn, normalization_mean, normalization_std,
                               style_img, content_img,
                               content_layers=content_layers_default,
                               style_layers=style_layers_default):
    cnn = copy.deepcopy(cnn)
    print("This is model':")
    print(cnn)
    print("~~~~~~~~~~~~~~~~~")

    # 规范化模块
    normalization = Normalization(normalization_mean, normalization_std).to(device)

    # 只是为了拥有可迭代的访问权限或列出内容/系统损失
    content_losses = []
    style_losses = []

    # 假设cnn是一个`nn.Sequential`,
    # 所以我们创建一个新的`nn.Sequential`来放入应该按顺序激活的模块
    model = nn.Sequential(normalization)

    i = 0  # increment every time we see a conv
    for layer in cnn.children():
        if isinstance(layer, nn.Conv2d):#判断一个对象是否是一个已知类型
            i += 1
            name = 'conv_{}'.format(i)
        elif isinstance(layer, nn.ReLU):
            name = 'relu_{}'.format(i)
            # 对于我们在下面插入的`ContentLoss`和`StyleLoss`,
            # 本地版本不能很好地发挥作用。所以我们在这里替换不合适的
            layer = nn.ReLU(inplace=False)
        elif isinstance(layer, nn.MaxPool2d):
            name = 'pool_{}'.format(i)
        elif isinstance(layer, nn.BatchNorm2d):#在卷积神经网络的卷积层之后总会添加BatchNorm2d进行数据的归一化处理,这使得数据在进行Relu之前不会因为数据过大而导致网络性能的不稳定
            name = 'bn_{}'.format(i)
        else:
            raise RuntimeError('Unrecognized layer: {}'.format(layer.__class__.__name__))

        model.add_module(name, layer)
        print(model)
        print('___________________')

        if name in content_layers:
            # 加入内容损失:
            target = model(content_img).detach()
            content_loss = ContentLoss(target)
            model.add_module("content_loss_{}".format(i), content_loss)
            content_losses.append(content_loss)

        if name in style_layers:
            # 加入风格损失:
            target_feature = model(style_img).detach()
            style_loss = StyleLoss(target_feature)
            model.add_module("style_loss_{}".format(i), style_loss)
            style_losses.append(style_loss)

    # 现在我们在最后的内容和风格损失之后剪掉了图层
    for i in range(len(model) - 1, -1, -1):
        if isinstance(model[i], ContentLoss) or isinstance(model[i], StyleLoss):
            break

    model = model[:(i + 1)] # 刷新model
    print("This is model':")
    print(model)
    print(style_losses)
    print(content_losses)
    return model, style_losses, content_losses
input_img = content_img.clone()
# 如果您想使用白噪声而取消注释以下行:
# input_img = torch.randn(content_img.data.size(), device=device)

# 将原始输入图像添加到图中:
plt.figure()
imshow(input_img, title='Input Image')
def get_input_optimizer(input_img):
    # 此行显示输入是需要渐变的参数,微调目标图像
    optimizer = optim.LBFGS([input_img.requires_grad_()])
    return optimizer

def run_style_transfer(cnn, normalization_mean, normalization_std,
                       content_img, style_img, input_img, num_steps=100,
                       style_weight=1000000, content_weight=1):
    """Run the style transfer."""
    print('Building the style transfer model..')
    model, style_losses, content_losses = get_style_model_and_losses(cnn,
        normalization_mean, normalization_std, style_img, content_img)
    optimizer = get_input_optimizer(input_img)

    print('Optimizing..')
    run = [0]
    print("This is run:")
    print(run)

    while run[0] <= num_steps:

        def closure():
            # 更正更新的输入图像的值
            input_img.data.clamp_(0, 1) #值放缩0~!

            optimizer.zero_grad()
            model(input_img)
            style_score = 0
            content_score = 0

            for sl in style_losses:
                style_score += sl.loss
            for cl in content_losses:
                content_score += cl.loss

            style_score *= style_weight
            content_score *= content_weight

            loss = style_score + content_score
            loss.backward()

            run[0] += 1
            if run[0] % 50 == 0:
                print("run {}:".format(run))
                print('Style Loss : {:4f} Content Loss: {:4f}'.format(
                    style_score.item(), content_score.item()))
                print()

            return style_score + content_score

        optimizer.step(closure)

    # 最后的修正......
    input_img.data.clamp_(0, 1)

    return input_img
if __name__=="__main__":
    output = run_style_transfer(cnn, cnn_normalization_mean, cnn_normalization_std,
                            content_img, style_img, input_img)

    plt.figure()
    imshow(output, title='Output Image')

    # sphinx_gallery_thumbnail_number = 4
    plt.ioff()
    plt.show()
  

效果展示

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

DL/ML/NLP交流学习群

在这里插入图片描述

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZCAIHUI_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值