Transformers 文件

影像處理器實用程式

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

影像處理器實用程式

本頁列出了影像處理器使用的所有實用函式,主要是用於處理影像的功能轉換。

這些功能大多隻在您研究庫中影像處理器的程式碼時有用。

影像轉換

transformers.image_transforms.center_crop

< >

( 影像: ndarray 尺寸: 元組 資料格式: typing.Union[transformers.image_utils.ChannelDimension, str, NoneType] = None 輸入資料格式: typing.Union[transformers.image_utils.ChannelDimension, str, NoneType] = None ) np.ndarray

引數

  • 影像 (np.ndarray) — 要裁剪的影像。
  • 尺寸 (tuple[int, int]) — 裁剪影像的目標尺寸。
  • 資料格式 (strChannelDimension, 可選) — 輸出影像的通道維度格式。可以是以下之一:
    • "channels_first"ChannelDimension.FIRST: 影像格式為 (num_channels, height, width)。
    • "channels_last"ChannelDimension.LAST: 影像格式為 (height, width, num_channels)。如果未設定,將使用輸入影像的推斷格式。
  • 輸入資料格式 (strChannelDimension, 可選) — 輸入影像的通道維度格式。可以是以下之一:
    • "channels_first"ChannelDimension.FIRST: 影像格式為 (num_channels, height, width)。
    • "channels_last"ChannelDimension.LAST: 影像格式為 (height, width, num_channels)。如果未設定,將使用輸入影像的推斷格式。

返回

np.ndarray

裁剪後的影像。

使用中心裁剪將 image 裁剪到指定的 size。請注意,如果影像太小而無法裁剪到給定尺寸,它將被填充(因此返回結果始終是 size 尺寸)。

transformers.image_transforms.center_to_corners_format

< >

( 中心邊界框: TensorType )

將邊界框從中心格式轉換為角點格式。

中心格式:包含框中心座標及其寬度、高度尺寸(center_x、center_y、width、height)角點格式:包含框左上角和右下角的座標(top_left_x、top_left_y、bottom_right_x、bottom_right_y)

transformers.image_transforms.corners_to_center_format

< >

( 角點邊界框: TensorType )

將邊界框從角點格式轉換為中心格式。

角點格式:包含框的左上角和右下角座標(top_left_x, top_left_y, bottom_right_x, bottom_right_y)中心格式:包含框中心座標及其寬度、高度尺寸(center_x, center_y, width, height)

transformers.image_transforms.id_to_rgb

< >

( id_map )

將唯一 ID 轉換為 RGB 顏色。

transformers.image_transforms.normalize

< >

( 影像: ndarray 平均值: typing.Union[float, collections.abc.Collection[float]] 標準差: typing.Union[float, collections.abc.Collection[float]] 資料格式: typing.Optional[transformers.image_utils.ChannelDimension] = None 輸入資料格式: typing.Union[transformers.image_utils.ChannelDimension, str, NoneType] = None )

引數

  • 影像 (np.ndarray) — 要標準化的影像。
  • 平均值 (floatCollection[float]) — 用於標準化的平均值。
  • 標準差 (floatCollection[float]) — 用於標準化的標準差。
  • 資料格式 (ChannelDimension, 可選) — 輸出影像的通道維度格式。如果未設定,將使用輸入推斷的格式。
  • 輸入資料格式 (ChannelDimension, 可選) — 輸入影像的通道維度格式。如果未設定,將使用輸入推斷的格式。

使用 meanstd 指定的平均值和標準差對 image 進行歸一化。

影像 = (影像 - 平均值) / 標準差

transformers.image_transforms.pad

< >

( 影像: ndarray 填充: typing.Union[int, tuple[int, int], collections.abc.Iterable[tuple[int, int]]] 模式: PaddingMode = <PaddingMode.CONSTANT: 'constant'> 常量值: typing.Union[float, collections.abc.Iterable[float]] = 0.0 資料格式: typing.Union[transformers.image_utils.ChannelDimension, str, NoneType] = None 輸入資料格式: typing.Union[transformers.image_utils.ChannelDimension, str, NoneType] = None ) np.ndarray

