Image Processor

Transformers documentation

Natural Language Processing

Performance and scalability

Reinforcement learning models

An image processor is in charge of preparing input features for vision models and post processing their outputs. This includes transformations such as resizing, normalization, and conversion to PyTorch, TensorFlow, Flax and Numpy tensors. It may also include model specific post-processing such as converting logits to segmentation masks.

## [](#transformers.ImageProcessingMixin)ImageProcessingMixin

### class transformers.ImageProcessingMixin

[](#transformers.ImageProcessingMixin)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/image_processing_utils.py#L59)

( \*\*kwargs )

This is an image processor mixin used to provide saving/loading functionality for sequential and image feature extractors.

#### from\_pretrained

[](#transformers.ImageProcessingMixin.from_pretrained)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/image_processing_utils.py#L83)

( pretrained\_model\_name\_or\_path: typing.Union\[str, os.PathLike\]\*\*kwargs )

Instantiate a type of [ImageProcessingMixin](/docs/transformers/v4.30.0/en/internal/image_processing_utils#transformers.ImageProcessingMixin) from an image processor.

Examples:

```

image_processor = CLIPImageProcessor.from_pretrained(
    "openai/clip-vit-base-patch32"
)  
image_processor = CLIPImageProcessor.from_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

[](#transformers.ImageProcessingMixin.save_pretrained)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/image_processing_utils.py#L169)

( save\_directory: typing.Union\[str, os.PathLike\]push\_to\_hub: bool = False\*\*kwargs )

Parameters

-   [](#transformers.ImageProcessingMixin.save_pretrained.save_directory)**save\_directory** (`str` or `os.PathLike`) — Directory where the image processor JSON file will be saved (will be created if it does not exist).
-   [](#transformers.ImageProcessingMixin.save_pretrained.push_to_hub)**push\_to\_hub** (`bool`, _optional_, defaults to `False`) — Whether or not to push your model to the Hugging Face model hub after saving it. You can specify the repository you want to push to with `repo_id` (will default to the name of `save_directory` in your namespace). kwargs — Additional key word arguments passed along to the [push\_to\_hub()](/docs/transformers/v4.30.0/en/internal/image_processing_utils#transformers.ImageProcessingMixin.push_to_hub) method.

Save an image processor object to the directory `save_directory`, so that it can be re-loaded using the [from\_pretrained()](/docs/transformers/v4.30.0/en/internal/image_processing_utils#transformers.ImageProcessingMixin.from_pretrained) class method.

## [](#transformers.BatchFeature)BatchFeature

### class transformers.BatchFeature

[](#transformers.BatchFeature)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/feature_extraction_utils.py#L60)

( data: typing.Union\[typing.Dict\[str, typing.Any\], NoneType\] = Nonetensor\_type: typing.Union\[NoneType, str, transformers.utils.generic.TensorType\] = None )

Parameters

-   [](#transformers.BatchFeature.data)**data** (`dict`) — Dictionary of lists/arrays/tensors returned by the **call**/pad methods (‘input\_values’, ‘attention\_mask’, etc.).
-   [](#transformers.BatchFeature.tensor_type)**tensor\_type** (`Union[None, str, TensorType]`, _optional_) — You can give a tensor\_type here to convert the lists of integers in PyTorch/TensorFlow/Numpy Tensors at initialization.

Holds the output of the [pad()](/docs/transformers/v4.30.0/en/main_classes/feature_extractor#transformers.SequenceFeatureExtractor.pad) and feature extractor specific `__call__` methods.

This class is derived from a python dictionary and can be used as a dictionary.

#### convert\_to\_tensors

[](#transformers.BatchFeature.convert_to_tensors)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/feature_extraction_utils.py#L114)

( tensor\_type: typing.Union\[str, transformers.utils.generic.TensorType, NoneType\] = None )

Parameters

-   [](#transformers.BatchFeature.convert_to_tensors.tensor_type)**tensor\_type** (`str` or [TensorType](/docs/transformers/v4.30.0/en/internal/file_utils#transformers.TensorType), _optional_) — The type of tensors to use. If `str`, should be one of the values of the enum [TensorType](/docs/transformers/v4.30.0/en/internal/file_utils#transformers.TensorType). If `None`, no modification is done.

Convert the inner content to tensors.

#### to

[](#transformers.BatchFeature.to)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/feature_extraction_utils.py#L187)

( \*args\*\*kwargs ) → [BatchFeature](/docs/transformers/v4.30.0/en/main_classes/image_processor#transformers.BatchFeature)

Parameters

-   [](#transformers.BatchFeature.to.args)**args** (`Tuple`) — Will be passed to the `to(...)` function of the tensors.
-   [](#transformers.BatchFeature.to.kwargs)**kwargs** (`Dict`, _optional_) — Will be passed to the `to(...)` function of the tensors.

The same instance after modification.

Send all values to device by calling `v.to(*args, **kwargs)` (PyTorch only). This should support casting in different `dtypes` and sending the `BatchFeature` to a different `device`.

## [](#transformers.image_processing_utils.BaseImageProcessor)BaseImageProcessor

### class transformers.image\_processing\_utils.BaseImageProcessor

[](#transformers.image_processing_utils.BaseImageProcessor)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/image_processing_utils.py#L458)

( \*\*kwargs )