-
Notifications
You must be signed in to change notification settings - Fork 27.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Image Feature Extraction pipeline #28216
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
12a4da1
Draft pipeline
amyeroberts ec40e7d
Fixup
amyeroberts 26467fc
Fix docstrings
amyeroberts 1d0a6dd
Update doctest
amyeroberts 81303dc
Update pipeline_model_mapping
amyeroberts ba549d5
Update docstring
amyeroberts 1468c54
Update tests
amyeroberts 102422b
Update src/transformers/pipelines/image_feature_extraction.py
amyeroberts 466ce4e
Fix docstrings - review comments
amyeroberts 1923eb1
Remove pipeline mapping for composite vision models
amyeroberts 9e598c9
Add to pipeline tests
amyeroberts f990de9
Remove for flava (multimodal)
amyeroberts 999e1e1
safe pil import
amyeroberts 3f85b37
Add requirements for pipeline run
amyeroberts 960b74a
Account for super slow efficientnet
amyeroberts e4098cb
Review comments
amyeroberts 50c0f63
Fix tests
amyeroberts 42853ce
Swap order of kwargs
amyeroberts 4b059f0
Use build_pipeline_init_args
amyeroberts 2c5c2fb
Add back FE pipeline for Vilt
amyeroberts 95f9c58
Include image_processor_kwargs in docstring
amyeroberts 94945ec
Mark test as flaky
amyeroberts 0a839a0
Update TODO
amyeroberts 3fd0797
Update tests/pipelines/test_pipelines_image_feature_extraction.py
amyeroberts 04c109c
Add license header
amyeroberts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
from typing import Dict | ||
|
||
from ..utils import add_end_docstrings, is_vision_available | ||
from .base import GenericTensor, Pipeline, build_pipeline_init_args | ||
|
||
|
||
if is_vision_available(): | ||
from ..image_utils import load_image | ||
|
||
|
||
@add_end_docstrings( | ||
build_pipeline_init_args(has_image_processor=True), | ||
""" | ||
image_processor_kwargs (`dict`, *optional*): | ||
Additional dictionary of keyword arguments passed along to the image processor e.g. | ||
{"size": {"height": 100, "width": 100}} | ||
""", | ||
) | ||
class ImageFeatureExtractionPipeline(Pipeline): | ||
""" | ||
Image feature extraction pipeline uses no model head. This pipeline extracts the hidden states from the base | ||
transformer, which can be used as features in downstream tasks. | ||
|
||
Example: | ||
|
||
```python | ||
>>> from transformers import pipeline | ||
|
||
>>> extractor = pipeline(model="google/vit-base-patch16-224", task="image-feature-extraction") | ||
>>> result = extractor("https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png", return_tensors=True) | ||
>>> result.shape # This is a tensor of shape [1, sequence_lenth, hidden_dimension] representing the input image. | ||
torch.Size([1, 197, 768]) | ||
``` | ||
|
||
Learn more about the basics of using a pipeline in the [pipeline tutorial](../pipeline_tutorial) | ||
|
||
This image feature extraction pipeline can currently be loaded from [`pipeline`] using the task identifier: | ||
`"image-feature-extraction"`. | ||
|
||
All vision models may be used for this pipeline. See a list of all models, including community-contributed models on | ||
[huggingface.co/models](https://huggingface.co/models). | ||
""" | ||
|
||
def _sanitize_parameters(self, image_processor_kwargs=None, return_tensors=None, **kwargs): | ||
preprocess_params = {} if image_processor_kwargs is None else image_processor_kwargs | ||
postprocess_params = {"return_tensors": return_tensors} if return_tensors is not None else {} | ||
|
||
if "timeout" in kwargs: | ||
preprocess_params["timeout"] = kwargs["timeout"] | ||
|
||
return preprocess_params, {}, postprocess_params | ||
|
||
def preprocess(self, image, timeout=None, **image_processor_kwargs) -> Dict[str, GenericTensor]: | ||
image = load_image(image, timeout=timeout) | ||
model_inputs = self.image_processor(image, return_tensors=self.framework, **image_processor_kwargs) | ||
return model_inputs | ||
|
||
def _forward(self, model_inputs): | ||
model_outputs = self.model(**model_inputs) | ||
return model_outputs | ||
|
||
def postprocess(self, model_outputs, return_tensors=False): | ||
# [0] is the first available tensor, logits or last_hidden_state. | ||
if return_tensors: | ||
return model_outputs[0] | ||
if self.framework == "pt": | ||
return model_outputs[0].tolist() | ||
elif self.framework == "tf": | ||
return model_outputs[0].numpy().tolist() | ||
|
||
def __call__(self, *args, **kwargs): | ||
""" | ||
Extract the features of the input(s). | ||
|
||
Args: | ||
images (`str`, `List[str]`, `PIL.Image` or `List[PIL.Image]`): | ||
The pipeline handles three types of images: | ||
|
||
- A string containing a http link pointing to an image | ||
- A string containing a local path to an image | ||
- An image loaded in PIL directly | ||
|
||
The pipeline accepts either a single image or a batch of images, which must then be passed as a string. | ||
Images in a batch must all be in the same format: all as http links, all as local paths, or all as PIL | ||
images. | ||
timeout (`float`, *optional*, defaults to None): | ||
The maximum time in seconds to wait for fetching images from the web. If None, no timeout is used and | ||
the call may block forever. | ||
Return: | ||
A nested list of `float`: The features computed by the model. | ||
""" | ||
return super().__call__(*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need a licence
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add one 👍 The reason there isn't one here is that there isn't one for all of the
pipelines/xxx.py
files.