From 3ca68bf22fa0fb3087029fb0a7b2861900188414 Mon Sep 17 00:00:00 2001 From: wenmeng zhou Date: Fri, 17 Feb 2023 12:01:54 +0800 Subject: [PATCH 1/4] remove profiling log (#113) UT test by default does not run profiling --- tests/run.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/run.py b/tests/run.py index ea78fdd0..5ca06599 100644 --- a/tests/run.py +++ b/tests/run.py @@ -600,7 +600,7 @@ if __name__ == '__main__': parser.add_argument( '--level', default=0, type=int, help='2 -- all, 1 -- p1, 0 -- p0') parser.add_argument( - '--disable_profile', action='store_true', help='disable profiling') + '--profile', action='store_true', help='enable profiling') parser.add_argument( '--run_config', default=None, @@ -634,7 +634,7 @@ if __name__ == '__main__': set_test_level(args.level) os.environ['REGRESSION_BASELINE'] = '1' logger.info(f'TEST LEVEL: {test_level()}') - if not args.disable_profile: + if args.profile: from utils import profiler logger.info('enable profile ...') profiler.enable() From 443d714557446a89154a417706c84de19c398766 Mon Sep 17 00:00:00 2001 From: wenmeng zhou Date: Fri, 17 Feb 2023 13:33:18 +0800 Subject: [PATCH 2/4] Refine README.md and add README_zh.md (#114) * update readme and add chinese readme * add inference gif and refine doc --- .github/workflows/citest.yaml | 2 +- README.md | 240 +++++++++++++++++++++++++++++- README_zh.md | 273 ++++++++++++++++++++++++++++++++++ 3 files changed, 508 insertions(+), 7 deletions(-) create mode 100644 README_zh.md diff --git a/.github/workflows/citest.yaml b/.github/workflows/citest.yaml index 00c6bbbf..1399e49d 100644 --- a/.github/workflows/citest.yaml +++ b/.github/workflows/citest.yaml @@ -27,7 +27,7 @@ on: - "tools/**" - ".dev_scripts/**" - "README.md" - - "README_zh-CN.md" + - "README_*.md" - "NOTICE" - ".github/workflows/lint.yaml" - ".github/workflows/publish.yaml" diff --git a/README.md b/README.md index f0c65769..e62059a8 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,10 @@ +

+
+ +
+

+

[![PyPI](https://img.shields.io/pypi/v/modelscope)](https://pypi.org/project/modelscope/) @@ -12,6 +18,13 @@ +

+

+ English | + 中文 | +

+

+
@@ -23,20 +36,235 @@ The Python library offers the layered-APIs necessary for model contributors to i Apart from harboring implementations of various models, ModelScope library also enables the necessary interactions with ModelScope backend services, particularly with the Model-Hub and Dataset-Hub. Such interactions facilitate management of various entities (models and datasets) to be performed seamlessly under-the-hood, including entity lookup, version control, cache management, and many others. +# Models and Online Demos + +Hundreds of models are made publicly available on ModelScope (600+ and counting), covering the latest development in areas such as NLP, CV, Audio, Multi-modality, and AI for Science, etc. Many of these models represent the SOTA in the fields, and made their open-sourced debut on ModelScope. Users can visit ModelScope([modelscope.cn](http://www.modelscope.cn)) and experience first-hand how these models perform via online experience, with just a few clicks. Immediate developer-experience is also possible through the ModelScope Notebook, which is backed by ready-to-use cloud CPU/GPU development environment, and is only a click away on ModelScope website. + +Some of the representative examples include: + +NLP: + +* [nlp_gpt3_text-generation_2.7B](https://modelscope.cn/models/damo/nlp_gpt3_text-generation_2.7B) + +* [ChatYuan-large](https://modelscope.cn/models/ClueAI/ChatYuan-large) + +* [mengzi-t5-base](https://modelscope.cn/models/langboat/mengzi-t5-base) + +* [nlp_csanmt_translation_en2zh](https://modelscope.cn/models/damo/nlp_csanmt_translation_en2zh) + +* [nlp_raner_named-entity-recognition_chinese-base-news](https://modelscope.cn/models/damo/nlp_raner_named-entity-recognition_chinese-base-news) + +* [nlp_structbert_word-segmentation_chinese-base](https://modelscope.cn/models/damo/nlp_structbert_word-segmentation_chinese-base) + +* [Erlangshen-RoBERTa-330M-Sentiment](https://modelscope.cn/models/fengshenbang/Erlangshen-RoBERTa-330M-Sentiment) + +* [nlp_convai_text2sql_pretrain_cn](https://modelscope.cn/models/damo/nlp_convai_text2sql_pretrain_cn) + +Audio: + +* [speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch](https://modelscope.cn/models/damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch) + +* [speech_sambert-hifigan_tts_zh-cn_16k](https://modelscope.cn/models/damo/speech_sambert-hifigan_tts_zh-cn_16k) + +* [speech_charctc_kws_phone-xiaoyun](https://modelscope.cn/models/damo/speech_charctc_kws_phone-xiaoyun) + +* [u2pp_conformer-asr-cn-16k-online](https://modelscope.cn/models/wenet/u2pp_conformer-asr-cn-16k-online) + +* [speech_frcrn_ans_cirm_16k](https://modelscope.cn/models/damo/speech_frcrn_ans_cirm_16k) + +* [speech_dfsmn_aec_psm_16k](https://modelscope.cn/models/damo/speech_dfsmn_aec_psm_16k) + + +CV: + +* [cv_tinynas_object-detection_damoyolo](https://modelscope.cn/models/damo/cv_tinynas_object-detection_damoyolo) + +* [cv_unet_person-image-cartoon_compound-models](https://modelscope.cn/models/damo/cv_unet_person-image-cartoon_compound-models) + +* [cv_convnextTiny_ocr-recognition-general_damo](https://modelscope.cn/models/damo/cv_convnextTiny_ocr-recognition-general_damo) + +* [cv_resnet18_human-detection](https://modelscope.cn/models/damo/cv_resnet18_human-detection) + +* [cv_resnet50_face-detection_retinaface](https://modelscope.cn/models/damo/cv_resnet50_face-detection_retinaface) + +* [cv_unet_image-matting](https://modelscope.cn/models/damo/cv_unet_image-matting) + +* [cv_F3Net_product-segmentation](https://modelscope.cn/models/damo/cv_F3Net_product-segmentation) + +* [cv_resnest101_general_recognition](https://modelscope.cn/models/damo/cv_resnest101_general_recognition) + + +Multi-Modal: + +* [multi-modal_clip-vit-base-patch16_zh](https://modelscope.cn/models/damo/multi-modal_clip-vit-base-patch16_zh) + +* [ofa_pretrain_base_zh](https://modelscope.cn/models/damo/ofa_pretrain_base_zh) + +* [Taiyi-Stable-Diffusion-1B-Chinese-v0.1](https://modelscope.cn/models/fengshenbang/Taiyi-Stable-Diffusion-1B-Chinese-v0.1) + +* [mplug_visual-question-answering_coco_large_en](https://modelscope.cn/models/damo/mplug_visual-question-answering_coco_large_en) + +AI for Science: + +* [uni-fold-monomer](https://modelscope.cn/models/DPTech/uni-fold-monomer/summary) + +* [uni-fold-multimer](https://modelscope.cn/models/DPTech/uni-fold-multimer/summary) + +# QuickTour + +We provide unified interface for inference using `pipeline`, finetuning and evaluation using `Trainer` for different tasks. + +For any given task with any type of input (image, text, audio, video...), inference pipeline can be implemented with only a few lines of code, which will automatically load the associated model to get inference result, as is exemplified below: + +```python +>>> from modelscope.pipelines import pipeline +>>> word_segmentation = pipeline('word-segmentation',model='damo/nlp_structbert_word-segmentation_chinese-base') +>>> word_segmentation('今天天气不错,适合出去游玩') +{'output': '今天 天气 不错 , 适合 出去 游玩'} +``` + +Given an image, you can use following code to cut out the human. + +![image](https://resouces.modelscope.cn/document/docdata/2023-2-16_20:53/dist/ModelScope%20Library%E6%95%99%E7%A8%8B/resources/1656989748829-9ab3aa9b-461d-44f8-98fb-c85bc6f670f9.png) + +```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']) +``` +The output image is +![image](https://resouces.modelscope.cn/document/docdata/2023-2-16_20:53/dist/ModelScope%20Library%E6%95%99%E7%A8%8B/resources/1656989768092-5470f8ac-cda8-4703-ac98-dbb6fd675b34.png) + +For finetuning and evaluation, you need ten more lines of code to construct dataset and trainer, and by calling `traner.train()` and +`trainer.evaluate()` you can finish finetuning and evaluating a certain model. + +For example, we use the gpt3 1.3B model to load the chinese poetry dataset and finetune the model, the resulted model can be used for poetry generation. + +```python +>>> from modelscope.metainfo import Trainers +>>> from modelscope.msdatasets import MsDataset +>>> from modelscope.trainers import build_trainer + +>>> train_dataset = MsDataset.load('chinese-poetry-collection', split='train'). remap_columns({'text1': 'src_txt'}) +>>> eval_dataset = MsDataset.load('chinese-poetry-collection', split='test').remap_columns({'text1': 'src_txt'}) +>>> max_epochs = 10 +>>> tmp_dir = './gpt3_poetry' + +>>> kwargs = dict( + model='damo/nlp_gpt3_text-generation_1.3B', + train_dataset=train_dataset, + eval_dataset=eval_dataset, + max_epochs=max_epochs, + work_dir=tmp_dir) + +>>> trainer = build_trainer(name=Trainers.gpt3_trainer, default_args=kwargs) +>>> trainer.train() +``` + +# Why should I use ModelScope library + +1. A unified and concise user interface is abstracted for different tasks and different models. Three lines of code complete the inference, and 10 lines of code complete the model training. It is convenient for users to use different models in multiple fields in the ModelScope community. It is ready to use and easy to get started with AI. and teaching. + +2. Construct a model-centric development and application experience, support model training, inference, export and deployment, and facilitate users to build their own MLOps based on the ModelScope Library. + +3. For the model inference and training process, a modular design is carried out, and a wealth of functional module implementations are provided, which is convenient for users to customize development to customize their own model inference, training and other processes. + +4. For distributed model training, especially for large models, it provides rich training strategy support, including data parallel, model parallel, hybrid parallel and so on. + # Installation -Please refer to [installation](https://modelscope.cn/docs/%E7%8E%AF%E5%A2%83%E5%AE%89%E8%A3%85). +## Docker +ModelScope Library currently supports tensorflow and pytorch deep learning framework for model training and inference, and it is tested and run on Python 3.7+, Pytorch 1.8+, Tensorflow1.15 or Tensorflow2.0+. -# Get Started +In order to allow everyone to directly use all the models on the ModelScope platform without configuring the environment, ModelScope provides official docker image for developers who need it. Based on the official image, you can skip all environment installation and configuration and use it directly. Currently, the latest version of the CPU image and GPU image we provide can be obtained from the following address -You can refer to [quick_start](https://modelscope.cn/docs/%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7%8B) for quick start. +CPU docker image +```shell +registry.cn-hangzhou.aliyuncs.com/modelscope-repo/modelscope:ubuntu20.04-py37-torch1.11.0-tf1.15.5-1.3.0 +``` -We also provide other documentations including: +GPU docker image +```shell +registry.cn-hangzhou.aliyuncs.com/modelscope-repo/modelscope:ubuntu20.04-cuda11.3.0-py37-torch1.11.0-tf1.15.5-1.3.0 +``` + +## Setup Local Python Environment + +Also you can setup your local python environment using pip and conda. We suggest to use [anaconda](https://docs.anaconda.com/anaconda/install/) to create your python environment: + +```shell +conda create -n modelscope python=3.7 +conda activate modelscope +``` + +Then you can install pytorch or tensorflow according to your model requirements. +* Install pytorch [doc](https://pytorch.org/get-started/locally/) +* Install tensorflow [doc](https://www.tensorflow.org/install/pip) + +After installing the necessary framework, you can install modelscope library as follows: + +If you only want to download models and datasets, install modelscope framework +```shell +pip install modelscope +``` + +If you want to use multi-modal models: +```shell +pip install modelscope[multi-modal] +``` + +If you want to use nlp models: +```shell +pip install modelscope[nlp] -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html +``` + +If you want to use cv models: +```shell +pip install modelscope[cv] -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html +``` + +If you want to use audio models: +```shell +pip install modelscope[audio] -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html +``` + +If you want to use science models: +```shell +pip install modelscope[science] -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html +``` + +`Notes`: +1. Currently, some audio-task models only support python3.7, tensorflow1.15.4 Linux environments. Most other models can be installed and used on windows and Mac (x86). + +2. Some models in the audio field use the third-party library SoundFile for wav file processing. On the Linux system, users need to manually install libsndfile of SoundFile([doc link](https://github.com/bastibe/python-soundfile#installation)). On Windows and MacOS, it will be installed automatically without user operation. For example, on Ubuntu, you can use following commands: + ```shell + sudo apt-get update + sudo apt-get install libsndfile1 + ``` + +3. Some models in computer vision need mmcv-full, you can refer to mmcv [installation guide](https://github.com/open-mmlab/mmcv#installation), a minimal installation is as follows: + + ```shell + pip uninstall mmcv # if you have installed mmcv, uninstall it + pip install -U openmim + mim install mmcv-full + ``` + + + +# Learn More + +We provide additional documentations including: +* [More detailed Installation Guide](https://modelscope.cn/docs/%E7%8E%AF%E5%A2%83%E5%AE%89%E8%A3%85) * [Introduction to tasks](https://modelscope.cn/docs/%E4%BB%BB%E5%8A%A1%E7%9A%84%E4%BB%8B%E7%BB%8D) * [Use pipeline for model inference](https://modelscope.cn/docs/%E6%A8%A1%E5%9E%8B%E7%9A%84%E6%8E%A8%E7%90%86Pipeline) -* [Finetune example](https://modelscope.cn/docs/%E6%A8%A1%E5%9E%8B%E7%9A%84%E8%AE%AD%E7%BB%83Train) +* [Finetuning example](https://modelscope.cn/docs/%E6%A8%A1%E5%9E%8B%E7%9A%84%E8%AE%AD%E7%BB%83Train) * [Preprocessing of data](https://modelscope.cn/docs/%E6%95%B0%E6%8D%AE%E7%9A%84%E9%A2%84%E5%A4%84%E7%90%86) -* [Evaluation metrics](https://modelscope.cn/docs/%E6%A8%A1%E5%9E%8B%E7%9A%84%E8%AF%84%E4%BC%B0) +* [Evaluation](https://modelscope.cn/docs/%E6%A8%A1%E5%9E%8B%E7%9A%84%E8%AF%84%E4%BC%B0) +* [Contribute your own model to ModelScope](https://modelscope.cn/docs/ModelScope%E6%A8%A1%E5%9E%8B%E6%8E%A5%E5%85%A5%E6%B5%81%E7%A8%8B%E6%A6%82%E8%A7%88) # License diff --git a/README_zh.md b/README_zh.md new file mode 100644 index 00000000..a5dcbbfa --- /dev/null +++ b/README_zh.md @@ -0,0 +1,273 @@ + +

+
+ +
+

+ +

+ +[![PyPI](https://img.shields.io/pypi/v/modelscope)](https://pypi.org/project/modelscope/) + +[![license](https://img.shields.io/github/license/modelscope/modelscope.svg)](https://github.com/modelscope/modelscope/blob/master/LICENSE) +[![open issues](https://isitmaintained.com/badge/open/modelscope/modelscope.svg)](https://github.com/modelscope/modelscope/issues) +[![GitHub pull-requests](https://img.shields.io/github/issues-pr/modelscope/modelscope.svg)](https://GitHub.com/modelscope/modelscope/pull/) +[![GitHub latest commit](https://badgen.net/github/last-commit/modelscope/modelscope)](https://GitHub.com/modelscope/modelscope/commit/) +[![Leaderboard](https://img.shields.io/badge/ModelScope-Check%20Your%20Contribution-orange)](https://opensource.alibaba.com/contribution_leaderboard/details?projectValue=modelscope) + + + + +

+

+ English | + 中文 | +

+

+ + +
+ +# 简介 + +[ModelScope]( https://www.modelscope.cn) 是一个“模型即服务”(MaaS)平台,旨在汇集来自AI社区的最先进的机器学习模型,并简化在实际应用中使用AI模型的流程。ModelScope库使开发人员能够通过丰富的API设计执行推理、训练和评估,从而促进跨不同AI领域的最先进模型的统一体验。 + +ModelScope Library为模型贡献者提供了必要的分层API,以便将来自 CV、NLP、语音、多模态以及科学计算的模型集成到ModelScope生态系统中。所有这些不同模型的实现都以一种简单统一访问的方式进行封装,用户只需几行代码即可完成模型推理、微调和评估。同时,灵活的模块化设计使得在必要时也可以自定义模型训练推理过程中的不同组件。 + +除了包含各种模型的实现之外,ModelScope Library还支持与ModelScope后端服务进行必要的交互,特别是与Model-Hub和Dataset-Hub的交互。这种交互促进了模型和数据集的管理在后台无缝执行,包括模型数据集查询、版本控制、缓存管理等。 + +# 部分模型和在线体验 +ModelScope开源了数百个(当前600+)模型,涵盖自然语言处理、计算机视觉、语音、多模态、科学计算等,其中包含数百个SOTA模型。用户可以进入ModelScope网站([modelscope.cn](http://www.modelscope.cn))的模型中心零门槛在线体验,或者Notebook方式体验模型。 + +

+
+ +
+

+ +示例如下: + +自然语言处理: + +* [GPT-3预训练生成模型-中文-2.7B](https://modelscope.cn/models/damo/nlp_gpt3_text-generation_2.7B) + +* [元语功能型对话大模型](https://modelscope.cn/models/ClueAI/ChatYuan-large) + +* [孟子T5预训练生成模型-中文-base](https://modelscope.cn/models/langboat/mengzi-t5-base) + +* [CSANMT连续语义增强机器翻译-英中-通用领域-large](https://modelscope.cn/models/damo/nlp_csanmt_translation_en2zh) + +* [RaNER命名实体识别-中文-新闻领域-base](https://modelscope.cn/models/damo/nlp_raner_named-entity-recognition_chinese-base-news) + +* [BAStructBERT分词-中文-新闻领域-base](https://modelscope.cn/models/damo/nlp_structbert_word-segmentation_chinese-base) + +* [二郎神-RoBERTa-330M-情感分类](https://modelscope.cn/models/fengshenbang/Erlangshen-RoBERTa-330M-Sentiment) + +* [SPACE-T表格问答预训练模型-中文-通用领域-base](https://modelscope.cn/models/damo/nlp_convai_text2sql_pretrain_cn) + +语音: + +* [Paraformer语音识别-中文-通用-16k-离线-large-pytorch](https://modelscope.cn/models/damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch) + +* [语音合成-中文-多情感领域-16k-多发音人](https://modelscope.cn/models/damo/speech_sambert-hifigan_tts_zh-cn_16k) + +* [CTC语音唤醒-移动端-单麦-16k-小云小云](https://modelscope.cn/models/damo/speech_charctc_kws_phone-xiaoyun) + +* [WeNet-U2pp_Conformer-语音识别-中文-16k-实时](https://modelscope.cn/models/wenet/u2pp_conformer-asr-cn-16k-online) + +* [FRCRN语音降噪-单麦-16k](https://modelscope.cn/models/damo/speech_frcrn_ans_cirm_16k) + +* [DFSMN回声消除-单麦单参考-16k](https://modelscope.cn/models/damo/speech_dfsmn_aec_psm_16k) + + +计算机视觉: + +* [DAMOYOLO-高性能通用检测模型-S](https://modelscope.cn/models/damo/cv_tinynas_object-detection_damoyolo) + +* [DCT-Net人像卡通化](https://modelscope.cn/models/damo/cv_unet_person-image-cartoon_compound-models) + +* [读光-文字识别-行识别模型-中英-通用领域](https://modelscope.cn/models/damo/cv_convnextTiny_ocr-recognition-general_damo) + +* [人体检测-通用-Base](https://modelscope.cn/models/damo/cv_resnet18_human-detection) + +* [RetinaFace人脸检测关键点模型](https://modelscope.cn/models/damo/cv_resnet50_face-detection_retinaface) + +* [BSHM人像抠图](https://modelscope.cn/models/damo/cv_unet_image-matting) + +* [图像分割-商品展示图场景的商品分割-电商领域](https://modelscope.cn/models/damo/cv_F3Net_product-segmentation) + +* [万物识别-中文-通用领域](https://modelscope.cn/models/damo/cv_resnest101_general_recognition) + + +多模态: + +* [CLIP模型-中文-通用领域-base](https://modelscope.cn/models/damo/multi-modal_clip-vit-base-patch16_zh) + +* [OFA预训练模型-中文-通用领域-base](https://modelscope.cn/models/damo/ofa_pretrain_base_zh) + +* [太乙-Stable-Diffusion-1B-中文-v0.1](https://modelscope.cn/models/fengshenbang/Taiyi-Stable-Diffusion-1B-Chinese-v0.1) + +* [mPLUG视觉问答模型-英文-large](https://modelscope.cn/models/damo/mplug_visual-question-answering_coco_large_en) + +科学计算: + +* [Uni-Fold-Monomer 开源的蛋白质单体结构预测模型](https://modelscope.cn/models/DPTech/uni-fold-monomer/summary) + +* [Uni-Fold-Multimer 开源的蛋白质复合物结构预测模型](https://modelscope.cn/models/DPTech/uni-fold-multimer/summary) + +# 快速上手 + +我们针对不同任务提供了统一的使用接口, 使用`pipeline`进行模型推理、使用`Trainer`进行微调和评估。 + +对于任意类型输入(图像、文本、音频、视频...)的任何任务,只需3行代码即可加载模型并获得推理结果,如下所示: +```python +>>> from modelscope.pipelines import pipeline +>>> word_segmentation = pipeline('word-segmentation',model='damo/nlp_structbert_word-segmentation_chinese-base') +>>> word_segmentation('今天天气不错,适合出去游玩') +{'output': '今天 天气 不错 , 适合 出去 游玩'} +``` + +给定一张图片,你可以使用如下代码进行人像抠图. + +![image](https://resouces.modelscope.cn/document/docdata/2023-2-16_20:53/dist/ModelScope%20Library%E6%95%99%E7%A8%8B/resources/1656989748829-9ab3aa9b-461d-44f8-98fb-c85bc6f670f9.png) + +```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']) +``` +输出图像如下 +![image](https://resouces.modelscope.cn/document/docdata/2023-2-16_20:53/dist/ModelScope%20Library%E6%95%99%E7%A8%8B/resources/1656989768092-5470f8ac-cda8-4703-ac98-dbb6fd675b34.png) + +对于微调和评估模型, 你需要通过十多行代码构建dataset和trainer,调用`trainer.train()`和`trainer.evaluate()`即可。 + +例如我们利用gpt3 1.3B的模型,加载是诗歌数据集进行finetune,可以完成古诗生成模型的训练。 +```python +>>> from modelscope.metainfo import Trainers +>>> from modelscope.msdatasets import MsDataset +>>> from modelscope.trainers import build_trainer + +>>> train_dataset = MsDataset.load('chinese-poetry-collection', split='train'). remap_columns({'text1': 'src_txt'}) +>>> eval_dataset = MsDataset.load('chinese-poetry-collection', split='test').remap_columns({'text1': 'src_txt'}) +>>> max_epochs = 10 +>>> tmp_dir = './gpt3_poetry' + +>>> kwargs = dict( + model='damo/nlp_gpt3_text-generation_1.3B', + train_dataset=train_dataset, + eval_dataset=eval_dataset, + max_epochs=max_epochs, + work_dir=tmp_dir) + +>>> trainer = build_trainer(name=Trainers.gpt3_trainer, default_args=kwargs) +>>> trainer.train() +``` + +# 为什么要用ModelScope library + +1. 针对不同任务、不同模型抽象了统一简洁的用户接口,3行代码完成推理,10行代码完成模型训练,方便用户使用ModelScope社区中多个领域的不同模型,开箱即用,便于AI入门和教学。 + +2. 构造以模型为中心的开发应用体验,支持模型训练、推理、导出部署,方便用户基于ModelScope Library构建自己的MLOps. + +3. 针对模型推理、训练流程,进行了模块化的设计,并提供了丰富的功能模块实现,方便用户定制化开发来自定义自己的推理、训练等过程。 + +4. 针对分布式模型训练,尤其是大模型,提供了丰富的训练策略支持,包括数据并行、模型并行、混合并行等。 + +# 安装 + +## 镜像 +ModelScope Library目前支持tensorflow,pytorch深度学习框架进行模型训练、推理, 在Python 3.7+, Pytorch 1.8+, Tensorflow1.15/Tensorflow2.0+测试可运行。 + +为了让大家能直接用上ModelScope平台上的所有模型,无需配置环境,ModelScope提供了官方镜像,方便有需要的开发者获取。地址如下: + +CPU镜像 +```shell +registry.cn-hangzhou.aliyuncs.com/modelscope-repo/modelscope:ubuntu20.04-py37-torch1.11.0-tf1.15.5-1.3.0 +``` + +GPU镜像 +```shell +registry.cn-hangzhou.aliyuncs.com/modelscope-repo/modelscope:ubuntu20.04-cuda11.3.0-py37-torch1.11.0-tf1.15.5-1.3.0 +``` + +## 搭建本地Python环境 + +你也可以使用pip和conda搭建本地python环境,我们推荐使用[Anaconda](https://docs.anaconda.com/anaconda/install/),安装完成后,执行如下命令为modelscope library创建对应的python环境: +```shell +conda create -n modelscope python=3.7 +conda activate modelscope +``` + +接下来根据所需使用的模型依赖安装底层计算框架 +* 安装Pytorch [文档链接](https://pytorch.org/get-started/locally/) +* 安装tensorflow [文档链接](https://www.tensorflow.org/install/pip) + + +安装完前置依赖,你可以按照如下方式安装ModelScope Library。 + +ModelScope Libarary由核心框架,以及不同领域模型的对接组件组成。如果只需要ModelScope模型和数据集访问等基础能力,可以只安装ModelScope的核心框架: +```shell +pip install modelscope +``` + +如仅需体验多模态领域的模型,可执行如下命令安装领域依赖: +```shell +pip install modelscope[multi-modal] +``` + +如仅需体验NLP领域模型,可执行如下命令安装领域依赖(因部分依赖由ModelScope独立host,所以需要使用"-f"参数): +```shell +pip install modelscope[nlp] -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html +``` + +If you want to use cv models: +```shell +pip install modelscope[cv] -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html +``` + +如仅需体验语音领域模型,可执行如下命令安装领域依赖(因部分依赖由ModelScope独立host,所以需要使用"-f"参数): +```shell +pip install modelscope[audio] -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html +``` + +`注意`:当前大部分语音模型需要在Linux环境上使用,并且推荐使用python3.7 + tensorflow 1.x的组合。 + +如仅需体验科学计算领域模型,可执行如下命令安装领域依赖(因部分依赖由ModelScope独立host,所以需要使用"-f"参数): +```shell +pip install modelscope[science] -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html +``` + +`注`: +1. 目前部分语音相关的模型仅支持 python3.7,tensorflow1.15.4的Linux环境使用。 其他绝大部分模型可以在windows、mac(x86)上安装使用。. + +2. 语音领域中一部分模型使用了三方库SoundFile进行wav文件处理,在Linux系统上用户需要手动安装SoundFile的底层依赖库libsndfile,在Windows和MacOS上会自动安装不需要用户操作。详细信息可参考[SoundFile 官网](https://github.com/bastibe/python-soundfile#installation)。以Ubuntu系统为例,用户需要执行如下命令: + ```shell + sudo apt-get update + sudo apt-get install libsndfile1 + ``` + +3. CV领域的少数模型,需要安装mmcv-full, 如果运行过程中提示缺少mmcv,请参考mmcv[安装手册](https://github.com/open-mmlab/mmcv#installation)进行安装。 这里提供一个最简版的mmcv-full安装步骤,但是要达到最优的mmcv-full的安装效果(包括对于cuda版本的兼容),请根据自己的实际机器环境,以mmcv官方安装手册为准。 + ```shell + pip uninstall mmcv # if you have installed mmcv, uninstall it + pip install -U openmim + mim install mmcv-full + ``` + + +# 更多教程 + +除了上述内容,我们还提供如下信息: +* [更加详细的安装文档](https://modelscope.cn/docs/%E7%8E%AF%E5%A2%83%E5%AE%89%E8%A3%85) +* [任务的介绍](https://modelscope.cn/docs/%E4%BB%BB%E5%8A%A1%E7%9A%84%E4%BB%8B%E7%BB%8D) +* [模型推理](https://modelscope.cn/docs/%E6%A8%A1%E5%9E%8B%E7%9A%84%E6%8E%A8%E7%90%86Pipeline) +* [模型微调](https://modelscope.cn/docs/%E6%A8%A1%E5%9E%8B%E7%9A%84%E8%AE%AD%E7%BB%83Train) +* [数据预处理](https://modelscope.cn/docs/%E6%95%B0%E6%8D%AE%E7%9A%84%E9%A2%84%E5%A4%84%E7%90%86) +* [模型评估](https://modelscope.cn/docs/%E6%A8%A1%E5%9E%8B%E7%9A%84%E8%AF%84%E4%BC%B0) +* [贡献模型到ModelScope](https://modelscope.cn/docs/ModelScope%E6%A8%A1%E5%9E%8B%E6%8E%A5%E5%85%A5%E6%B5%81%E7%A8%8B%E6%A6%82%E8%A7%88) + +# License + +本项目使用[Apache License (Version 2.0)](https://github.com/modelscope/modelscope/blob/master/LICENSE). From ad300d084a4325996ea9cc3bfc43102d42ac05cd Mon Sep 17 00:00:00 2001 From: Jinpeng Zhang <39855704+zjpbinary@users.noreply.github.com> Date: Fri, 17 Feb 2023 16:34:35 +0800 Subject: [PATCH 3/4] Add word alignment model (#101) * word alignment * Delete ast_index_file.py * add introduction * fix-wordalignmentpreprocessor --- modelscope/metainfo.py | 2 + modelscope/models/nlp/bert/__init__.py | 1 + modelscope/models/nlp/bert/word_alignment.py | 173 ++++++++++++++++++ modelscope/outputs/nlp_outputs.py | 10 +- modelscope/outputs/outputs.py | 5 + modelscope/pipelines/nlp/__init__.py | 2 + .../pipelines/nlp/word_alignment_pipeline.py | 68 +++++++ modelscope/preprocessors/__init__.py | 6 +- modelscope/preprocessors/nlp/__init__.py | 4 + .../nlp/word_alignment_preprocessor.py | 131 +++++++++++++ modelscope/utils/constant.py | 1 + 11 files changed, 399 insertions(+), 4 deletions(-) create mode 100644 modelscope/models/nlp/bert/word_alignment.py create mode 100644 modelscope/pipelines/nlp/word_alignment_pipeline.py create mode 100644 modelscope/preprocessors/nlp/word_alignment_preprocessor.py diff --git a/modelscope/metainfo.py b/modelscope/metainfo.py index 63ddd6d9..dd36bd47 100644 --- a/modelscope/metainfo.py +++ b/modelscope/metainfo.py @@ -405,6 +405,7 @@ class Pipelines(object): dialog_state_tracking = 'dialog-state-tracking' zero_shot_classification = 'zero-shot-classification' text_error_correction = 'text-error-correction' + word_alignment = 'word-alignment' plug_generation = 'plug-generation' gpt3_generation = 'gpt3-generation' gpt_moe_generation = 'gpt-moe-generation' @@ -925,6 +926,7 @@ class Preprocessors(object): sbert_token_cls_tokenizer = 'sbert-token-cls-tokenizer' zero_shot_cls_tokenizer = 'zero-shot-cls-tokenizer' text_error_correction = 'text-error-correction' + word_alignment = 'word-alignment' sentence_embedding = 'sentence-embedding' text_ranking = 'text-ranking' sequence_labeling_tokenizer = 'sequence-labeling-tokenizer' diff --git a/modelscope/models/nlp/bert/__init__.py b/modelscope/models/nlp/bert/__init__.py index 6578a0d7..00373f1c 100644 --- a/modelscope/models/nlp/bert/__init__.py +++ b/modelscope/models/nlp/bert/__init__.py @@ -17,6 +17,7 @@ if TYPE_CHECKING: from .token_classification import BertForTokenClassification from .document_segmentation import BertForDocumentSegmentation from .siamese_uie import SiameseUieModel + from .word_alignment import MBertForWordAlignment else: _import_structure = { 'backbone': [ diff --git a/modelscope/models/nlp/bert/word_alignment.py b/modelscope/models/nlp/bert/word_alignment.py new file mode 100644 index 00000000..526ec07d --- /dev/null +++ b/modelscope/models/nlp/bert/word_alignment.py @@ -0,0 +1,173 @@ +# Copyright (c) Alibaba, Inc. and its affiliates. +# Copyright 2018 The Google AI Language Team Authors and The HuggingFace Inc. team. +# Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. +# All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import torch +import torch.nn as nn +import torch.utils.checkpoint + +from modelscope.metainfo import Models +from modelscope.models.builder import MODELS +from modelscope.outputs import WordAlignmentOutput +from modelscope.utils import logger as logging +from modelscope.utils.constant import Tasks +from .backbone import BertModel, BertPreTrainedModel + +logger = logging.get_logger() + + +@MODELS.register_module(Tasks.word_alignment, module_name=Models.bert) +class MBertForWordAlignment(BertPreTrainedModel): + r"""MBert Model for the Word Alignment task. + + Code for EMNLP Findings 2022 paper, "Third-Party Aligner for Neural Word Alignments". + https://arxiv.org/abs/2211.04198 + + Parameters: + config (:class:`~modelscope.models.nlp.structbert.SbertConfig`): Model configuration class with + all the parameters of the model. + Initializing with a config file does not load the weights associated with the model, only the + configuration. Check out the :meth:`~transformers.PreTrainedModel.from_pretrained` method to load the model + weights. + """ + _keys_to_ignore_on_load_unexpected = [r'pooler'] + _keys_to_ignore_on_load_missing = [ + r'position_ids', r'predictions.decoder.bias' + ] + + def __init__(self, config, **kwargs): + super().__init__(config) + + if config.is_decoder: + logger.warning( + 'If you want to use `BertForMaskedLM` make sure `config.is_decoder=False` for ' + 'bi-directional self-attention.') + config.num_hidden_layers = kwargs.get('encoder_layers', 8) + + self.bert = BertModel(config, add_pooling_layer=False) + + # Initialize weights and apply final processing + self.post_init() + + def forward( + self, + src_input_ids=None, + src_attention_mask=None, + src_b2w_map=None, + tgt_input_ids=None, + tgt_attention_mask=None, + tgt_b2w_map=None, + threshold=0.001, + bpe_level=False, + ): + """ + Args: src_input_ids: + Indices of source input sequence tokens in the vocabulary. + src_attention_mask: + Source mask to avoid performing attention on padding token indices. + src_b2w_map: + Word order numner of subword in source sequence. + tgt_input_ids: + Indices of target input sequence tokens in the vocabulary. + tgt_attention_mask: + Target mask to avoid performing attention on padding token indices. + tgt_b2w_map: + Word order numner of subword in target sequence. + threshold: + The threshold used to extract alignment. + bpe_level: + Return subword-level alignment or not. + Example: + { + 'src_input_ids': LongTensor([[2478,242,24,4]]), + 'src_attention_mask': BoolTensor([[1,1,1,1]]), + 'src_b2w_map': LongTensor([[0,1,2,3]]), + 'tgt_input_ids': LongTensor([[1056,356,934,263,7]]), + 'tgt_attention_mask': BoolTensor([[1,1,1,1,1]]), + 'tgt_b2w_map': longtensor([[0,1,1,2,3]]), + 'threshold': 0.001, + 'bpe_level': False, + } + Returns `modelscope.outputs.WordAlignmentOutput` + """ + with torch.no_grad(): + src_encoder_out = self.bert( + input_ids=src_input_ids, + attention_mask=src_attention_mask.float(), + head_mask=None, + inputs_embeds=None, + output_hidden_states=True, + ) + tgt_encoder_out = self.bert( + input_ids=tgt_input_ids, + attention_mask=tgt_attention_mask.float(), + head_mask=None, + inputs_embeds=None, + output_hidden_states=True, + ) + + atten_mask_src = (1 - ( + (src_input_ids != 101) & (src_input_ids != 102) + & src_attention_mask)[:, None, None, :].float()) * -10000 + atten_mask_tgt = (1 - ( + (tgt_input_ids != 101) & (tgt_input_ids != 102) + & tgt_attention_mask)[:, None, None, :].float()) * -10000 + + src_align_out = src_encoder_out[0] + tgt_align_out = tgt_encoder_out[0] + + bpe_sim = torch.bmm(src_align_out, tgt_align_out.transpose(1, 2)) + + attention_scores_src = bpe_sim.unsqueeze(1) + atten_mask_tgt + attention_scores_tgt = bpe_sim.unsqueeze(1) + atten_mask_src.transpose( + -1, -2) + + attention_probs_src = nn.Softmax(dim=-1)(attention_scores_src) + attention_probs_tgt = nn.Softmax(dim=-2)(attention_scores_tgt) + + align_matrix = (attention_probs_src > threshold) * ( + attention_probs_tgt > threshold) + align_matrix = align_matrix.squeeze(1) + + len_src = (atten_mask_src == 0).sum(dim=-1).unsqueeze(-1) + len_tgt = (atten_mask_tgt == 0).sum(dim=-1).unsqueeze(-1) + + attention_probs_src = nn.Softmax(dim=-1)( + attention_scores_src / torch.sqrt(len_src.float())) + attention_probs_tgt = nn.Softmax(dim=-2)( + attention_scores_tgt / torch.sqrt(len_tgt.float())) + + word_aligns = [] + + for idx, (line_align, b2w_src, b2w_tgt) in enumerate( + zip(align_matrix, src_b2w_map, tgt_b2w_map)): + aligns = dict() + non_specials = torch.where(line_align) + for i, j in zip(*non_specials): + if not bpe_level: + word_pair = (src_b2w_map[idx][i - 1].item(), + tgt_b2w_map[idx][j - 1].item()) + if word_pair not in aligns: + aligns[word_pair] = bpe_sim[idx][i, j].item() + else: + aligns[word_pair] = max(aligns[word_pair], + bpe_sim[idx][i, j].item()) + else: + aligns[(i.item() - 1, + j.item() - 1)] = bpe_sim[idx][i, j].item() + word_aligns.append(aligns) + + return WordAlignmentOutput(predictions=word_aligns) diff --git a/modelscope/outputs/nlp_outputs.py b/modelscope/outputs/nlp_outputs.py index 8ed8c94a..a48e3b0e 100644 --- a/modelscope/outputs/nlp_outputs.py +++ b/modelscope/outputs/nlp_outputs.py @@ -1,5 +1,5 @@ from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import List, Optional, Tuple, Union import numpy as np @@ -328,6 +328,14 @@ class TextErrorCorrectionOutput(ModelOutputBase): predictions: np.ndarray = None +@dataclass +class WordAlignmentOutput(ModelOutputBase): + """The output class for word alignment models. + """ + + predictions: List = None + + @dataclass class TextGenerationModelOutput(ModelOutputBase): """The output class for text generation models. diff --git a/modelscope/outputs/outputs.py b/modelscope/outputs/outputs.py index d8217aa0..75d9eaa5 100644 --- a/modelscope/outputs/outputs.py +++ b/modelscope/outputs/outputs.py @@ -709,6 +709,11 @@ TASK_OUTPUTS = { # "output": "我想吃苹果" # } Tasks.text_error_correction: [OutputKeys.OUTPUT], + # word_alignment result for a single sample + # { + # "output": "0-0 1-3 2-4 3-1 4-2 5-5" + # } + Tasks.word_alignment: [OutputKeys.OUTPUT], Tasks.sentence_embedding: [OutputKeys.TEXT_EMBEDDING, OutputKeys.SCORES], Tasks.text_ranking: [OutputKeys.SCORES], diff --git a/modelscope/pipelines/nlp/__init__.py b/modelscope/pipelines/nlp/__init__.py index b4ea93be..55c98776 100644 --- a/modelscope/pipelines/nlp/__init__.py +++ b/modelscope/pipelines/nlp/__init__.py @@ -25,6 +25,7 @@ if TYPE_CHECKING: from .summarization_pipeline import SummarizationPipeline from .translation_quality_estimation_pipeline import TranslationQualityEstimationPipeline from .text_error_correction_pipeline import TextErrorCorrectionPipeline + from .word_alignment_pipeline import WordAlignmentPipeline from .text_generation_pipeline import TextGenerationPipeline, TextGenerationT5Pipeline from .fid_dialogue_pipeline import FidDialoguePipeline from .token_classification_pipeline import TokenClassificationPipeline @@ -70,6 +71,7 @@ else: ['TableQuestionAnsweringPipeline'], 'text_classification_pipeline': ['TextClassificationPipeline'], 'text_error_correction_pipeline': ['TextErrorCorrectionPipeline'], + 'word_alignment_pipeline': ['WordAlignmentPipeline'], 'text_generation_pipeline': ['TextGenerationPipeline', 'TextGenerationT5Pipeline'], 'fid_dialogue_pipeline': ['FidDialoguePipeline'], diff --git a/modelscope/pipelines/nlp/word_alignment_pipeline.py b/modelscope/pipelines/nlp/word_alignment_pipeline.py new file mode 100644 index 00000000..8513f28e --- /dev/null +++ b/modelscope/pipelines/nlp/word_alignment_pipeline.py @@ -0,0 +1,68 @@ +# Copyright (c) Alibaba, Inc. and its affiliates. + +from typing import Any, Dict, Optional, Union + +import numpy as np + +from modelscope.metainfo import Pipelines +from modelscope.models import Model +from modelscope.outputs import OutputKeys +from modelscope.pipelines.base import Pipeline +from modelscope.pipelines.builder import PIPELINES +from modelscope.preprocessors import WordAlignmentPreprocessor +from modelscope.utils.constant import Tasks + +__all__ = ['WordAlignmentPipeline'] + + +@PIPELINES.register_module( + Tasks.word_alignment, module_name=Pipelines.word_alignment) +class WordAlignmentPipeline(Pipeline): + + def __init__(self, + model: Union[Model, str], + preprocessor: WordAlignmentPreprocessor = None, + config_file: str = None, + device: str = 'gpu', + auto_collate=True, + sequence_length=128, + **kwargs): + """Use `model` and `preprocessor` to create a nlp text dual encoder then generates the text representation. + Args: + model (str or Model): Supply either a local model dir which supported the WS task, + or a model id from the model hub, or a torch model instance. + preprocessor (Preprocessor): A WordAlignmentPreprocessor. + kwargs (dict, `optional`): + Extra kwargs passed into the preprocessor's constructor. + Example: + >>> from modelscope.pipelines import pipeline + >>> from modelscope.utils.constant import Tasks + >>> model_id = 'damo/Third-Party-Supervised-Word-Aligner-mBERT-base-zhen' + >>> input = {"sentence_pair": '贝利 在 墨西哥 推出 自传 。||| pele promotes autobiography in mexico .'} + >>> pipeline_ins = pipeline(Tasks.word_alignment, model=model_id) + >>> print(pipeline_ins(input)['output']) + """ + super().__init__( + model=model, + preprocessor=preprocessor, + config_file=config_file, + device=device, + auto_collate=auto_collate) + if preprocessor is None: + self.preprocessor = WordAlignmentPreprocessor.from_pretrained( + self.model.model_dir, + sequence_length=sequence_length, + **kwargs) + + def forward(self, inputs: Dict[str, Any], + **forward_params) -> Dict[str, Any]: + return self.model(**inputs, **forward_params) + + def postprocess(self, inputs: Dict[str, Any]) -> Dict[str, Any]: + + align = [] + for k in inputs[0][0].keys(): + align.append(f'{k[0]}-{k[1]}') + align = ' '.join(align) + + return {OutputKeys.OUTPUT: align} diff --git a/modelscope/preprocessors/__init__.py b/modelscope/preprocessors/__init__.py index 3a13828b..1cd25e50 100644 --- a/modelscope/preprocessors/__init__.py +++ b/modelscope/preprocessors/__init__.py @@ -30,9 +30,9 @@ if TYPE_CHECKING: TextGenerationSentencePiecePreprocessor, TokenClassificationTransformersPreprocessor, TextErrorCorrectionPreprocessor, TextGenerationT5Preprocessor, - TextGenerationTransformersPreprocessor, Tokenize, - WordSegmentationBlankSetToLabelPreprocessor, CodeGeeXPreprocessor, - MGLMSummarizationPreprocessor, + WordAlignmentPreprocessor, TextGenerationTransformersPreprocessor, + Tokenize, WordSegmentationBlankSetToLabelPreprocessor, + CodeGeeXPreprocessor, MGLMSummarizationPreprocessor, ZeroShotClassificationTransformersPreprocessor, TextGenerationJiebaPreprocessor, SentencePiecePreprocessor, DialogIntentPredictionPreprocessor, DialogModelingPreprocessor, diff --git a/modelscope/preprocessors/nlp/__init__.py b/modelscope/preprocessors/nlp/__init__.py index 23fa9f94..f0660374 100644 --- a/modelscope/preprocessors/nlp/__init__.py +++ b/modelscope/preprocessors/nlp/__init__.py @@ -4,6 +4,7 @@ from typing import TYPE_CHECKING from modelscope.utils.import_utils import LazyImportModule if TYPE_CHECKING: + from .word_alignment_preprocessor import WordAlignmentPreprocessor from .text_error_correction import TextErrorCorrectionPreprocessor from .text_generation_preprocessor import TextGenerationJiebaPreprocessor from .bert_seq_cls_tokenizer import Tokenize @@ -66,6 +67,9 @@ else: 'text_error_correction': [ 'TextErrorCorrectionPreprocessor', ], + 'word_alignment_preprocessor': [ + 'WordAlignmentPreprocessor', + ], 'mglm_summarization_preprocessor': ['MGLMSummarizationPreprocessor'], 'token_classification_thai_preprocessor': [ 'NERPreprocessorThai', diff --git a/modelscope/preprocessors/nlp/word_alignment_preprocessor.py b/modelscope/preprocessors/nlp/word_alignment_preprocessor.py new file mode 100644 index 00000000..390deb40 --- /dev/null +++ b/modelscope/preprocessors/nlp/word_alignment_preprocessor.py @@ -0,0 +1,131 @@ +# Copyright (c) Alibaba, Inc. and its affiliates. + +import itertools +import os +import os.path as osp +from typing import Any, Dict, Optional, Union + +import numpy as np +import torch + +from modelscope.metainfo import Preprocessors +from modelscope.preprocessors.base import Preprocessor +from modelscope.preprocessors.builder import PREPROCESSORS +from modelscope.utils.constant import Fields, ModeKeys +from modelscope.utils.hub import get_model_type +from modelscope.utils.logger import get_logger +from .transformers_tokenizer import NLPTokenizer + + +@PREPROCESSORS.register_module( + Fields.nlp, module_name=Preprocessors.word_alignment) +class WordAlignmentPreprocessor(Preprocessor): + """The tokenizer preprocessor used in word alignment . + """ + + def __init__(self, + model_dir: str, + sequence_pair='sentence_pair', + mode=ModeKeys.INFERENCE, + use_fast: bool = False, + sequence_length: int = None, + **kwargs): + """The preprocessor for word alignment task. + + Args: + model_dir: The model dir used to initialize the tokenizer. + sequence_pair: The key of the sequence pair. + mode: The mode for the preprocessor. + use_fast: Use the fast tokenizer or not. + sequence_length: The max sequence length which the model supported, + will be passed into tokenizer as the 'max_length' param. + **kwargs: Extra args input. + {sequence_length: The sequence length which the model supported.} + """ + self.sequence_pair = sequence_pair + + kwargs[ + 'sequence_length'] = sequence_length if sequence_length is not None else kwargs.get( + 'max_length', 128) + self.max_length = kwargs['sequence_length'] + kwargs.pop('max_length', None) + model_type = None + + if model_dir is not None: + model_type = get_model_type(model_dir) + self.nlp_tokenizer = NLPTokenizer( + model_dir, model_type, use_fast=use_fast, tokenize_kwargs=kwargs) + super().__init__(mode=mode) + + def __call__(self, data: Dict, **kwargs) -> Dict[str, Any]: + """process the raw input data + + Args: + data Dict: + Example: + {"sentence_pair": "贝利 在 墨西哥 推出 自传 。||| pele promotes autobiography in mexico ."} + Returns: + Dict[str, Any]: the preprocessed data + """ + sentence_pair = data[self.sequence_pair] + source_sentences, target_sentences = sentence_pair.split('|||') + # src_lang = data.get("src_lang", 'en_XX') + # tgt_lang = data.get("tgt_lang", 'en_XX') + if 'return_tensors' not in kwargs: + kwargs[ + 'return_tensors'] = 'pt' if self.mode == ModeKeys.INFERENCE else None + + sent_src, sent_tgt = source_sentences.strip().split( + ), target_sentences.strip().split() + + token_src = [ + self.nlp_tokenizer.tokenizer.tokenize(word) for word in sent_src + ] + token_tgt = [ + self.nlp_tokenizer.tokenizer.tokenize(word) for word in sent_tgt + ] + wid_src = [ + self.nlp_tokenizer.tokenizer.convert_tokens_to_ids(x) + for x in token_src + ] + wid_tgt = [ + self.nlp_tokenizer.tokenizer.convert_tokens_to_ids(x) + for x in token_tgt + ] + + ids_tgt = self.nlp_tokenizer.tokenizer.prepare_for_model( + list(itertools.chain(*wid_tgt)), + return_tensors='pt', + max_length=self.max_length, + prepend_batch_axis=True)['input_ids'] + ids_src = self.nlp_tokenizer.tokenizer.prepare_for_model( + list(itertools.chain(*wid_src)), + return_tensors='pt', + max_length=self.max_length, + prepend_batch_axis=True)['input_ids'] + + bpe2word_map_src = [] + for i, word_list in enumerate(token_src): + bpe2word_map_src += [i for x in word_list] + bpe2word_map_src = torch.Tensor(bpe2word_map_src).type_as( + ids_src).view(1, -1) + bpe2word_map_tgt = [] + for i, word_list in enumerate(token_tgt): + bpe2word_map_tgt += [i for x in word_list] + bpe2word_map_tgt = torch.Tensor(bpe2word_map_tgt).type_as( + ids_tgt).view(1, -1) + attention_mask_src = ( + ids_src != self.nlp_tokenizer.tokenizer.pad_token_id) + attention_mask_tgt = ( + ids_tgt != self.nlp_tokenizer.tokenizer.pad_token_id) + + return { + 'src_input_ids': ids_src, + 'src_attention_mask': attention_mask_src, + 'src_b2w_map': bpe2word_map_src, + 'tgt_input_ids': ids_tgt, + 'tgt_attention_mask': attention_mask_tgt, + 'tgt_b2w_map': bpe2word_map_tgt, + 'threshold': 0.001, + 'bpe_level': False + } diff --git a/modelscope/utils/constant.py b/modelscope/utils/constant.py index c2d0c6f5..f289ea50 100644 --- a/modelscope/utils/constant.py +++ b/modelscope/utils/constant.py @@ -186,6 +186,7 @@ class NLPTasks(object): zero_shot_classification = 'zero-shot-classification' backbone = 'backbone' text_error_correction = 'text-error-correction' + word_alignment = 'word-alignment' faq_question_answering = 'faq-question-answering' information_extraction = 'information-extraction' document_segmentation = 'document-segmentation' From 350e76d296c9ea22fc0769dfeead02c5cd81eaaf Mon Sep 17 00:00:00 2001 From: Yingda Chen Date: Sun, 19 Feb 2023 18:43:40 +0800 Subject: [PATCH 4/4] refine readme --- README.md | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index e62059a8..c0c18433 100644 --- a/README.md +++ b/README.md @@ -30,17 +30,18 @@ # Introduction -[ModelScope]( https://www.modelscope.cn) is a “Model-as-a-Service” (MaaS) platform that seeks to bring together most advanced machine learning models from the AI community, and to streamline the process of leveraging AI models in real applications. The core ModelScope library enables developers to perform inference, training and evaluation, through rich layers of API designs that facilitate a unified experience across state-of-the-art models from different AI domains. +[ModelScope]( https://www.modelscope.cn) is built upon the notion of “Model-as-a-Service” (MaaS). It seeks to bring together most advanced machine learning models from the AI community, and streamlines the process of leveraging AI models in real-world applications. The core ModelScope library open-sourced in this repository provides the interfaces and implementations that allow developers to perform model inference, training and evaluation. -The Python library offers the layered-APIs necessary for model contributors to integrate models from CV, NLP, Speech, Multi-Modality, as well as Scientific-computation, into the ModelScope ecosystem. Implementations for all these different models are encapsulated within the library in a way that allows easy and unified access. With such integration, model inference, finetuning, and evaluations can be done with only a few lines of codes. In the meantime, flexibilities are provided so that different components in the model applications can be customized as well, where necessary. -Apart from harboring implementations of various models, ModelScope library also enables the necessary interactions with ModelScope backend services, particularly with the Model-Hub and Dataset-Hub. Such interactions facilitate management of various entities (models and datasets) to be performed seamlessly under-the-hood, including entity lookup, version control, cache management, and many others. +In particular, with rich layers of API-abstraction, the ModelScope library offers unified experience to explore state-of-the-art models spanning across domains such as CV, NLP, Speech, Multi-Modality, and Scientific-computation. Model contributors of different areas can integrate models into the ModelScope ecosystem through the layered-APIs, allowing easy and unified access to their models. Once integrated, model inference, fine-tuning, and evaluations can be done with only a few lines of codes. In the meantime, flexibilities are also provided so that different components in the model applications can be customized wherever necessary. -# Models and Online Demos +Apart from harboring implementations of a wide range of different models, ModelScope library also enables the necessary interactions with ModelScope backend services, particularly with the Model-Hub and Dataset-Hub. Such interactions facilitate management of various entities (models and datasets) to be performed seamlessly under-the-hood, including entity lookup, version control, cache management, and many others. -Hundreds of models are made publicly available on ModelScope (600+ and counting), covering the latest development in areas such as NLP, CV, Audio, Multi-modality, and AI for Science, etc. Many of these models represent the SOTA in the fields, and made their open-sourced debut on ModelScope. Users can visit ModelScope([modelscope.cn](http://www.modelscope.cn)) and experience first-hand how these models perform via online experience, with just a few clicks. Immediate developer-experience is also possible through the ModelScope Notebook, which is backed by ready-to-use cloud CPU/GPU development environment, and is only a click away on ModelScope website. +# Models and Online Accessibility -Some of the representative examples include: +Hundreds of models are made publicly available on [ModelScope]( https://www.modelscope.cn) (600+ and counting), covering the latest development in areas such as NLP, CV, Audio, Multi-modality, and AI for Science, etc. Many of these models represent the SOTA in their specific fields, and made their open-sourced debut on ModelScope. Users can visit ModelScope([modelscope.cn](http://www.modelscope.cn)) and experience first-hand how these models perform via online experience, with just a few clicks. Immediate developer-experience is also possible through the ModelScope Notebook, which is backed by ready-to-use CPU/GPU development environment in the cloud - only one click away on [ModelScope](https://www.modelscope.cn). + +Some representative examples include: NLP: @@ -112,9 +113,9 @@ AI for Science: # QuickTour -We provide unified interface for inference using `pipeline`, finetuning and evaluation using `Trainer` for different tasks. +We provide unified interface for inference using `pipeline`, fine-tuning and evaluation using `Trainer` for different tasks. -For any given task with any type of input (image, text, audio, video...), inference pipeline can be implemented with only a few lines of code, which will automatically load the associated model to get inference result, as is exemplified below: +For any given task with any type of input (image, text, audio, video...), inference pipeline can be implemented with only a few lines of code, which will automatically load the underlying model to get inference result, as is exemplified below: ```python >>> from modelscope.pipelines import pipeline @@ -123,7 +124,7 @@ For any given task with any type of input (image, text, audio, video...), infere {'output': '今天 天气 不错 , 适合 出去 游玩'} ``` -Given an image, you can use following code to cut out the human. +Given an image, portrait matting (aka. background-removal) can be accomplished with the following code snippet: ![image](https://resouces.modelscope.cn/document/docdata/2023-2-16_20:53/dist/ModelScope%20Library%E6%95%99%E7%A8%8B/resources/1656989748829-9ab3aa9b-461d-44f8-98fb-c85bc6f670f9.png) @@ -135,13 +136,14 @@ Given an image, you can use following code to cut out the human. >>> result = portrait_matting('https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/image_matting.png') >>> cv2.imwrite('result.png', result['output_img']) ``` -The output image is + +The output image with the background removed is: ![image](https://resouces.modelscope.cn/document/docdata/2023-2-16_20:53/dist/ModelScope%20Library%E6%95%99%E7%A8%8B/resources/1656989768092-5470f8ac-cda8-4703-ac98-dbb6fd675b34.png) -For finetuning and evaluation, you need ten more lines of code to construct dataset and trainer, and by calling `traner.train()` and -`trainer.evaluate()` you can finish finetuning and evaluating a certain model. +Fine-tuning and evaluation can also be done with a few more lines of code to set up training dataset and trainer, with the heavy-lifting work of training and evaluation a model encapsulated in the implementation of `traner.train()` and +`trainer.evaluate()` interfaces. -For example, we use the gpt3 1.3B model to load the chinese poetry dataset and finetune the model, the resulted model can be used for poetry generation. +For example, the gpt3 base model (1.3B) can be fine-tuned with the chinese-poetry dataset, resulting in a model that can be used for chinese-poetry generation. ```python >>> from modelscope.metainfo import Trainers @@ -166,20 +168,20 @@ For example, we use the gpt3 1.3B model to load the chinese poetry dataset and f # Why should I use ModelScope library -1. A unified and concise user interface is abstracted for different tasks and different models. Three lines of code complete the inference, and 10 lines of code complete the model training. It is convenient for users to use different models in multiple fields in the ModelScope community. It is ready to use and easy to get started with AI. and teaching. +1. A unified and concise user interface is abstracted for different tasks and different models. Model inferences and training can be implemented by as few as 3 and 10 lines of code, respectively. It is convenient for users to explore models in different fields in the ModelScope community. All models integrated into ModelScope are ready to use, which makes it easy to get started with AI, in both educational and industrial settings. -2. Construct a model-centric development and application experience, support model training, inference, export and deployment, and facilitate users to build their own MLOps based on the ModelScope Library. +2. ModelScope offers a model-centric development and application experience. It streamlines the support for model training, inference, export and deployment, and facilitates users to build their own MLOps based on the ModelScope ecosystem. -3. For the model inference and training process, a modular design is carried out, and a wealth of functional module implementations are provided, which is convenient for users to customize development to customize their own model inference, training and other processes. +3. For the model inference and training process, a modular design is put in place, and a wealth of functional module implementations are provided, which is convenient for users to customize their own model inference, training and other processes. 4. For distributed model training, especially for large models, it provides rich training strategy support, including data parallel, model parallel, hybrid parallel and so on. # Installation ## Docker -ModelScope Library currently supports tensorflow and pytorch deep learning framework for model training and inference, and it is tested and run on Python 3.7+, Pytorch 1.8+, Tensorflow1.15 or Tensorflow2.0+. +ModelScope Library currently supports popular deep learning framework for model training and inference, including PyTorch, TensorFlow and ONNX. All releases are tested and run on Python 3.7+, Pytorch 1.8+, Tensorflow1.15 or Tensorflow2.0+. -In order to allow everyone to directly use all the models on the ModelScope platform without configuring the environment, ModelScope provides official docker image for developers who need it. Based on the official image, you can skip all environment installation and configuration and use it directly. Currently, the latest version of the CPU image and GPU image we provide can be obtained from the following address +To allow out-of-box usage for all the models on ModelScope, official docker images are provided for all releases. Based on the docker image, developers can skip all environment installation and configuration and use it directly. Currently, the latest version of the CPU image and GPU image can be obtained from: CPU docker image ```shell @@ -193,20 +195,20 @@ registry.cn-hangzhou.aliyuncs.com/modelscope-repo/modelscope:ubuntu20.04-cuda11. ## Setup Local Python Environment -Also you can setup your local python environment using pip and conda. We suggest to use [anaconda](https://docs.anaconda.com/anaconda/install/) to create your python environment: +One can also set up local ModelScope environment using pip and conda. We suggest [anaconda](https://docs.anaconda.com/anaconda/install/) for creating local python environment: ```shell conda create -n modelscope python=3.7 conda activate modelscope ``` -Then you can install pytorch or tensorflow according to your model requirements. +PyTorch or TensorFlow can be installed separately according to each model's requirements. * Install pytorch [doc](https://pytorch.org/get-started/locally/) * Install tensorflow [doc](https://www.tensorflow.org/install/pip) -After installing the necessary framework, you can install modelscope library as follows: +After installing the necessary machine-learning framework, you can install modelscope library as follows: -If you only want to download models and datasets, install modelscope framework +If you only want to play around with the modelscope framework, of trying out model/dataset download, you can install the core modelscope components: ```shell pip install modelscope ``` @@ -237,7 +239,7 @@ pip install modelscope[science] -f https://modelscope.oss-cn-beijing.aliyuncs.co ``` `Notes`: -1. Currently, some audio-task models only support python3.7, tensorflow1.15.4 Linux environments. Most other models can be installed and used on windows and Mac (x86). +1. Currently, some audio-task models only support python3.7, tensorflow1.15.4 Linux environments. Most other models can be installed and used on Windows and Mac (x86). 2. Some models in the audio field use the third-party library SoundFile for wav file processing. On the Linux system, users need to manually install libsndfile of SoundFile([doc link](https://github.com/bastibe/python-soundfile#installation)). On Windows and MacOS, it will be installed automatically without user operation. For example, on Ubuntu, you can use following commands: ```shell