引數

  • 影像 (np.ndarray) — 要填充的影像。
  • 填充 (inttuple[int, int]Iterable[tuple[int, int]]) — 應用於高度、寬度軸邊緣的填充。可以是以下三種格式之一:
    • ((before_height, after_height), (before_width, after_width)) 每個軸的唯一填充寬度。
    • ((before, after),) 高度和寬度具有相同的填充寬度。
    • (pad,) 或 int 是所有軸的 before = after = pad 寬度的快捷方式。
  • 模式 (PaddingMode) — 要使用的填充模式。可以是以下之一:
    • "constant": 用常量值填充。
    • "reflect": 沿每個軸用向量在向量的第一個和最後一個值上的反射填充。
    • "replicate": 沿每個軸用陣列邊緣的最後一個值的複製填充。
    • "symmetric": 沿陣列邊緣用向量的反射填充。
  • 常量值 (floatIterable[float], 可選) — 如果 mode"constant",則用於填充的值。
  • 資料格式 (strChannelDimension, 可選) — 輸出影像的通道維度格式。可以是以下之一:
    • "channels_first"ChannelDimension.FIRST: 影像格式為 (num_channels, height, width)。
    • "channels_last"ChannelDimension.LAST: 影像格式為 (height, width, num_channels)。如果未設定,將與輸入影像的格式相同。
  • 輸入資料格式 (strChannelDimension, 可選) — 輸入影像的通道維度格式。可以是以下之一:
    • "channels_first"ChannelDimension.FIRST: 影像格式為 (num_channels, height, width)。
    • "channels_last"ChannelDimension.LAST: 影像格式為 (height, width, num_channels)。如果未設定,將使用輸入影像的推斷格式。

返回

np.ndarray

填充後的影像。

使用指定的 (height, width) paddingmodeimage 進行填充。

transformers.image_transforms.rgb_to_id

< >

( 顏色 )

將 RGB 顏色轉換為唯一 ID。

transformers.image_transforms.rescale

< >

( 影像: ndarray 比例: float 資料格式: typing.Optional[transformers.image_utils.ChannelDimension] = None dtype: dtype = <class 'numpy.float32'> 輸入資料格式: typing.Union[transformers.image_utils.ChannelDimension, str, NoneType] = None ) np.ndarray

引數

  • 影像 (np.ndarray) — 要重新縮放的影像。
  • 比例 (float) — 用於影像重新縮放的比例。
  • 資料格式 (ChannelDimension, 可選) — 影像的通道維度格式。如果未提供,將與輸入影像的格式相同。
  • 資料型別 (np.dtype, 可選, 預設為 np.float32) — 輸出影像的資料型別。預設為 np.float32。用於向後相容特徵提取器。
  • 輸入資料格式 (ChannelDimension, 可選) — 輸入影像的通道維度格式。如果未提供,將從輸入影像中推斷。

返回

np.ndarray

重新縮放的影像。

透過 scale 重新縮放 image

transformers.image_transforms.resize

< >

( 影像: ndarray 尺寸: 元組 重取樣: PILImageResampling = None 縮減間隙: typing.Optional[int] = None 資料格式: typing.Optional[transformers.image_utils.ChannelDimension] = None 返回numpy: bool = True 輸入資料格式: typing.Union[transformers.image_utils.ChannelDimension, str, NoneType] = None ) np.ndarray

引數

  • 影像 (np.ndarray) — 要調整大小的影像。
  • 尺寸 (tuple[int, int]) — 用於調整影像大小的尺寸。
  • 重取樣 (int, 可選, 預設為 PILImageResampling.BILINEAR) — 使用者重取樣的過濾器。
  • 縮減間隙 (int, 可選) — 透過兩步調整影像大小來應用最佳化。reducing_gap 越大,結果越接近公平重取樣。有關更多詳細資訊,請參閱相應的 Pillow 文件。
  • 資料格式 (ChannelDimension, 可選) — 輸出影像的通道維度格式。如果未設定,將使用輸入推斷的格式。
  • 返回numpy (bool, 可選, 預設為 True) — 是否將調整大小後的影像作為 numpy 陣列返回。如果為 False,則返回 PIL.Image.Image 物件。
  • 輸入資料格式 (ChannelDimension, 可選) — 輸入影像的通道維度格式。如果未設定,將使用輸入推斷的格式。

返回

np.ndarray

調整大小後的影像。

使用 PIL 庫將 image 調整為 size 指定的 (height, width)

transformers.image_transforms.to_pil_image

< >

( 影像: typing.Union[numpy.ndarray, ForwardRef('PIL.Image.Image'), ForwardRef('torch.Tensor'), ForwardRef('tf.Tensor'), ForwardRef('jnp.ndarray')] do_rescale: typing.Optional[bool] = None 影像模式: typing.Optional[str] = None 輸入資料格式: typing.Union[transformers.image_utils.ChannelDimension, str, NoneType] = None ) PIL.Image.Image

引數

  • 影像 (PIL.Image.Imagenumpy.ndarraytorch.Tensortf.Tensor) — 要轉換為 PIL.Image 格式的影像。
  • do_rescale (bool, 可選) — 是否應用縮放因子(使畫素值在 0 到 255 之間)。如果影像型別是浮點型別且轉換為 int 會導致精度損失,則預設為 True,否則為 False
  • 影像模式 (str, 可選) — 用於 PIL 影像的模式。如果未設定,將使用輸入影像型別的預設模式。
  • 輸入資料格式 (ChannelDimension, 可選) — 輸入影像的通道維度格式。如果未設定,將使用輸入推斷的格式。

