AIGC笔记--Stable Diffusion源码剖析之FrozenCLIPEmbedder

1--前言

        以论文《High-Resolution Image Synthesis with Latent Diffusion Models》  开源的项目为例,剖析Stable Diffusion经典组成部分,巩固学习加深印象。

2--FrozenCLIPEmbedder

        在默认提供的 txt2img.py 中,使用固定权重的 CLIP 模型来将文本条件编码成Embedding,源代码如下:

class FrozenCLIPEmbedder(AbstractEncoder):
    """Uses the CLIP transformer encoder for text (from Hugging Face)"""
    def __init__(self, version="openai/clip-vit-large-patch14", device="cuda", max_length=77):
        super().__init__()
        self.tokenizer = CLIPTokenizer.from_pretrained(version)
        self.transformer = CLIPTextModel.from_pretrained(version)
        self.device = device
        self.max_length = max_length
        self.freeze()

    def freeze(self):
        self.transformer = self.transformer.eval()
        for param in self.parameters():
            param.requires_grad = False

    def forward(self, text):
        batch_encoding = self.tokenizer(text, truncation=True, max_length=self.max_length, return_length=True,
                                        return_overflowing_tokens=False, padding="max_length", return_tensors="pt")
        tokens = batch_encoding["input_ids"].to(self.device)
        outputs = self.transformer(input_ids=tokens)

        z = outputs.last_hidden_state
        return z

    def encode(self, text):
        return self(text)

        在具体使用中,会利用上述代码生成 无条件Embedding 有条件embedding,用于 Classifier-Free Diffusion Guidance 来预测无条件噪声和有条件噪声。

相关推荐

  1. AIGC笔记--Stable Diffusion剖析FrozenCLIPEmbedder

    2024-06-06 21:42:07       36 阅读
  2. AIGC笔记--Stable Diffusion剖析UNetModel

    2024-06-06 21:42:07       22 阅读
  3. JVM剖析registerNatives方法

    2024-06-06 21:42:07       55 阅读
  4. JVM剖析信号处理机制

    2024-06-06 21:42:07       41 阅读
  5. GDAL剖析(十一)OGR投影说明

    2024-06-06 21:42:07       34 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-06 21:42:07       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-06 21:42:07       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-06 21:42:07       87 阅读
  4. Python语言-面向对象

    2024-06-06 21:42:07       96 阅读

热门阅读

  1. acm模式练习

    2024-06-06 21:42:07       28 阅读
  2. Docker安全配置

    2024-06-06 21:42:07       26 阅读
  3. CMPSC473 malloclab: writing a dynamic storage allocator

    2024-06-06 21:42:07       27 阅读
  4. Flask的模块化实践

    2024-06-06 21:42:07       24 阅读
  5. Flask启动重复注册schedule问题

    2024-06-06 21:42:07       29 阅读
  6. LeetCode-239.滑动窗口最大值

    2024-06-06 21:42:07       26 阅读
  7. Napster诞生25周年:文件共享革命的引领者

    2024-06-06 21:42:07       31 阅读
  8. ZCC5429 异步升压芯片

    2024-06-06 21:42:07       35 阅读