Diffusers 文件
CogView4
並獲得增強的文件體驗
開始使用
CogView4
此管道由zRzRzRzRzRzRzR貢獻。原始程式碼庫可在此處找到。原始權重可在hf.co/THUDM找到。
CogView4Pipeline
class diffusers.CogView4Pipeline
< 來源 >( tokenizer: AutoTokenizer text_encoder: GlmModel vae: AutoencoderKL transformer: CogView4Transformer2DModel scheduler: FlowMatchEulerDiscreteScheduler )
引數
- vae (AutoencoderKL) — 變分自動編碼器 (VAE) 模型,用於將影像編碼和解碼為潛在表示。
- text_encoder (
GLMModel
) — 凍結的文字編碼器。CogView4 使用 glm-4-9b-hf。 - tokenizer (
PreTrainedTokenizer
) — PreTrainedTokenizer 類別的分詞器。 - transformer (CogView4Transformer2DModel) — 一個文字條件
CogView4Transformer2DModel
,用於對編碼影像潛在表示進行去噪。 - scheduler (SchedulerMixin) — 與
transformer
結合使用以對編碼影像潛在表示進行去噪的排程器。
使用 CogView4 進行文字到影像生成的管道。
該模型繼承自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 sigmas: typing.Optional[typing.List[float]] = None guidance_scale: float = 5.0 num_images_per_prompt: int = 1 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 attention_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None 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 = 1024 ) → ~pipelines.cogview4.pipeline_CogView4.CogView4PipelineOutput
或 tuple
引數
- prompt (
str
或List[str]
, 可選) — 用於引導影像生成的提示或提示列表。如果未定義,則必須傳遞prompt_embeds
。 - negative_prompt (
str
或List[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
時的預設行為。必須按降序排列。 - sigmas (
List[float]
, 可選) — 用於去噪過程的自定義 sigmas,適用於支援set_timesteps
方法中sigmas
引數的排程器。如果未定義,將使用傳遞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.Generator
或List[torch.Generator]
, 可選) — 一個或多個 torch 生成器,用於使生成具有確定性。 - latents (
torch.FloatTensor
, 可選) — 預生成的噪聲潛在量,從高斯分佈中取樣,用作影像生成的輸入。可用於使用不同提示調整相同生成。如果未提供,將使用提供的隨機generator
取樣生成一個潛在量張量。 - prompt_embeds (
torch.FloatTensor
, 可選) — 預生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,文字嵌入將從prompt
輸入引數生成。 - negative_prompt_embeds (
torch.FloatTensor
, 可選) — 預生成的負面文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,負面提示嵌入將從negative_prompt
輸入引數生成。 - original_size (
Tuple[int]
, 可選, 預設為 (1024, 1024)) — 如果original_size
與target_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.Image
或np.array
之間選擇。 - return_dict (
bool
, 可選, 預設為True
) — 是否返回~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput
而不是普通元組。 - attention_kwargs (
dict
, optional) — 一個可選的 kwargs 字典,如果指定,將作為引數傳遞給self.processor
中定義的AttentionProcessor
,詳見 diffusers.models.attention_processor。 - callback_on_step_end (
Callable
, optional) — 在推理過程中,每個去噪步驟結束時呼叫的函式。該函式透過以下引數呼叫: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
, optional) —callback_on_step_end
函式的張量輸入列表。列表中指定的張量將作為callback_kwargs
引數傳遞。您只能包含管道類._callback_tensor_inputs
屬性中列出的變數。 - max_sequence_length (
int
, 預設為224
) — 編碼提示中的最大序列長度。可以設定為其他值,但可能會導致較差的結果。
返回
~pipelines.cogview4.pipeline_CogView4.CogView4PipelineOutput
或 tuple
如果 return_dict
為 True,則返回 ~pipelines.cogview4.pipeline_CogView4.CogView4PipelineOutput
,否則返回 tuple
。返回元組時,第一個元素是生成的影像列表。
呼叫管道進行生成時呼叫的函式。
示例
>>> import torch
>>> from diffusers import CogView4Pipeline
>>> pipe = CogView4Pipeline.from_pretrained("THUDM/CogView4-6B", 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
< source >( 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 device: typing.Optional[torch.device] = None dtype: typing.Optional[torch.dtype] = None max_sequence_length: int = 1024 )
引數
- prompt (
str
或List[str]
, 可選) — 待編碼的提示。 - negative_prompt (
str
或List[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
輸入引數生成。 - device — (
torch.device
, 可選): torch 裝置。 - dtype — (
torch.dtype
, 可選): torch 資料型別。 - max_sequence_length (
int
, 預設為1024
) — 編碼提示中的最大序列長度。可以設定為其他值,但可能會導致較差的結果。
將提示編碼為文字編碼器隱藏狀態。
CogView4PipelineOutput
class diffusers.pipelines.cogview4.pipeline_output.CogView4PipelineOutput
< source >( images: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray] )
CogView3 管道的輸出類。