processing_blip.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Copyright 2022 The HuggingFace Inc. team.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """
  15. Processor class for Blip.
  16. """
  17. from ...image_utils import ImageInput
  18. from ...processing_utils import ProcessingKwargs, ProcessorMixin, Unpack
  19. from ...tokenization_utils_base import BatchEncoding, PreTokenizedInput, TextInput
  20. from ...utils import auto_docstring
  21. class BlipProcessorKwargs(ProcessingKwargs, total=False):
  22. _defaults = {
  23. "text_kwargs": {
  24. "add_special_tokens": True,
  25. "padding": False,
  26. "stride": 0,
  27. "return_overflowing_tokens": False,
  28. "return_special_tokens_mask": False,
  29. "return_offsets_mapping": False,
  30. "return_token_type_ids": False,
  31. "return_length": False,
  32. "verbose": True,
  33. },
  34. }
  35. @auto_docstring
  36. class BlipProcessor(ProcessorMixin):
  37. def __init__(self, image_processor, tokenizer, **kwargs):
  38. tokenizer.return_token_type_ids = False
  39. super().__init__(image_processor, tokenizer)
  40. @auto_docstring
  41. def __call__(
  42. self,
  43. images: ImageInput | None = None,
  44. text: str | list[str] | TextInput | PreTokenizedInput | None = None,
  45. **kwargs: Unpack[BlipProcessorKwargs],
  46. ) -> BatchEncoding:
  47. if images is None and text is None:
  48. raise ValueError("You have to specify either images or text.")
  49. text_encoding = None
  50. # add pixel_values encoding. If we also have text_encoding, update image encoding and return it.
  51. # else, return the text encoding.
  52. output_kwargs = self._merge_kwargs(
  53. BlipProcessorKwargs,
  54. tokenizer_init_kwargs=self.tokenizer.init_kwargs,
  55. **kwargs,
  56. )
  57. if text is not None:
  58. text_encoding = self.tokenizer(text, **output_kwargs["text_kwargs"])
  59. if images is not None:
  60. encoding_image_processor = self.image_processor(images, **output_kwargs["images_kwargs"])
  61. if text_encoding is not None:
  62. encoding_image_processor.update(text_encoding)
  63. return encoding_image_processor
  64. return text_encoding
  65. @property
  66. def model_input_names(self):
  67. tokenizer_input_names = self.tokenizer.model_input_names
  68. image_processor_input_names = self.image_processor.model_input_names
  69. tokenizer_input_names = [name for name in tokenizer_input_names if name != "token_type_ids"]
  70. return tokenizer_input_names + image_processor_input_names
  71. __all__ = ["BlipProcessor"]