Untuk membolehkan pengguna ModelScope menggunakan pelbagai model yang disediakan oleh platform dengan cepat dan mudah, satu set perpustakaan Python berciri penuh disediakan, termasuk pelaksanaan model rasmi ModelScope, serta penggunaan ini. model untuk inferens, Kod finetune yang berkaitan dengan pra-pemprosesan data, pasca-pemprosesan, penilaian kesan dan fungsi lain yang diperlukan untuk tugasan lain, di samping menyediakan API yang ringkas dan mudah digunakan serta contoh penggunaan yang kaya. Dengan menghubungi perpustakaan, pengguna boleh menyelesaikan tugas seperti inferens model, latihan dan penilaian dengan menulis hanya beberapa baris kod Mereka juga boleh melakukan pembangunan sekunder dengan cepat atas dasar ini untuk merealisasikan idea inovatif mereka sendiri.
Model algoritma yang disediakan oleh perpustakaan pada masa ini meliputi lima medan AI utama: imej, pemprosesan bahasa semula jadi, pertuturan, pelbagai mod dan sains, dan berdozen tugasan senario aplikasi Untuk tugasan tertentu, sila rujuk dokumen: Pengenalan kepada tugas.
Perpustakaan ModelScope kini menyokong rangka kerja pembelajaran mendalam seperti Pytorch dan Tensorflow Lebih banyak rangka kerja akan dikemas kini dan dikembangkan secara berterusan pada masa hadapan, jadi nantikan! Semua model rasmi boleh digunakan untuk inferens model melalui Perpustakaan ModelScope, dan beberapa model juga boleh menggunakan perpustakaan untuk latihan dan penilaian. Untuk maklumat penggunaan yang lengkap, lihat kad model untuk model yang sepadan.
Dalam pembelajaran mendalam, inferens merujuk kepada proses yang mana model meramalkan data. Apabila ModelScope melakukan inferens, ia menggunakan saluran paip untuk melaksanakan operasi yang diperlukan secara berurutan. Saluran paip biasa biasanya merangkumi tiga langkah: prapemprosesan data, inferens ke hadapan model dan pasca pemprosesan data.
Kaedah pipeline() ialah salah satu kaedah pengguna paling asas dalam rangka kerja ModelScope dan boleh digunakan untuk melaksanakan inferens model dengan pantas dalam pelbagai bidang. Dengan kaedah pipeline(), pengguna boleh melengkapkan inferens model dengan mudah untuk tugasan tertentu dengan hanya satu baris kod. Kaedah
pipeline() ialah salah satu kaedah pengguna paling asas dalam rangka kerja ModelScope dan boleh digunakan untuk melaksanakan inferens model dengan pantas dalam pelbagai bidang. Dengan kaedah pipeline(), pengguna boleh melengkapkan inferens model dengan mudah untuk tugasan tertentu dengan hanya satu baris kod.
Artikel ini akan memperkenalkan secara ringkas cara menggunakan kaedah saluran paip untuk memuatkan model untuk inferens. Melalui kaedah saluran paip, pengguna boleh dengan mudah menarik model yang diperlukan dari gudang model untuk inferens berdasarkan jenis tugas dan nama model. Kelebihan utama kaedah ini ialah ia mudah digunakan dan boleh melakukan inferens model dengan cepat dan cekap. Kemudahan kaedah saluran paip ialah ia menyediakan cara langsung untuk mendapatkan dan menggunakan model tanpa memerlukan pengguna memahami butiran khusus model, dengan itu menurunkan ambang untuk menggunakan model. Through the pipeline method, users can focus more on solving problems and
Fungsi saluran paip menyokong penetapan nama tugas tertentu, memuatkan model lalai tugas dan mencipta objek saluran paip yang sepadan.
from modelscope.pipelines import pipelineword_segmentation = pipeline('word-segmentation')input_str = '开源技术小栈作者是Tinywan,你知道不?'print(word_segmentation(input_str))
<?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));
Alat penukaran dalam talian: 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_indexer2024-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 indexed2024-03-25 21:41:44,661 - modelscope - WARNING - Model revision not specified, use revision: v1.0.32024-03-25 21:41:44,879 - modelscope - INFO - initiate model from /home/www/.cache/modelscope/hub/damo/nlp_structbert_word-segmentation_chinese-base2024-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-baseYou 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', ',', '你', '知道', '不', '?']}
from modelscope.pipelines import pipelineword_segmentation = pipeline('word-segmentation')inputs =['开源技术小栈作者是Tinywan,你知道不?','webman这个框架不错,建议你看看']print(word_segmentation(inputs))
<?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', '这个', '框架', '不错', ',', '建议', '你', '看看']}]
inputs =['今天天气不错,适合出去游玩','这本书很好,建议你看看']# 指定batch_size参数来支持批量推理print(word_segmentation(inputs, batch_size=2))# 输出[{'output': ['今天', '天气', '不错', ',', '适合', '出去', '游玩']}, {'output': ['这', '本', '书', '很', '好', ',', '建议', '你', '看看']}]
Masukkan set data
from modelscope.msdatasets import MsDatasetfrom modelscope.pipelines import pipelineinputs = ['今天天气不错,适合出去游玩', '这本书很好,建议你看看']dataset = MsDataset.load(inputs, target='sentence')word_segmentation = pipeline('word-segmentation')outputs = word_segmentation(dataset)for o in outputs:print(o)# 输出{'output': ['今天', '天气', '不错', ',', '适合', '出去', '游玩']}{'output': ['这', '本', '书', '很', '好', ',', '建议', '你', '看看']}
Kod Python
from modelscope.models import Modelfrom modelscope.pipelines import pipelinemodel = 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 $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 Modelfrom modelscope.pipelines import pipelinefrom modelscope.preprocessors import Preprocessor, TokenClassificationTransformersPreprocessormodel = 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', '这个', '框架', '不错', ',', '建议', '你', '看看']}]
Outputpip install opencv-pythonSalin selepas log masuk
pip install tensorflow
import cv2from modelscope.pipelines import pipelineportrait_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 $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"));
没有安装会提示:PHP Fatal error: Uncaught PyError: No module named 'cv2' in /home/www/build/ai/demo3.php:4
否则提示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
图片
图片
import cv2from modelscope.pipelines import pipelineportrait_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 $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_indexer2024-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 indexed2024-03-25 22:17:26,990 - modelscope - WARNING - Model revision not specified, use revision: v1.0.02024-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 TensorRT2024-03-25 22:17:29,206 - modelscope - INFO - initiate model from /home/www/.cache/modelscope/hub/damo/cv_unet_image-matting2024-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 preprocessorWARNING: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.pbWARNING: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
图片
Atas ialah kandungan terperinci Rangka kerja pembelajaran mendalam TensorFlow talian paip inferens untuk inferens potongan potret. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!