Diffusers documentation
Bria Fibo 编辑
Bria Fibo 编辑
Fibo Edit 是一个拥有 80 亿参数的图像到图像模型,它引入了一种新的结构化控制范式,通过处理与源图像配对的 JSON 输入,实现确定性和可重复的编辑工作流。 该模型具备原生掩码功能,可实现精细的精准控制,超越了基于简单提示词的扩散模型,为生产环境提供了显式且可解释的控制。 其轻量级架构专为深度定制而设计,使研究人员能够针对特定领域任务构建专用的“Edit”模型,同时提供一流的美学质量。
请参阅 Bria Fibo Edit 的 Hugging Face 页面以了解更多信息。蒸馏检查点可在 Fibo-Edit-1.5-turbo 处获取。
用法
由于该模型受访问限制,在使用 diffusers 之前,您需要先前往 Bria Fibo Edit Hugging Face 页面,填写表单并接受访问限制。进入后,您需要登录,以便系统确认您已接受该访问限制。
使用以下命令登录:
hf auth login
BriaFiboEditPipeline
class diffusers.BriaFiboEditPipeline
< source >( transformer: BriaFiboTransformer2DModelscheduler: typing.Union[diffusers.schedulers.scheduling_flow_match_euler_discrete.FlowMatchEulerDiscreteScheduler, diffusers.schedulers.scheduling_utils.KarrasDiffusionSchedulers]vae: AutoencoderKLWantext_encoder: SmolLM3ForCausalLMtokenizer: AutoTokenizer )
参数
- transformer (
BriaFiboTransformer2DModel) — 用于 2D 扩散建模的 transformer 模型。 - scheduler(
FlowMatchEulerDiscreteScheduler或KarrasDiffusionSchedulers)— 与transformer配合使用,用于对编码后的潜变量进行去噪的调度器。 - vae (
AutoencoderKLWan) — 用于将图像编码为潜在表示并从中解码的变分自编码器。 - text_encoder (
SmolLM3ForCausalLM) — 用于处理输入提示的文本编码器。 - tokenizer (
AutoTokenizer) — 用于处理输入文本提示词以提供给 text_encoder 的 Tokenizer。
__call__
< source >( prompt: typing.Union[str, typing.List[str]] = Noneimage: typing.Union[PIL.Image.Image, typing.List[PIL.Image.Image], NoneType] = Nonemask: typing.Union[torch.FloatTensor, PIL.Image.Image, typing.List[PIL.Image.Image], typing.List[torch.FloatTensor], numpy.ndarray, typing.List[numpy.ndarray], NoneType] = Noneheight: int | None = Nonewidth: int | None = Nonenum_inference_steps: int = 30timesteps: typing.List[int] = Noneseed: int | None = Noneguidance_scale: float = 5negative_prompt: typing.Union[str, typing.List[str], NoneType] = Nonenum_images_per_prompt: typing.Optional[int] = 1generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Nonelatents: typing.Optional[torch.FloatTensor] = Noneoutput_type: str = 'pil'return_dict: bool = Truejoint_attention_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = Nonecallback_on_step_end: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = Nonecallback_on_step_end_tensor_inputs: typing.List[str] = ['latents']max_sequence_length: int = 3000do_patching = False_auto_resize: bool = True ) → ~pipelines.flux.BriaFiboPipelineOutput or tuple
参数
- prompt (
str或List[str]) — 用于引导图像生成的提示词或提示词列表。 - image (
PIL.Image.Image或List[PIL.Image.Image],可选) — 一个或多个用于引导图像生成的参考图像。列表被解释为多个参考(而非一个批次):每个参考都会按其自身的宽高比进行 VAE 编码,并放置在各自的 RoPE 时间平面 1、2、… 上。如果未定义,流水线将从零开始生成图像。 - mask (
PipelineMaskInput,可选) — 可选的掩码,用于定义image中需要编辑的区域。掩码覆盖的像素将被重新生成,而图像的其余部分将保持不变。 - height (
int,可选,默认为 self.unet.config.sample_size * self.vae_scale_factor) — 生成图像的高度(以像素为单位)。默认设置为 1024,以获得最佳效果。 - width (
int,可选,默认为 self.unet.config.sample_size * self.vae_scale_factor) — 生成图像的宽度(以像素为单位)。默认设置为 1024,以获得最佳效果。 - num_inference_steps (
int,可选,默认为 30) — 去噪步数。更多的去噪步数通常能生成更高质量的图像,但代价是推理速度变慢。 - seed (
int, 可选) — 用于使生成过程确定性的随机种子。 - timesteps (
List[int], 可选) — 用于支持timesteps参数的调度器去噪过程的自定义时间步,该参数位于其set_timesteps方法中。如果未定义,则使用传入num_inference_steps时的默认行为。必须按降序排列。 - guidance_scale (
float,可选,默认值为 5.0) — Classifier-Free Diffusion Guidance 中定义的引导尺度。guidance_scale定义为 Imagen Paper 中公式 2 的w。通过设置guidance_scale > 1来启用引导尺度。较高的引导尺度会促使生成与文本prompt紧密关联的图像,通常以牺牲图像质量为代价。 - negative_prompt (
str或List[str],可选) — 用于引导图像生成的反向提示词。当不使用引导时会被忽略(即当guidance_scale小于1时会被忽略)。 - num_images_per_prompt (
int,可选,默认为 1) — 每个提示词生成的图像数量。 - generator (
torch.Generator或List[torch.Generator],可选) — 一个或一组 torch 生成器,用于使生成过程具有确定性。 - latents (
torch.FloatTensor, 可选) — 预先生成的噪声潜变量,从高斯分布中采样,用作图像生成的输入。可用于使用不同的提示词对同一生成过程进行微调。如果未提供,将通过使用提供的随机generator进行采样来生成一个潜变量张量。 - output_type (
str,可选,默认为"pil") — 生成图像的输出格式。可选值为 PIL:PIL.Image.Image或np.array。 - return_dict (
bool,可选,默认为True) — 是否返回~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput而不是普通元组。 - joint_attention_kwargs (
dict, 可选) — 一个 kwargs 字典,如果指定,将作为参数传递给 diffusers.models.attention_processor 中self.processor下定义的AttentionProcessor。 - 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,默认为 3000)— 与prompt配合使用的最大序列长度。 - do_patching (
bool,可选,默认为False) — 是否使用 patching。 - _auto_resize (
bool,可选,默认为True) — 是否将默认输出分辨率(取自第一张参考图像)对齐到首选分辨率。
返回值
~pipelines.flux.BriaFiboPipelineOutput 或 tuple
如果 return_dict 为 True,则返回 ~pipelines.flux.BriaFiboPipelineOutput,否则返回 tuple。当返回元组时,第一个元素是一个包含生成图像的列表。
调用生成 pipeline 时调用的函数。
示例:
import torch
from PIL import Image
from diffusers import BriaFiboEditPipeline
from diffusers.modular_pipelines import ModularPipelineBlocks
# This prompt-to-JSON block calls Gemini and needs GEMINI_API_KEY in the environment.
vlm_pipe = ModularPipelineBlocks.from_pretrained("briaai/FIBO-edit-gemini-prompt-to-JSON", trust_remote_code=True)
vlm_pipe = vlm_pipe.init_pipeline()
pipe = BriaFiboEditPipeline.from_pretrained(
"briaai/Fibo-Edit-1.5-base",
torch_dtype=torch.bfloat16,
)
pipe.to("cuda")
image = Image.open("owl.png")
json_prompt = vlm_pipe(image=image, prompt="Make the owl into a cat").values["json_prompt"]
result = pipe(prompt=json_prompt, image=image, num_inference_steps=30, guidance_scale=5)
# Multiple reference images: pass a list. Each reference conditions the edit at its
# own aspect ratio; the output resolution follows the first reference.
owl, forest = Image.open("owl.png"), Image.open("forest.png")
json_prompt = vlm_pipe(
image=[owl, forest], prompt="Place the owl from the first image in the forest from the second image"
).values["json_prompt"]
result = pipe(
prompt=json_prompt,
image=[owl, forest],
num_inference_steps=30,
guidance_scale=5,
)
# The distilled Turbo checkpoint edits in 4 steps without classifier-free guidance.
pipe = BriaFiboEditPipeline.from_pretrained("briaai/Fibo-Edit-1.5-turbo", torch_dtype=torch.bfloat16)
pipe.to("cuda")
result = pipe(prompt=json_prompt, image=[owl, forest], num_inference_steps=4, guidance_scale=1)encode_prompt
< source >( prompt: typing.Union[str, typing.List[str]]device: typing.Optional[torch.device] = Nonenum_images_per_prompt: int = 1guidance_scale: float = 5negative_prompt: typing.Union[str, typing.List[str], NoneType] = Nonemax_sequence_length: int = 3000lora_scale: bool | None = None )
prepare_reference_latents
< source >( image: Imagenum_channels_latents: intdtype: dtypedevice: devicedo_patching: bool = Falsereference_index: int = 1 )
使用 VAE 以原始尺寸对一张 PIL 参考图像进行编码,并将其打包为编辑上下文 token 流。