Transformers 文档
图像处理器
并获得增强的文档体验
开始使用
图像处理器
图像处理器负责为视觉模型准备输入特征并进行后处理输出。这包括诸如调整大小、归一化以及转换为 PyTorch、TensorFlow、Flax 和 Numpy 张量之类的转换。它还可能包括特定于模型的后处理,例如将 logits 转换为分割掩码。
快速图像处理器可用于少数模型,将来会添加更多模型。它们基于 torchvision 库,并提供显着的加速,尤其是在 GPU 上处理时。它们具有与基本图像处理器相同的 API,并且可以用作直接替换。要使用快速图像处理器,您需要安装 torchvision
库,并将 use_fast
参数设置为 True
以在实例化图像处理器时启用。
from transformers import AutoImageProcessor
processor = AutoImageProcessor.from_pretrained("facebook/detr-resnet-50", use_fast=True)
请注意,在未来的版本中,use_fast
将默认设置为 True
。
当使用快速图像处理器时,您还可以设置 device
参数来指定应在其上完成处理的设备。默认情况下,如果输入是张量,则在与输入相同的设备上完成处理,否则在 CPU 上完成处理。
from torchvision.io import read_image
from transformers import DetrImageProcessorFast
images = read_image("image.jpg")
processor = DetrImageProcessorFast.from_pretrained("facebook/detr-resnet-50")
images_processed = processor(images, return_tensors="pt", device="cuda")
以下是 DETR
和 RT-DETR
模型的基本图像处理器和快速图像处理器之间的一些速度比较,以及它们如何影响整体推理时间




