概述
为了使ModelScope的用户能够快速、方便的使用平台提供的各类模型,提供了一套功能完备的Python library,其中包含了ModelScope官方模型的实现,以及使用这些模型进行推理,finetune等任务所需的数据预处理,后处理,效果评估等功能相关的代码,同时也提供了简单易用的API,以及丰富的使用样例。通过调用library,用户可以只写短短的几行代码,就可以完成模型的推理、训练和评估等任务,也可以在此基础上快速进行二次开发,实现自己的创新想法。
目前library提供的算法模型,涵盖了图像,自然语言处理,语音,多模态,科学5个主要的AI领域,数十个应用场景任务,具体任务可参考文档:任务的介绍。
深度学习框架
ModelScope Library当前支持的深度学习框架包括Pytorch和Tensorflow,后续将持续更新拓展,敬请期待! 当前的官方模型均支持使用ModelScope Library进行模型推理,部分支持使用该库进行训练和评估,具体可参看相应模型的模型卡片,了解完整使用信息。
模型推理Pipeline
模型的推理
推理在深度学习中表示模型的预测过程。ModelScope的推理会使用pipeline来执行所需要的操作。一个完整的pipeline一般包括了数据的前处理、模型的前向推理、数据的后处理三个过程。
Pipeline介绍
pipeline()方法是ModelScope框架上最基础的用户方法之一,可对多领域的多种模型进行快速推理。通过pipeline()方法,用户可以只需要一行代码即可完成对特定任务的模型推理。
pipeline()方法是ModelScope框架上最基础的用户方法之一,可对多领域的多种模型进行快速推理。通过pipeline()方法,用户可以只需要一行代码即可完成对特定任务的模型推理。
Pipeline的使用
本文简单介绍如何使用pipeline方法加载模型进行推理。pipeline方法支持按照任务类型、模型名称从模型仓库拉取模型进行进行推理,包含以下几个方面:
- 环境准备
- 重要参数
- Pipeline基本用法
- 指定预处理、模型进行推理
- 不同场景任务推理pipeline使用示例
Pipeline基本用法
中文分词
pipeline函数支持指定特定任务名称,加载任务默认模型,创建对应pipeline对象。
Python代码
from modelscope.pipelines import pipeline
word_segmentation = pipeline('word-segmentation')
input_str = '开源技术小栈作者是Tinywan,你知道不?'
print(word_segmentation(input_str))
PHP 代码
<?php
$operator = PyCore::import("operator");
$builtins = PyCore::import("builtins");
$pipeline = PyCore::import('modelscope.pipelines')->pipeline;
$word_segmentation = $pipeline("word-segmentation");
$input_str = "开源技术小栈作者是Tinywan,你知道不?";
PyCore::print($word_segmentation($input_str));
在线转换工具:https://www.swoole.com/py2php/
输出结果
/usr/local/php-8.2.14/bin/php demo.php
2024-03-25 21:41:42,434 - modelscope - INFO - PyTorch version 2.2.1 Found.
2024-03-25 21:41:42,434 - modelscope - INFO - Loading ast index from /home/www/.cache/modelscope/ast_indexer
2024-03-25 21:41:42,577 - modelscope - INFO - Loading done! Current index file version is 1.13.0, with md5 f54e9d2dceb89a6c989540d66db83a65 and a total number of 972 components indexed
2024-03-25 21:41:44,661 - modelscope - WARNING - Model revision not specified, use revision: v1.0.3
2024-03-25 21:41:44,879 - modelscope - INFO - initiate model from /home/www/.cache/modelscope/hub/damo/nlp_structbert_word-segmentation_chinese-base
2024-03-25 21:41:44,879 - modelscope - INFO - initiate model from location /home/www/.cache/modelscope/hub/damo/nlp_structbert_word-segmentation_chinese-base.
2024-03-25 21:41:44,880 - modelscope - INFO - initialize model from /home/www/.cache/modelscope/hub/damo/nlp_structbert_word-segmentation_chinese-base
You are using a model of type bert to instantiate a model of type structbert. This is not supported for all configurations of models and can yield errors.
2024-03-25 21:41:48,633 - modelscope - WARNING - No preprocessor field found in cfg.
2024-03-25 21:41:48,633 - modelscope - WARNING - No val key and type key found in preprocessor domain of configuration.json file.
2024-03-25 21:41:48,633 - modelscope - WARNING - Cannot find available config to build preprocessor at mode inference, current config: {'model_dir': '/home/www/.cache/modelscope/hub/damo/nlp_structbert_word-segmentation_chinese-base'}. trying to build by task and model information.
2024-03-25 21:41:48,639 - modelscope - INFO - cuda is not available, using cpu instead.
2024-03-25 21:41:48,640 - modelscope - WARNING - No preprocessor field found in cfg.
2024-03-25 21:41:48,640 - modelscope - WARNING - No val key and type key found in preprocessor domain of configuration.json file.
2024-03-25 21:41:48,640 - modelscope - WARNING - Cannot find available config to build preprocessor at mode inference, current config: {'model_dir': '/home/www/.cache/modelscope/hub/damo/nlp_structbert_word-segmentation_chinese-base', 'sequence_length': 512}. trying to build by task and model information.
/home/www/anaconda3/envs/tinywan-modelscope/lib/python3.10/site-packages/transformers/modeling_utils.py:962: FutureWarning: The `device` argument is deprecated and will be removed in v5 of Transformers.
warnings.warn(
{'output': ['开源', '技术', '小', '栈', '作者', '是', 'Tinywan', ',', '你', '知道', '不', '?']}
输入多条样本
pipeline对象也支持传入多个样本列表输入,返回对应输出列表,每个元素对应输入样本的返回结果。多条文本的推理方式是输入data在pipeline内部用迭代器单条处理后append到同一个返回List中。
Python代码
from modelscope.pipelines import pipeline
word_segmentation = pipeline('word-segmentation')
inputs = ['开源技术小栈作者是Tinywan,你知道不?','webman这个框架不错,建议你看看']
print(word_segmentation(inputs))
PHP 代码
<?php
$operator = PyCore::import("operator");
$builtins = PyCore::import("builtins");
$pipeline = PyCore::import('modelscope.pipelines')->pipeline;
$word_segmentation = $pipeline("word-segmentation");
$inputs = new PyList(["开源技术小栈作者是Tinywan,你知道不?", "webman这个框架不错,建议你看看"]);
PyCore::print($word_segmentation($inputs));
输出
[{'output': ['开源', '技术', '小', '栈', '作者', '是', 'Tinywan', ',', '你', '知道', '不', '?']},
{'output': ['webman', '这个', '框架', '不错', ',', '建议', '你', '看看']}]
批量推理
pipeline对于批量推理的支持类似于上面的“输入多条文本”,区别在于会在用户指定的batch_size尺度上,在模型forward过程实现批量前向推理。
inputs = ['今天天气不错,适合出去游玩','这本书很好,建议你看看']
# 指定batch_size参数来支持批量推理
print(word_segmentation(inputs, batch_size=2))
# 输出
[{'output': ['今天', '天气', '不错', ',', '适合', '出去', '游玩']}, {'output': ['这', '本', '书', '很', '好', ',', '建议', '你', '看看']}]
输入一个数据集
from modelscope.msdatasets import MsDataset
from modelscope.pipelines import pipeline
inputs = ['今天天气不错,适合出去游玩', '这本书很好,建议你看看']
dataset = MsDataset.load(inputs, target='sentence')
word_segmentation = pipeline('word-segmentation')
outputs = word_segmentation(dataset)
for o in outputs:
print(o)
# 输出
{'output': ['今天', '天气', '不错', ',', '适合', '出去', '游玩']}
{'output': ['这', '本', '书', '很', '好', ',', '建议', '你', '看看']}
指定预处理、模型进行推理
pipeline函数支持传入实例化的预处理对象、模型对象,从而支持用户在推理过程中定制化预处理、模型。
创建模型对象进行推理
Python代码
from modelscope.models import Model
from modelscope.pipelines import pipeline
model = Model.from_pretrained('damo/nlp_structbert_word-segmentation_chinese-base')
word_segmentation = pipeline('word-segmentation', model=model)
inputs = ['开源技术小栈作者是Tinywan,你知道不?','webman这个框架不错,建议你看看']
print(word_segmentation(inputs))
PHP 代码
<?php
$operator = PyCore::import("operator");
$builtins = PyCore::import("builtins");
$Model = PyCore::import('modelscope.models')->Model;
$pipeline = PyCore::import('modelscope.pipelines')->pipeline;
$model = $Model->from_pretrained("damo/nlp_structbert_word-segmentation_chinese-base");
$word_segmentation = $pipeline("word-segmentation", model: $model);
$inputs = new PyList(["开源技术小栈作者是Tinywan,你知道不?", "webman这个框架不错,建议你看看"]);
PyCore::print($word_segmentation($inputs));
输出
[{'output': ['开源', '技术', '小', '栈', '作者', '是', 'Tinywan', ',', '你', '知道', '不', '?']},
{'output': ['webman', '这个', '框架', '不错', ',', '建议', '你', '看看']}]
创建预处理器和模型对象进行推理
from modelscope.models import Model
from modelscope.pipelines import pipeline
from modelscope.preprocessors import Preprocessor, TokenClassificationTransformersPreprocessor
model = Model.from_pretrained('damo/nlp_structbert_word-segmentation_chinese-base')
tokenizer = Preprocessor.from_pretrained(model.model_dir)
# Or call the constructor directly:
# tokenizer = TokenClassificationTransformersPreprocessor(model.model_dir)
word_segmentation = pipeline('word-segmentation', model=model, preprocessor=tokenizer)
inputs = ['开源技术小栈作者是Tinywan,你知道不?','webman这个框架不错,建议你看看']
print(word_segmentation(inputs))
[{'output': ['开源', '技术', '小', '栈', '作者', '是', 'Tinywan', ',', '你', '知道', '不', '?']},
{'output': ['webman', '这个', '框架', '不错', ',', '建议', '你', '看看']}]
图像
注意:
- 确保你已经安装了OpenCV库。如果没有安装,你可以通过pip安装
pip install opencv-python
没有安装会提示:PHP Fatal error: Uncaught PyError: No module named 'cv2' in /home/www/build/ai/demo3.php:4
- 确保你已经安装深度学习框架包TensorFlow库
否则提示modelscope.pipelines.cv.image_matting_pipeline requires the TensorFlow library but it was not found in your environment. Checkout the instructions on the installation page: https://www.tensorflow.org/install and follow the ones that match your environment.。
报错信息表明,你正在尝试使用一个名为 modelscope.pipelines.cv.image_matting_pipeline 的模块,该模块依赖于 TensorFlow 库。然而,该模块无法正常工作,因为缺少必要的 TensorFlow 依赖。
可以使用以下命令安装最新版本的 TensorFlow
pip install tensorflow
图片
人像抠图('portrait-matting')
输入图片
图片
Python 代码
import cv2
from modelscope.pipelines import pipeline
portrait_matting = pipeline('portrait-matting')
result = portrait_matting('https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/image_matting.png')
cv2.imwrite('result.png', result['output_img'])
PHP 代码 tinywan-images.php
<?php
$operator = PyCore::import("operator");
$builtins = PyCore::import("builtins");
$cv2 = PyCore::import('cv2');
$pipeline = PyCore::import('modelscope.pipelines')->pipeline;
$portrait_matting = $pipeline("portrait-matting");
$result = $portrait_matting("https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/image_matting.png");
$cv2->imwrite("tinywan_result.png", $result->__getitem__("output_img"));
加载本地文件图片$result = $portrait_matting("./tinywan.png");
执行结果
/usr/local/php-8.2.14/bin/php tinywan-images.php
2024-03-25 22:17:25,630 - modelscope - INFO - PyTorch version 2.2.1 Found.
2024-03-25 22:17:25,631 - modelscope - INFO - TensorFlow version 2.16.1 Found.
2024-03-25 22:17:25,631 - modelscope - INFO - Loading ast index from /home/www/.cache/modelscope/ast_indexer
2024-03-25 22:17:25,668 - modelscope - INFO - Loading done! Current index file version is 1.13.0, with md5 f54e9d2dceb89a6c989540d66db83a65 and a total number of 972 components indexed
2024-03-25 22:17:26,990 - modelscope - WARNING - Model revision not specified, use revision: v1.0.0
2024-03-25 22:17:27.623085: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-03-25 22:17:27.678592: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-03-25 22:17:28.551510: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
2024-03-25 22:17:29,206 - modelscope - INFO - initiate model from /home/www/.cache/modelscope/hub/damo/cv_unet_image-matting
2024-03-25 22:17:29,206 - modelscope - INFO - initiate model from location /home/www/.cache/modelscope/hub/damo/cv_unet_image-matting.
2024-03-25 22:17:29,209 - modelscope - WARNING - No preprocessor field found in cfg.
2024-03-25 22:17:29,210 - modelscope - WARNING - No val key and type key found in preprocessor domain of configuration.json file.
2024-03-25 22:17:29,210 - modelscope - WARNING - Cannot find available config to build preprocessor at mode inference, current config: {'model_dir': '/home/www/.cache/modelscope/hub/damo/cv_unet_image-matting'}. trying to build by task and model information.
2024-03-25 22:17:29,210 - modelscope - WARNING - Find task: portrait-matting, model type: None. Insufficient information to build preprocessor, skip building preprocessor
WARNING:tensorflow:From /home/www/anaconda3/envs/tinywan-modelscope/lib/python3.10/site-packages/modelscope/utils/device.py:60: is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.config.list_physical_devices('GPU')` instead.
2024-03-25 22:17:29,213 - modelscope - INFO - loading model from /home/www/.cache/modelscope/hub/damo/cv_unet_image-matting/tf_graph.pb
WARNING:tensorflow:From /home/www/anaconda3/envs/tinywan-modelscope/lib/python3.10/site-packages/modelscope/pipelines/cv/image_matting_pipeline.py:45: FastGFile.__init__ (from tensorflow.python.platform.gfile) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.gfile.GFile.
2024-03-25 22:17:29,745 - modelscope - INFO - load model done
输出图片
图片