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.

pipeline.md 3.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Pipeline使用教程
  2. 本文将简单介绍如何使用`pipeline`函数加载模型进行推理。`pipeline`函数支持按照任务类型、模型名称从模型仓库
  3. 拉取模型进行进行推理,当前支持的任务有
  4. * 人像抠图 (image-matting)
  5. * 基于bert的语义情感分析 (bert-sentiment-analysis)
  6. 本文将从如下方面进行讲解如何使用Pipeline模块:
  7. * 使用pipeline()函数进行推理
  8. * 指定特定预处理、特定模型进行推理
  9. * 不同场景推理任务示例
  10. ## Pipeline基本用法
  11. 1. pipeline函数支持指定特定任务名称,加载任务默认模型,创建对应Pipeline对象
  12. 注: 当前还未与modelhub进行打通,需要手动下载模型,创建pipeline时需要指定本地模型路径,未来会支持指定模型名称从远端仓库
  13. 拉取模型并初始化。
  14. 下载模型文件
  15. ```shell
  16. wget http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs.com/data/test/maas/image_matting/matting_person.pb
  17. ```
  18. 执行python命令
  19. ```python
  20. >>> from maas_lib.pipelines import pipeline
  21. >>> img_matting = pipeline(task='image-matting', model_path='matting_person.pb')
  22. ```
  23. 2. 传入单张图像url进行处理
  24. ``` python
  25. >>> import cv2
  26. >>> result = img_matting('http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs.com/data/test/maas/image_matting/test.png')
  27. >>> cv2.imwrite('result.png', result['output_png'])
  28. ```
  29. pipeline对象也支持传入一个列表输入,返回对应输出列表,每个元素对应输入样本的返回结果
  30. ```python
  31. results = img_matting(
  32. [
  33. 'http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs.com/data/test/maas/image_matting/test.png',
  34. 'http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs.com/data/test/maas/image_matting/test.png',
  35. 'http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs.com/data/test/maas/image_matting/test.png',
  36. ])
  37. ```
  38. 如果pipeline对应有一些后处理参数,也支持通过调用时候传入.
  39. ```python
  40. pipe = pipeline(task_name)
  41. result = pipe(input, post_process_args)
  42. ```
  43. ## 指定预处理、模型进行推理
  44. pipeline函数支持传入实例化的预处理对象、模型对象,从而支持用户在推理过程中定制化预处理、模型。
  45. 下面以文本情感分类为例进行介绍。
  46. 注: 当前release版本还未实现AutoModel的语法糖,需要手动实例化模型,后续会加上对应语法糖简化调用
  47. 下载模型文件
  48. ```shell
  49. wget https://atp-modelzoo-sh.oss-cn-shanghai.aliyuncs.com/release/easynlp_modelzoo/alibaba-pai/bert-base-sst2.zip && unzip bert-base-sst2.zip
  50. ```
  51. 创建tokenizer和模型
  52. ```python
  53. >>> from maas_lib.models.nlp import SequenceClassificationModel
  54. >>> path = 'bert-base-sst2'
  55. >>> model = SequenceClassificationModel(path)
  56. >>> from maas_lib.preprocessors import SequenceClassificationPreprocessor
  57. >>> tokenizer = SequenceClassificationPreprocessor(
  58. path, first_sequence='sentence', second_sequence=None)
  59. ```
  60. 使用tokenizer和模型对象创建pipeline
  61. ```python
  62. >>> from maas_lib.pipelines import pipeline
  63. >>> semantic_cls = pipeline('text-classification', model=model, preprocessor=tokenizer)
  64. >>> semantic_cls("Hello world!")
  65. ```
  66. ## 不同场景任务推理示例
  67. 人像抠图、语义分类建上述两个例子。 其他例子未来添加。

致力于通过开放的社区合作,开源AI模型以及相关创新技术,推动基于模型即服务的生态繁荣发展