当前位置:   article > 正文

使用 Keras 的 Stable Diffusion 实现高性能文生图_keras stablediffusion

keras stablediffusion

前言

在本文中,我们将使用基于 KerasCV 实现的 Stable Diffusion 模型进行图像生成,这是由 stable.ai 开发的文本生成图像的多模态模型。

Stable Diffusion 是一种功能强大的开源的文本到图像生成模型。虽然市场上存在多种开源实现可以让用户根据文本提示轻松创建图像,但 KerasCV 有一些独特的优势来加速图片生成,其中包括 XLA 编译混合精度支持等特性。所以本文除了介绍如何使用 KerasCV 内置的 StableDiffusion 模块来生成图像,另外我们还通过对比展示了使用 KerasCV 特性所带来的图片加速优势。

准备

  • N 卡,建议 24 G ,在下文使用 KerasCV 实际生成图像过程中至少需要 20 G
  • 安装 python 3.10 的 anaconda 虚拟环境
  • 安装 tensorflow gpu 2.10
  • 一颗充满想象力的大脑,主要是用来构建自己的文本 prompt

这里有一个工具函数 plot_images ,主要是用来把模型生成的图像进行展示。

scss
复制代码
def plot_images(images):
    plt.figure(figsize=(20, 20))
    for i in range(len(images)):
        plt.subplot(1, len(images), i + 1)
        plt.imshow(images[i])
        plt.axis("off")
    plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

模型工作原理

超分辨率工作可以训练深度学习模型来对输入图像进行去噪,从而将其转换为更高分辨率的效果。为了实现这一目的,深度学习模型并不是通过恢复低分辨率输入图像中丢失的信息做到的,而是模型使用其训练数据分布来填充最有可能的给定输入的视觉细节。

然后将这个想法推向极限,在纯噪声上运行这样的模型,然后使用该模型不断去噪最终产生一个全新的图像。这就是潜在扩散模型的关键思想,在 2020 年的 High-Resolution Image Synthesis with Latent Diffusion Models 中提出。

flowers.gif

现在要从潜在扩散过渡到文本生成图像的效果,需要添加关键字控制生成图像的能力,简单来说就是将一段文本的向量加入到到带噪图片中,然后在数据集上训练模型即可得到我们想要的文生图模型 Stable Diffusion 。这就产生了 Stable Diffusion 架构,主要由三部分组成:

  • text encoder:可将用户的提示转换为向量。
  • diffusion model:反复对 64x64 潜在图像进行去噪。
  • decoder:将最终生成的 64x64 潜在图像转换为更高分辨率的 512x512 图像。

基本模型架构图如下:

image.png

benchmark

我们使用 keras_cv 中的 StableDiffusion 模块构造一个文生图基准模型 model ,在对模型进行基准测试之前,先执行一次 text_to_image 函数来预热模型,以确保 TensorFlow graph已被跟踪,这样在后续使用模型进行推理时候的速度测试才是准确的。可以从日志中看到第一次运行的时间是 22 s ,这个不用去管他,我们只看第二个时间。

我这里的提示词是“There is a pink BMW Mini at the exhibition where the lights focus” ,生成 3 张图像,耗时 10.32 s

执行结束之后运行 keras.backend.clear_session() 清除刚刚运行的模型,以保证不会影响到后面的试验。

ini
复制代码
model = keras_cv.models.StableDiffusion(img_width=512, img_height=512, jit_compile=False)
model.text_to_image("warming up the model", batch_size=3)
start = time.time()
images = model.text_to_image("There is a pink BMW Mini at the exhibition where the lights focus", batch_size=3)
print(f"Standard model: {(time.time() - start):.2f} seconds")
plot_images(images)
keras.backend.clear_session()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

日志打印:

arduino
复制代码
25/25 [==============================] - 22s 399ms/step
25/25 [==============================] - 10s 400ms/step
Standard model: 10.32 seconds
  • 1
  • 2
  • 3
  • 4
  • 5

319f63da759ac3c6d2b850d9465fef9.png

benchmark + Mixed precision

正如日志中打印的信息可以看到,我们这里构建的模型现在使用混合精度计算,利用 float16 运算的速度进行计算,同时以 float32 精度存储变量,这是因为 NVIDIA GPU 内核处理同样的操作,使用 float16 比 float32 要快得多。

我们这里和上面一样先将模型预热加载,然后针对我的提示词“There is a black BMW Mini at the exhibition where the lights focus”生成了 3 张图像,耗时 5.30s ,可以看到在 benchmark 基础上使用混合精度生成速度提升将近一倍。