返回

PIL.Image.Image

轉換後的影像。

image 轉換為 PIL 影像。根據需要可選地重新縮放並重新放置通道維度作為最後一個軸。

ImageProcessingMixin

class transformers.ImageProcessingMixin

< >

( **kwargs )

這是一個影像處理器 mixin,用於為順序和影像特徵提取器提供儲存/載入功能。

fetch_images

< >

( image_url_or_urls: typing.Union[str, list[str]] )

將單個或URL列表轉換為相應的 PIL.Image 物件。

如果傳入單個URL,則返回值為單個物件。如果傳入列表,則返回物件列表。

from_dict

< >

( image_processor_dict: dict **kwargs ) ImageProcessingMixin

引數

  • image_processor_dict (dict[str, Any]) — 用於例項化影像處理器物件的字典。可以透過使用 to_dict() 方法從預訓練檢查點檢索此類字典。
  • kwargs (dict[str, Any]) — 用於初始化影像處理器物件的附加引數。

返回

ImageProcessingMixin

從這些引數例項化的影像處理器物件。

從 Python 引數字典例項化 ImageProcessingMixin 型別。

from_json_file

< >

( json_file: typing.Union[str, os.PathLike] ) ImageProcessingMixin 型別的影像處理器

引數

  • json_file (stros.PathLike) — 包含引數的 JSON 檔案路徑。

返回

ImageProcessingMixin 型別的影像處理器

從該 JSON 檔案例項化的影像處理器物件。

從 JSON 引數檔案路徑例項化 ImageProcessingMixin 型別的影像處理器。

from_pretrained

< >

