成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

TensorFlow深度學習框架模型推理Pipeline進行人像摳圖推理

人工智能
ModelScope Library當前支持的深度學習框架包括Pytorch和Tensorflow,后續將持續更新拓展,敬請期待! 當前的官方模型均支持使用ModelScope Library進行模型推理,部分支持使用該庫進行訓練和評估,具體可參看相應模型的模型卡片,了解完整使用信息。

概述

為了使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', '這個', '框架', '不錯', ',', '建議', '你', '看看']}]

圖像

注意:

  1. 確保你已經安裝了OpenCV庫。如果沒有安裝,你可以通過pip安裝
pip install opencv-python

沒有安裝會提示:PHP Fatal error: Uncaught PyError: No module named 'cv2' in /home/www/build/ai/demo3.php:4

  1. 確保你已經安裝深度學習框架包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

輸出圖片

圖片圖片

責任編輯:武曉燕 來源: 開源技術小棧
相關推薦

2023-11-19 23:36:50

2022-04-11 15:40:34

機器學習研究推理

2025-02-27 10:33:36

2021-09-27 09:55:52

深度學習編程人工智能

2022-12-09 09:52:47

AI深度學習

2017-08-12 13:23:43

深度學習神經網絡推理加速

2024-02-01 08:34:30

大模型推理框架NVIDIA

2017-06-23 14:11:56

2025-01-20 07:58:51

2021-03-29 15:13:23

深度學習人臉解鎖算法

2024-11-12 13:40:00

2025-05-14 08:16:46

?Redis機器學習推薦模型

2017-08-16 10:57:52

深度學習TensorFlowNLP

2025-03-05 00:22:00

2025-05-29 03:00:00

混合推理模型LHRMAI

2025-06-10 03:30:00

2019-05-07 14:42:03

深度學習編程人工智能

2019-10-21 13:40:20

代碼開發工具

2022-05-06 12:13:55

模型AI

2018-01-03 10:00:39

深度學習摳圖消除背景
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 精品www | 欧美一级免费 | 国产视频线观看永久免费 | 欧美精品三区 | 精品久久久久久亚洲精品 | 国产日韩视频 | 免费观看毛片 | 麻豆av电影网 | 最新日韩欧美 | 国产精品久久久久久久久久久免费看 | 久久久久国产一区二区 | 欧美日韩福利 | 久久男人 | 犬夜叉在线观看 | 免费欧美 | 国产精品揄拍一区二区 | 中文字字幕一区二区三区四区五区 | 久婷婷| 亚洲嫩草 | 一区二区三区中文字幕 | 午夜av影院 | 一区中文字幕 | 爱爱视频在线观看 | 国产精品.xx视频.xxtv | 亚洲图片一区二区三区 | 国产亚洲高清视频 | 成人亚洲精品久久久久软件 | 日本免费黄色 | 久草中文在线 | 2021天天干夜夜爽 | av无遮挡| 日本一道本 | 午夜午夜精品一区二区三区文 | 亚洲在线 | 综合色在线 | 在线日韩欧美 | 久久久噜噜噜久久中文字幕色伊伊 | 久一久 | av免费观看在线 | 免费在线观看成人 | 欧美爱爱视频 |