Diffusers 文档
VAE 图像处理器
并获得增强的文档体验
开始使用
VAE 图像处理器
VaeImageProcessor
为 StableDiffusionPipeline 提供了一个统一的 API,用于准备 VAE 编码的图像输入,并在解码后进行后处理输出。这包括调整大小、归一化以及 PIL 图像、PyTorch 和 NumPy 数组之间的转换等转换。
所有带有 VaeImageProcessor
的 pipelines 都接受 PIL 图像、PyTorch 张量或 NumPy 数组作为图像输入,并根据用户通过 output_type
参数指定的输出类型返回输出。您可以将编码后的图像潜在空间直接传递给 pipeline,并使用 output_type
参数(例如 output_type="latent"
)从 pipeline 返回潜在空间作为特定输出。这允许您从一个 pipeline 中获取生成的潜在空间,并将其作为输入传递给另一个 pipeline,而无需离开潜在空间。通过在不同 pipelines 之间直接传递 PyTorch 张量,这也使得一起使用多个 pipelines 变得更加容易。
VaeImageProcessor
class diffusers.image_processor.VaeImageProcessor
< source >( do_resize: bool = True vae_scale_factor: int = 8 vae_latent_channels: int = 4 resample: str = 'lanczos' do_normalize: bool = True do_binarize: bool = False do_convert_rgb: bool = False do_convert_grayscale: bool = False )
参数
- do_resize (
bool
, 可选, 默认为True
) — 是否将图像的(高度,宽度)尺寸缩小到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 的图像处理器。
apply_overlay
< source >( mask: Image init_image: Image image: Image crop_coords: typing.Optional[typing.Tuple[int, int, int, int]] = None ) → PIL.Image.Image
将蒙版和修复后的图像叠加到原始图像上。
binarize
< 源码 >( image: Image ) → PIL.Image.Image
创建蒙版。
blur
< 源码 >( image: Image blur_factor: int = 4 ) → PIL.Image.Image
对图像应用高斯模糊。
convert_to_grayscale
< 源码 >( image: Image ) → PIL.Image.Image
将给定的 PIL 图像转换为灰度图。
convert_to_rgb
< 源码 >( image: Image ) → PIL.Image.Image
将 PIL 图像转换为 RGB 格式。
denormalize
< 源码 >( images: typing.Union[numpy.ndarray, torch.Tensor] ) → np.ndarray
or torch.Tensor
将图像数组反归一化到 [0,1]。
get_crop_region
< 源码 >( mask_image: Image width: int height: int pad = 0 ) → tuple
查找包含图像中所有蒙版区域的矩形区域,并扩展该区域以匹配原始图像的宽高比;例如,如果用户在 128x32 区域绘制了蒙版,而处理的尺寸为 512x512,则该区域将扩展到 128x128。
get_default_height_width
< 源码 >( image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor] height: typing.Optional[int] = None width: typing.Optional[int] = None ) → Tuple[int, int]
参数
- image (
Union[PIL.Image.Image, np.ndarray, torch.Tensor]
) — 图像输入,可以是 PIL 图像、NumPy 数组或 PyTorch 张量。 如果是 NumPy 数组,则应具有形状[batch, height, width]
或[batch, height, width, channels]
。 如果是 PyTorch 张量,则应具有形状[batch, channels, height, width]`。
- height (
Optional[int]
, optional, defaults toNone
) — 预处理图像的高度。 如果为None
,则将使用image
输入的高度。 - width (
Optional[int]
, optional, defaults toNone
) — 预处理图像的宽度。 如果为None
,则将使用image
输入的宽度。
返回
Tuple[int, int]
包含高度和宽度的元组,两者都调整为最接近 vae_scale_factor
整数倍的大小。
返回图像的高度和宽度,向下缩放到 `vae_scale_factor` 的下一个整数倍。
normalize
< 源码 >( 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: typing.Union[typing.List[PIL.Image.Image], PIL.Image.Image] ) → np.ndarray
将 PIL 图像或 PIL 图像列表转换为 NumPy 数组。
postprocess
< source >( image: Tensor output_type: str = 'pil' do_denormalize: typing.Optional[typing.List[bool]] = None ) → PIL.Image.Image
, np.ndarray
或 torch.Tensor
将张量输出的图像后处理为 output_type
。
preprocess
< source >( image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, typing.List[PIL.Image.Image], typing.List[numpy.ndarray], typing.List[torch.Tensor]] height: typing.Optional[int] = None width: typing.Optional[int] = None resize_mode: str = 'default' crops_coords: typing.Optional[typing.Tuple[int, int, int, int]] = 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
,将调整图像大小以适应指定的宽度和高度,保持宽高比,然后在尺寸内居中图像,并裁剪多余部分。请注意,仅 PIL 图像输入支持fill
和crop
调整大小模式。 - crops_coords (
List[Tuple[int, int, int, int]]
, 可选,默认为None
) — 批次中每张图像的裁剪坐标。如果为None
,则不会裁剪图像。
返回
torch.Tensor
预处理后的图像。
预处理图像输入。
pt_to_numpy
< source >( images: Tensor ) → np.ndarray
将 PyTorch 张量转换为 NumPy 图像。
resize
< source >( image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor] height: int width: int resize_mode: str = 'default' ) → PIL.Image.Image
, np.ndarray
或 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
调整大小模式仅支持 PIL 图像输入。
返回
PIL.Image.Image
, np.ndarray
或 torch.Tensor
调整大小后的图像。
调整图像大小。
VaeImageProcessorLDM3D
VaeImageProcessorLDM3D
接受 RGB 和深度输入,并返回 RGB 和深度输出。
class diffusers.image_processor.VaeImageProcessorLDM3D
< source >( do_resize: bool = True vae_scale_factor: int = 8 resample: str = 'lanczos' do_normalize: bool = True )
用于 VAE LDM3D 的图像处理器。
depth_pil_to_numpy
< source >( images: typing.Union[typing.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 图像列表。
preprocess
< source >( rgb: typing.Union[torch.Tensor, PIL.Image.Image, numpy.ndarray] depth: typing.Union[torch.Tensor, PIL.Image.Image, numpy.ndarray] height: typing.Optional[int] = None width: typing.Optional[int] = None target_res: typing.Optional[int] = None ) → Tuple[torch.Tensor, torch.Tensor]
参数
- rgb (
Union[torch.Tensor, PIL.Image.Image, np.ndarray]
) — RGB 输入图像,可以是单张图像或一批图像。 - depth (
Union[torch.Tensor, PIL.Image.Image, np.ndarray]
) — 深度输入图像,可以是单张图像或一批图像。 - height (
Optional[int]
, 可选, 默认为None
) — 处理后图像的期望高度。如果为None
,则默认为输入图像的高度。 - width (
Optional[int]
, 可选, 默认为None
) — 处理后图像的期望宽度。如果为None
,则默认为输入图像的宽度。 - target_res (
Optional[int]
, 可选, 默认为None
) — 用于调整图像大小的目标分辨率。如果指定,则覆盖高度和宽度。
返回
Tuple[torch.Tensor, torch.Tensor]
包含处理后的 RGB 和深度图像作为 PyTorch 张量的元组。
预处理图像输入。接受的格式为 PIL 图像、NumPy 数组或 PyTorch 张量。
rgblike_to_depthmap
< source >( image: typing.Union[numpy.ndarray, torch.Tensor] ) → Union[np.ndarray, torch.Tensor]
将类 RGB 深度图像转换为深度图。
PixArtImageProcessor
class diffusers.image_processor.PixArtImageProcessor
< source >( do_resize: bool = True vae_scale_factor: int = 8 resample: str = 'lanczos' do_normalize: bool = True do_binarize: bool = False do_convert_grayscale: bool = False )
参数
- do_resize (
bool
, 可选, 默认为True
) — 是否将图像的 (高度, 宽度) 尺寸缩小到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: int width: int ratios: dict ) → Tuple[int, int]
返回基于宽高比的分箱高度和宽度。
调整张量图像的大小并裁剪到指定的尺寸。
IPAdapterMaskProcessor
class diffusers.image_processor.IPAdapterMaskProcessor
< source >( do_resize: bool = True vae_scale_factor: int = 8 resample: str = 'lanczos' do_normalize: bool = False do_binarize: bool = True do_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 图像掩码的图像处理器。
downsample
< source >( mask: Tensor batch_size: int num_queries: int value_embed_dim: int ) → torch.Tensor
下采样提供的掩码张量,以匹配缩放点积注意力的预期尺寸。如果掩码的宽高比与输出图像的宽高比不匹配,则会发出警告。