直接使用
请打开使用EAS Python SDK完成模型部署,并点击右上角 “ 在DSW中打开” 。
概述
针对在线推理场景,PAI平台提供了在线预测服务PAI-EAS(Elastic Algorithm Service),支持基于异构硬件(CPU和GPU)的模型加载和数据请求的实时响应。通过PAI-EAS,您可以将模型快速部署为RESTful API,再通过HTTP请求的方式调用该服务。
同时,您可以使用EAS提供的命令工具和Python SDK来管理您的模型服务。本文为您介绍如何使用Python来创建服务、查看服务列表。以及,使用EAS的预测SDK,来用代码调用EAS服务。
前提条件
试用本文档介绍的内容前,请先确认您的账号满足以下条件:
- 开通PAI-EAS(同时会拥有公共资源组),详情见:《开通及购买》
- 已获取阿里云账户的AccessKey ID和AccessKey Secret,详情请参见获取AccessKey。
- 当前使用的JupyterLab环境,安装了 eascmd,安装步骤可参考:下载并认证客户端(如果您使用PAI-DSW,初始镜像中已预置该CLI)
- 因示例代码中用到了一些python库,请按需决定是否执行以下python包的安装命令
部署前准备工作
步骤一:安装示例需要的Python包
# !pip install tensorflow tensorflow_datasets # !pip install opencv-python !pip install eas-prediction alibabacloud_eas20210701==1.1.2 --upgrade !pip install tensorflow_datasets
步骤二:训练并产出一个模型
我们参考tensorflow的基础示例(https://www.tensorflow.org/datasets/keras_example ),来训练一个模型,以便演示EAS的使用流程。
import tensorflow as tf import tensorflow_datasets as tfds (ds_train, ds_test), ds_info = tfds.load( 'mnist', split=['train', 'test'], data_dir='./cached_datasets', shuffle_files=True, as_supervised=True, with_info=True, ) def normalize_img(image, label): """Normalizes images: `uint8` -> `float32`.""" return tf.cast(image, tf.float32) / 255., label ds_train = ds_train.map( normalize_img) ds_train = ds_train.cache() ds_train = ds_train.shuffle(ds_info.splits['train'].num_examples) ds_train = ds_train.batch(128) ds_train = ds_train.prefetch(10) ds_test = ds_test.map(normalize_img) ds_test = ds_test.batch(128) ds_test = ds_test.cache() ds_test = ds_test.prefetch(10)
model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(input_shape=(28, 28)), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dense(10) ]) model.compile( optimizer=tf.keras.optimizers.Adam(0.001), loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=[tf.keras.metrics.SparseCategoricalAccuracy()], ) model.fit( ds_train, epochs=6, validation_data=ds_test, ) model.save('./eas_demo_output3')
至此,我们已经训练了一个简单的tensorflow模型,并输出到目录:./eas_demo_output3/
下一步,我们会将这个模型部署到EAS,并验证服务的正常。
部署模型到EAS(基于EAS Python SDK)
PAI-EAS提供了eascmd客户端管理工具,可以对服务进行创建、修改、切换、删除等操作,同时也提供了Python SDK供编程使用,当然它们的后端都是EAS的Open API。
步骤一:创建EAS的客户端对象
from alibabacloud_eas20210701.client import Client as eas20210701Client from alibabacloud_tea_openapi import models as open_api_models from alibabacloud_eas20210701 import models as eas_20210701_models from alibabacloud_tea_util import models as util_models from alibabacloud_tea_util.client import Client as UtilClient from alibabacloud_eas20210701.models import (ListServicesRequest, CreateServiceRequest) access_key_id = "*您的阿里云账户的Key ID*" access_key_secret = "*您的阿里云装好的Key Secret*" import os.path config_path="/mnt/data/pai.config" if os.path.isfile(config_path): with open(config_path) as f: access_key_id = f.readline().strip('\n') access_key_secret = f.readline().strip('\n') config = open_api_models.Config( access_key_id=access_key_id, access_key_secret=access_key_secret ) # 访问的域名 region = "cn-beijing" config.endpoint = f'pai-eas.{region}.aliyuncs.com' eas_client = eas20210701Client(config)
步骤二:准备模型文件
EAS支持从您的OSS Bucket中读取模型文件,前提是完成对PAI EAS的授权。
# 按需要拷贝模型目录到OSS上 # !mkdir /mnt/data_oss/my_model_dir # !cp -r ./eas_demo_output3/* /mnt/data_oss/my_model_dir/
步骤三:创建EAS服务
import json resource_config = { "instance": 1, "memory": 7000, "cpu": 4} model_path = "oss://dlcyolo/dlc_demo/dsw_demo_model/" service_config = {"name": "service_from_dsw", "model_path": model_path, "processor": "tensorflow_cpu_2.4", "metadata": resource_config} print(json.dumps(service_config)) service1 = eas_client.create_service(CreateServiceRequest(body=json.dumps(service_config))).body print(service1)
步骤四:查看EAS服务状态
service2 = eas_client.describe_service(cluster_id='cn-beijing', service_name=service1.service_name).body print(service2.status)
调用EAS服务进行预测
步骤一:创建测试样本
# import tensorflow.compat.v2 as tf import tensorflow_datasets as tfds import matplotlib.pyplot as plt import numpy as np # Construct a tf.data.Dataset ds = tfds.load('mnist', split='train', data_dir='./cached_datasets', shuffle_files=False) # Build your input pipeline ds = ds.shuffle(1024).take(3) target = [] for example in ds.take(1): image, label = example['image'], example['label'] print(label) target = np.reshape(image, 784) plt.imshow(tf.squeeze(image)) plt.show()
tf.Tensor(6, shape=(), dtype=int64)
步骤二:进行在线预测
使用eas_prediction,调用已部署的服务。详细信息请参考公网地址调用。
查看所有服务:
res = eas_client.list_services(ListServicesRequest()) for s in res.body.services: print(s.service_name, s.status)
mnist_first Running mnist_first2 Stopped service_from_dsw Running
选取一个服务做预测:
service3 = eas_client.describe_service(cluster_id='cn-beijing', service_name='service_from_dsw').body print(service3)
构造PredictClient对象来调用服务:
from eas_prediction import PredictClient, TFRequest import urllib
client = PredictClient(urllib.parse.urlsplit(service3.internet_endpoint).hostname, service3.service_name) client.set_token(service3.access_token) client.init() req = TFRequest('serving_default') # signature_name 参数:serving_default req.add_feed('flatten_1_input', [1, 28, 28], TFRequest.DT_FLOAT, target) resp = client.predict(req) print((resp.response.outputs).keys)