scss
复制代码
keras.mixed_precision.set_global_policy("mixed_float16")
model = keras_cv.models.StableDiffusion(jit_compile=False)
print("Compute dtype:", model.diffusion_model.compute_dtype)
print("Variable dtype:",  model.diffusion_model.variable_dtype)
model.text_to_image("warming up the model", batch_size=3)
start = time.time()
images = model.text_to_image( "There is a black BMW Mini at the exhibition where the lights focus", batch_size=3,)
print(f"Mixed precision model: {(time.time() - start):.2f} seconds")
plot_images(images)
keras.backend.clear_session()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

日志打印:

yaml
复制代码
Compute dtype: float16
Variable dtype: float32
25/25 [==============================] - 9s 205ms/step
25/25 [==============================] - 5s 202ms/step
Mixed precision model: 5.30 seconds
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

179ce83c7bb1e25e5958d3c8a9dda51.png

benchmark + XLA Compilation

XLA(加速线性代数)是一种用于机器学习的开源编译器。XLA 编译器从 PyTorch、TensorFlow 和 JAX 等常用框架中获取模型,并优化模型以在不同的硬件平台(包括 GPU、CPU 和机器学习加速器)上实现高性能执行。

TensorFlow 和 JAX 附带 XLA , keras_cv.models.StableDiffusion 支持开箱即用的 jit_compile 参数。 将此参数设置为 True 可启用 XLA 编译,从而显著提高速度。

从日志中可以看到,在 benchmark 基础上使用 XLA 生成时间减少了 3.34 s

ini
复制代码
keras.mixed_precision.set_global_policy("float32")
model = keras_cv.models.StableDiffusion(jit_compile=True)
model.text_to_image("warming up the model", batch_size=3)
start = time.time()
images = model.text_to_image("There is a black ford mustang at the exhibition where the lights focus", batch_size=3, )
print(f"With XLA: {(time.time() - start):.2f} seconds")
plot_images(images)
keras.backend.clear_session()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

日志打印:

vbnet
复制代码
25/25 [==============================] - 34s 271ms/step
25/25 [==============================] - 7s 271ms/step
With XLA: 6.98 seconds
  • 1
  • 2
  • 3
  • 4
  • 5

0fe51809c822d71ad91d8a770dc517f.png

benchmark + Mixed precision + XLA Compilation

最后我们在 benchmark 基础上同时使用混合精度计算和 XLA 编译,最终生成同样的 3 张图像,时间仅为 3.96s ,与 benchmark 相比生成时间减少了 6.36 s ,生成时间大幅缩短!

ini
复制代码
keras.mixed_precision.set_global_policy("mixed_float16")
model = keras_cv.models.StableDiffusion(jit_compile=True)
model.text_to_image("warming up the model", batch_size=3, )
start = time.time()
images = model.text_to_image( "There is a purple ford mustang at the exhibition where the lights focus", batch_size=3,)
print(f"XLA + mixed precision: {(time.time() - start):.2f} seconds")
plot_images(images)
keras.backend.clear_session()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

日志打印:

arduino
复制代码
25/25 [==============================] - 28s 144ms/step
25/25 [==============================] - 4s 152ms/step
XLA + mixed precision: 3.96 seconds
  • 1
  • 2
  • 3
  • 4
  • 5

630d45a4d883874517055b22ff61dce.png

结论

四种情况的耗时对比结果,展示了使用 KerasCV 生成图片确实在速度方面有特别之处:

  • benchmark : 10.32s
  • benchmark + Mixed precision :5.3 s
  • benchmark + XLA Compilation : 6.98s
  • benchmark + Mixed precision + XLA Compilation : 3.96s

如何学习大模型 AI ?

由于新岗位的生产效率,要优于被取代岗位的生产效率,所以实际上整个社会的生产效率是提升的。

但是具体到个人,只能说是:

“最先掌握AI的人,将会比较晚掌握AI的人有竞争优势”。

这句话,放在计算机、互联网、移动互联网的开局时期,都是一样的道理。

我在一线互联网企业工作十余年里,指导过不少同行后辈。帮助很多人得到了学习和成长。

我意识到有很多经验和知识值得分享给大家,也可以通过我们的能力和经验解答大家在人工智能学习中的很多困惑,所以在工作繁忙的情况下还是坚持各种整理和分享。但苦于知识传播途径有限,很多互联网行业朋友无法获得正确的资料得到学习提升,故此将并将重要的AI大模型资料包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/790179
推荐阅读
相关标签