VAE 图像处理器
VaeImageProcessor 为 StableDiffusionPipeline 提供了统一的 API,用于在 VAE 编码前准备图像输入,以及在解码后对输出进行后处理。这包括调整大小、归一化以及 PIL Image、PyTorch 和 NumPy 数组之间的转换等操作。
所有使用 VaeImageProcessor 的 pipeline 均接受 PIL Image、PyTorch tensor 或 NumPy 数组作为图像输入,并根据用户提供的 output_type 参数返回相应的输出。您可以直接将编码后的图像 latents 传入 pipeline,并通过 output_type 参数(例如 output_type="latent")将 latents 作为特定输出从 pipeline 返回。这使得您可以将一个 pipeline 生成的 latents 直接作为输入传递给另一个 pipeline,而无需离开 latent 空间。此外,通过在不同 pipeline 之间直接传递 PyTorch tensor,使用多个 pipeline 协同工作也变得更加简单。
VaeImageProcessor
类 diffusers.VaeImageProcessor
< source >( do_resize: bool = Truevae_scale_factor: int = 8vae_latent_channels: int = 4resample: str = 'lanczos'reducing_gap: int | None = Nonedo_normalize: bool = Truedo_binarize: bool = Falsedo_convert_rgb: bool = Falsedo_convert_grayscale: bool = False )
参数
- do_resize (
bool,可选,默认为True) — 是否将图像的 (height, width) 维度缩小为vae_scale_factor的倍数。可以接受来自 image_processor.VaeImageProcessor.preprocess() 方法的height和width参数。 - vae_scale_factor (
int,可选,默认为8) — VAE 缩放因子。如果do_resize为True,图像将自动调整为此因子倍数的尺寸。 - resample (
str,可选,默认为lanczos) — 调整图像大小时使用的重采样滤波器。 - do_normalize (
bool,可选,默认为True) — 是否将图像归一化到 [-1,1]。 - do_binarize (
bool,可选,默认为False) — 是否将图像二值化为 0/1。 - do_convert_rgb (
bool,可选,默认为False) — 是否将图像转换为 RGB 格式。 - do_convert_grayscale (
bool,可选,默认为False) — 是否将图像转换为灰度格式。
VAE 的图像处理器。
应用叠加层
< source >( mask: Imageinit_image: Imageimage: Imagecrop_coords: tuple[int, int, int, int] | None = None ) → PIL.Image.Image
在原始图像上叠加显示掩码和修复后的图像。
binarize
< source >( image: Image ) → PIL.Image.Image
创建一个掩码。
模糊
< source >( image: Imageblur_factor: int = 4 ) → PIL.Image.Image
对图像应用高斯模糊。
convert_to_grayscale
< source >( image: Image ) → PIL.Image.Image
将给定的 PIL 图像转换为灰度图像。
convert_to_rgb
< source >( image: Image ) → PIL.Image.Image
将 PIL 图像转换为 RGB 格式。
denormalize
< source >( images: typing.Union[numpy.ndarray, torch.Tensor] ) → np.ndarray or torch.Tensor
将图像数组反归一化到 [0,1] 范围。
get_crop_region
< source >( mask_image: Imagewidth: intheight: intpad = 0 ) → tuple
查找包含图像中所有掩码区域的矩形区域,并将该区域扩展以匹配原始图像的宽高比;例如,如果用户在 128x32 的区域中绘制了掩码,而处理尺寸为 512x512,则该区域将被扩展为 128x128。
get_default_height_width
< source >( image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor]height: int | None = Nonewidth: int | None = None ) → tuple[int, int]
参数
- image (
PIL.Image.Image | np.ndarray | torch.Tensor) — 图像输入,可以是 PIL 图像、NumPy 数组或 PyTorch 张量。如果是 NumPy 数组,其形状应为[batch, height, width]或[batch, height, width, channels]。如果是 PyTorch 张量,其形状应为[batch, channels, height, width]。 - height (
int | None,可选,默认为None) — 预处理图像的高度。如果为None,则使用image输入的高度。 - width (
int | None,可选,默认为None) — 预处理图像的宽度。如果为None,则使用image输入的宽度。
返回值
tuple[int, int]
一个元组,包含高度和宽度,两者均调整为 vae_scale_factor 的最近整数倍。
返回图像的高度和宽度,向下取整为 vae_scale_factor 的下一个整数倍。
normalize
< source >( images: typing.Union[numpy.ndarray, torch.Tensor] ) → np.ndarray or torch.Tensor
将图像数组归一化到 [-1,1]。
numpy_to_pil
< source >( images: ndarray ) → list[PIL.Image.Image]
将 numpy 图像或一批图像转换为 PIL 图像。
numpy_to_pt
< source >( images: ndarray ) → torch.Tensor
将 NumPy 图像转换为 PyTorch 张量。
pil_to_numpy
< source >( images: list[PIL.Image.Image] | PIL.Image.Image ) → np.ndarray
将 PIL 图像或 PIL 图像列表转换为 NumPy 数组。
后处理
< source >( image: Tensoroutput_type: str = 'pil'do_denormalize: list[bool] | None = None ) → PIL.Image.Image, np.ndarray or torch.Tensor
将图像输出从张量后处理为output_type。
预处理
< source >( image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, list[PIL.Image.Image], list[numpy.ndarray], list[torch.Tensor]]height: int | None = Nonewidth: int | None = Noneresize_mode: str = 'default'crops_coords: tuple[int, int, int, int] | None = None ) → torch.Tensor
参数
- image (
PipelineImageInput) — 图像输入,接受的格式为 PIL 图像、NumPy 数组、PyTorch 张量;也接受支持格式的列表。 - height (
int, 可选) — 预处理图像的高度。如果为None,将使用get_default_height_width()获取默认高度。 - width (
int, 可选) — 预处理后的宽度。如果为None,将使用 get_default_height_width()` 获取默认宽度。 - resize_mode (
str, 可选, 默认为default) — 缩放模式,可以是default或fill之一。如果为default,则将图像缩放以适应指定的宽度和高度,且可能不保持原始宽高比。如果为fill,则将图像缩放以适应指定的宽度和高度,保持宽高比,然后将图像居中于该尺寸内,并用图像数据填充空白区域。如果为crop,则将图像缩放以适应指定的宽度和高度,保持宽高比,然后将图像居中于该尺寸内,并裁剪掉多余部分。请注意,fill和crop这两种 resize_mode 仅支持 PIL 图像输入。 - crops_coords (
list[tuple[int, int, int, int]],可选,默认为None) — 批次中每张图片的裁剪坐标。如果为None,则不对图片进行裁剪。
返回值
torch.Tensor
预处理后的图像。
预处理图像输入。
pt_to_numpy
< source >( images: Tensor ) → np.ndarray
将 PyTorch 张量转换为 NumPy 图像。
调整大小
< source >( image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor]height: intwidth: intresize_mode: str = 'default' ) → PIL.Image.Image, np.ndarray or torch.Tensor
参数
- image (
PIL.Image.Image、np.ndarray或torch.Tensor) — 图像输入,可以是 PIL 图像、numpy 数组或 pytorch 张量。 - height (
int) — 要调整到的目标高度。 - width (
int) — 要调整到的宽度。 - resize_mode (
str, 可选, 默认为default) — 要使用的缩放模式,可以是default或fill之一。如果为default,则将图像缩放以适应指定的宽度和高度,且可能不保持原始宽高比。如果为fill,则将图像缩放以适应指定的宽度和高度,保持宽高比,然后将图像居中于该尺寸内,并用图像数据填充空白区域。如果为crop,则将图像缩放以适应指定的宽度和高度,保持宽高比,然后将图像居中于该尺寸内,并裁剪掉多余部分。请注意,fill和crop这两种 resize_mode 仅支持 PIL 图像输入。
返回值
PIL.Image.Image、np.ndarray 或 torch.Tensor
调整大小后的图像。
调整图像大小。
InpaintProcessor
InpaintProcessor 接受 mask 和 image 输入并一起处理它们。可选地,它可以接受 padding_mask_crop 并应用 mask 叠加。
类 diffusers.InpaintProcessor
< source >( do_resize: bool = Truevae_scale_factor: int = 8vae_latent_channels: int = 4resample: str = 'lanczos'reducing_gap: int | None = Nonedo_normalize: bool = Truedo_binarize: bool = Falsedo_convert_grayscale: bool = Falsemask_do_normalize: bool = Falsemask_do_binarize: bool = Truemask_do_convert_grayscale: bool = True )
用于图像和掩码修复的图像处理器。
后处理
< source >( image: Tensoroutput_type: str = 'pil'original_image: PIL.Image.Image | None = Noneoriginal_mask: PIL.Image.Image | None = Nonecrops_coords: tuple[int, int, int, int] | None = None )
对图像进行后处理,可选地应用遮罩叠加
预处理
< source >( image: Imagemask: PIL.Image.Image | None = Noneheight: int | None = Nonewidth: int | None = Nonepadding_mask_crop: int | None = None )
预处理图像和掩码。
VaeImageProcessorLDM3D
VaeImageProcessorLDM3D 接受 RGB 和深度输入,并返回 RGB 和深度输出。
类 diffusers.VaeImageProcessorLDM3D
< source >( do_resize: bool = Truevae_scale_factor: int = 8resample: str = 'lanczos'do_normalize: bool = True )
VAE LDM3D 的图像处理器。
depth_pil_to_numpy
< source >( images: list[PIL.Image.Image] | PIL.Image.Image ) → np.ndarray
将 PIL 图像或 PIL 图像列表转换为 NumPy 数组。
numpy_to_depth
< source >( images: ndarray ) → list[PIL.Image.Image]
将 NumPy 深度图像或一批图像转换为 PIL 图像列表。
numpy_to_pil
< source >( images: ndarray ) → list[PIL.Image.Image]
将 NumPy 图像或一批图像转换为 PIL 图像列表。
预处理
< source >( rgb: typing.Union[torch.Tensor, PIL.Image.Image, numpy.ndarray]depth: typing.Union[torch.Tensor, PIL.Image.Image, numpy.ndarray]height: int | None = Nonewidth: int | None = Nonetarget_res: int | None = None ) → tuple[torch.Tensor, torch.Tensor]
参数
- rgb (
torch.Tensor | PIL.Image.Image | np.ndarray) — RGB 输入图像,可以是单张图像或一个批次。 - depth (
torch.Tensor | PIL.Image.Image | np.ndarray) — 深度输入图像,可以是单张图像或一个批次。 - height (
int | None,可选,默认为None) — 处理后图像的目标高度。如果为None,则默认为输入图像的高度。 - width (
int | None,可选,默认为None) — 处理后图像的期望宽度。如果为None,则默认为输入图像的宽度。 - target_res (
int | None,可选,默认为None) — 用于调整图像大小的目标分辨率。如果指定,将覆盖 height 和 width。
返回值
tuple[torch.Tensor, torch.Tensor]
一个元组,包含处理后的 RGB 图像和深度图像,均为 PyTorch 张量。
预处理图像输入。接受的格式为 PIL 图像、NumPy 数组或 PyTorch 张量。
将类似 RGB 的深度图像转换为深度图。
PixArtImageProcessor
class diffusers.PixArtImageProcessor
< source >( do_resize: bool = Truevae_scale_factor: int = 8resample: str = 'lanczos'do_normalize: bool = Truedo_binarize: bool = Falsedo_convert_grayscale: bool = False )
参数
- do_resize (
bool,可选,默认为True) — 是否将图像的 (height, width) 维度缩小为vae_scale_factor的倍数。可以接受来自 image_processor.VaeImageProcessor.preprocess() 方法的height和width参数。 - vae_scale_factor (
int,可选,默认为8) — VAE 缩放因子。如果do_resize为True,图像将自动调整为此因子倍数的尺寸。 - resample (
str,可选,默认为lanczos) — 调整图像大小时使用的重采样滤波器。 - do_normalize (
bool,可选,默认为True) — 是否将图像归一化到 [-1,1]。 - do_binarize (
bool,可选,默认为False) — 是否将图像二值化为 0/1。 - do_convert_rgb (
bool,可选,默认为False) — 是否将图像转换为 RGB 格式。 - do_convert_grayscale (
bool,可选,默认为False) — 是否将图像转换为灰度格式。
用于 PixArt 图像缩放和裁剪的图像处理器。
classify_height_width_bin
< source >( height: intwidth: intratios: dict ) → tuple[int, int]
根据宽高比返回分箱后的高度和宽度。
resize_and_crop_tensor
< source >( samples: Tensornew_width: intnew_height: int ) → torch.Tensor
将图像张量调整大小并裁剪到指定尺寸。
IPAdapterMaskProcessor
类 diffusers.IPAdapterMaskProcessor
< source >( do_resize: bool = Truevae_scale_factor: int = 8resample: str = 'lanczos'do_normalize: bool = Falsedo_binarize: bool = Truedo_convert_grayscale: bool = True )
参数
- do_resize (
bool,可选,默认为True) — 是否将图像的(高度,宽度)维度缩小为vae_scale_factor的倍数。 - vae_scale_factor (
int,可选,默认为8) — VAE 缩放因子。如果do_resize为True,图像将自动调整为此因子倍数的尺寸。 - resample (
str,可选,默认为lanczos) — 调整图像大小时使用的重采样滤波器。 - do_normalize (
bool,可选,默认为False) — 是否将图像归一化到 [-1,1]。 - do_binarize (
bool,可选,默认为True) — 是否将图像二值化为 0/1。 - do_convert_grayscale (
bool,可选,默认为True) — 是否将图像转换为灰度格式。
用于 IP Adapter 图像蒙版的图像处理器。
下采样
< source >( mask: Tensorbatch_size: intnum_queries: intvalue_embed_dim: int ) → torch.Tensor
将提供的掩码张量下采样,以匹配缩放点积注意力所需的预期维度。如果掩码的宽高比与输出图像的宽高比不匹配,则会发出警告。