single_model_pipeline.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  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 .base import BasePipeline
  15. class _SingleModelPipeline(BasePipeline):
  16. def __init__(self, model, batch_size=1, device=None, predictor_kwargs=None):
  17. super().__init__(device, predictor_kwargs)
  18. self._build_predictor(model)
  19. self.set_predictor(batch_size=batch_size, device=device)
  20. def _build_predictor(self, model):
  21. self.model = self._create(model)
  22. def set_predictor(self, batch_size=None, device=None):
  23. if batch_size:
  24. self.model.set_predictor(batch_size=batch_size)
  25. if device:
  26. self.model.set_predictor(device=device)
  27. def predict(self, input, **kwargs):
  28. self.set_predictor(**kwargs)
  29. yield from self.model(input)
  30. class ImageClassification(_SingleModelPipeline):
  31. entities = "image_classification"
  32. class ObjectDetection(_SingleModelPipeline):
  33. entities = "object_detection"
  34. class InstanceSegmentation(_SingleModelPipeline):
  35. entities = "instance_segmentation"
  36. class SemanticSegmentation(_SingleModelPipeline):
  37. entities = "semantic_segmentation"
  38. class TSFc(_SingleModelPipeline):
  39. entities = "ts_fc"
  40. class TSAd(_SingleModelPipeline):
  41. entities = "ts_ad"
  42. class TSCls(_SingleModelPipeline):
  43. entities = "ts_cls"
  44. class MultiLableImageClas(_SingleModelPipeline):
  45. entities = "multi_label_image_classification"
  46. class SmallObjDet(_SingleModelPipeline):
  47. entities = "small_object_detection"
  48. class AnomalyDetection(_SingleModelPipeline):
  49. entities = "anomaly_detection"