这些基准测试是在 AWS EC2 g5.2xlarge 实例上运行的,利用了 NVIDIA A10G Tensor Core GPU。
ImageProcessingMixin
这是一个图像处理器 mixin,用于为序列和图像特征提取器提供保存/加载功能。
from_pretrained
< source >( 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 (
str
或os.PathLike
) — 可以是以下之一:- 一个字符串,即托管在 huggingface.co 模型仓库中的预训练 image_processor 的模型 ID。
- 一个目录的路径,其中包含使用 save_pretrained() 方法保存的图像处理器文件,例如,
./my_model_directory/
。 - 已保存的图像处理器 JSON 文件的路径或 URL,例如,
./my_model_directory/preprocessor_config.json
。
- cache_dir (
str
或os.PathLike
, 可选) — 缓存下载的预训练模型图像处理器的目录路径,如果不想使用标准缓存。 - force_download (
bool
, 可选, 默认为False
) — 是否强制(重新)下载图像处理器文件并覆盖缓存的版本(如果存在)。 - resume_download — 已弃用并忽略。现在,所有下载在可能的情况下都默认恢复。将在 Transformers v5 中移除。
- proxies (
Dict[str, str]
, 可选) — 按协议或端点使用的代理服务器字典,例如,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}.
代理用于每个请求。 - token (
str
或bool
, 可选) — 用作远程文件的 HTTP Bearer 授权的令牌。如果为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}
save_pretrained
< source >( save_directory: typing.Union[str, os.PathLike] push_to_hub: bool = False **kwargs )
参数
- save_directory (
str
或os.PathLike
) — 将在其中保存图像处理器 JSON 文件的目录(如果不存在将创建)。 - push_to_hub (
bool
, 可选, 默认为False
) — 是否在保存模型后将其推送到 Hugging Face 模型中心。您可以使用repo_id
指定要推送到的仓库(默认为您命名空间中save_directory
的名称)。 - kwargs (
Dict[str, Any]
, 可选) — 传递给 push_to_hub() 方法的其他关键字参数。
将图像处理器对象保存到目录 save_directory
,以便可以使用 from_pretrained() 类方法重新加载它。
BatchFeature
class transformers.BatchFeature
< source >( data: typing.Optional[typing.Dict[str, typing.Any]] = None tensor_type: typing.Union[NoneType, str, transformers.utils.generic.TensorType] = None )
保存 pad() 和特征提取器特定 __call__
方法的输出。
此类派生自 python 字典,可以用作字典。
convert_to_tensors
< source >( tensor_type: typing.Union[str, transformers.utils.generic.TensorType, NoneType] = None )
参数
- tensor_type (
str
or TensorType, optional) — The type of tensors to use. Ifstr
, should be one of the values of the enum TensorType. IfNone
, no modification is done.
将内部内容转换为 tensors。
to
< source >( *args **kwargs ) → BatchFeature
通过调用 v.to(*args, **kwargs)
(仅限 PyTorch) 将所有值发送到设备。这应该支持在不同的 dtypes
中进行转换,并将 BatchFeature
发送到不同的 device
。
BaseImageProcessor
center_crop
< source >( image: ndarray size: dict data_format: typing.Union[str, transformers.image_utils.ChannelDimension, NoneType] = None input_data_format: typing.Union[str, transformers.image_utils.ChannelDimension, NoneType] = None **kwargs )
参数
- image (
np.ndarray
) — 要进行中心裁剪的图像。 - size (
Dict[str, int]
) — 输出图像的尺寸。 - data_format (
str
orChannelDimension
, optional) — 输出图像的通道维度格式。如果未设置,则使用输入图像的通道维度格式。可以是以下之一:"channels_first"
或ChannelDimension.FIRST
:图像格式为 (num_channels, height, width)。"channels_last"
或ChannelDimension.LAST
:图像格式为 (height, width, num_channels)。
- input_data_format (
ChannelDimension
orstr
, optional) — 输入图像的通道维度格式。如果未设置,则从输入图像推断通道维度格式。可以是以下之一:"channels_first"
或ChannelDimension.FIRST
:图像格式为 (num_channels, height, width)。"channels_last"
或ChannelDimension.LAST
:图像格式为 (height, width, num_channels)。
将图像中心裁剪为 (size["height"], size["width"])
。如果输入尺寸在任何边缘上小于 crop_size
,则图像将用 0 填充,然后进行中心裁剪。
normalize
< source >( image: ndarray mean: typing.Union[float, collections.abc.Iterable[float]] std: typing.Union[float, collections.abc.Iterable[float]] data_format: typing.Union[str, transformers.image_utils.ChannelDimension, NoneType] = None input_data_format: typing.Union[str, transformers.image_utils.ChannelDimension, NoneType] = None **kwargs ) → np.ndarray
参数
- image (
np.ndarray
) — 要标准化的图像。 - mean (
float
orIterable[float]
) — 用于标准化的图像均值。 - std (
float
orIterable[float]
) — 用于标准化的图像标准差。 - data_format (
str
orChannelDimension
, optional) — 输出图像的通道维度格式。如果未设置,则使用输入图像的通道维度格式。可以是以下之一:"channels_first"
或ChannelDimension.FIRST
:图像格式为 (num_channels, height, width)。"channels_last"
或ChannelDimension.LAST
:图像格式为 (height, width, num_channels)。
- input_data_format (
ChannelDimension
orstr
, optional) — 输入图像的通道维度格式。如果未设置,则从输入图像推断通道维度格式。可以是以下之一:"channels_first"
或ChannelDimension.FIRST
:图像格式为 (num_channels, height, width)。"channels_last"
或ChannelDimension.LAST
:图像格式为 (height, width, num_channels)。
返回值
np.ndarray
标准化的图像。
标准化图像。 image = (image - image_mean) / image_std.
rescale
< source >( image: ndarray scale: float data_format: typing.Union[str, transformers.image_utils.ChannelDimension, NoneType] = None input_data_format: typing.Union[str, transformers.image_utils.ChannelDimension, NoneType] = None **kwargs ) → np.ndarray
参数
- image (
np.ndarray
) — 要重新缩放的图像。 - scale (
float
) — 用于重新缩放像素值的缩放因子。 - data_format (
str
orChannelDimension
, optional) — 输出图像的通道维度格式。如果未设置,则使用输入图像的通道维度格式。可以是以下之一:"channels_first"
或ChannelDimension.FIRST
:图像格式为 (num_channels, height, width)。"channels_last"
或ChannelDimension.LAST
:图像格式为 (height, width, num_channels)。
- input_data_format (
ChannelDimension
orstr
, optional) — 输入图像的通道维度格式。如果未设置,则从输入图像推断通道维度格式。可以是以下之一:"channels_first"
或ChannelDimension.FIRST
:图像格式为 (num_channels, height, width)。"channels_last"
或ChannelDimension.LAST
:图像格式为 (height, width, num_channels)。
返回值
np.ndarray
重新缩放的图像。
通过缩放因子重新缩放图像。 image = image * scale.
BaseImageProcessorFast
class transformers.BaseImageProcessorFast
< source >( **kwargs: typing_extensions.Unpack[transformers.image_processing_utils_fast.DefaultFastImageProcessorKwargs] )
参数
- do_resize (
bool
, optional, defaults toself.do_resize
) — 是否将图像的(高度,宽度)尺寸调整为指定的size
。 可以被preprocess
方法中的do_resize
参数覆盖。 - size (
dict
, optional, defaults toself.size
) — 调整大小后输出图像的尺寸。可以被preprocess
方法中的size
参数覆盖。 - default_to_square (
bool
, optional, defaults toself.default_to_square
) — 当调整大小时,如果 size 是整数,是否默认输出为方形图像。 - resample (
PILImageResampling
, optional, defaults toself.resample
) — 如果调整图像大小,要使用的重采样滤波器。仅当do_resize
设置为True
时才有效。可以被preprocess
方法中的resample
参数覆盖。 - do_center_crop (
bool
, optional, defaults toself.do_center_crop
) — 是否将图像中心裁剪为指定的crop_size
。可以被preprocess
方法中的do_center_crop
覆盖。 - crop_size (
Dict[str, int]
optional, defaults toself.crop_size
) — 应用center_crop
后输出图像的尺寸。可以被preprocess
方法中的crop_size
覆盖。 - do_rescale (
bool
, optional, defaults toself.do_rescale
) — 是否按照指定的比例rescale_factor
缩放图像。可以被preprocess
方法中的do_rescale
参数覆盖。 - rescale_factor (
int
orfloat
, optional, defaults toself.rescale_factor
) — 如果缩放图像,要使用的缩放因子。仅当do_rescale
设置为True
时才有效。可以被preprocess
方法中的rescale_factor
参数覆盖。 - do_normalize (
bool
, optional, defaults toself.do_normalize
) — 是否对图像进行归一化。可以被preprocess
方法中的do_normalize
参数覆盖。 可以被preprocess
方法中的do_normalize
参数覆盖。 - image_mean (
float
orList[float]
, optional, defaults toself.image_mean
) — 如果对图像进行归一化,则使用的均值。这是一个浮点数或浮点数列表,其长度是图像中通道的数量。可以被preprocess
方法中的image_mean
参数覆盖。 可以被preprocess
方法中的image_mean
参数覆盖。 - image_std (
float
orList[float]
, optional, defaults toself.image_std
) — 如果对图像进行归一化,则使用的标准差。这是一个浮点数或浮点数列表,其长度是图像中通道的数量。可以被preprocess
方法中的image_std
参数覆盖。 可以被preprocess
方法中的image_std
参数覆盖。 - do_convert_rgb (
bool
, optional, defaults toself.do_convert_rgb
) — 是否将图像转换为 RGB 格式。 - return_tensors (
str
orTensorType
, optional, defaults toself.return_tensors
) — 如果设置为 `pt`,则返回堆叠的张量,否则返回张量列表。 - data_format (
ChannelDimension
orstr
, optional, defaults toself.data_format
) — 仅支持ChannelDimension.FIRST
。为了与慢速处理器兼容而添加。 - input_data_format (
ChannelDimension
orstr
, optional, defaults toself.input_data_format
) — 输入图像的通道维度格式。如果未设置,则通道维度格式从输入图像推断。可以是以下之一:"channels_first"
或ChannelDimension.FIRST
:图像格式为 (num_channels, height, width)。"channels_last"
或ChannelDimension.LAST
:图像格式为 (height, width, num_channels)。"none"
或ChannelDimension.NONE
:图像格式为 (height, width)。
- device (
torch.device
, optional, defaults toself.device
) — 处理图像的设备。如果未设置,则设备从输入图像推断。
构建一个快速的基础图像处理器。
center_crop
< source >( image: torch.Tensor size: dict **kwargs ) → torch.Tensor
将图像中心裁剪为 (size["height"], size["width"])
。如果输入尺寸在任何边缘上小于 crop_size
,则图像将用 0 填充,然后进行中心裁剪。
convert_to_rgb
< source >( image: typing.Union[ForwardRef('PIL.Image.Image'), numpy.ndarray, ForwardRef('torch.Tensor'), list['PIL.Image.Image'], list[numpy.ndarray], list['torch.Tensor']] ) → ImageInput
将图像转换为 RGB 格式。仅当图像类型为 PIL.Image.Image 时才进行转换,否则按原样返回图像。
从 kwargs 字典中过滤掉未使用的 kwargs。
normalize
< source >( image: torch.Tensor mean: typing.Union[float, collections.abc.Iterable[float]] std: typing.Union[float, collections.abc.Iterable[float]] **kwargs ) → torch.Tensor
标准化图像。 image = (image - image_mean) / image_std.
preprocess
< source >( images: typing.Union[ForwardRef('PIL.Image.Image'), numpy.ndarray, ForwardRef('torch.Tensor'), list['PIL.Image.Image'], list[numpy.ndarray], list['torch.Tensor']] **kwargs: typing_extensions.Unpack[transformers.image_processing_utils_fast.DefaultFastImageProcessorKwargs] )
参数
- images (
ImageInput
) — 要预处理的图像。 期望是像素值范围为 0 到 255 的单张或批量图像。 如果传入的图像像素值介于 0 和 1 之间,请设置do_rescale=False
。 - do_resize (
bool
, 可选, 默认为self.do_resize
) — 是否调整图像大小。 - size (
Dict[str, int]
, 可选, 默认为self.size
) — 描述模型的最大输入尺寸。 - resample (
PILImageResampling
或InterpolationMode
, 可选, 默认为self.resample
) — 如果调整图像大小,则使用的重采样滤波器。可以是枚举类型PILImageResampling
之一。 仅当do_resize
设置为True
时才生效。 - do_center_crop (
bool
, 可选, 默认为self.do_center_crop
) — 是否对图像进行中心裁剪。 - crop_size (
Dict[str, int]
, 可选, 默认为self.crop_size
) — 应用center_crop
后输出图像的大小。 - do_rescale (
bool
, 可选, 默认为self.do_rescale
) — 是否重新缩放图像。 - rescale_factor (
float
, 可选, 默认为self.rescale_factor
) — 如果do_rescale
设置为True
,则用于重新缩放图像的缩放因子。 - do_normalize (
bool
, 可选, 默认为self.do_normalize
) — 是否标准化图像。 - image_mean (
float
或List[float]
, 可选, 默认为self.image_mean
) — 用于归一化的图像均值。 仅当do_normalize
设置为True
时才生效。 - image_std (
float
或List[float]
, 可选, 默认为self.image_std
) — 用于归一化的图像标准差。 仅当do_normalize
设置为True
时才生效。 - do_convert_rgb (
bool
, 可选, 默认为self.do_convert_rgb
) — 是否将图像转换为 RGB 格式。 - return_tensors (
str
或TensorType
, 可选, 默认为self.return_tensors
) — 如果设置为 `pt`,则返回堆叠的张量,否则返回张量列表。 - data_format (
ChannelDimension
或str
, 可选, 默认为self.data_format
) — 仅支持ChannelDimension.FIRST
。 添加此项是为了与慢速处理器兼容。 - input_data_format (
ChannelDimension
或str
, 可选, 默认为self.input_data_format
) — 输入图像的通道维度格式。 如果未设置,则通道维度格式从输入图像推断。 可以是以下之一:"channels_first"
或ChannelDimension.FIRST
:(num_channels, height, width) 格式的图像。"channels_last"
或ChannelDimension.LAST
:(height, width, num_channels) 格式的图像。"none"
或ChannelDimension.NONE
:(height, width) 格式的图像。
- device (
torch.device
, 可选, 默认为self.device
) — 在其上处理图像的设备。 如果未设置,则设备从输入图像推断。
预处理单张或批量图像。
rescale
< source >( image: torch.Tensor scale: float **kwargs ) → torch.Tensor
通过缩放因子重新缩放图像。 image = image * scale.
rescale_and_normalize
< source >( images: torch.Tensor do_rescale: bool rescale_factor: float do_normalize: bool image_mean: typing.Union[float, list[float]] image_std: typing.Union[float, list[float]] )
重新缩放并标准化图像。
resize
< source >( image: torch.Tensor size: SizeDict interpolation: F.InterpolationMode = None antialias: bool = True **kwargs ) → torch.Tensor
将图像大小调整为 (size["height"], size["width"])
。