processing_tvp.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # coding=utf-8
  2. # Copyright 2023 The Intel AIA 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 TVP.
  17. """
  18. from ...processing_utils import ProcessingKwargs, ProcessorMixin
  19. class TvpProcessorKwargs(ProcessingKwargs, total=False):
  20. _defaults = {
  21. "text_kwargs": {
  22. "truncation": True,
  23. "padding": "max_length",
  24. "pad_to_max_length": True,
  25. "return_token_type_ids": False,
  26. },
  27. }
  28. class TvpProcessor(ProcessorMixin):
  29. r"""
  30. Constructs an TVP processor which wraps a TVP image processor and a Bert tokenizer into a single processor.
  31. [`TvpProcessor`] offers all the functionalities of [`TvpImageProcessor`] and [`BertTokenizerFast`]. See the
  32. [`~TvpProcessor.__call__`] and [`~TvpProcessor.decode`] for more information.
  33. Args:
  34. image_processor ([`TvpImageProcessor`], *optional*):
  35. The image processor is a required input.
  36. tokenizer ([`BertTokenizerFast`], *optional*):
  37. The tokenizer is a required input.
  38. """
  39. attributes = ["image_processor", "tokenizer"]
  40. image_processor_class = "TvpImageProcessor"
  41. tokenizer_class = ("BertTokenizer", "BertTokenizerFast")
  42. def __init__(self, image_processor=None, tokenizer=None, **kwargs):
  43. super().__init__(image_processor, tokenizer)
  44. self.video_processor = image_processor
  45. def post_process_video_grounding(self, logits, video_durations):
  46. """
  47. Compute the time of the video.
  48. Args:
  49. logits (`torch.Tensor`):
  50. The logits output of TvpForVideoGrounding.
  51. video_durations (`float`):
  52. The video's duration.
  53. Returns:
  54. start (`float`):
  55. The start time of the video.
  56. end (`float`):
  57. The end time of the video.
  58. """
  59. start, end = (
  60. round(logits.tolist()[0][0] * video_durations, 1),
  61. round(logits.tolist()[0][1] * video_durations, 1),
  62. )
  63. return start, end
  64. __all__ = ["TvpProcessor"]