You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_builder.py 2.9 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import os
  3. import unittest
  4. from typing import Any, Dict, List, Union
  5. from modelscope.fileio import io
  6. from modelscope.models.base import Model
  7. from modelscope.pipelines import Pipeline, pipeline
  8. from modelscope.pipelines.builder import PIPELINES
  9. from modelscope.utils.constant import (ConfigFields, Frameworks, ModelFile,
  10. Tasks)
  11. from modelscope.utils.logger import get_logger
  12. logger = get_logger()
  13. @PIPELINES.register_module(
  14. group_key=Tasks.image_classification, module_name='custom_single_model')
  15. class CustomSingleModelPipeline(Pipeline):
  16. def __init__(self,
  17. config_file: str = None,
  18. model: List[Union[str, Model]] = None,
  19. preprocessor=None,
  20. **kwargs):
  21. super().__init__(config_file, model, preprocessor, **kwargs)
  22. assert isinstance(model, str), 'model is not str'
  23. print(model)
  24. def postprocess(self, inputs: Dict[str, Any]) -> Dict[str, Any]:
  25. return super().postprocess(inputs)
  26. @PIPELINES.register_module(
  27. group_key=Tasks.image_classification, module_name='model1_model2')
  28. class CustomMultiModelPipeline(Pipeline):
  29. def __init__(self,
  30. config_file: str = None,
  31. model: List[Union[str, Model]] = None,
  32. preprocessor=None,
  33. **kwargs):
  34. super().__init__(config_file, model, preprocessor, **kwargs)
  35. assert isinstance(model, list), 'model is not list'
  36. for m in model:
  37. assert isinstance(m, str), 'submodel is not str'
  38. print(m)
  39. def postprocess(self, inputs: Dict[str, Any]) -> Dict[str, Any]:
  40. return super().postprocess(inputs)
  41. class PipelineInterfaceTest(unittest.TestCase):
  42. def prepare_dir(self, dirname, pipeline_name):
  43. if not os.path.exists(dirname):
  44. os.makedirs(dirname)
  45. cfg_file = os.path.join(dirname, ModelFile.CONFIGURATION)
  46. cfg = {
  47. ConfigFields.framework: Frameworks.torch,
  48. ConfigFields.task: Tasks.image_classification,
  49. ConfigFields.pipeline: {
  50. 'type': pipeline_name,
  51. }
  52. }
  53. io.dump(cfg, cfg_file)
  54. def setUp(self) -> None:
  55. self.prepare_dir('/tmp/custom_single_model', 'custom_single_model')
  56. self.prepare_dir('/tmp/model1', 'model1_model2')
  57. self.prepare_dir('/tmp/model2', 'model1_model2')
  58. def test_single_model(self):
  59. pipe = pipeline(
  60. Tasks.image_classification, model='/tmp/custom_single_model')
  61. assert isinstance(pipe, CustomSingleModelPipeline)
  62. def test_multi_model(self):
  63. pipe = pipeline(
  64. Tasks.image_classification, model=['/tmp/model1', '/tmp/model2'])
  65. assert isinstance(pipe, CustomMultiModelPipeline)
  66. if __name__ == '__main__':
  67. unittest.main()