( pretrained_model_name_or_path: typing.Union[str, os.PathLike] cache_dir: typing.Union[str, os.PathLike, NoneType] = None force_download: bool = False local_files_only: bool = False token: typing.Union[str, bool, NoneType] = None revision: str = 'main' **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 這可以是以下任一選項:

    • 一個字串,即 huggingface.co 上模型倉庫中預訓練的 `image_processor` 的模型 ID
    • 一個目錄的路徑,該目錄包含使用 `save_pretrained()` 方法儲存的影像處理器檔案,例如 ./my_model_directory/
    • 一個儲存的影像處理器 JSON 檔案的路徑或 URL,例如 ./my_model_directory/preprocessor_config.json
  • cache_dir (stros.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型影像處理器應快取的目錄路徑。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載影像處理器檔案並覆蓋快取版本(如果存在)。
  • resume_download — 已棄用並忽略。現在所有下載在可能的情況下預設都會恢復。將在 Transformers v5 中移除。
  • proxies (dict[str, str], 可選) — 要按協議或端點使用的代理伺服器字典,例如,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理用於每個請求。
  • token (strbool, 可選) — 用於遠端檔案的 HTTP 持有者授權令牌。如果為 True 或未指定,將使用執行 huggingface-cli login 時生成的令牌(儲存在 ~/.huggingface 中)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 Git 的系統在 huggingface.co 上儲存模型和其他工件,因此 revision 可以是 Git 允許的任何識別符號。

從影像處理器例項化一個 ImageProcessingMixin 型別。

示例

# We can't instantiate directly the base class *ImageProcessingMixin* so let's show the examples on a
# derived class: *CLIPImageProcessor*
image_processor = CLIPImageProcessor.from_pretrained(
    "openai/clip-vit-base-patch32"
)  # Download image_processing_config from huggingface.co and cache.
image_processor = CLIPImageProcessor.from_pretrained(
    "./test/saved_model/"
)  # E.g. image processor (or model) was saved using *save_pretrained('./test/saved_model/')*
image_processor = CLIPImageProcessor.from_pretrained("./test/saved_model/preprocessor_config.json")
image_processor = CLIPImageProcessor.from_pretrained(
    "openai/clip-vit-base-patch32", do_normalize=False, foo=False
)
assert image_processor.do_normalize is False
image_processor, unused_kwargs = CLIPImageProcessor.from_pretrained(
    "openai/clip-vit-base-patch32", do_normalize=False, foo=False, return_unused_kwargs=True
)
assert image_processor.do_normalize is False
assert unused_kwargs == {"foo": False}

get_image_processor_dict

< >

( pretrained_model_name_or_path: typing.Union[str, os.PathLike] **kwargs ) tuple[Dict, Dict]

引數

  • pretrained_model_name_or_path (stros.PathLike) — 我們要獲取引數字典的預訓練檢查點識別符號。
  • subfolder (str, 可選, 預設為 "") — 如果相關檔案位於 huggingface.co 上模型倉庫的子資料夾中,您可以在此處指定資料夾名稱。
  • image_processor_filename (str, 可選, 預設為 "config.json") — 模型目錄中用於影像處理器配置的檔名。

返回

tuple[Dict, Dict]

將用於例項化影像處理器物件的字典。

從 `pretrained_model_name_or_path` 解析為引數字典,用於使用 `from_dict` 例項化 `~image_processor_utils.ImageProcessingMixin` 型別的影像處理器。

push_to_hub

< >

( repo_id: str use_temp_dir: typing.Optional[bool] = None commit_message: typing.Optional[str] = None private: typing.Optional[bool] = None token: typing.Union[bool, str, NoneType] = None max_shard_size: typing.Union[str, int, NoneType] = '5GB' create_pr: bool = False safe_serialization: bool = True revision: typing.Optional[str] = None commit_description: typing.Optional[str] = None tags: typing.Optional[list[str]] = None **deprecated_kwargs )

引數

  • repo_id (str) — 您要將影像處理器推送到的倉庫名稱。當推送到給定組織時,它應包含您的組織名稱。
  • use_temp_dir (bool, 可選) — 是否使用臨時目錄來儲存在推送到 Hub 之前儲存的檔案。如果沒有名為 repo_id 的目錄,則預設為 True,否則為 False
  • commit_message (str, 可選) — 推送時的提交訊息。預設為 "Upload image processor"
  • private (bool, 可選) — 是否將倉庫設為私有。如果為 None(預設),則倉庫將是公開的,除非組織的預設設定是私有。如果倉庫已存在,則此值將被忽略。
  • token (boolstr, 可選) — 用於遠端檔案的 HTTP 持有者授權令牌。如果為 True,將使用執行 huggingface-cli login 時生成的令牌(儲存在 ~/.huggingface 中)。如果未指定 repo_url,則預設為 True
  • max_shard_size (intstr, 可選, 預設為 "5GB") — 僅適用於模型。檢查點分片前的最大大小。檢查點分片的大小將低於此大小。如果表示為字串,則需要是數字後跟單位(如 "5MB")。我們預設將其設為 "5GB",以便使用者可以在免費的 Google Colab 例項上輕鬆載入模型,而不會出現任何 CPU OOM 問題。
  • create_pr (bool, 可選, 預設為 False) — 是否為上傳的檔案建立 PR 或直接提交。
  • safe_serialization (bool, 可選, 預設為 True) — 是否將模型權重轉換為 safetensors 格式以進行更安全的序列化。
  • revision (str, 可選) — 要將上傳檔案推送到的分支。
  • commit_description (str, 可選) — 將建立的提交的描述
  • tags (list[str], 可選) — 要推送到 Hub 的標籤列表。

將影像處理器檔案上傳到 🤗 模型中心。

示例

from transformers import AutoImageProcessor

image processor = AutoImageProcessor.from_pretrained("google-bert/bert-base-cased")

# Push the image processor to your namespace with the name "my-finetuned-bert".
image processor.push_to_hub("my-finetuned-bert")

# Push the image processor to an organization with the name "my-finetuned-bert".
image processor.push_to_hub("huggingface/my-finetuned-bert")

register_for_auto_class

< >

( auto_class = 'AutoImageProcessor' )

引數

  • auto_class (strtype, 可選, 預設為 "AutoImageProcessor ") — 用於註冊此新影像處理器的自動類。

將此類別註冊到給定的自動類別。這僅應用於自定義影像處理器,因為庫中的影像處理器已對映到 `AutoImageProcessor`。

save_pretrained

< >

( save_directory: typing.Union[str, os.PathLike] push_to_hub: bool = False **kwargs )

引數

  • save_directory (stros.PathLike) — 影像處理器 JSON 檔案將儲存的目錄(如果不存在將建立)。
  • push_to_hub (bool, 可選, 預設為 False) — 是否在儲存模型後將其推送到 Hugging Face 模型中心。您可以使用 repo_id 指定要推送到的倉庫(預設為您名稱空間中 save_directory 的名稱)。
  • kwargs (dict[str, Any], 可選) — 額外傳遞給 `push_to_hub()` 方法的關鍵字引數。

將影像處理器物件儲存到 save_directory 目錄,以便可以使用 `from_pretrained()` 類方法重新載入它。

to_dict

< >

( ) dict[str, Any]

返回

dict[str, Any]

構成此影像處理器例項的所有屬性的字典。

將此例項序列化為 Python 字典。

to_json_file

< >

( json_file_path: typing.Union[str, os.PathLike] )

引數

  • json_file_path (stros.PathLike) — 此 image_processor 例項的引數將儲存到的 JSON 檔案路徑。

將此例項儲存到 JSON 檔案。

to_json_string

< >

( ) str

返回

字串

包含此 feature_extractor 例項所有屬性的 JSON 格式字串。

將此例項序列化為 JSON 字串。

< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.