TensorFlow深度學(xué)習(xí)框架模型推理Pipeline進(jìn)行人像摳圖推理
概述
為了使ModelScope的用戶能夠快速、方便的使用平臺提供的各類模型,提供了一套功能完備的Python library,其中包含了ModelScope官方模型的實(shí)現(xiàn),以及使用這些模型進(jìn)行推理,finetune等任務(wù)所需的數(shù)據(jù)預(yù)處理,后處理,效果評估等功能相關(guān)的代碼,同時(shí)也提供了簡單易用的API,以及豐富的使用樣例。通過調(diào)用library,用戶可以只寫短短的幾行代碼,就可以完成模型的推理、訓(xùn)練和評估等任務(wù),也可以在此基礎(chǔ)上快速進(jìn)行二次開發(fā),實(shí)現(xiàn)自己的創(chuàng)新想法。
目前l(fā)ibrary提供的算法模型,涵蓋了圖像,自然語言處理,語音,多模態(tài),科學(xué)5個(gè)主要的AI領(lǐng)域,數(shù)十個(gè)應(yīng)用場景任務(wù),具體任務(wù)可參考文檔:任務(wù)的介紹。
深度學(xué)習(xí)框架
ModelScope Library當(dāng)前支持的深度學(xué)習(xí)框架包括Pytorch和Tensorflow,后續(xù)將持續(xù)更新拓展,敬請期待! 當(dāng)前的官方模型均支持使用ModelScope Library進(jìn)行模型推理,部分支持使用該庫進(jìn)行訓(xùn)練和評估,具體可參看相應(yīng)模型的模型卡片,了解完整使用信息。
模型推理Pipeline
模型的推理
推理在深度學(xué)習(xí)中表示模型的預(yù)測過程。ModelScope的推理會使用pipeline來執(zhí)行所需要的操作。一個(gè)完整的pipeline一般包括了數(shù)據(jù)的前處理、模型的前向推理、數(shù)據(jù)的后處理三個(gè)過程。
Pipeline介紹
pipeline()方法是ModelScope框架上最基礎(chǔ)的用戶方法之一,可對多領(lǐng)域的多種模型進(jìn)行快速推理。通過pipeline()方法,用戶可以只需要一行代碼即可完成對特定任務(wù)的模型推理。
pipeline()方法是ModelScope框架上最基礎(chǔ)的用戶方法之一,可對多領(lǐng)域的多種模型進(jìn)行快速推理。通過pipeline()方法,用戶可以只需要一行代碼即可完成對特定任務(wù)的模型推理。
Pipeline的使用
本文簡單介紹如何使用pipeline方法加載模型進(jìn)行推理。pipeline方法支持按照任務(wù)類型、模型名稱從模型倉庫拉取模型進(jìn)行進(jìn)行推理,包含以下幾個(gè)方面:
- 環(huán)境準(zhǔn)備
- 重要參數(shù)
- Pipeline基本用法
- 指定預(yù)處理、模型進(jìn)行推理
- 不同場景任務(wù)推理pipeline使用示例
Pipeline基本用法
中文分詞
pipeline函數(shù)支持指定特定任務(wù)名稱,加載任務(wù)默認(rèn)模型,創(chuàng)建對應(yīng)pipeline對象。
Python代碼
from modelscope.pipelines import pipeline
word_segmentation = pipeline('word-segmentation')
input_str = '開源技術(shù)小棧作者是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 = "開源技術(shù)小棧作者是Tinywan,你知道不?";
PyCore::print($word_segmentation($input_str));
在線轉(zhuǎn)換工具:https://www.swoole.com/py2php/
輸出結(jié)果
/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': ['開源', '技術(shù)', '小', '棧', '作者', '是', 'Tinywan', ',', '你', '知道', '不', '?']}
輸入多條樣本
pipeline對象也支持傳入多個(gè)樣本列表輸入,返回對應(yīng)輸出列表,每個(gè)元素對應(yīng)輸入樣本的返回結(jié)果。多條文本的推理方式是輸入data在pipeline內(nèi)部用迭代器單條處理后append到同一個(gè)返回List中。
Python代碼
from modelscope.pipelines import pipeline
word_segmentation = pipeline('word-segmentation')
inputs = ['開源技術(shù)小棧作者是Tinywan,你知道不?','webman這個(gè)框架不錯(cuò),建議你看看']
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(["開源技術(shù)小棧作者是Tinywan,你知道不?", "webman這個(gè)框架不錯(cuò),建議你看看"]);
PyCore::print($word_segmentation($inputs));
輸出
[{'output': ['開源', '技術(shù)', '小', '棧', '作者', '是', 'Tinywan', ',', '你', '知道', '不', '?']},
{'output': ['webman', '這個(gè)', '框架', '不錯(cuò)', ',', '建議', '你', '看看']}]
批量推理
pipeline對于批量推理的支持類似于上面的“輸入多條文本”,區(qū)別在于會在用戶指定的batch_size尺度上,在模型forward過程實(shí)現(xiàn)批量前向推理。
inputs = ['今天天氣不錯(cuò),適合出去游玩','這本書很好,建議你看看']
# 指定batch_size參數(shù)來支持批量推理
print(word_segmentation(inputs, batch_size=2))
# 輸出
[{'output': ['今天', '天氣', '不錯(cuò)', ',', '適合', '出去', '游玩']}, {'output': ['這', '本', '書', '很', '好', ',', '建議', '你', '看看']}]
輸入一個(gè)數(shù)據(jù)集
from modelscope.msdatasets import MsDataset
from modelscope.pipelines import pipeline
inputs = ['今天天氣不錯(cuò),適合出去游玩', '這本書很好,建議你看看']
dataset = MsDataset.load(inputs, target='sentence')
word_segmentation = pipeline('word-segmentation')
outputs = word_segmentation(dataset)
for o in outputs:
print(o)
# 輸出
{'output': ['今天', '天氣', '不錯(cuò)', ',', '適合', '出去', '游玩']}
{'output': ['這', '本', '書', '很', '好', ',', '建議', '你', '看看']}
指定預(yù)處理、模型進(jìn)行推理
pipeline函數(shù)支持傳入實(shí)例化的預(yù)處理對象、模型對象,從而支持用戶在推理過程中定制化預(yù)處理、模型。
創(chuàng)建模型對象進(jìn)行推理
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 = ['開源技術(shù)小棧作者是Tinywan,你知道不?','webman這個(gè)框架不錯(cuò),建議你看看']
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(["開源技術(shù)小棧作者是Tinywan,你知道不?", "webman這個(gè)框架不錯(cuò),建議你看看"]);
PyCore::print($word_segmentation($inputs));
輸出
[{'output': ['開源', '技術(shù)', '小', '棧', '作者', '是', 'Tinywan', ',', '你', '知道', '不', '?']},
{'output': ['webman', '這個(gè)', '框架', '不錯(cuò)', ',', '建議', '你', '看看']}]
創(chuàng)建預(yù)處理器和模型對象進(jìn)行推理
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 = ['開源技術(shù)小棧作者是Tinywan,你知道不?','webman這個(gè)框架不錯(cuò),建議你看看']
print(word_segmentation(inputs))
[{'output': ['開源', '技術(shù)', '小', '棧', '作者', '是', 'Tinywan', ',', '你', '知道', '不', '?']},
{'output': ['webman', '這個(gè)', '框架', '不錯(cuò)', ',', '建議', '你', '看看']}]
圖像
注意:
- 確保你已經(jīng)安裝了OpenCV庫。如果沒有安裝,你可以通過pip安裝
pip install opencv-python
沒有安裝會提示:PHP Fatal error: Uncaught PyError: No module named 'cv2' in /home/www/build/ai/demo3.php:4
- 確保你已經(jīng)安裝深度學(xué)習(xí)框架包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.。
報(bào)錯(cuò)信息表明,你正在嘗試使用一個(gè)名為 modelscope.pipelines.cv.image_matting_pipeline 的模塊,該模塊依賴于 TensorFlow 庫。然而,該模塊無法正常工作,因?yàn)槿鄙俦匾?nbsp;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");
執(zhí)行結(jié)果
/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
輸出圖片
圖片