铁岭 建筑公司网站 中企动力建设,响应式单页网站模板,动画制作软件an,无锡嘉饰茂建设网站的公司降低分辨率降低通道数使用latent diffusion#xff0c;例如stable使用低精度训练
下面将分别讲解如何使用这些方法。
使用latent diffusion
目前的主流模型都用了encoder降低中间层resolution#xff0c;但是有一些比较老的工作#xff0c;比如sr3#xff0c;还是老架构…降低分辨率降低通道数使用latent diffusion例如stable使用低精度训练
下面将分别讲解如何使用这些方法。
使用latent diffusion
目前的主流模型都用了encoder降低中间层resolution但是有一些比较老的工作比如sr3还是老架构。GitHub上有很多改进的工作可以找找看。不想换的话只需要配置好diffusers库然后 使用图像的低维表示而不是实际的像素空间这使得它的内存效率更高。编码器将图像压缩为更小的表示解码器将压缩的表示转换回图像。对于文本到图像模型您需要一个分词器和一个编码器来生成文本嵌入。SD具有三个独立的预训练模型。
使用**from_pretrained()方法加载所有这些组件。您可以在预训练[runwayml/stable-diffusion-v1-5](https://huggingface.co/runwayml/stable-diffusion-v1-5)**检查点中找到它们每个组件都存储在单独的子文件夹中
from PIL import Image
import torch
from transformers import CLIPTextModel, CLIPTokenizer
from diffusers import AutoencoderKL, UNet2DConditionModel, PNDMScheduler
vae AutoencoderKL.from_pretrained(CompVis/stable-diffusion-v1-5, subfoldervae, use_safetensorsTrue)
tokenizer CLIPTokenizer.from_pretrained(CompVis/stable-diffusion-v1-5, subfoldertokenizer)
text_encoder CLIPTextModel.from_pretrained(CompVis/stable-diffusion-v1-5, subfoldertext_encoder, use_safetensorsTrue
)
unet UNet2DConditionModel.from_pretrained(CompVis/stable-diffusion-v1-5, subfolderunet, use_safetensorsTrue
)代替默认的**PNDMScheduler将其替换为UniPCMultistepScheduler**以插入不同的调度程序
from diffusers import UniPCMultistepScheduler
scheduler UniPCMultistepScheduler.from_pretrained(CompVis/stable-diffusion-v1-4, subfolderscheduler)使用低精度推理
参考照着来即可
https://datawhalechina.github.io/thorough-pytorch/%E7%AC%AC%E5%85%AD%E7%AB%A0/6.4%20%E5%8D%8A%E7%B2%BE%E5%BA%A6%E8%AE%AD%E7%BB%83.html在PyTorch中使用autocast配置半精度训练同时需要在下面三处加以设置
import autocast
from torch.cuda.amp import autocast 模型设置
在模型定义中使用python的装饰器方法用autocast装饰模型中的forward函数。关于装饰器的使用可以参考这里
autocast() def forward(self, x): … return x 训练过程
在训练过程中只需在将数据输入模型及其之后的部分放入“with autocast():“即可
for x in train_loader: x x.cuda() with autocast(): output model(x) … 注意
半精度训练主要适用于数据本身的size比较大比如说3D图像、视频等。当数据本身的size并不大时比如手写数字MNIST数据集的图片尺寸只有28*28使用半精度训练则可能不会带来显著的提升。