stable-diffusion - 具有潜在扩散模型的高分辨率图像合成

Created at: 2022-08-10 22:36:44
Language: Jupyter Notebook
License: NOASSERTION

稳定扩散

由于与稳定性AIT台的合作,稳定扩散成为可能,并建立在我们之前的工作之上:

使用潜在扩散模型
的高分辨率图像合成 罗宾·隆巴赫*, 安德烈亚斯·布拉特曼*, 多米尼克·洛伦茨帕特里克·埃塞尔比约恩·奥默
CVPR '22 口头|||项目页面

txt2img-stable2 稳定扩散是一种潜在的文本到图像扩散模型。由于稳定性AI的慷慨计算捐赠和LAION的支持,我们能够在来自LAION-5B数据库子集的512x512图像上训练潜在扩散模型。与谷歌的图像类似,该模型使用冻结的CLIP ViT-L / 14文本编码器在文本提示下调节模型。凭借其860M UNet和123M文本编码器,该模型相对轻巧,可在至少具有10GB VRAM的GPU上运行。请参阅下面的此部分模型卡

要求

可以通过以下方式创建和激活名为 的合适 conda 环境:

ldm

conda env create -f environment.yaml
conda activate ldm

你还可以通过运行

conda install pytorch torchvision -c pytorch
pip install transformers==4.19.2 diffusers invisible-watermark
pip install -e .

稳定扩散 v1

稳定扩散 v1 是指模型体系结构的特定配置,该体系结构使用带有 860M UNet 和 CLIP ViT-L/14 文本编码器的缩减采样因子 8 自动编码器作为扩散模型。该模型在256x256图像上进行了预训练,然后在512x512图像上进行了微调。

注意:稳定扩散 v1 是一般的文本到图像扩散模型,因此反映了其训练数据中存在的偏差和(错误)概念。有关训练过程和数据的详细信息,以及模型的预期用途,可以在相应的模型卡中找到。

这些权重可通过拥抱面孔的CompVis组织获得,该许可证包含特定的基于使用的限制,以防止模型卡告知的误用和伤害,但在其他方面仍然是允许的。虽然根据许可条款允许商业用途,但我们不建议在没有额外安全机制和考虑的情况下为服务或产品使用所提供的权重,因为权重存在已知的局限性和偏差,并且对一般文本到图像模型的安全和道德部署的研究正在进行中。权重是研究工件,应按此处理。

创意ML开放铁路M许可证是一个开放铁路M许可证,改编自大科学铁路倡议在负责任的人工智能许可领域共同开展的工作。另请参阅有关我们的许可证所基于的BLOOM开放铁路许可证的文章

权重

我们目前提供以下检查点:

  • sd-v1-1.ckpt
    :在拉里昂2B-en的分辨率下为237k步长。在拉里昂高分辨率上以194k步进(来自LAION-5B的170M示例具有分辨率)。
    256x256
    512x512
    >= 1024x1024
  • sd-v1-2.ckpt
    :从 恢复。在 laion 美学 v2 5+ (laion2B-en 的一个子集,具有估计的美学分数,并额外过滤到具有原始大小的图像,以及估计的水印概率)的分辨率下为 515k 步长。水印估计来自LAION-5B元数据,美学分数是使用LAION美学预测器V2估计的)。
    sd-v1-1.ckpt
    512x512
    > 5.0
    >= 512x512
    < 0.5
  • sd-v1-3.ckpt
    :从 恢复。在“laion美学v2 5 +”的分辨率下有195k步进,文本条件反射下降了10%,以改善无分类器的指导采样
    sd-v1-2.ckpt
    512x512
  • sd-v1-4.ckpt
    :从 恢复。在“laion美学v2 5 +”的分辨率下有225k步长,文本条件反射下降了10%,以改善无分类器的指导采样
    sd-v1-2.ckpt
    512x512

使用不同无分类器指导等级(1.5、2.0、3.0、4.0、5.0、6.0、7.0、8.0)和 50 PLMS 采样步骤的评估显示了检查点的相对改进:可持续发展评估结果

具有稳定扩散的文本到图像

txt2img-stable2 txt2img-stable2

稳定扩散是一种潜在扩散模型,以 CLIP ViT-L/14 文本编码器的(非池化)文本嵌入为条件。我们提供了一个用于采样的参考脚本,但也存在扩散器集成,我们希望看到更积极的社区发展。

参考采样脚本

我们提供了一个参考抽样脚本,其中包含

获得稳定扩散 v1*-原始权重后,链接它们

