stable Diffusion模型结构

详细描述一下stable Diffusion的推理过程

其实很简单

prompt先经过textencoder tokenizer,embedding

随机生成噪声图片 通过vae encode压缩成潜空间大小

unet with cross attn 去噪 并融合文本信息

上面两个信息如何混合

cross-attention

sd模型中各种不同的采样器作用在哪个步骤

1. 预测当前时间步 t 的噪声

noise_pred = unet(latents, t, encoder_hidden_states=text_embeddings).sample

2. 通过调度器 scheduler 从 z_t 计算出 z_{t-1}

latents = scheduler.step(noise_pred, t, latents).prev_sample

这里noise_pred已经预测出来了 为什么还要通过scheduler.step

采样器原来是我一直以来说的减去的操作,就是噪声已经生成出来了其实

因为噪声不是直接加的,还加了系数/权重

像E一样的那个字母表示噪声

noise_pred已经是噪声了,直接用噪声图片减去这个noise_pred,不就得到最终的图片了吗 为什么还要scheduler.step

ddpm

重点---加噪过程

import torch

import torch.nn as nn

import torch.nn.functional as F

from transformers import CLIPProcessor, CLIPModel

from torchvision import transforms

from PIL import Image

import numpy as np

1. 文本编码器:CLIP 模型

class TextEncoder(nn.Module):

def init(self):

super().init()

使用 CLIP 模型来处理文本

self.clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")

self.processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")

def forward(self, text):

将文本转换为潜在向量

inputs = self.processor(text=text, return_tensors="pt", padding=True)

text_features = self.clip_model.get_text_features(**inputs)

return text_features

2. U-Net 网络定义(简化版)

class UNet(nn.Module):

def init(self, in_channels, out_channels, channels=64):

super().init()

self.conv1 = nn.Conv2d(in_channels, channels, kernel_size=3, padding=1)

self.conv2 = nn.Conv2d(channels, channels, kernel_size=3, padding=1)

self.conv3 = nn.Conv2d(channels, out_channels, kernel_size=3, padding=1)

def forward(self, x):

x = F.relu(self.conv1(x))

x = F.relu(self.conv2(x))

x = self.conv3(x)

return x

3. 反向去噪过程 (简化版)

class DiffusionModel(nn.Module):

def init(self, text_encoder, unet, image_size=64):

super().init()

self.text_encoder = text_encoder

self.unet = unet

self.image_size = image_size

def forward(self, text, noise=None):

1. 获取文本的潜在向量

text_features = self.text_encoder(text)

2. 初始化噪声图像 (如果没有提供的话)

if noise is None:

noise = torch.randn(1, 3, self.image_size, self.image_size)

3. 将噪声图像和文本特征一起输入 U-Net

noise = self.unet(noise)

4. 返回生成的图像

return noise

4. 图像解码器(简化版)

def decode_latent_to_image(latent_tensor):

这里简单地将潜在图像的输出处理为一个可视化的图像

img = latent_tensor.squeeze(0).detach().cpu().numpy().transpose(1, 2, 0)

img = (img * 255).clip(0, 255).astype(np.uint8)

img = Image.fromarray(img)

return img

5. 生成过程

def generate_image(prompt, image_size=64):

初始化文本编码器和 U-Net

text_encoder = TextEncoder()

unet = UNet(3, 3)

初始化 Diffusion 模型

model = DiffusionModel(text_encoder, unet, image_size)

推理过程

generated_image = model(prompt)

解码生成的图像

img = decode_latent_to_image(generated_image)

return img

6. 测试生成

prompt = ["A futuristic city skyline at night"]

generated_img = generate_image(prompt)

显示生成的图像

generated_img.show()

相关推荐
爱分享的飘哥3 天前
第三十七章:文生图的炼金术:Stable Diffusion完整工作流深度解析
人工智能·pytorch·stable diffusion·文生图·ai绘画·代码实战·cfg
EndingCoder6 天前
Three.js + AI:结合 Stable Diffusion 生成纹理贴图
开发语言·前端·javascript·人工智能·stable diffusion·ecmascript·three.js
那年一路北6 天前
Deforum Stable Diffusion,轻松实现AI视频生成自由!
人工智能·stable diffusion·音视频
全宝6 天前
🎨【AI绘画实战】从零搭建Stable Diffusion环境,手把手教你生成超可爱Q版大头照!
人工智能·python·stable diffusion
sculida12 天前
秋叶sd-webui频繁出现生成后无反应的问题
stable diffusion
HORSE RUNNING WILD15 天前
【物理与机器学习】从非平衡热力学到扩散模型
人工智能·学习·机器学习·stable diffusion
取不好名字=17 天前
【ComfyUI学习笔记01】下载安装 | 运行第一个工作流 | 学习思路
笔记·学习·ai作画·stable diffusion
qq_398898931 个月前
【简单三步】Stable diffusion Webai本地部署无法加载模型并报openai/clip-vit-large-patch14错误的解决方法
stable diffusion
ai_xiaogui1 个月前
AIStarter用户与创作者模式详解:一键管理Stable Diffusion项目!
人工智能·stable diffusion·一键发布ai项目·熊哥aistarter教程·开发者必备aistarter
一禅(OneZen)1 个月前
「Windows/Mac OS」AIGC图片生成视频 ,webui + stable-diffusion环境部署教程
windows·stable diffusion