processing_bridgetower.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 Optional
  19. from ...processing_utils import ImagesKwargs, ProcessingKwargs, ProcessorMixin
  20. class BridgeTowerImagesKwargs(ImagesKwargs):
  21. size_divisor: Optional[int]
  22. class BridgeTowerProcessorKwargs(ProcessingKwargs, total=False):
  23. images_kwargs: BridgeTowerImagesKwargs
  24. _defaults = {
  25. "text_kwargs": {
  26. "add_special_tokens": True,
  27. "padding": False,
  28. "stride": 0,
  29. "return_overflowing_tokens": False,
  30. "return_special_tokens_mask": False,
  31. "return_offsets_mapping": False,
  32. "return_length": False,
  33. "verbose": True,
  34. },
  35. "images_kwargs": {
  36. "do_normalize": True,
  37. "do_center_crop": True,
  38. },
  39. }
  40. class BridgeTowerProcessor(ProcessorMixin):
  41. r"""
  42. Constructs a BridgeTower processor which wraps a Roberta tokenizer and BridgeTower image processor into a single
  43. processor.
  44. [`BridgeTowerProcessor`] offers all the functionalities of [`BridgeTowerImageProcessor`] and
  45. [`RobertaTokenizerFast`]. See the docstring of [`~BridgeTowerProcessor.__call__`] and
  46. [`~BridgeTowerProcessor.decode`] for more information.
  47. Args:
  48. image_processor (`BridgeTowerImageProcessor`):
  49. An instance of [`BridgeTowerImageProcessor`]. The image processor is a required input.
  50. tokenizer (`RobertaTokenizerFast`):
  51. An instance of ['RobertaTokenizerFast`]. The tokenizer is a required input.
  52. """
  53. attributes = ["image_processor", "tokenizer"]
  54. image_processor_class = "BridgeTowerImageProcessor"
  55. tokenizer_class = ("RobertaTokenizer", "RobertaTokenizerFast")
  56. valid_processor_kwargs = BridgeTowerProcessorKwargs
  57. def __init__(self, image_processor, tokenizer):
  58. super().__init__(image_processor, tokenizer)
  59. __all__ = ["BridgeTowerProcessor"]