-
Notifications
You must be signed in to change notification settings - Fork 3k
Add Image Text Retrieval taskflow&pipelines API #4516
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
e29e4a5
Add vision language taskflow API
w5688414 b3c7d0a
Merge branch 'develop' of https://github.com/PaddlePaddle/PaddleNLP i…
w5688414 88ea9fb
Update image text retrieval taskflow api
w5688414 2970900
Add multimodal_retriever of pipelines
w5688414 6840d71
Add image text retrieval pipelines application
w5688414 3bcb212
Change image text retrieval to feature extraction
w5688414 dd4c83b
Add feature extraction docs
w5688414 f1c08b9
Add onnx support
w5688414 5840835
Fix some bugs and remove unused comments
w5688414 90d0a66
Merge branch 'develop' of https://github.com/PaddlePaddle/PaddleNLP i…
w5688414 932cd02
fix some errors and adjust onnx ouput config
w5688414 70cf0d8
Update docs
w5688414 858a686
Add taskflow loading finetune model
w5688414 2651dae
Rename mode to export_type
w5688414 f8fcbae
Remove clip english models
w5688414 d6c5638
Add unit test for feature extraction taskflow
w5688414 0cd4588
set delta to 1e-5
w5688414 75d72dc
change delta to 1e-5
w5688414 d07f8c4
change delta to 1e-5
w5688414 1b2e89d
Change to is_static_model
w5688414 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 hidden or 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 |
---|---|---|
|
@@ -42,6 +42,7 @@ | |
TextToImageGenerationTask, | ||
TextToImageStableDiffusionTask, | ||
) | ||
from .vision_language_embedding import VisionLanguageTask | ||
from .word_segmentation import SegJiebaTask, SegLACTask, SegWordTagTask | ||
from .zero_shot_text_classification import ZeroShotTextClassificationTask | ||
|
||
|
@@ -486,6 +487,15 @@ | |
}, | ||
"default": {"model": "utc-large"}, | ||
}, | ||
"vision_language": { | ||
"models": { | ||
"PaddlePaddle/ernie_vil-2.0-base-zh": { | ||
"task_class": VisionLanguageTask, | ||
"task_flag": "vision_language_embeddings-2.0-base-zh", | ||
}, | ||
}, | ||
"default": {"model": "PaddlePaddle/ernie_vil-2.0-base-zh"}, | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 感觉除了ernie_vil应该也可以接一下clip和chineseclip吧 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已经添加 |
||
} | ||
|
||
support_schema_list = [ | ||
|
This file contains hidden or 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,116 @@ | ||
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import paddle | ||
from PIL import Image | ||
|
||
from ..transformers import ErnieViLModel, ErnieViLProcessor | ||
from .task import Task | ||
|
||
|
||
class VisionLanguageTask(Task): | ||
""" | ||
The text_to_image generation model to generate the image. | ||
Args: | ||
task(string): The name of task. | ||
model(string): The model name in the task. | ||
kwargs (dict, optional): Additional keyword arguments passed along to the specific task. | ||
""" | ||
|
||
def __init__(self, task, model, **kwargs): | ||
super().__init__(task=task, model=model, **kwargs) | ||
self._seed = None | ||
# we do not use batch | ||
self._batch_size = 1 | ||
self._construct_tokenizer(image_model=model, text_model="ernie_vil-2.0-base-zh") | ||
self._construct_model(model) | ||
|
||
def _construct_model(self, model): | ||
""" | ||
Construct the inference model for the predictor. | ||
""" | ||
self._model = ErnieViLModel.from_pretrained(model) | ||
self._model.eval() | ||
|
||
def _construct_tokenizer(self, image_model, text_model): | ||
""" | ||
Construct the tokenizer for the predictor. | ||
""" | ||
self._processor = ErnieViLProcessor.from_pretrained(image_model) | ||
|
||
def _batchify(self, data, batch_size): | ||
""" | ||
Generate input batches. | ||
""" | ||
|
||
def _parse_batch(batch_examples): | ||
batch_texts = batch_examples["texts"] | ||
batch_images = [Image.open(item) for item in batch_examples["images"]] | ||
|
||
tokenizerd_inputs = self._processor( | ||
text=batch_texts, images=batch_images, return_tensors="pd", padding="max_length", truncation=True | ||
) | ||
|
||
return tokenizerd_inputs | ||
|
||
# Seperates data into some batches. | ||
# breakpoint() | ||
yield _parse_batch(data[0]) | ||
# one_batch = [] | ||
# for example in data: | ||
# one_batch.append(example) | ||
# if len(one_batch) == batch_size: | ||
# yield _parse_batch(one_batch) | ||
# one_batch = [] | ||
# if one_batch: | ||
# yield _parse_batch(one_batch) | ||
|
||
def _preprocess(self, inputs): | ||
""" | ||
Transform the raw text to the model inputs, two steps involved: | ||
1) Transform the raw text to token ids. | ||
2) Generate the other model inputs from the raw text and token ids. | ||
""" | ||
# inputs = self._check_input_text(inputs) | ||
batches = self._batchify(inputs, self._batch_size) | ||
outputs = {"batches": batches, "text": inputs} | ||
return outputs | ||
|
||
def _run_model(self, inputs): | ||
""" | ||
Run the task model from the outputs of the `_preprocess` function. | ||
""" | ||
all_texts = [] | ||
all_images = [] | ||
for batch_inputs in inputs["batches"]: | ||
if len(batch_inputs["input_ids"]) > 0: | ||
text_features = self._model.get_text_features(input_ids=batch_inputs["input_ids"]) | ||
all_texts.append(text_features) | ||
if len(batch_inputs["pixel_values"]) > 0: | ||
image_features = self._model.get_image_features(pixel_values=batch_inputs["pixel_values"]) | ||
all_images.append(image_features) | ||
inputs.update({"text_features": all_texts}) | ||
inputs.update({"image_features": all_images}) | ||
return inputs | ||
|
||
def _postprocess(self, inputs): | ||
return inputs | ||
|
||
def _construct_input_spec(self): | ||
""" | ||
Construct the input spec for the convert dygraph model to static model. | ||
""" | ||
self._input_spec = [ | ||
paddle.static.InputSpec(shape=[None, None], dtype="int64", name="input_ids"), | ||
] |
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.
ERNIE系列模型这里是否可以简化为
ernie_vil-2.0-base-zh
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.
已跟余军沟通,ernie_vil 2.0上传的时候当成社区模型了