121 lines
4.7 KiB
Python
121 lines
4.7 KiB
Python
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
|
|
# Copyright 2023 The Salesforce Team Authors and The HuggingFace Team. 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.
|
|
|
|
"""
|
|
Processor class for BLIP-2.
|
|
"""
|
|
|
|
from typing import List, Optional, Union
|
|
|
|
from ..processing_utils import ProcessorMixin
|
|
from ..tokenizer_utils_base import (
|
|
BatchEncoding,
|
|
PreTokenizedInput,
|
|
TensorType,
|
|
TextInput,
|
|
)
|
|
|
|
__all__ = [
|
|
"Blip2Processor",
|
|
]
|
|
|
|
|
|
class Blip2Processor(ProcessorMixin):
|
|
r"""
|
|
Constructs a BLIP-2 processor which wraps a BLIP image processor and an OPT/T5 tokenizer into a single processor.
|
|
[`BlipProcessor`] offers all the functionalities of [`BlipImageProcessor`] and [`AutoTokenizer`]. See the docstring
|
|
of [`~BlipProcessor.__call__`] and [`~BlipProcessor.decode`] for more information.
|
|
Args:
|
|
image_processor (`BlipImageProcessor`):
|
|
An instance of [`BlipImageProcessor`]. The image processor is a required input.
|
|
tokenizer (`AutoTokenizer`):
|
|
An instance of ['PreTrainedTokenizer`]. The tokenizer is a required input.
|
|
"""
|
|
attributes = ["image_processor", "tokenizer"]
|
|
image_processor_class = "BlipImageProcessor"
|
|
tokenizer_class = "AutoTokenizer"
|
|
|
|
# Copied from paddlenlp.transformers.blip.processing.BlipProcessor.__init__
|
|
def __init__(self, image_processor, tokenizer):
|
|
tokenizer.return_token_type_ids = False
|
|
super().__init__(image_processor, tokenizer)
|
|
self.current_processor = self.image_processor
|
|
|
|
# Copied from paddlenlp.transformers.blip.processing.BlipProcessor.__call__
|
|
def __call__(
|
|
self,
|
|
images=None,
|
|
text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None,
|
|
return_tensors: Optional[Union[str, TensorType]] = None,
|
|
**kwargs,
|
|
) -> BatchEncoding:
|
|
"""
|
|
This method uses [`BlipImageProcessor.__call__`] method to prepare image(s) for the model, and
|
|
[`BertTokenizerFast.__call__`] to prepare text for the model.
|
|
Please refer to the docstring of the above two methods for more information.
|
|
"""
|
|
if images is None and text is None:
|
|
raise ValueError("You have to specify either images or text.")
|
|
|
|
# Get only text
|
|
if images is None:
|
|
self.current_processor = self.tokenizer
|
|
text_encoding = self.tokenizer(
|
|
text=text,
|
|
return_tensors=return_tensors,
|
|
**kwargs,
|
|
)
|
|
return text_encoding
|
|
|
|
# add pixel_values
|
|
encoding_image_processor = self.image_processor(images, return_tensors=return_tensors)
|
|
|
|
if text is not None:
|
|
text_encoding = self.tokenizer(
|
|
text=text,
|
|
return_tensors=return_tensors,
|
|
**kwargs,
|
|
)
|
|
else:
|
|
text_encoding = None
|
|
|
|
if text_encoding is not None:
|
|
encoding_image_processor.update(text_encoding)
|
|
|
|
return encoding_image_processor
|
|
|
|
# Copied from paddlenlp.transformers.blip.processing.BlipProcessor.batch_decode with BertTokenizerFast->PreTrainedTokenizer
|
|
def batch_decode(self, *args, **kwargs):
|
|
"""
|
|
This method forwards all its arguments to PreTrainedTokenizer's [`~PreTrainedTokenizer.batch_decode`]. Please
|
|
refer to the docstring of this method for more information.
|
|
"""
|
|
return self.tokenizer.batch_decode(*args, **kwargs)
|
|
|
|
# Copied from paddlenlp.transformers.blip.processing.BlipProcessor.decode with BertTokenizerFast->PreTrainedTokenizer
|
|
def decode(self, *args, **kwargs):
|
|
"""
|
|
This method forwards all its arguments to PreTrainedTokenizer's [`~PreTrainedTokenizer.decode`]. Please refer
|
|
to the docstring of this method for more information.
|
|
"""
|
|
return self.tokenizer.decode(*args, **kwargs)
|
|
|
|
@property
|
|
# Copied from paddlenlp.transformers.blip.processing.BlipProcessor.model_input_names
|
|
def model_input_names(self):
|
|
tokenizer_input_names = self.tokenizer.model_input_names
|
|
image_processor_input_names = self.image_processor.model_input_names
|
|
return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names))
|