使用Stable Diffusion和PyTorch创建艺术二维码

大家好,本文将介绍如何利用Stable Diffusion和PyTorch的能力来创建AI生成的QR码艺术。通过将这些技术相结合,可以生成独特的、具有视觉吸引力的艺术作品,其中包含QR码,为艺术作品增添了互动元素。

Stable Diffusion和PyTorch

稳定扩散(Stable Diffusion)是一种用于图像处理和计算机视觉的技术,可对图像进行可控转换。另一方面,PyTorch是一种流行的深度学习框架,提供了搭建和训练神经网络的工具。通过结合这两项技术,可以创建一个强大的管道,用于生成AI艺术作品。

为了开始工作,需要安装必要的软件包,这些软件包对于处理二维码和图像处理至关重要。

pip -q install diffusers transformers accelerate torch xformers qrcode

同时还需要支持Nvidia GPU的系统,如果正在使用Google Colab,可以将TPU设置为运行时,它将为进程启用Nvidia GPU,可以在google colab中使用以下命令来检查GPU是否启用。

用户将得到如下输出:

+-----------------------------------------------------------------------------+
| NVIDIA-SMI 525.85.12 Driver Version: 525.85.12 CUDA Version: 12.0 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 Tesla T4 Off | 00000000:00:04.0 Off | 0 |
| N/A 61C P8 10W / 70W | 0MiB / 15360MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| No running processes found |
+-----------------------------------------------------------------------------

导入库

import torch
from PIL import Image
import qrcode
from pathlib import Path
from multiprocessing import cpu_count
import requests
import io
import os
from PIL import Image
from diffusers import (
  StableDiffusionPipeline,
  StableDiffusionControlNetImg2ImgPipeline,
  ControlNetModel,
  DDIMScheduler,
  DPMSolverMultistepScheduler,
  DEISMultistepScheduler,
  HeunDiscreteScheduler,
  EulerDiscreteScheduler,
  )

生成QR码并使用预训练模型

通过使用qrcode软件包并指定所需的参数(例如纠错和方框大小),可以创建编码特定信息的QR码。

qrcode_generator = qrcode.QRCode(
  version=1,
  error_correction=qrcode.ERROR_CORRECT_H,
  box_size=10,
  border=4,
  )

controlnet = ControlNetModel.from_pretrained(
  "DionTimmer/controlnet_qrcode-control_v1p_sd15", torch_dtype=torch.float16
  )

创建稳定的扩散管道

pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
  "runwayml/stable-diffusion-v1-5",
  controlnet=controlnet,
  safety_checker=None,
  torch_dtype=torch.float16,
  ).to("cuda")
pipe.enable_xformers_memory_efficient_attention()

用于调整图像大小的附加功能

def resize_for_condition_image(input_image: Image.Image, resolution: int):
  input_image = input_image.convert("RGB")
  W, H = input_image.size
  k = float(resolution) / min(H, W)
  H *= k
  W *= k
  H = int(round(H / 64.0)) * 64
  W = int(round(W / 64.0)) * 64
  img = input_image.resize((W, H), resample=Image.LANCZOS)
  return img

Sampler的字典

