大家好,本文將介紹如何利用Stable Diffusion和PyTorch的能力來創(chuàng)建AI生成的QR碼藝術。通過將這些技術相結合,可以生成獨特的、具有視覺吸引力的藝術作品,其中包含QR碼,為藝術作品增添了互動元素。
Stable Diffusion和PyTorch
穩(wěn)定擴散(Stable Diffusion)是一種用于圖像處理和計算機視覺的技術,可對圖像進行可控轉換。另一方面,PyTorch是一種流行的深度學習框架,提供了搭建和訓練神經(jīng)網(wǎng)絡的工具。通過結合這兩項技術,可以創(chuàng)建一個強大的管道,用于生成AI藝術作品。
為了開始工作,需要安裝必要的軟件包,這些軟件包對于處理二維碼和圖像處理至關重要。
pip -q install diffusers transformers accelerate torch xformers qrcode
同時還需要支持Nvidia GPU的系統(tǒng),如果正在使用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
軟件包并指定所需的參數(shù)(例如糾錯和方框大?。?,可以創(chuàng)建編碼特定信息的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
)
創(chuàng)建穩(wěn)定的擴散管道
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)
試驗不同參數(shù)
為了達到理想的藝術效果,可以嘗試使用不同的參數(shù),例如擴散強度、推理步數(shù)和引導尺度。這些參數(shù)可對最終輸出產(chǎn)生重大影響,并允許進行創(chuàng)意性探索。
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
創(chuàng)建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生成藝術的新領域。通過進一步的實驗和探索,藝術家和開發(fā)人員可以突破創(chuàng)造力的界限,創(chuàng)造出引人入勝的互動藝術作品,從而吸引和啟發(fā)觀眾。二維碼的使用為藝術作品增添了互動元素,使觀眾可以通過掃描二維碼獲取更多信息或內容。文章來源:http://www.zghlxwxcb.cn/news/detail-572063.html
總之,Stable Diffusion、PyTorch和QR碼的結合為生成AI藝術品提供了一個強大的流程。通過利用這些技術,藝術家和開發(fā)人員可以創(chuàng)造出獨特的、具有視覺吸引力的藝術作品,并將互動元素融入其中。隨著進一步的實驗和探索,AI生成藝術的可能性是無限的,可以期待在未來看到更多創(chuàng)新和迷人的藝術作品。文章來源地址http://www.zghlxwxcb.cn/news/detail-572063.html
到了這里,關于使用Stable Diffusion和PyTorch創(chuàng)建藝術二維碼的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!