processing_clip.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # coding=utf-8
  2. # Copyright 2021 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. Image/Text processor class for CLIP
  17. """
  18. import warnings
  19. from ...processing_utils import ProcessorMixin
  20. class CLIPProcessor(ProcessorMixin):
  21. r"""
  22. Constructs a CLIP processor which wraps a CLIP image processor and a CLIP tokenizer into a single processor.
  23. [`CLIPProcessor`] offers all the functionalities of [`CLIPImageProcessor`] and [`CLIPTokenizerFast`]. See the
  24. [`~CLIPProcessor.__call__`] and [`~CLIPProcessor.decode`] for more information.
  25. Args:
  26. image_processor ([`CLIPImageProcessor`], *optional*):
  27. The image processor is a required input.
  28. tokenizer ([`AutoTokenizer`], *optional*):
  29. The tokenizer is a required input.
  30. """
  31. attributes = ["image_processor", "tokenizer"]
  32. image_processor_class = ("CLIPImageProcessor", "CLIPImageProcessorFast")
  33. tokenizer_class = "AutoTokenizer"
  34. def __init__(self, image_processor=None, tokenizer=None, **kwargs):
  35. feature_extractor = None
  36. if "feature_extractor" in kwargs:
  37. warnings.warn(
  38. "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
  39. " instead.",
  40. FutureWarning,
  41. )
  42. feature_extractor = kwargs.pop("feature_extractor")
  43. image_processor = image_processor if image_processor is not None else feature_extractor
  44. super().__init__(image_processor, tokenizer)
  45. @property
  46. def feature_extractor_class(self):
  47. warnings.warn(
  48. "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.",
  49. FutureWarning,
  50. )
  51. return self.image_processor_class
  52. @property
  53. def feature_extractor(self):
  54. warnings.warn(
  55. "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.",
  56. FutureWarning,
  57. )
  58. return self.image_processor
  59. __all__ = ["CLIPProcessor"]