SAMPLER_MAP = {
"DPM++ Karras SDE": lambda config: DPMSolverMultistepScheduler.from_config(config
"DPM++ Karras": lambda config: DPMSolverMultistepScheduler.from_config(config, use
"Heun": lambda config: HeunDiscreteScheduler.from_config(config),
"Euler": lambda config: EulerDiscreteScheduler.from_config(config),
"DDIM": lambda config: DDIMScheduler.from_config(config),
"DEIS": lambda config: DEISMultistepScheduler.from_config(config),
}
pipe.scheduler = SAMPLER_MAP[sampler](pipe.scheduler.config)

试验不同参数

为了达到理想的艺术效果,可以尝试使用不同的参数,例如扩散强度、推理步数和引导尺度。这些参数可对最终输出产生重大影响,并允许进行创意性探索。

qr_code_content: str = "https://www.linkedin.com/in/zeel-sheladiya-772513176/"
prompt: str = "A beautiful nature and river surrounded by the flamigos"
negative_prompt: str = "ugly, disfigured, low quality, blurry, nsfw"
guidance_scale: float = 7.5
controlnet_conditioning_scale: float = 1.3
strength: float = 0.9
seed: int = 5392011833
init_image: Image.Image | None = None
qrcode_image: Image.Image | None = None
use_qr_code_as_init_image = True
sampler = "DPM++ Karras SDE"
generator = torch.manual_seed(seed) if seed != -1 else torch.Generator()
if qr_code_content != "" or qrcode_image.size == (1, 1):
  print("Generating QR Code from content")
  qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_H,
    box_size=10,
    border=4,
    )
  qr.add_data(qr_code_content)
  qr.make(fit=True)
  qrcode_image = qr.make_image(fill_color="black", back_color="white")
  qrcode_image = resize_for_condition_image(qrcode_image, 768)
else:
  print("Using QR Code Image")
  qrcode_image = resize_for_condition_image(qrcode_image, 768)

init_image = qrcode_image

创建AI生成的QR码并输入

out = pipe(
  prompt=prompt,
  negative_prompt=negative_prompt,
  image=qrcode_image,
  control_image=qrcode_image, # 类型:忽略
  width=768, # 类型:忽略
  height=768, # 类型:忽略
  guidance_scale=float(guidance_scale),
  controlnet_conditioning_scale=float(controlnet_conditioning_scale), # type: i
  generator=generator,
  strength=float(strength),
  num_inference_steps=40,
out.images[0].show()

 

通过结合Stable Diffusion、PyTorch和QR码,可以开启AI生成艺术的新领域。通过进一步的实验和探索,艺术家和开发人员可以突破创造力的界限,创造出引人入胜的互动艺术作品,从而吸引和启发观众。二维码的使用为艺术作品增添了互动元素,使观众可以通过扫描二维码获取更多信息或内容。

总之,Stable Diffusion、PyTorch和QR码的结合为生成AI艺术品提供了一个强大的流程。通过利用这些技术,艺术家和开发人员可以创造出独特的、具有视觉吸引力的艺术作品,并将互动元素融入其中。随着进一步的实验和探索,AI生成艺术的可能性是无限的,可以期待在未来看到更多创新和迷人的艺术作品。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Stable Diffusion 是由 Stability AI 公司开发的一款预训练人工智能模型,主要用于生成高质量的文本,但它不是开源项目,所以直接获取其源代码是不可能的。通常,像 GPT-3、DALL-E 2 这样的大型预训练模型的源代码是不对外公开的,因为这涉及到大量的商业机密和技术细节。 如果你对自然语言处理(NLP)的开源工具库感兴趣,例如像 Hugging Face 的 transformers 库,它提供了基于 Transformer架构的模型,你可以通过 GitHub 获取源代码并进行学习和使用。对于自定义模型或深度学习项目,你可能会编写自己的代码,包括模型训练、微调和部署的部分。 要获取和使用开源 NLP 模块,可以按照以下步骤操作: 1. **选择开源库**:研究并选择一个适合你的需求的开源 NLP 库,比如 TensorFlow, PyTorch 或者 fastai。 2. **克隆仓库**:使用 Git 工具从 GitHub (https://github.com/huggingface/transformers) 或其他代码托管平台下载源代码。 3. **安装依赖**:确保你的开发环境安装了必要的 Python 包和框架(如 pip install transformers)。 4. **了解API**:阅读文档和教程,理解如何加载模型、进行前处理和生成文本。 5. **编写代码**:根据你的项目需求,编写代码来加载模型、提供输入、处理输出等。 6. **训练和微调**:如果模型需要训练或微调,按照库提供的指南进行。 7. **评估性能**:通过测试数据验证模型的性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

python慕遥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值