Diffusers 文件

CogView3Plus

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

CogView3Plus

CogView3: 透過接力擴散實現更精細、更快速的文字到影像生成,作者:清華大學 & 智譜AI 的 Wendi Zheng, Jiayan Teng, Zhuoyi Yang, Weihan Wang, Jidong Chen, Xiaotao Gu, Yuxiao Dong, Ming Ding, Jie Tang。

論文摘要如下:

最近文字到影像生成系統的進展主要由擴散模型驅動。然而,單階段文字到影像擴散模型在計算效率和影像細節的細化方面仍面臨挑戰。為了解決這個問題,我們提出了 CogView3,一個創新的級聯框架,可增強文字到影像擴散的效能。CogView3 是第一個在文字到影像生成領域實現接力擴散的模型,它透過首先建立低解析度影像,然後應用基於接力的超解析度來執行任務。這種方法不僅產生了具有競爭力的文字到影像輸出,而且大大降低了訓練和推理成本。我們的實驗結果表明,CogView3 在人工評估中比當前最先進的開源文字到影像擴散模型 SDXL 高出 77.0%,同時推理時間僅為 SDXL 的 1/2。CogView3 的蒸餾變體在僅使用 SDXL 1/10 推理時間的情況下實現了可比的效能。

請務必檢視排程器指南,瞭解如何權衡排程器速度和質量,並檢視跨管道重用元件部分,瞭解如何有效地將相同元件載入到多個管道中。

此管道由 zRzRzRzRzRzRzR 貢獻。原始程式碼庫可在此處找到。原始權重可在 hf.co/THUDM 下找到。

CogView3PlusPipeline

class diffusers.CogView3PlusPipeline

< >

( tokenizer: T5Tokenizer text_encoder: T5EncoderModel vae: AutoencoderKL transformer: CogView3PlusTransformer2DModel scheduler: typing.Union[diffusers.schedulers.scheduling_ddim_cogvideox.CogVideoXDDIMScheduler, diffusers.schedulers.scheduling_dpm_cogvideox.CogVideoXDPMScheduler] )

引數

  • vae (AutoencoderKL) — 變分自動編碼器(VAE)模型,用於將影像編碼和解碼為潛在表示。
  • text_encoder (T5EncoderModel) — 凍結的文字編碼器。CogView3Plus 使用 T5;具體是 t5-v1_1-xxl 變體。
  • tokenizer (T5Tokenizer) — T5Tokenizer 類的分詞器。
  • transformer (CogView3PlusTransformer2DModel) — 一個文字條件 CogView3PlusTransformer2DModel,用於對編碼後的影像潛在進行去噪。
  • scheduler (SchedulerMixin) — 一個與 transformer 結合使用的排程器,用於對編碼後的影像潛在進行去噪。

用於使用 CogView3Plus 生成文字到影像的管道。

此模型繼承自 DiffusionPipeline。請檢視超類文件,瞭解庫為所有管道實現的通用方法(例如下載或儲存、在特定裝置上執行等)。

__call__

< >

( prompt: typing.Union[str, typing.List[str], NoneType] = None negative_prompt: typing.Union[str, typing.List[str], NoneType] = None height: typing.Optional[int] = None width: typing.Optional[int] = None num_inference_steps: int = 50 timesteps: typing.Optional[typing.List[int]] = None guidance_scale: float = 5.0 num_images_per_prompt: int = 1 eta: float = 0.0 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.FloatTensor] = None prompt_embeds: typing.Optional[torch.FloatTensor] = None negative_prompt_embeds: typing.Optional[torch.FloatTensor] = None original_size: typing.Optional[typing.Tuple[int, int]] = None crops_coords_top_left: typing.Tuple[int, int] = (0, 0) output_type: str = 'pil' return_dict: bool = True callback_on_step_end: typing.Union[typing.Callable[[int, int, typing.Dict], NoneType], diffusers.callbacks.PipelineCallback, diffusers.callbacks.MultiPipelineCallbacks, NoneType] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] max_sequence_length: int = 224 ) CogView3PipelineOutput or tuple

