3D Style Transfer

代码源参见:(https://github.com/tensorflow/lucid)
通过使用[可区分图像]实现从纹理3D模型和样式图像到3D模型的新纹理的样式转换参数](https://distill.pub/2018/differentiable-parameterizations/#section-style-transfer-3d)。

这款笔记本没有介绍清晰的抽象背后; 您可能还希望阅读[Lucid教程](https://colab.research.google.com/github/tensorflow/lucid/blob/master/notebooks/tutorial.ipynb)。

注意:使用本教程的最简单方法是作为colab笔记本,它允许您在没有设置的情况下潜入。

安装,导入,初始化OpenGL,并加载CNN模型

本教程使用OpenGL,因此需要 GPU,
您可以检查GPU是否可用并且是否正确配置了tensorflow:

import tensorflow as tf
assert tf.test.is_gpu_available()

如果上面的断言语句失败,您可以随时在colab上运行笔记本并使用免费的GPU选择:

Runtime   →   Change runtime type   →   Hardware Accelerator: GPU

!pip install -q lucid>=0.2.3
import os
import io
import sys
from string import Template
from pathlib import Pathimport numpy as np
import PIL.Image
import matplotlib.pylab as pl
from google.colab import filesfrom IPython.display import clear_output, display, Image, HTMLfrom lucid.misc.gl.glcontext import create_opengl_context
import OpenGL.GL as glfrom lucid.misc.gl import meshutil
from lucid.misc.gl import glrenderer
import lucid.misc.io.showing as show
import lucid.misc.io as lucid_io
from lucid.misc.tfutil import create_sessionfrom lucid.modelzoo import vision_models
from lucid.optvis import objectives
from lucid.optvis import param
from lucid.optvis.style import StyleLoss, mean_l1_loss
from lucid.optvis.param.spatial import sample_bilinear

您可以检查已安装的OpenGL版本:

create_opengl_context()
gl.glGetString(gl.GL_VERSION)
model = vision_models.InceptionV1()
model.load_graphdef()

加载3D模型

我们先下载一些3D模型。 这类似于[3D功能可视化笔记本]中的步骤(https://colab.research.google.com/github/tensorflow/lucid/blob/master/notebooks/differentiable-parameterizations/texture_synth_3d.ipynb)如果你’ 热衷于细节,还没有完成那个笔记本。

TEXTURE_SIZE = 1024
!gsutil cp gs://deepdream/article_models.zip . && \unzip -qo article_models.zip && \ls -al article_models && \cat article_models/readme.txt
def prepare_image(fn, size=None):data = lucid_io.reading.read(fn)im = PIL.Image.open(io.BytesIO(data)).convert('RGB')if size:im = im.resize(size, PIL.Image.ANTIALIAS)return np.float32(im)/255.0
mesh = meshutil.load_obj('article_models/bunny.obj')
mesh = meshutil.normalize_mesh(mesh)original_texture = prepare_image('article_models/bunny.png', (TEXTURE_SIZE, TEXTURE_SIZE))
style_url = 'https://upload.wikimedia.org/wikipedia/commons/d/db/RIAN_archive_409362_Literaturnaya_Gazeta_article_about_YuriGagarin%2C_first_man_in_space.jpg'
style = prepare_image(style_url)
show.image(style, 'jpeg')

纹理合成

renderer = glrenderer.MeshRenderer((512, 512))
googlenet_style_layers = ['conv2d2','mixed3a','mixed3b','mixed4a','mixed4b','mixed4c',
]googlenet_content_layer = 'mixed3b'
content_weight = 100.0
# Style Gram matrix weighted average decay coefficient
style_decay = 0.95sess = create_session(timeout_sec=0)# t_fragments is used to feed rasterized UV coordinates for the current view.
# Channels: [U, V, _, Alpha]. Alpha is 1 for pixels covered by the object, and
# 0 for background.
t_fragments = tf.placeholder(tf.float32, [None, None, 4])
t_uv = t_fragments[...,:2]
t_alpha = t_fragments[...,3:]# Texture atlas to optimize
t_texture = param.image(TEXTURE_SIZE, fft=True, decorrelate=True)[0]# Variable to store the original mesh texture used to render content views
content_var = tf.Variable(tf.zeros([TEXTURE_SIZE, TEXTURE_SIZE, 3]), trainable=False)# Sample current and original textures with provided pixel data
t_joined_texture = tf.concat([t_texture, content_var], -1)
t_joined_frame = sample_bilinear(t_joined_texture, t_uv) * t_alpha
t_frame_current, t_frame_content = t_joined_frame[...,:3], t_joined_frame[...,3:]
t_joined_frame = tf.stack([t_frame_current, t_frame_content], 0)# Feeding the rendered frames to the Neural Network
t_input = tf.placeholder_with_default(t_joined_frame, [None, None, None, 3])
model.import_graph(t_input)# style loss
style_layers = [sess.graph.get_tensor_by_name('import/%s:0'%s)[0] for s in googlenet_style_layers]
# L1-loss seems to be more stable for GoogleNet
# Note that we use style_decay>0 to average style-describing Gram matrices
# over the recent viewports. Please refer to StyleLoss for the details.
sl = StyleLoss(style_layers, style_decay, loss_func=mean_l1_loss)# content loss
content_layer = sess.graph.get_tensor_by_name('import/%s:0'%googlenet_content_layer)
content_loss = mean_l1_loss(content_layer[0], content_layer[1]) * content_weight# setup optimization
total_loss = content_loss + sl.style_loss
t_lr = tf.constant(0.05)
trainer = tf.train.AdamOptimizer(t_lr)
train_op = trainer.minimize(total_loss)init_op = tf.global_variables_initializer()
loss_log = []def reset(style_img, content_texture):del loss_log[:]init_op.run()sl.set_style({t_input: style_img[None,...]})content_var.load(content_texture)def run(mesh, step_n=400):for i in range(step_n):fragments = renderer.render_mesh(modelview=meshutil.sample_view(10.0, 12.0),position=mesh['position'], uv=mesh['uv'],face=mesh['face'])_, loss = sess.run([train_op, [content_loss, sl.style_loss]], {t_fragments: fragments})loss_log.append(loss)if i==0 or (i+1)%50 == 0:clear_output()last_frame, last_content = sess.run([t_frame_current, t_frame_content], {t_fragments: fragments})show.images([last_frame, last_content], ['current frame', 'content'])if i==0 or (i+1)%10 == 0:print(len(loss_log), loss)
reset(style, original_texture)
run(mesh)


pl.plot(loss_log);
pl.legend(['Content Loss', 'Style Loss'])
pl.show()

显示结果

对于质地较不复杂的模型 - 即。 所有除了兔子模型 - 内容目标可能是微妙的。 例如,在头骨的情况下,最容易观察到在观察[颅缝线]时内容物丢失是有效的(https://en.wikipedia.org/wiki/Suture_(anatomy))。 您可以单击并拖动下一个单元格的输出以查看头骨模型的侧面和顶部。

texture = t_texture.eval()
show.textured_mesh(mesh, texture)
show.image(texture, 'jpeg')


作为感兴趣的读者的旁白:上面的纹理仍然显示在渲染期间未看到的补丁中的原始随机初始化。 你能想到一种让这些部件变黑的方法,那么纹理压缩得更好吗?

*提示:一种方法可能是在整体纹理的平均值上添加损失。*试一试!

批量纹理生成

我们有时被问到如何为交互式[Distill](https://distill.pub)文章生成数据。 通常这个过程不是很有趣,所以我们并不总是将它包含在我们的笔记本中。 但是,对于感兴趣的读者来说,这是一个为许多不同的输入图像运行colab函数的例子。

这些单元在colab运行时的VM上本地保存资产。 运行这些单元格后,我们使用google.colab.files模块将这些资源下载到我们的开发机器中。

styles = '''
starry  https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg/606px-Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg
onwhite https://upload.wikimedia.org/wikipedia/commons/c/c4/Vassily_Kandinsky%2C_1923_-_On_White_II.jpg
mosaic  https://upload.wikimedia.org/wikipedia/commons/thumb/9/9e/Fernand_L%C3%A9ger_-_Grand_parade_with_red_background_%28mosaic%29_1958_made.jpg/637px-Fernand_L%C3%A9ger_-_Grand_parade_with_red_background_%28mosaic%29_1958_made.jpg
points https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/Robert_Delaunay%2C_1906%2C_Portrait_de_Metzinger%2C_oil_on_canvas%2C_55_x_43_cm%2C_DSC08255.jpg/449px-Robert_Delaunay%2C_1906%2C_Portrait_de_Metzinger%2C_oil_on_canvas%2C_55_x_43_cm%2C_DSC08255.jpg
scream https://upload.wikimedia.org/wikipedia/commons/thumb/f/f4/The_Scream.jpg/471px-The_Scream.jpg
noodles https://upload.wikimedia.org/wikipedia/commons/thumb/d/d9/Noodles_and_eggs20170520_1035.jpg/526px-Noodles_and_eggs20170520_1035.jpg
newspaper https://upload.wikimedia.org/wikipedia/commons/d/db/RIAN_archive_409362_Literaturnaya_Gazeta_article_about_YuriGagarin%2C_first_man_in_space.jpg
birds https://canyouseedotca.files.wordpress.com/2016/01/mce-birds.jpg
cross https://upload.wikimedia.org/wikipedia/commons/thumb/5/50/Cross_stitch_detail.jpg/640px-Cross_stitch_detail.jpg
galaxy https://upload.wikimedia.org/wikipedia/commons/thumb/c/c3/NGC_4414_%28NASA-med%29.jpg/582px-NGC_4414_%28NASA-med%29.jpg
cd https://upload.wikimedia.org/wikipedia/commons/thumb/d/d5/CD_autolev_crop.jpg/480px-CD_autolev_crop.jpg
'''.split()
styles = list(zip(styles[::2], styles[1::2]))
HTML(" ".join('<a href="%s">%s</a>'%(url, name) for name, url in styles))
from google.colab import files
from lucid.misc.io.writing import writedef export_mesh(name, mesh):data_to_save = {'position': mesh['position'].ravel(), 'uv': mesh['uv'].ravel(), 'face': np.uint32(mesh['face'].ravel())}for key, value in data_to_save.items():data = value.tobytes()filename = '%s_%s.3d'%(name, key)write(data, filename)
for mesh_path in Path('article_models/').glob('*.obj'):mesh_name = mesh_path.stemprint(mesh_name)tex_path = mesh_path.with_suffix('.jpg')if not tex_path.exists():tex_path = mesh_path.with_suffix('.png')mesh = meshutil.load_obj(str(mesh_path))mesh = meshutil.normalize_mesh(mesh)original_texture = prepare_image(str(tex_path), (TEXTURE_SIZE, TEXTURE_SIZE))export_mesh(mesh_name, mesh)lucid_io.save(original_texture, mesh_name+'_tex.jpg', quality=90)for style_name, url in styles:if style_name[0] == '#':continuestyle_img = prepare_image(url)reset(style_img, original_texture)run(mesh, step_n=800)texture = t_texture.eval()filename = '%s_tex_%s.jpg'%(mesh_name, style_name)lucid_io.save(texture, filename, quality=90)

3D Style Transfer相关推荐

  1. 吴恩达老师深度学习视频课笔记:神经风格迁移(neural style transfer)

            什么是神经风格迁移(neural style transfer):如下图,Content为原始拍摄的图像,Style为一种风格图像.如果用Style来重新创造Content照片,神经风 ...

  2. 吴恩达深度学习课程deeplearning.ai课程作业:Class 4 Week 4 Art Generation with Neural Style Transfer

    吴恩达deeplearning.ai课程作业,自己写的答案. 补充说明: 1. 评论中总有人问为什么直接复制这些notebook运行不了?请不要直接复制粘贴,不可能运行通过的,这个只是notebook ...

  3. 15.深度学习练习:Deep Learning Art: Neural Style Transfer

    本文节选自吴恩达老师<深度学习专项课程>编程作业,在此表示感谢. 课程链接:https://www.deeplearning.ai/deep-learning-specialization ...

  4. 吴恩达深度学习4.4练习_Convolutional Neural Networks_Art Generation with Neural Style Transfer

    转载自吴恩达老师深度学习课程作业notebook Deep Learning & Art: Neural Style Transfer Welcome to the second assign ...

  5. Neural Style Transfer: A Review

    这篇是风格转移方面的综述,文中总结了多种风格转移的方法.因为18年看过很多风格转移方面的论文,因此翻译这篇综述. Gatys等人的开创性工作.通过分离和重新组合图像内容和风格,展示了卷积神经网络(CN ...

  6. [Style Transfer]——Joint Bilateral Learning for Real-time Universal Photorealistic Style Transfer

    Joint Bilateral Learning for Real-time Universal Photorealistic Style Transfer 基于联合双边学习的通用写实风格转换
 fr ...

  7. 论文笔记-Real-Time MDE using Synthetic Data with Domain Adaptation via Image Style Transfer

    论文信息 标题: Real-Time Monocular Depth Estimation using Synthetic Data with Domain Adaptation via Image ...

  8. 项目总结四:神经风格迁移项目(Art generation with Neural Style Transfer)

    1.项目介绍 神经风格转换 (NST) 是深部学习中最有趣的技术之一.它合并两个图像, 即 内容图像 C(content image) 和 样式图像S(style image), 以生成图像 G(ge ...

  9. CCPL: Contrastive Coherence Preserving Loss for Versatile Style Transfer

    文章目录 Abstract 1 Introduction 2 Related Works 3 Methods 3.1 Contrastive Coherence Preserving Loss 3.2 ...

  10. 深度摄影风格转换--Deep Photo Style Transfer

    Deep Photo Style Transfer https://arxiv.org/abs/1703.07511 Code: https://github.com/luanfujun/deep-p ...

最新文章

  1. 科学家们竟用乐高观察细胞,网友:万万没想到啊
  2. Mui Webview下来刷新上拉加载实现
  3. 不同时间段的欢迎语言
  4. VS2017 按ctrl+f5执行程序窗口依然一闪而过的问题(图文)
  5. 本月 Firefox 65 将加入 Flexbox Inspector 开发者工具
  6. org.apache.commons.lang.StringUtils
  7. why metadata request for GM4 via http will be redirected to https via 307 s
  8. 12 MM配置-主数据-定义物料组
  9. toast, 警告窗
  10. Android—修改button属性
  11. C++_类和对象_C++继承_菱形继承_或钻石继承_问题及利用虚继承解决该问题---C++语言工作笔记068
  12. 区块链只能算半个“信任机器”,隐私计算才是真正的信任机器?
  13. C#用户进行LDAP验证并返回员工信息
  14. 跨平台音频编辑器ocenaudio(十七)
  15. 1.1 OC类的认识
  16. 【一天一个C++小知识】011.C++编译的流程
  17. 计算机标准差平方差怎么按,数学标准差公式
  18. 计算机局域网地址设置方法,怎么设置局域网电脑的ip地址和DNS?
  19. 学计算机的当大学老师,待遇那么低,研究那么苦,你为啥还去大学当老师?
  20. 快速使用 Docker 部署 Spring Boot 项目

热门文章

  1. 为什么外包公司这么不受欢迎 ?
  2. 30天自制操作系统第一天
  3. [疯狂Java]I/O:流模型(I/O流、字节/字符流、节点/处理流)
  4. 举头皮皮虾机器人_一种仿生水下皮皮虾机器人通信系统的制作方法
  5. PLM( 产品生命周期管理)的简单介绍
  6. 图片文字怎么合并转发_微信怎么转发别人的图片带文字
  7. 云业务“探路” 中国联通成立产业互联网子公司
  8. visio2013(64位)
  9. gephi mysql_用Gephi移动多个节点(Moving multiple nodes with Gephi)
  10. 如何免费制作表白二维码?