processing_flava.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # coding=utf-8
  2. # Copyright 2022 Meta Platforms authors and The HuggingFace 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. Image/Text processor class for FLAVA
  17. """
  18. import warnings
  19. from collections.abc import Iterable
  20. from typing import Optional, Union
  21. from ...processing_utils import ImagesKwargs, ProcessingKwargs, ProcessorMixin
  22. class FlavaImagesKwargs(ImagesKwargs):
  23. # Mask related params
  24. return_image_mask: Optional[bool]
  25. input_size_patches: Optional[int]
  26. total_mask_patches: Optional[int]
  27. mask_group_min_patches: Optional[int]
  28. mask_group_max_patches: Optional[int]
  29. mask_group_min_aspect_ratio: Optional[float]
  30. mask_group_max_aspect_ratio: Optional[float]
  31. # Codebook related params
  32. return_codebook_pixels: Optional[bool]
  33. codebook_do_resize: Optional[bool]
  34. codebook_size: Optional[bool]
  35. codebook_resample: Optional[int]
  36. codebook_do_center_crop: Optional[bool]
  37. codebook_crop_size: Optional[int]
  38. codebook_do_rescale: Optional[bool]
  39. codebook_rescale_factor: Optional[Union[int, float]]
  40. codebook_do_map_pixels: Optional[bool]
  41. codebook_do_normalize: Optional[bool]
  42. codebook_image_mean: Optional[Union[float, Iterable[float]]]
  43. codebook_image_std: Optional[Union[float, Iterable[float]]]
  44. class FlavaProcessorKwargs(ProcessingKwargs, total=False):
  45. images_kwargs: FlavaImagesKwargs
  46. _defaults = {}
  47. class FlavaProcessor(ProcessorMixin):
  48. r"""
  49. Constructs a FLAVA processor which wraps a FLAVA image processor and a FLAVA tokenizer into a single processor.
  50. [`FlavaProcessor`] offers all the functionalities of [`FlavaImageProcessor`] and [`BertTokenizerFast`]. See the
  51. [`~FlavaProcessor.__call__`] and [`~FlavaProcessor.decode`] for more information.
  52. Args:
  53. image_processor ([`FlavaImageProcessor`], *optional*): The image processor is a required input.
  54. tokenizer ([`BertTokenizerFast`], *optional*): The tokenizer is a required input.
  55. """
  56. attributes = ["image_processor", "tokenizer"]
  57. image_processor_class = "FlavaImageProcessor"
  58. tokenizer_class = ("BertTokenizer", "BertTokenizerFast")
  59. valid_processor_kwargs = FlavaProcessorKwargs
  60. def __init__(self, image_processor=None, tokenizer=None, **kwargs):
  61. feature_extractor = None
  62. if "feature_extractor" in kwargs:
  63. warnings.warn(
  64. "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
  65. " instead.",
  66. FutureWarning,
  67. )
  68. feature_extractor = kwargs.pop("feature_extractor")
  69. image_processor = image_processor if image_processor is not None else feature_extractor
  70. super().__init__(image_processor, tokenizer)
  71. self.current_processor = self.image_processor
  72. @property
  73. def feature_extractor_class(self):
  74. warnings.warn(
  75. "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.",
  76. FutureWarning,
  77. )
  78. return self.image_processor_class
  79. @property
  80. def feature_extractor(self):
  81. warnings.warn(
  82. "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.",
  83. FutureWarning,
  84. )
  85. return self.image_processor
  86. __all__ = ["FlavaProcessor"]