Diffusers 文件
VAE 影像處理器
並獲得增強的文件體驗
開始使用
VAE 影像處理器
VaeImageProcessor
提供統一的 API,用於 StableDiffusionPipeline 準備 VAE 編碼的影像輸入並在解碼後處理輸出。這包括調整大小、歸一化以及 PIL 影像、PyTorch 和 NumPy 陣列之間的轉換等操作。
所有帶有 VaeImageProcessor
的管道都接受 PIL 影像、PyTorch 張量或 NumPy 陣列作為影像輸入,並根據使用者提供的 output_type
引數返回輸出。您可以將編碼後的影像潛在特徵直接傳遞給管道,並使用 output_type
引數(例如 output_type="latent"
)將潛在特徵作為特定輸出從管道返回。這允許您將一個管道生成的潛在特徵傳遞給另一個管道作為輸入,而無需離開潛在空間。它還使得透過在不同管道之間直接傳遞 PyTorch 張量來更容易地同時使用多個管道。
VaeImageProcessor
class diffusers.image_processor.VaeImageProcessor
< source >( do_resize: bool = True vae_scale_factor: int = 8 vae_latent_channels: int = 4 resample: str = 'lanczos' reducing_gap: int = None 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 影像處理器。
應用疊加
< source >( mask: Image init_image: Image image: Image crop_coords: typing.Optional[typing.Tuple[int, int, int, int]] = None ) → PIL.Image.Image
在原始影像上疊加遮罩和修復後的影像。
二值化
< source >( image: Image ) → PIL.Image.Image
建立遮罩。
模糊
< source >( image: Image blur_factor: int = 4 ) → PIL.Image.Image
對影像應用高斯模糊。
轉換為灰度
< source >( image: Image ) → PIL.Image.Image
將給定的 PIL 影像轉換為灰度。
轉換為 RGB
< source >( image: Image ) → PIL.Image.Image
將 PIL 影像轉換為 RGB 格式。
去歸一化
< source >( images: typing.Union[numpy.ndarray, torch.Tensor] ) → np.ndarray
或 torch.Tensor
將影像陣列去歸一化到 [0,1]。
獲取裁剪區域
< source >( mask_image: Image width: int height: int pad = 0 ) → tuple
查詢影像中包含所有遮罩區域的矩形區域,並擴充套件該區域以匹配原始影像的寬高比;例如,如果使用者在 128x32 區域繪製遮罩,並且處理尺寸為 512x512,則該區域將擴充套件到 128x128。
獲取預設高度和寬度
< source >( 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]
, 可選, 預設為None
) — 預處理影像的高度。如果為None
,將使用image
輸入的高度。 - width (
Optional[int]
, 可選, 預設為None
) — 預處理影像的寬度。如果為None
,將使用image
輸入的寬度。
返回
Tuple[int, int]
一個包含高度和寬度的元組,兩者都已調整為 vae_scale_factor
的最近整數倍。
返回影像的高度和寬度,已縮減到 vae_scale_factor
的下一個整數倍。
歸一化
< source >( images: typing.Union[numpy.ndarray, torch.Tensor] ) → np.ndarray
或 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
or 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
,影像將調整大小以適應指定的寬度和高度,保持縱橫比,然後將影像居中放置在維度內,並裁剪多餘部分。請注意,調整大小模式fill
和crop
僅支援 PIL 影像輸入。 - 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
or torch.Tensor
引數
- image (
PIL.Image.Image
,np.ndarray
ortorch.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
, optional, 預設為False
) — 是否將影像轉換為 RGB 格式。 - do_convert_grayscale (
bool
, optional, 預設為False
) — 是否將影像轉換為灰度格式。
PixArt 影像處理器,用於影像大小調整和裁剪。
classify_height_width_bin
< source 原始碼 >( height: int width: int ratios: dict ) → Tuple[int, int]
根據縱橫比返回已分箱的高度和寬度。
resize_and_crop_tensor
< source 原始碼 >( samples: Tensor new_width: int new_height: int ) → torch.Tensor
將影像張量調整大小並裁剪到指定尺寸。
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
, optional, 預設為True
) — 是否將影像的 (高度, 寬度) 維度縮小為vae_scale_factor
的倍數。 - vae_scale_factor (
int
, optional, 預設為8
) — VAE 縮放因子。如果do_resize
為True
,影像將自動調整大小為該因子的倍數。 - resample (
str
, optional, 預設為'lanczos'
) — 調整影像大小時使用的重取樣濾波器。 - do_normalize (
bool
, optional, 預設為False
) — 是否將影像歸一化到 [-1,1]。 - do_binarize (
bool
, optional, 預設為True
) — 是否將影像二值化為 0/1。 - do_convert_grayscale (
bool
, optional, 預設為True
) — 是否將影像轉換為灰度格式。
IP Adapter 影像遮罩的影像處理器。
下采樣
< source 原始碼 >( mask: Tensor batch_size: int num_queries: int value_embed_dim: int ) → torch.Tensor
將提供的遮罩張量下采樣以匹配縮放點積注意力機制的預期維度。如果遮罩的縱橫比與輸出影像的縱橫比不匹配,則會發出警告。