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

前言

在本文中,我们将使用基于 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()

模型工作原理

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

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

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

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

基本模型架构图如下:

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()

日志打印:

arduino 复制代码
25/25 [==============================] - 22s 399ms/step
25/25 [==============================] - 10s 400ms/step
Standard model: 10.32 seconds

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()

日志打印:

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

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()

日志打印:

vbnet 复制代码
25/25 [==============================] - 34s 271ms/step
25/25 [==============================] - 7s 271ms/step
With XLA: 6.98 seconds

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()

日志打印:

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

结论

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

  • benchmark : 10.32s
  • benchmark + Mixed precision :5.3 s
  • benchmark + XLA Compilation : 6.98s
  • benchmark + Mixed precision + XLA Compilation : 3.96s
相关推荐
AI糊涂是福6 分钟前
人工智能、机器学习与深度学习:概念解析与内在联系
人工智能·深度学习·机器学习
嘉图明16 分钟前
《AI革命重塑未来五年:医疗诊断精准度翻倍、自动驾驶事故锐减90%,全球科技版图加速变革》
人工智能·科技·自动驾驶
SAP工博科技24 分钟前
泰国SAP ERP实施如何应对挑战?工博科技赋能中企出海EEC战略
人工智能·科技
__Benco42 分钟前
OpenHarmony外设驱动使用 (四),Face_auth
人工智能·驱动开发·计算机视觉·harmonyos
Oliverro1 小时前
WebRTC技术EasyRTC嵌入式音视频通信SDK助力智能电视搭建沉浸式实时音视频交互
人工智能·音视频
小袁拒绝摆烂1 小时前
OpenCV-去噪效果和评估指标方法
人工智能·opencv·计算机视觉
Douglassssssss1 小时前
【深度学习】残差网络(ResNet)
网络·人工智能·深度学习
孟意昶1 小时前
中级统计师-统计学基础知识-第三章 参数估计
人工智能·机器学习·概率论
gogoMark7 小时前
口播视频怎么剪!利用AI提高口播视频剪辑效率并增强”网感”
人工智能·音视频
2201_754918417 小时前
OpenCV 特征检测全面解析与实战应用
人工智能·opencv·计算机视觉