processing_vilt.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # coding=utf-8
  2. # Copyright 2022 The HuggingFace Inc. team.
  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 ViLT.
  17. """
  18. import warnings
  19. from typing import Optional
  20. from ...processing_utils import ImagesKwargs, ProcessingKwargs, ProcessorMixin
  21. class ViltImagesKwargs(ImagesKwargs):
  22. size_divisor: Optional[int]
  23. class ViltProcessorKwargs(ProcessingKwargs, total=False):
  24. images_kwargs: ViltImagesKwargs
  25. _defaults = {
  26. "text_kwargs": {
  27. "add_special_tokens": True,
  28. "padding": False,
  29. "stride": 0,
  30. "return_overflowing_tokens": False,
  31. "return_special_tokens_mask": False,
  32. "return_offsets_mapping": False,
  33. "return_length": False,
  34. "verbose": True,
  35. },
  36. }
  37. class ViltProcessor(ProcessorMixin):
  38. r"""
  39. Constructs a ViLT processor which wraps a BERT tokenizer and ViLT image processor into a single processor.
  40. [`ViltProcessor`] offers all the functionalities of [`ViltImageProcessor`] and [`BertTokenizerFast`]. See the
  41. docstring of [`~ViltProcessor.__call__`] and [`~ViltProcessor.decode`] for more information.
  42. Args:
  43. image_processor (`ViltImageProcessor`, *optional*):
  44. An instance of [`ViltImageProcessor`]. The image processor is a required input.
  45. tokenizer (`BertTokenizerFast`, *optional*):
  46. An instance of ['BertTokenizerFast`]. The tokenizer is a required input.
  47. """
  48. attributes = ["image_processor", "tokenizer"]
  49. image_processor_class = "ViltImageProcessor"
  50. tokenizer_class = ("BertTokenizer", "BertTokenizerFast")
  51. valid_processor_kwargs = ViltProcessorKwargs
  52. def __init__(self, image_processor=None, tokenizer=None, **kwargs):
  53. feature_extractor = None
  54. if "feature_extractor" in kwargs:
  55. warnings.warn(
  56. "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
  57. " instead.",
  58. FutureWarning,
  59. )
  60. feature_extractor = kwargs.pop("feature_extractor")
  61. image_processor = image_processor if image_processor is not None else feature_extractor
  62. super().__init__(image_processor, tokenizer)
  63. self.current_processor = self.image_processor
  64. @property
  65. def feature_extractor_class(self):
  66. warnings.warn(
  67. "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.",
  68. FutureWarning,
  69. )
  70. return self.image_processor_class
  71. @property
  72. def feature_extractor(self):
  73. warnings.warn(
  74. "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.",
  75. FutureWarning,
  76. )
  77. return self.image_processor
  78. __all__ = ["ViltProcessor"]