processing_bridgetower.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # coding=utf-8
  2. # Copyright 2023 The Intel Labs Team Authors, The Microsoft Research Team Authors and HuggingFace Inc. team. All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """
  16. Processor class for BridgeTower.
  17. """
  18. from typing import List, Union
  19. from ...processing_utils import ProcessingKwargs, ProcessorMixin, Unpack
  20. from ...tokenization_utils_base import BatchEncoding, PreTokenizedInput, TextInput
  21. class BridgeTowerProcessorKwargs(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_length": False,
  31. "verbose": True,
  32. },
  33. "images_kwargs": {
  34. "do_normalize": True,
  35. "do_center_crop": True,
  36. },
  37. }
  38. class BridgeTowerProcessor(ProcessorMixin):
  39. r"""
  40. Constructs a BridgeTower processor which wraps a Roberta tokenizer and BridgeTower image processor into a single
  41. processor.
  42. [`BridgeTowerProcessor`] offers all the functionalities of [`BridgeTowerImageProcessor`] and
  43. [`RobertaTokenizerFast`]. See the docstring of [`~BridgeTowerProcessor.__call__`] and
  44. [`~BridgeTowerProcessor.decode`] for more information.
  45. Args:
  46. image_processor (`BridgeTowerImageProcessor`):
  47. An instance of [`BridgeTowerImageProcessor`]. The image processor is a required input.
  48. tokenizer (`RobertaTokenizerFast`):
  49. An instance of ['RobertaTokenizerFast`]. The tokenizer is a required input.
  50. """
  51. attributes = ["image_processor", "tokenizer"]
  52. image_processor_class = "BridgeTowerImageProcessor"
  53. tokenizer_class = ("RobertaTokenizer", "RobertaTokenizerFast")
  54. def __init__(self, image_processor, tokenizer):
  55. super().__init__(image_processor, tokenizer)
  56. def __call__(
  57. self,
  58. images,
  59. text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None,
  60. audio=None,
  61. videos=None,
  62. **kwargs: Unpack[BridgeTowerProcessorKwargs],
  63. ) -> BatchEncoding:
  64. """
  65. This method uses [`BridgeTowerImageProcessor.__call__`] method to prepare image(s) for the model, and
  66. [`RobertaTokenizerFast.__call__`] to prepare text for the model.
  67. Please refer to the docstring of the above two methods for more information.
  68. """
  69. output_kwargs = self._merge_kwargs(
  70. BridgeTowerProcessorKwargs,
  71. tokenizer_init_kwargs=self.tokenizer.init_kwargs,
  72. **kwargs,
  73. )
  74. encoding = self.tokenizer(text=text, **output_kwargs["text_kwargs"])
  75. # add pixel_values + pixel_mask
  76. encoding_image_processor = self.image_processor(images, **output_kwargs["images_kwargs"])
  77. encoding.update(encoding_image_processor)
  78. return encoding
  79. def batch_decode(self, *args, **kwargs):
  80. """
  81. This method forwards all its arguments to RobertaTokenizerFast's [`~PreTrainedTokenizer.batch_decode`]. Please
  82. refer to the docstring of this method for more information.
  83. """
  84. return self.tokenizer.batch_decode(*args, **kwargs)
  85. def decode(self, *args, **kwargs):
  86. """
  87. This method forwards all its arguments to RobertaTokenizerFast's [`~PreTrainedTokenizer.decode`]. Please refer
  88. to the docstring of this method for more information.
  89. """
  90. return self.tokenizer.decode(*args, **kwargs)
  91. @property
  92. def model_input_names(self):
  93. tokenizer_input_names = self.tokenizer.model_input_names
  94. image_processor_input_names = self.image_processor.model_input_names
  95. return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names))