引數

  • prompt (strList[str], 可選) — 用於指導影像生成的提示詞。如果未定義,則必須傳遞 prompt_embeds
  • negative_prompt (strList[str], 可選) — 不用於指導影像生成的提示詞。如果未定義,則必須傳遞 negative_prompt_embeds。當不使用引導時(即,如果 guidance_scale 小於 1 時),此引數將被忽略。
  • height (int, 可選, 預設為 self.transformer.config.sample_size * self.vae_scale_factor) — 生成影像的高度(畫素)。如果未提供,則設定為 1024。
  • width (int, 可選, 預設為 self.transformer.config.sample_size * self.vae_scale_factor) — 生成影像的寬度(畫素)。如果未提供,則設定為 1024。
  • num_inference_steps (int, 可選, 預設為 50) — 去噪步數。更多的去噪步數通常會帶來更高的影像質量,但推理速度會變慢。
  • timesteps (List[int], 可選) — 使用在其 set_timesteps 方法中支援 timesteps 引數的排程器進行去噪過程的自定義時間步。如果未定義,將使用傳遞 num_inference_steps 時的預設行為。必須按降序排列。
  • guidance_scale (float, 可選, 預設為 5.0) — 如 Classifier-Free Diffusion Guidance 中定義的引導比例。guidance_scale 定義為 Imagen Paper 中公式 2 的 w。透過設定 guidance_scale > 1 啟用引導比例。更高的引導比例鼓勵生成與文字 prompt 密切相關的影像,通常以犧牲影像質量為代價。
  • num_images_per_prompt (int, 可選, 預設為 1) — 每個提示詞生成的影像數量。
  • generator (torch.GeneratorList[torch.Generator], 可選) — 一個或多個 torch 生成器,用於使生成具有確定性。
  • latents (torch.FloatTensor, 可選) — 預生成的帶噪潛在向量,從高斯分佈中取樣,用作影像生成的輸入。可用於使用不同提示詞調整相同的生成。如果未提供,則將透過使用提供的隨機 generator 進行取樣來生成潛在張量。
  • prompt_embeds (torch.FloatTensor, 可選) — 預生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示詞加權。如果未提供,將從 prompt 輸入引數生成文字嵌入。
  • negative_prompt_embeds (torch.FloatTensor, 可選) — 預生成的負面文字嵌入。可用於輕鬆調整文字輸入,例如提示詞加權。如果未提供,negative_prompt_embeds 將從 negative_prompt 輸入引數生成。
  • original_size (Tuple[int], 可選, 預設為 (1024, 1024)) — 如果 original_sizetarget_size 不同,影像將顯示為縮小或放大。如果未指定,original_size 預設為 (height, width)。作為 SDXL 微條件的一部分,如 https://huggingface.co/papers/2307.01952 第 2.2 節所述。
  • crops_coords_top_left (Tuple[int], 可選, 預設為 (0, 0)) — crops_coords_top_left 可用於生成一個似乎從 crops_coords_top_left 位置向下“裁剪”的影像。透過將 crops_coords_top_left 設定為 (0, 0) 通常可以獲得有利的、居中的影像。作為 SDXL 微條件的一部分,如 https://huggingface.co/papers/2307.01952 第 2.2 節所述。
  • output_type (str, 可選, 預設為 "pil") — 生成影像的輸出格式。在 PIL: PIL.Image.Imagenp.array 之間選擇。
  • return_dict (bool, 可選, 預設為 True) — 是否返回 ~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput 而不是普通的元組。
  • attention_kwargs (dict, 可選) — 一個 kwargs 字典,如果指定,則會傳遞給 diffusers.models.attention_processor 中定義的 self.processorAttentionProcessor
  • callback_on_step_end (Callable, 可選) — 在推理過程中,每個去噪步驟結束時呼叫的函式。該函式透過以下引數呼叫:callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)callback_kwargs將包含callback_on_step_end_tensor_inputs中指定的所有張量列表。
  • callback_on_step_end_tensor_inputs (List, 可選) — callback_on_step_end函式的張量輸入列表。列表中指定的張量將作為callback_kwargs引數傳遞。您只能包含管道類._callback_tensor_inputs屬性中列出的變數。
  • max_sequence_length (int, 預設為 224) — 編碼提示中的最大序列長度。可以設定為其他值,但可能會導致較差的結果。

返回

CogView3PipelineOutputtuple

如果return_dict為 True,則為CogView3PipelineOutput,否則為tuple。當返回元組時,第一個元素是包含生成影像的列表。

呼叫管道進行生成時呼叫的函式。

示例

>>> import torch
>>> from diffusers import CogView3PlusPipeline

>>> pipe = CogView3PlusPipeline.from_pretrained("THUDM/CogView3-Plus-3B", torch_dtype=torch.bfloat16)
>>> pipe.to("cuda")

>>> prompt = "A photo of an astronaut riding a horse on mars"
>>> image = pipe(prompt).images[0]
>>> image.save("output.png")

encode_prompt

< >

( prompt: typing.Union[str, typing.List[str]] negative_prompt: typing.Union[str, typing.List[str], NoneType] = None do_classifier_free_guidance: bool = True num_images_per_prompt: int = 1 prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None max_sequence_length: int = 224 device: typing.Optional[torch.device] = None dtype: typing.Optional[torch.dtype] = None )

引數

  • prompt (strList[str], 可選) — 待編碼的提示
  • negative_prompt (strList[str], 可選) — 不用於引導影像生成的提示。如果未定義,則必須傳遞negative_prompt_embeds。當不使用指導時(即,如果guidance_scale小於1),則忽略。
  • do_classifier_free_guidance (bool, 可選, 預設為 True) — 是否使用無分類器指導。
  • num_images_per_prompt (int, 可選, 預設為 1) — 每個提示應生成的影像數量。用於放置結果嵌入的torch裝置。
  • prompt_embeds (torch.Tensor, 可選) — 預生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,文字嵌入將從prompt輸入引數生成。
  • negative_prompt_embeds (torch.Tensor, 可選) — 預生成的負文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,negative_prompt_embeds將從negative_prompt輸入引數生成。
  • max_sequence_length (int, 預設為 224) — 編碼提示中的最大序列長度。可以設定為其他值,但可能會導致較差的結果。
  • device — (torch.device, 可選): torch裝置
  • dtype — (torch.dtype, 可選): torch資料型別

將提示編碼為文字編碼器隱藏狀態。

CogView3PipelineOutput

class diffusers.pipelines.cogview3.pipeline_output.CogView3PipelineOutput

< >

( images: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray] )

引數

  • images (List[PIL.Image.Image]np.ndarray) — 長度為batch_size的去噪PIL影像列表,或形狀為(batch_size, height, width, num_channels)的numpy陣列。PIL影像或numpy陣列表示擴散管道的去噪影像。

CogView3管道的輸出類。

< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.