模型地址
操作步骤
1、创建DSW实例
镜像版本选择:dsw-registry-vpc.cn-hangzhou.cr.aliyuncs.com/pai/modelscope:1.13.3-pytorch2.1.2tensorflow2.14.0-gpu-py310-cu121-ubuntu22.04
2、代码快速测试模型功能
import cv2 from modelscope.outputs import OutputKeys from modelscope.pipelines import pipeline from modelscope.utils.constant import Tasks img_cartoon = pipeline(Tasks.image_portrait_stylization, model='damo/cv_unet_person-image-cartoon_compound-models') img_path = 'https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/image_cartoon.png' result = img_cartoon(img_path) cv2.imwrite('result.png', result[OutputKeys.OUTPUT_IMG]) print('finished!')
3、Python SDK部署模型
# 安装PAI Python SDK pip install alipai>=0.4.3 # 配置阿里云相关服务信息 python -m pai.toolkit.config from pai.common.utils import random_str from pai.session import get_default_session from pai.modelscope.model import ModelScopeModel # 获取阿里云PAI平台相关默认配置,如遇到配置问题,请参考 # https://help.aliyun.com/zh/pai/user-guide/install-and-configure-pai-python-sdk sess = get_default_session() # ModelScope模型信息:https://www.modelscope.cn/models/lskhh/moran-cv_unet_person-image-cartoon-genshin_compound-models/files hub = { "MODEL_ID": "lskhh/moran-cv_unet_person-image-cartoon-genshin_compound-models", "TASK": "image-portrait-stylization", "MODEL_VERSION": "v1.0.5", "DEPLOY_ENV": "eas", } # 创建 ModelScopeModel 对象 m = ModelScopeModel( command="python -u /usr/src/app/app.py", port=9000, image_uri=f"eas-registry-vpc.{sess.region_id}.cr.aliyuncs.com/pai-eas/modelscope-inference:eas-deploy-common-v17.3.2", environment_variables=hub ) # 部署服务到阿里云PAI平台 # 请注意,部署服务会产生EAS费用账单 p = m.deploy( service_name=f"taro_ms_serving_{random_str(6)}", # 设置模型服务的名称 instance_type="ecs.gn6v-c8g1.2xlarge", # 额外的服务部署配置,如指定服务端超时时间、配置额外硬盘空间等 options={ "metadata.rpc.keepalive": 300000, "features.eas.aliyun.com/extra-ephemeral-storage":"30Gi", }, )
4、控制台查看模型部署情况
5、EAS SDK调用服务
安装包:pip install -U eas-prediction --user
import requests import base64 import json from eas_prediction import PredictClient, TFRequest from eas_prediction import StringRequest # http://12825281********.cn-hangzhou.pai-eas.aliyuncs.com/api/predict/taro_ms_serving_ydv5hf client = PredictClient('http://12825281********.cn-hangzhou.pai-eas.aliyuncs.com', 'taro_ms_serving_ydv5hf') client.set_token('NDE0ZTk3N2UxNTI0ZmZhNDkzMzE0MGQ2ZjFiZD***************==') client.init() # 读取网页图片 url = "https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/image_cartoon.png" resp = requests.get(url) image_data = resp.content base64_data = base64.b64encode(image_data).decode('utf-8') data = { 'input': { 'image': base64_data } } req = StringRequest(json.dumps(data)) # json格式化request body,否则请求出错 res = client.predict(req) result = json.loads(res.response_data.decode()) result_img = base64.b64decode(result['Data']['output_img'])
保存图片
!pip install Pillow from PIL import Image import io # 假设 binary_data 是包含图片二进制数据的变量 binary_data = result_img # 使用 io.BytesIO 将二进制数据转换成文件对象 image_file = io.BytesIO(binary_data) # 用 PIL 打开这个文件对象 image = Image.open(image_file) # 可以选择在这里对图片进行任何处理,如缩放、裁剪等 # 保存为 PNG 格式的文件 image.save('output1.png', format='PNG')