predictor.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Union
  15. from ....modules.video_classification.model_list import MODELS
  16. from ....utils.func_register import FuncRegister
  17. from ...common.batch_sampler import VideoBatchSampler
  18. from ...common.reader import ReadVideo
  19. from ..base import BasePredictor
  20. from .processors import (
  21. CenterCrop,
  22. Image2Array,
  23. NormalizeVideo,
  24. Scale,
  25. ToBatch,
  26. VideoClasTopk,
  27. )
  28. from .result import TopkVideoResult
  29. class VideoClasPredictor(BasePredictor):
  30. entities = MODELS
  31. _FUNC_MAP = {}
  32. register = FuncRegister(_FUNC_MAP)
  33. def __init__(self, topk: Union[int, None] = None, *args, **kwargs):
  34. super().__init__(*args, **kwargs)
  35. self.topk = topk
  36. self.pre_tfs, self.infer, self.post_op = self._build()
  37. def _build_batch_sampler(self):
  38. return VideoBatchSampler()
  39. def _get_result_class(self):
  40. return TopkVideoResult
  41. def _build(self):
  42. pre_tfs = {}
  43. for cfg in self.config["PreProcess"]["transform_ops"]:
  44. tf_key = list(cfg.keys())[0]
  45. assert tf_key in self._FUNC_MAP
  46. func = self._FUNC_MAP[tf_key]
  47. args = cfg.get(tf_key, {})
  48. name, op = func(self, **args) if args else func(self)
  49. if op:
  50. pre_tfs[name] = op
  51. pre_tfs["ToBatch"] = ToBatch()
  52. infer = self.create_static_infer()
  53. post_op = {}
  54. for key in self.config["PostProcess"]:
  55. func = self._FUNC_MAP.get(key)
  56. args = self.config["PostProcess"].get(key, {})
  57. name, op = func(self, **args) if args else func(self)
  58. post_op[name] = op
  59. return pre_tfs, infer, post_op
  60. def process(self, batch_data, topk: Union[int, None] = None):
  61. batch_raw_videos = self.pre_tfs["ReadVideo"](videos=batch_data)
  62. batch_videos = self.pre_tfs["Scale"](videos=batch_raw_videos)
  63. batch_videos = self.pre_tfs["CenterCrop"](videos=batch_videos)
  64. batch_videos = self.pre_tfs["Image2Array"](videos=batch_videos)
  65. batch_videos = self.pre_tfs["NormalizeVideo"](videos=batch_videos)
  66. x = self.pre_tfs["ToBatch"](videos=batch_videos)
  67. batch_preds = self.infer(x=x)
  68. batch_class_ids, batch_scores, batch_label_names = self.post_op["Topk"](
  69. batch_preds, topk=topk or self.topk
  70. )
  71. return {
  72. "input_path": batch_data,
  73. "class_ids": batch_class_ids,
  74. "scores": batch_scores,
  75. "label_names": batch_label_names,
  76. }
  77. @register("ReadVideo")
  78. def build_readvideo(
  79. self,
  80. num_seg=8,
  81. target_size=224,
  82. seg_len=1,
  83. sample_type=None,
  84. ):
  85. return "ReadVideo", ReadVideo(
  86. backend="decord",
  87. num_seg=num_seg,
  88. seg_len=seg_len,
  89. sample_type=sample_type,
  90. )
  91. @register("Scale")
  92. def build_scale(self, short_size=224):
  93. return "Scale", Scale(
  94. short_size=short_size,
  95. fixed_ratio=True,
  96. keep_ratio=None,
  97. do_round=False,
  98. )
  99. @register("CenterCrop")
  100. def build_center_crop(self, target_size=224):
  101. return "CenterCrop", CenterCrop(target_size=target_size)
  102. @register("Image2Array")
  103. def build_image2array(self, data_format="tchw"):
  104. return "Image2Array", Image2Array(transpose=True, data_format="tchw")
  105. @register("NormalizeVideo")
  106. def build_normalize(
  107. self,
  108. mean=[0.485, 0.456, 0.406],
  109. std=[0.229, 0.224, 0.225],
  110. ):
  111. return "NormalizeVideo", NormalizeVideo(mean=mean, std=std)
  112. @register("Topk")
  113. def build_topk(self, topk, label_list=None):
  114. if not self.topk:
  115. self.topk = int(topk)
  116. return "Topk", VideoClasTopk(class_ids=label_list)
  117. @register("KeepKeys")
  118. def foo(self, *args, **kwargs):
  119. return None, None