elman神经网络 python实现_使用深度神经网络进行风格转换(Python实现)

article/2025/11/4 0:02:05

在论文(Image Style Transfer Using Convolutional Neural Networks)中(https://www.cv-foundation.org/openaccess/content_cvpr_2016/papers/Gatys_Image_Style_Transfer_CVPR_2016_paper.pdf),风格转换使用了19层VGG网络中的特征,它由一系列卷积层和池化层以及几个全连接层组成。在下面的图像中,卷积层是根据堆栈及其在堆栈中的顺序命名的。Conv_1_1是图像在第一个堆栈中通过的第一个卷积层。Conv_2_1是第二个堆栈中的第一个卷积层。网络中卷积层最深的是conv_5_4。

d999f31adeed3bc7c2648a1123456177.png

VGG19中的卷积层堆栈

分离风格与内容

风格转换依赖于分离图像的内容和风格。给定一个内容图像和一个风格图像,我们的目标是创建一个新的目标图像,该图像应该包含我们想要的内容和风格组件:

  • 对象及其排列与内容图像相似
  • 样式、颜色和纹理与风格图像相似

下面是一个例子,内容图像是一只猫,风格图像是葛饰北斋的巨浪。所生成的目标图像仍然包含猫,但是用波浪、蓝色和米色的颜色进行了风格化处理。

ae8928ba6ddf3a978bc0af404946aee1.png

在本文中,我们将使用一个预训练的VGG19网络从传入的图像中提取内容或风格特征。然后,我们将形成内容和风格损失的概念,并使用这些损失来迭代地更新我们的目标图像,直到得到我们想要的结果。

# import resources%matplotlib inlinefrom PIL import Imageimport matplotlib.pyplot as pltimport numpy as npimport torchimport torch.optim as optimfrom torchvision import transforms, models
a98ef309ca5fbbd26624656314c74dd7.png

加载VGG19(特征)

VGG19分为两部分:

  • vgg19.features,卷积层和池化层
  • vgg19.classifier,最后的三个线性、分类器层

我们只需要特征部分,我们将加载并“冻结”权重。

# get the "features" portion of VGG19 (we will not need the "classifier" portion)vgg = models.vgg19(pretrained=True).features# freeze all VGG parameters since we're only optimizing the target imagefor param in vgg.parameters(): param.requires_grad_(False)# move the model to GPU, if availabledevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")
830911fd3354c37203f53eb5b0ae710d.png

加载内容和风格图像

你可以载入任何你想要的图片!下面,我们提供了一个辅助函数,用于加载任何类型和大小的图像。load_image函数还将图像转换为归一化张量。

def load_image(img_path, max_size=400, shape=None): ''' Load in and transform an image, making sure the image is <= 400 pixels in the x-y dims.'''  image = Image.open(img_path).convert('RGB')  # large images will slow down processing if max(image.size) > max_size: size = max_size else: size = max(image.size)  if shape is not None: size = shape  in_transform = transforms.Compose([ transforms.Resize(size), transforms.ToTensor(), transforms.Normalize((0.485, 0.456, 0.406),  (0.229, 0.224, 0.225))]) # discard the transparent, alpha channel (that's the :3) and add the batch dimension image = in_transform(image)[:3,:,:].unsqueeze(0)  return image
f8af32187fcae5f66ee89b3dab9a4c38.png

接下来,我将按文件名加载图像,并强制风格图像与内容图像的大小相同。Python代码如下:

# load in content and style imagecontent = load_image('images/octopus.jpg').to(device)# Resize style to match content, makes code easierstyle = load_image('images/hockney.jpg', shape=content.shape[-2:]).to(device)# helper function for un-normalizing an image # and converting it from a Tensor image to a NumPy image for displaydef im_convert(tensor): """ Display a tensor as an image. """  image = tensor.to("cpu").clone().detach() image = image.numpy().squeeze() image = image.transpose(1,2,0) image = image * np.array((0.229, 0.224, 0.225)) + np.array((0.485, 0.456, 0.406)) image = image.clip(0, 1) return image# display the imagesfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 10))# content and style ims side-by-sideax1.imshow(im_convert(content))ax2.imshow(im_convert(style))
e82cda13daf059652526dd7df72386bf.png
ca780dd6c56567583d56ba2b7f844ee1.png

VGG19 Layers

要获得图像的内容和风格表示,我们必须通过VGG19网络向forward through图像,直到到达所需的层,然后从该层获得输出。

print(vgg)
72bde03fa5ff29a691476e370a49024a.png

内容和风格特征

TODO:完成将层名称映射到文章中用于内容表示和风格表示的名称。

def get_features(image, model, layers=None): """ Run an image forward through a model and get the features for  a set of layers. Default layers are for VGGNet matching Gatys et al (2016) """  ## TODO: Complete mapping layer names of PyTorch's VGGNet to names from the paper ## Need the layers for the content and style representations of an image if layers is None: layers = {'0': 'conv1_1', '5': 'conv2_1', '10': 'conv3_1', '19': 'conv4_1', '28': 'conv5_1', '21': 'conv4_2'}   ## -- do not need to change the code below this line -- ## features = {} x = image # model._modules is a dictionary holding each module in the model for name, layer in model._modules.items(): x = layer(x) if name in layers: features[layers[name]] = x  return features
a7657e715cff16a9f1418f2f337d8324.png

Gram矩阵

每个卷积层的输出都是一个张量,张量的维数与batch_size、深度d以及高度(h)和宽度( w)相关,卷积层的Gram矩阵计算如下:

  • 使用batch_size,d,h,w = tensort . size,获取张量的深度、高度和宽度
  • Reshape张量,使空间维度flattened
  • 计算g矩阵的方法是用重塑的张量乘以它的转置

注意:您可以使用torch.mm(matrix1 matrix2)将两个矩阵相乘。

TODO:完成gram_matrix函数

def gram_matrix(tensor): """ Calculate the Gram Matrix of a given tensor  Gram Matrix: https://en.wikipedia.org/wiki/Gramian_matrix """  ## get the batch_size, depth, height, and width of the Tensor batch_size, d, h, w = tensor.size()  ## reshape it, so we're multiplying the features for each channel tensor = tensor.view(tensor.shape[1], -1)  ## calculate the gram matrix gram = torch.mm(tensor, torch.t(tensor))  return gram
cda1a1892816e197adc1d0250f40f6f0.png

把它们放在一起

现在我们已经编写了提取特征和计算给定卷积层的矩阵的函数;让我们把这些放在一起!我们将从图像中提取我们的特征,并在风格表示中计算每个层的gram矩阵。

# get content and style features only once before forming the target imagecontent_features = get_features(content, vgg)style_features = get_features(style, vgg)# calculate the gram matrices for each layer of our style representationstyle_grams = {layer: gram_matrix(style_features[layer]) for layer in style_features}# create a third "target" image and prep it for change# it is a good idea to start of with the target as a copy of our *content* image# then iteratively change its styletarget = content.clone().requires_grad_(True).to(device)
1e3e708256ffd501614ea81a94b4a953.png

损失和权重

单层样式权重

下面,您可以选择在每个相关层对风格表示进行加权。建议您使用0-1之间的范围来对这些层进行加权。通过对前面的层(conv1_1和conv2_1)进行更多的加权,您可以期望在最终的目标图像中得到更大风格特征。如果您选择对后面的层进行加权,那么您将更加强调较小的特征。这是因为每一层都有不同的大小,它们一起创建了多尺度的风格表示!

内容和风格权重

就像在论文中一样,我们定义了一个alpha (content_weight)和一个beta (style_weight)。这个比例会影响最终图像的风格化程度。建议您保留content_weight = 1并设置style_weight以达到您想要的比例。

# weights for each style layer # weighting earlier layers more will result in *larger* style artifacts# notice we are excluding `conv4_2` our content representationstyle_weights = {'conv1_1': 1., 'conv2_1': 0.8, 'conv3_1': 0.5, 'conv4_1': 0.3, 'conv5_1': 0.1}# you may choose to leave these as iscontent_weight = 1 # alphastyle_weight = 1e6 # beta
346d8bf8a7bbe816b1014e0ded0e914e.png

更新目标并计算损失

您将决定更新图像的步骤,这类似于您之前看到的训练循环,只是我们正在更改目标图像而不是VGG19或任何其他图像。因此,步骤的数量取决于你的设置!我建议使用至少2000步以获得良好效果。但是,如果您只是测试不同的权重值或尝试不同的图像,您可能希望以较少的步骤开始

在迭代循环中,您将计算内容和风格损失,并相应地更新目标图像。

内容损失

在conv4_2层,内容损失将是目标和内容特征之间的均方差。这可以计算如下:

content_loss = torch.mean((target_features['conv4_2'] - content_features['conv4_2'])**2)

风格损失

风格损失的计算方法与此类似,只是您必须遍历许多层,这些层由字典style_weights中的名称指定。

您将计算目标图像的gram矩阵、target_gram和每个层的样式图像style_gram,并比较这些gram矩阵,计算layer_style_loss。

全部损失

最后,通过将风格和内容损失相加并使用指定的alpha和beta对它们进行加权,您将创建总损失!

如果损失很大,不要惊慌。图像样式的改变需要一些时间,您应该关注目标图像的外观,而不是任何损失。但是,您应该看到随着迭代次数的增加,这种损失会减少。

# for displaying the target image, intermittentlyshow_every = 20# iteration hyperparametersoptimizer = optim.Adam([target], lr=0.003)steps = 100 # decide how many iterations to update your image (5000)for ii in range(1, steps+1):  ## TODO: get the features from your target image  ## Then calculate the content loss target_features = get_features(target, vgg) content_loss = torch.mean((target_features['conv4_2'] - content_features['conv4_2'])**2)  # the style loss # initialize the style loss to 0 style_loss = 0 # iterate through each style layer and add to the style loss for layer in style_weights: # get the "target" style representation for the layer target_feature = target_features[layer] _, d, h, w = target_feature.shape  ## TODO: Calculate the target gram matrix target_gram = gram_matrix(target_feature)  ## TODO: get the "style" style representation style_gram = style_grams[layer] ## TODO: Calculate the style loss for one layer, weighted appropriately layer_style_loss = style_weights[layer] * torch.mean((target_gram - style_gram)**2)  # add to the style loss style_loss += layer_style_loss / (d * h * w)   ## TODO: calculate the *total* loss total_loss = content_weight * content_loss + style_weight * style_loss  ## -- do not need to change code, below -- ## # update your target image optimizer.zero_grad() total_loss.backward() optimizer.step()  # display intermediate images and print the loss if ii % show_every == 0: print('Total loss: ', total_loss.item()) plt.imshow(im_convert(target)) plt.show()
06b65100d3e75c95cf506dadbb4739fd.png
d1af21cb11738ee1b8b87589f1221f9e.png
b81194a98c1a70c516b6bc9656fa0800.png

显示目标图像

# display content and final, target imagefig, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 10))ax1.imshow(im_convert(content))ax2.imshow(im_convert(target))
51e062a9924b7ebab12a06a614ead75c.png
cdd24e1095574278b4bc14d1b122fe0d.png

http://chatgpt.dhexx.cn/article/y893g8iR.shtml

相关文章

深度学习知识图谱笔记

最近花了几天时间对目前常用神经网络模型(backbone)&#xff0c;生成对抗GAN&#xff0c;模型压缩&#xff0c;NPL&#xff0c;距离计算&#xff0c;优化器进行了分类整理&#xff0c;用简短的几句话对相关特性进行了总结描述&#xff0c;如有误请指正&#xff0c;方便强化记忆…

深度强化学习综述论文 A Brief Survey of Deep Reinforcement Learning

A Brief Survey of Deep Reinforcement Learning 深度强化学习的简要概述 作者&#xff1a; Kai Arulkumaran, Marc Peter Deisenroth, Miles Brundage, Anil Anthony Bharath 文章目录 摘要 Abstract1. 引言 Introduction2. 奖励驱动行为 Reward-Driven Behavior2.1. 马尔科夫…

深度学习领域,最惊艳的论文!

科研路上我们往往会读到让自己觉得想法很惊艳的论文&#xff0c;心中对不同的论文也会有一个排名&#xff0c;以下介绍了一些知乎作者心中白月光般存在的深度学习领域论文&#xff0c;看看是否你们拥有同样心目中的The one。 提名一 ResNet和Transformer 作者&#xff1a;王…

深度学习领域,你心目中 idea 最惊艳的论文是哪篇?

点击上方“视学算法”&#xff0c;选择加"星标"或“置顶” 重磅干货&#xff0c;第一时间送达 来源丨知乎问答&#xff08;回答均已授权&#xff09; 编辑丨极市平台 科研路上我们往往会读到让自己觉得想法很惊艳的论文&#xff0c;心中对不同的论文也会有一个排名&a…

深度学习:STGCN学习笔记

目录标题 基于图神经网络的图分类问题GCN在行为识别领域的应用主要任务研究思路 ST-GCN(SpatialTemporal Graph Convolutional Networks for Skeleton-Based Action Recognition)解读原论文解决问题主要贡献核心思想简介OpenPose 预处理基于人体关键点构造graph构造单帧graph&a…

神经网络中神经元是什么_是什么使神经网络脆弱

神经网络中神经元是什么 What do the images below have in common? 以下图片有什么共同点&#xff1f; Most readers will quickly catch on that they are all seats, as in places to sit. It may have taken you less than a second to recognize this common characteri…

卷积神经网络超详细介绍

文章目录 1、卷积神经网络的概念2、 发展过程3、如何利用CNN实现图像识别的任务4、CNN的特征5、CNN的求解6、卷积神经网络注意事项7、CNN发展综合介绍8、LeNet-5结构分析9、AlexNet10、ZFNet10.1 意义10.2 实现方法10.3 训练细节10.4 卷积网络可视化10.6 总结 11、VGGNet11.1 结…

经典神经网络

文章目录 第四章 经典网络解读4.1 LeNet-54.1.1 模型介绍4.1.2 模型结构4.1.3 模型特性 4.2 AlexNet4.2.1 模型介绍4.2.2 模型结构4.2.3 模型特性 4.3 ZFNet4.3.1 模型介绍4.3.2 模型结构4.3.3 模型特性 4.4 Network in Network4.4.1 模型介绍4.4.2 模型结构4.4.3 模型特点 4.5…

卷积神经网络 CNN 学习

什么是神经网络 人工神经网络&#xff08;artificial neural network&#xff0c;ANN&#xff09;&#xff0c;简称神经网络&#xff08;neural network&#xff0c;NN&#xff09;&#xff0c;是一种模仿生物神经网络的结构和功能的数学模型或计算模型。神经网络由大量的人工…

【CS224W】(task7)标签传播与节点分类(semi-supervised)

note 对某一节点的标签进行预测&#xff0c;需要其本身特征、邻居的标签和特征。message passing的假设是图中相似的节点之间会存在链接&#xff0c;也就是相邻节点有标签相同的倾向。这种现象可以用homophily&#xff08;相似节点倾向于聚集&#xff09;、influence&#xff…

4.经典网络

文章目录 第四章 经典网络解读4.1 LeNet-54.1.1 模型介绍4.1.2 模型结构4.1.3 模型特性 4.2 AlexNet4.2.1 模型介绍4.2.2 模型结构4.2.3 模型特性 4.3 ZFNet4.3.1 模型介绍4.3.2 模型结构4.3.3 模型特性 4.4 Network in Network4.4.1 模型介绍4.4.2 模型结构4.4.3 模型特点 4.5…

Python 深度学习

Pytorch 一 、深度学习概览1、工具篇2、流程介绍3、基础知识&#xff08;常用操作&#xff09;1、数据结构类型 4、常见名词概念 二、深度学习Pytorch1、神经网络1.1 如何构建神经网络1.2 核心组件 2、数据处理工具2.1 torchvision&#xff08;可视化处理工具&#xff09;2.1.1…

神经网络与深度学习作业8:RNN - 简单循环网络

1. 使用Numpy实现SRN import numpy as npinputs np.array([[1., 1.],[1., 1.],[2., 2.]]) # 初始化输入序列 print(inputs is , inputs)state_t np.zeros(2, ) # 初始化存储器 print(state_t is , state_t)w1, w2, w3, w4, w5, w6, w7, w8 1., 1., 1., 1., 1., 1., 1., 1.…

深度神经网络回归_深度神经网络

深度神经网络回归 深度神经网络 (Deep Neural Networks) A deep neural network (DNN) is an ANN with multiple hidden layers between the input and output layers. Similar to shallow ANNs, DNNs can model complex non-linear relationships. 深度神经网络(DNN)是在输入和…

DNN深度神经网络、RBM受限玻尔兹曼机、DBN深度置信网络

DNN前向传播算法和反向传播算法 感知机的模型大家都比较熟悉&#xff0c;它是一个有若干输入和一个输出的模型&#xff0c;如下图: 输出和输入之间学习到一个线性关系&#xff0c;得到中间输出结果&#xff1a; 接着是一个神经元激活函数: 从而得到我们想要的输出结果1或者-…

十道CSS+HTML高频企业级面试题

有句古话说得好&#xff0c;面试造火箭&#xff0c;工作拧螺丝。经历过职场的小伙伴都清楚&#xff0c;对于一般的工作需求&#xff0c;用不到太过高深的技术&#xff0c;但是&#xff0c;往往面试过程中&#xff0c;会进行所谓深层次的技术交流&#xff0c;所以&#xff0c;跳…

详细前端面试题HTML篇

CSS篇 JS篇 Vue篇 TypeScript篇 React篇 微信小程序篇 前端面试题汇总大全&#xff08;含答案超详细&#xff0c;HTML,JS,CSS汇总篇&#xff09;-- 持续更新 前端面试题汇总大全二&#xff08;含答案超详细&#xff0c;Vue&#xff0c;TypeScript&#xff0c;React&…

前端面试题---html/css

文章目录 1. html标签的类型&#xff08;head&#xff0c; body&#xff0c;&#xff01;Doctype&#xff09; 他们的作用是什么2. 在head标签里面的标签的作用分别是啥&#xff1f;3. 在 HTML 中插入 css 样式表的方法4. 比较插入 css 样式的链接方式和导入方式5. html5 新特性…

HTML 常见面试题

一、HTML5&#xff08;超文本标记语言&#xff0c;第五次重大修改&#xff09; 二、HTML5新特性 ①&#xff1a;新的语义标签 header footer nav aside article section ②&#xff1a;新的表单控件 calendar date time email url search ③&#xff1a;音频、视频&#xff08;…

经典HTML前端面试题总结

经典HTML前端面试题总结 1. 1简述一下你对 HTML 语义化的理解&#xff1f;.1.2 标签上 title 与 alt 属性的区别是什么&#xff1f;1.3 iframe的优缺点&#xff1f;1.4 href 与 src&#xff1f;1.5 HTML、XHTML、XML有什么区别1.6 知道img的srcset的作用是什么&#xff1f;1.7 …