深度学总结:Image Style Transfer pytorch方式实现,这个是非基于autoencoder和domain adversrial方式
文章目录
- 论文链接:
- 主要思路:
- pytorch实现:
- 计算content的Loss:
- 计算style 的Loss:
- 计算total的Loss:
- 训练过程:
论文链接:
https://www.cv-foundation.org/openaccess/content_cvpr_2016/papers/Gatys_Image_Style_Transfer_CVPR_2016_paper.pdf
作者运用了一个披着深度学习外表的传统方法做这个问题,技术不提倡,思路、想法很天才。
主要思路:
1、想办法分离style features和content features;
2、怎么得到content features,作者偷懒直接取vgg19里面conv4_2的features,当作content features;
3、怎么得到style features,作者发挥天才的偷懒,认为style就是features之间的相关度,作者直接取vgg19里面conv1_1,conv2_1,conv3_1,conv4_1,conv5_1这5层的feature map来算风格相关度,作者又发现不同的layer对风格有影响,前面的风格细腻,后面的风格粗犷,又给这5个层的loss误差加了权重。
4、那么目标是什么了?就是new_image和content_image的内容接近,new_image和stytle_image的风格接近,LtotalL_{total}Ltotal = LcontentL_{content}Lcontent+LstyleL_{style}Lstyle,首先要像,其次才是风格,所以LcontentL_{content}Lcontent的比重要大。
pytorch实现:
计算content的Loss:
前面提到了作者偷懒直接取vgg19里面conv4_2的features,当作content features,那么只需要比较两个feature map的差别就行了。
# the content losscontent_loss = torch.mean((target_features['conv4_2'] - content_features['conv4_2'])**2)
计算style 的Loss:
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_, d, h, w = tensor.size()# reshape so we're multiplying the features for each channeltensor = tensor.view(d, h * w)# calculate the gram matrixgram = torch.mm(tensor, tensor.t())return gram
# weights for each style layer
# weighting earlier layers more will result in *larger* style artifacts
# notice we are excluding `conv4_2` our content representation
style_weights = {'conv1_1': 1.,'conv2_1': 0.75,'conv3_1': 0.2,'conv4_1': 0.2,'conv5_1': 0.2}# get style features only once before training
style_features = get_features(style, vgg)# calculate the gram matrices for each layer of our style representation
style_grams = {layer: gram_matrix(style_features[layer]) for layer in style_features}for layer in style_weights:# get the "target" style representation for the layertarget_feature = target_features[layer]target_gram = gram_matrix(target_feature)_, d, h, w = target_feature.shape# get the "style" style representationstyle_gram = style_grams[layer]# the style loss for one layer, weighted appropriatelylayer_style_loss = style_weights[layer] * torch.mean((target_gram - style_gram)**2)# add to the style lossstyle_loss += layer_style_loss / (d * h * w)
计算total的Loss:
content_weight = 1 # alpha
style_weight = 1e6 # beta# calculate the *total* loss
total_loss = content_weight * content_loss + style_weight * style_loss
训练过程:
有人可能问VGG参数固定住了,需要的参数是什么了,参数就是新图啊?通过上述loss调整图像的像素值,这个和我们一般了解到的有点不一样。
新图就是在原图的基础上慢慢变化,f复制原图并设置新图为trainable的:
# 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 style
target = content.clone().requires_grad_(True).to(device)
训练大概代码如下:
# for displaying the target image, intermittently
show_every = 400# iteration hyperparameters
optimizer = optim.Adam([target], lr=0.003)
steps = 2000 # decide how many iterations to update your image (5000)for ii in range(1, steps+1):# get the features from your target imagetarget_features = get_features(target, vgg)# the content losscontent_loss = torch.mean((target_features['conv4_2'] - content_features['conv4_2'])**2)# the style loss# initialize the style loss to 0style_loss = 0# then add to it for each layer's gram matrix lossfor layer in style_weights:# get the "target" style representation for the layertarget_feature = target_features[layer]target_gram = gram_matrix(target_feature)_, d, h, w = target_feature.shape# get the "style" style representationstyle_gram = style_grams[layer]# the style loss for one layer, weighted appropriatelylayer_style_loss = style_weights[layer] * torch.mean((target_gram - style_gram)**2)# add to the style lossstyle_loss += layer_style_loss / (d * h * w)# calculate the *total* losstotal_loss = content_weight * content_loss + style_weight * style_loss# update your target imageoptimizer.zero_grad()total_loss.backward()optimizer.step()
深度学总结:Image Style Transfer pytorch方式实现,这个是非基于autoencoder和domain adversrial方式相关推荐
- 一行命令配置深度学所需所有环境PyTorch, TensorFlow, CUDA, cuDNN, and NVIDIA Drivers.
参考 https://lambdalabs.com/lambda-stack-deep-learning-software GitHub 文档 运行下面命令在新安装的Ubuntu(20.04, 18. ...
- Video Style Transfer汇总
Video Style Transfer 非深度方法 Processing images and video for an impressionist effect. (ACM Press/Addis ...
- Pytorch 风格迁移(Style transfer)
Pytorch 风格迁移 0. 环境介绍 环境使用 Kaggle 里免费建立的 Notebook 教程使用李沐老师的 动手学深度学习 网站和 视频讲解 小技巧:当遇到函数看不懂的时候可以按 Shift ...
- Neural Style Transfer 风格迁移经典论文讲解与 PyTorch 实现
今天花半小时看懂了"Image Style Transfer Using Convolutional Neural Networks Leon"这篇论文,又花半小时看懂了其 PyT ...
- 吴恩达老师深度学习视频课笔记:神经风格迁移(neural style transfer)
什么是神经风格迁移(neural style transfer):如下图,Content为原始拍摄的图像,Style为一种风格图像.如果用Style来重新创造Content照片,神经风 ...
- 深度学习(三十五)——Style Transfer(2), YOLOv3, Tiny-YOLO, One-stage vs. Two-stage
Style Transfer Texture Networks: Feed-forward Synthesis of Textures and Stylized Images 这篇论文属于fast s ...
- 吴恩达深度学习4.4练习_Convolutional Neural Networks_Art Generation with Neural Style Transfer
转载自吴恩达老师深度学习课程作业notebook Deep Learning & Art: Neural Style Transfer Welcome to the second assign ...
- 深度学习之Style Transfer
Style Transfer 1.引入 最近看了一些基于深度学习的Style Transfer, 也就是风格迁移相关的paper,感觉挺有意思的. 所谓风格迁移,其实就是提供一幅画(Reference ...
- 深度摄影风格转换--Deep Photo Style Transfer
Deep Photo Style Transfer https://arxiv.org/abs/1703.07511 Code: https://github.com/luanfujun/deep-p ...
最新文章
- 详解 | Dropout为何能防止过拟合?
- Django Rest Framework之用户频率/访问次数限制
- 文档型数据库列一般都是弱项
- SAP Spartacus B2B Unit page convertListItem方法的工作原理
- c语言 define 关键字,c语言中define的一个不常见的用法
- Shell编程: Shell 变量
- PAT乙级(1016 部分A+B)
- nodeJs 接收请求参数和发送请求参数
- python制作密码字典_Python实现生成密码字典的方法示例
- SRA数据下载以及转换格式
- 单总线结构CPU数据通路
- 【PAT A-1013】Battle Over Cities
- 扫码枪扫描多个二维码在明细行自动增行自动定位输入框
- 【网络设备】Cisco路由器密码重置及配置
- 用LangChain构建大语言模型应用
- CTF封神台第三关通关
- 几个好用Maven 镜像仓库地址
- 农业计算机英语词汇,考研英语作文主题词汇:农业
- MySQL关于把AM/PM格式的时间转换为24小时制的时间格式
- 任务型对话机器人简介
热门文章
- 八代i7装服务器系统2012,八代I7能装win7系统吗,会影响性能吗-8代win7,8代cpu完美装win7...
- 线性代数可以速成吗_英语真的可以速成吗?
- java 转xml 变成两根下划线_XStream实现xml和java对象之间的互相转换(包括对属性,别名,下划线_的处理),同理JSON也可以...
- python绘制坐标点画出半径_24行Python代码让小球“弹”起来
- php采集一言代码_PHP简单实现一言 / 随机一句功能
- python自动化库_Python自动化测试常用库整理
- Apache common包应用集合
- WebLogic 12c 添加默认应用
- python 删除文件、目录_python实现删除文件与目录的方法
- bios文件查看工具_何必花钱升级显卡!AMD鸡血BIOS杀到