2022-05-30 11:53:53 +08:00
|
|
|
|
# 快速开始
|
|
|
|
|
|
|
2022-05-31 11:49:46 +08:00
|
|
|
|
## python环境配置
|
|
|
|
|
|
首先,参考[文档](https://docs.anaconda.com/anaconda/install/) 安装配置Anaconda环境
|
2022-05-30 11:53:53 +08:00
|
|
|
|
|
2022-05-31 11:49:46 +08:00
|
|
|
|
安装完成后,执行如下命令为maas library创建对应的python环境。
|
2022-05-30 11:53:53 +08:00
|
|
|
|
```shell
|
2022-06-09 20:16:26 +08:00
|
|
|
|
conda create -n modelscope python=3.6
|
|
|
|
|
|
conda activate modelscope
|
2022-05-30 11:53:53 +08:00
|
|
|
|
```
|
2022-05-31 11:49:46 +08:00
|
|
|
|
检查python和pip命令是否切换到conda环境下。
|
|
|
|
|
|
```shell
|
|
|
|
|
|
which python
|
2022-06-09 20:16:26 +08:00
|
|
|
|
# ~/workspace/anaconda3/envs/modelscope/bin/python
|
2022-05-31 11:49:46 +08:00
|
|
|
|
|
|
|
|
|
|
which pip
|
2022-06-09 20:16:26 +08:00
|
|
|
|
# ~/workspace/anaconda3/envs/modelscope/bin/pip
|
2022-05-31 11:49:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
注: 本项目只支持`python3`环境,请勿使用python2环境。
|
|
|
|
|
|
|
|
|
|
|
|
## 第三方依赖安装
|
|
|
|
|
|
|
2022-06-09 20:16:26 +08:00
|
|
|
|
ModelScope Library目前支持tensorflow,pytorch两大深度学习框架进行模型训练、推理, 在Python 3.6+, Pytorch 1.8+, Tensorflow 2.6上测试可运行,用户可以根据所选模型对应的计算框架进行安装,可以参考如下链接进行安装所需框架:
|
2022-05-31 11:49:46 +08:00
|
|
|
|
|
|
|
|
|
|
* [Pytorch安装指导](https://pytorch.org/get-started/locally/)
|
|
|
|
|
|
* [Tensorflow安装指导](https://www.tensorflow.org/install/pip)
|
|
|
|
|
|
|
2022-05-30 11:53:53 +08:00
|
|
|
|
|
2022-06-09 20:16:26 +08:00
|
|
|
|
## ModelScope library 安装
|
2022-05-31 11:49:46 +08:00
|
|
|
|
|
|
|
|
|
|
注: 如果在安装过程中遇到错误,请前往[常见问题](faq.md)查找解决方案。
|
|
|
|
|
|
|
|
|
|
|
|
### pip安装
|
|
|
|
|
|
```shell
|
2022-06-09 20:16:26 +08:00
|
|
|
|
pip install -r http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs.com/release/maas/modelscope.txt
|
2022-05-31 11:49:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
安装成功后,可以执行如下命令进行验证安装是否正确
|
|
|
|
|
|
```shell
|
2022-06-09 20:16:26 +08:00
|
|
|
|
python -c "from modelscope.pipelines import pipeline;print(pipeline('image-matting',model='damo/image-matting-person')('http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs.com/data/test/maas/image_matting/test.png'))"
|
2022-05-31 11:49:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-06-01 10:20:53 +08:00
|
|
|
|
### 使用源码安装
|
2022-05-31 11:49:46 +08:00
|
|
|
|
|
|
|
|
|
|
适合本地开发调试使用,修改源码后可以直接执行
|
2022-05-30 11:53:53 +08:00
|
|
|
|
```shell
|
2022-06-09 20:16:26 +08:00
|
|
|
|
git clone git@gitlab.alibaba-inc.com:Ali-MaaS/MaaS-lib.git modelscope
|
2022-05-31 11:49:46 +08:00
|
|
|
|
git fetch origin master
|
|
|
|
|
|
git checkout master
|
2022-05-30 11:53:53 +08:00
|
|
|
|
|
2022-06-09 20:16:26 +08:00
|
|
|
|
cd modelscope
|
2022-05-30 11:53:53 +08:00
|
|
|
|
|
|
|
|
|
|
#安装依赖
|
|
|
|
|
|
pip install -r requirements.txt
|
|
|
|
|
|
|
|
|
|
|
|
# 设置PYTHONPATH
|
|
|
|
|
|
export PYTHONPATH=`pwd`
|
|
|
|
|
|
```
|
|
|
|
|
|
|
2022-05-31 11:49:46 +08:00
|
|
|
|
安装成功后,可以执行如下命令进行验证安装是否正确
|
|
|
|
|
|
```shell
|
2022-06-09 20:16:26 +08:00
|
|
|
|
python -c "from modelscope.pipelines import pipeline;print(pipeline('image-matting',model='damo/image-matting-person')('http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs.com/data/test/maas/image_matting/test.png'))"
|
2022-05-31 11:49:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
2022-05-30 11:53:53 +08:00
|
|
|
|
|
|
|
|
|
|
## 训练
|
|
|
|
|
|
|
|
|
|
|
|
to be done
|
|
|
|
|
|
|
|
|
|
|
|
## 评估
|
|
|
|
|
|
|
|
|
|
|
|
to be done
|
|
|
|
|
|
|
|
|
|
|
|
## 推理
|
|
|
|
|
|
|
2022-05-31 11:49:46 +08:00
|
|
|
|
pipeline函数提供了简洁的推理接口,示例如下, 更多pipeline介绍和示例请参考[pipeline使用教程](tutorials/pipeline.md)
|
2022-05-30 11:53:53 +08:00
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
|
import cv2
|
2022-05-31 11:49:46 +08:00
|
|
|
|
import os.path as osp
|
2022-06-09 20:16:26 +08:00
|
|
|
|
from modelscope.pipelines import pipeline
|
|
|
|
|
|
from modelscope.utils.constant import Tasks
|
2022-05-30 11:53:53 +08:00
|
|
|
|
|
|
|
|
|
|
# 根据任务名创建pipeline
|
2022-06-01 10:20:53 +08:00
|
|
|
|
img_matting = pipeline(Tasks.image_matting, model='damo/image-matting-person')
|
2022-05-30 11:53:53 +08:00
|
|
|
|
|
2022-06-01 10:20:53 +08:00
|
|
|
|
# 直接提供图像文件的url作为pipeline推理的输入
|
2022-05-30 11:53:53 +08:00
|
|
|
|
result = img_matting(
|
2022-05-31 11:49:46 +08:00
|
|
|
|
'http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs.com/data/test/maas/image_matting/test.png'
|
|
|
|
|
|
)
|
2022-05-30 11:53:53 +08:00
|
|
|
|
cv2.imwrite('result.png', result['output_png'])
|
2022-06-01 10:20:53 +08:00
|
|
|
|
print(f'Output written to {osp.abspath("result.png")}')
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
此外,pipeline接口也能接收Dataset作为输入,上面的代码同样可以实现为
|
2022-06-08 18:29:39 +08:00
|
|
|
|
|
2022-06-01 10:20:53 +08:00
|
|
|
|
```python
|
|
|
|
|
|
import cv2
|
|
|
|
|
|
import os.path as osp
|
2022-06-09 20:16:26 +08:00
|
|
|
|
from modelscope.pipelines import pipeline
|
|
|
|
|
|
from modelscope.utils.constant import Tasks
|
|
|
|
|
|
from modelscope.pydatasets import PyDataset
|
2022-06-01 10:20:53 +08:00
|
|
|
|
|
|
|
|
|
|
# 使用图像url构建PyDataset,此处也可通过 input_location = '/dir/to/images' 来使用本地文件夹
|
|
|
|
|
|
input_location = [
|
|
|
|
|
|
'http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs.com/data/test/maas/image_matting/test.png'
|
|
|
|
|
|
]
|
|
|
|
|
|
dataset = PyDataset.load(input_location, target='image')
|
|
|
|
|
|
img_matting = pipeline(Tasks.image_matting, model='damo/image-matting-person')
|
|
|
|
|
|
# 输入为PyDataset时,输出的结果为迭代器
|
|
|
|
|
|
result = img_matting(dataset)
|
|
|
|
|
|
cv2.imwrite('result.png', next(result)['output_png'])
|
|
|
|
|
|
print(f'Output written to {osp.abspath("result.png")}')
|
2022-05-31 11:49:46 +08:00
|
|
|
|
```
|