mkdir -p models/ldm/stable-diffusion-v1/
ln -s <path/to/model.ckpt> models/ldm/stable-diffusion-v1/model.ckpt 

和样品与

python scripts/txt2img.py --prompt "a photograph of an astronaut riding a horse" --plms 

默认情况下,这使用凯瑟琳·克劳森实现的 PLMS 采样器的指导比例,并以 50 步为单位渲染大小为 512x512(训练时)的图像。下面列出了所有支持的参数(类型 )。

--scale 7.5
python scripts/txt2img.py --help

usage: txt2img.py [-h] [--prompt [PROMPT]] [--outdir [OUTDIR]] [--skip_grid] [--skip_save] [--ddim_steps DDIM_STEPS] [--plms] [--laion400m] [--fixed_code] [--ddim_eta DDIM_ETA]
                  [--n_iter N_ITER] [--H H] [--W W] [--C C] [--f F] [--n_samples N_SAMPLES] [--n_rows N_ROWS] [--scale SCALE] [--from-file FROM_FILE] [--config CONFIG] [--ckpt CKPT]
                  [--seed SEED] [--precision {full,autocast}]

optional arguments:
  -h, --help            show this help message and exit
  --prompt [PROMPT]     the prompt to render
  --outdir [OUTDIR]     dir to write results to
  --skip_grid           do not save a grid, only individual samples. Helpful when evaluating lots of samples
  --skip_save           do not save individual samples. For speed measurements.
  --ddim_steps DDIM_STEPS
                        number of ddim sampling steps
  --plms                use plms sampling
  --laion400m           uses the LAION400M model
  --fixed_code          if enabled, uses the same starting code across samples
  --ddim_eta DDIM_ETA   ddim eta (eta=0.0 corresponds to deterministic sampling
  --n_iter N_ITER       sample this often
  --H H                 image height, in pixel space
  --W W                 image width, in pixel space
  --C C                 latent channels
  --f F                 downsampling factor
  --n_samples N_SAMPLES
                        how many samples to produce for each given prompt. A.k.a. batch size
  --n_rows N_ROWS       rows in the grid (default: n_samples)
  --scale SCALE         unconditional guidance scale: eps = eps(x, empty) + scale * (eps(x, cond) - eps(x, empty))
  --from-file FROM_FILE
                        if specified, load prompts from this file
  --config CONFIG       path to config which constructs model
  --ckpt CKPT           path to checkpoint of model
  --seed SEED           the seed (for reproducible sampling)
  --precision {full,autocast}
                        evaluate at this precision

Note: The inference config for all v1 versions is designed to be used with EMA-only checkpoints. For this reason

use_ema=False
is set in the configuration, otherwise the code will try to switch from non-EMA to EMA weights. If you want to examine the effect of EMA vs no EMA, we provide "full" checkpoints which contain both types of weights. For these,
use_ema=False
will load and use the non-EMA weights.

Diffusers Integration

A simple way to download and sample Stable Diffusion is by using the diffusers library:

# make sure you're logged in with `huggingface-cli login`
from torch import autocast
from diffusers import StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained(
	"CompVis/stable-diffusion-v1-4", 
	use_auth_token=True
).to("cuda")

prompt = "a photo of an astronaut riding a horse on mars"
with autocast("cuda"):
    image = pipe(prompt)["sample"][0]  
    
image.save("astronaut_rides_horse.png")

Image Modification with Stable Diffusion

By using a diffusion-denoising mechanism as first proposed by SDEdit, the model can be used for different tasks such as text-guided image-to-image translation and upscaling. Similar to the txt2img sampling script, we provide a script to perform image modification with Stable Diffusion.

The following describes an example where a rough sketch made in Pinta is converted into a detailed artwork.

python scripts/img2img.py --prompt "A fantasy landscape, trending on artstation" --init-img <path-to-img.jpg> --strength 0.8

Here, strength is a value between 0.0 and 1.0, that controls the amount of noise that is added to the input image. Values that approach 1.0 allow for lots of variations but will also produce images that are not semantically consistent with the input. See the following example.

Input

草图

Outputs

输出3 输出2

This procedure can, for example, also be used to upscale samples from the base model.

Comments

BibTeX

@misc{rombach2021highresolution,
      title={High-Resolution Image Synthesis with Latent Diffusion Models}, 
      author={Robin Rombach and Andreas Blattmann and Dominik Lorenz and Patrick Esser and Björn Ommer},
      year={2021},
      eprint={2112.10752},
      archivePrefix={arXiv},
      primaryClass={cs.CV}
}