本文选择使用 SAP Leonardo 里的 一个Product Image Classification API,即给定一张产品的图片,该 API 能识别出此产品的类别。
简要了解一下这个 API 的功能:该API 模型是由 SAP 基于大约5万张 Icecat 图片训练而成,能区分29种不同的类别,这些类别具体罗列于官方文档上,比如电脑显示器,数码相机,外部存储设备,键盘,液晶电视,手机充电器,笔记本和其他外设等等。如果我们消费这个API时指定的图片代表的产品不属于这 29 种类别之一,API 的表现如何?先卖个关子,文末解答。

这里假定我们已经找到了该 API,点击进入明细页面,将 API Key复制下来,后续的UI5应用需要使用到。


然后进入SAP云平台的 Neo环境。本文介绍的步骤,使用免费的SAP Cloud Platform Neo测试环境即可。

在Service列表里找到 WebIDE——我们将使用WebIDE进行UI5应用的开发。

大家可以直接在 WebIDE 里 clone 这个 Github 仓库,或者把仓库的内容以zip包的形式下载到本地,再使用WebIDE的本地Import功能导入。


我们要告诉 UI5应用这个API的url,因此在Neo环境里创建一个Destination(作用和ABAP Netweaver事务码SM59里创建的Destination相同):

属性如上图所示,因为是Neo测试环境,所以url为对应的sandbox环境:
https://sandbox.api.sap.com/ml
记下这个Destination名称 sapui5ml-api, 因为稍后的 UI5 代码里需要使用。
记得维护额外的属性WebIDEnabled为true,这样该Destination才能在UI5应用里被使用。点击Check Connection确保看到绿灯。

打开WebIDE里UI5工程里的settings.json文件,将您之前从API console里拷贝的API Key粘贴到此处:

在项目根目录下的neo-app.json文件里,把类型为destination的target对象的名称维护成之前在SAP Cloud Platform Cockpit里创建的destination相同的名称。

运行这个UI5应用,能看到如下界面:

做一些简单的测试:

SAP Leonardo的机器学习API,识别出这张图片有74.7%的可能性是一台笔记本电脑,13.8%的可能性是键盘,11.3%的可能性是Tablets.

点击按钮 View JSON,能看到调用 SAP Leonardo API 返回的技术明细。

鼠标的图片也成功识别出来了:

本文开头提到,Product Image Classification API 只支持29种产品类别:

如果我们开发应用时需要支持额外的产品类别,就需要自行提供该产品类别的图片来重新训练模型。

下面介绍SAP Leonardo上机器学习模型的重新训练步骤。
假设我们期望重新训练之后,Product Image Classfication这个模型能够识别出不同种类的花,那么我们首先得搞到大量花的图片。Tensorflow的官网上,已经体贴地给想做模型训练的学习者们,提供了一个做练习用的压缩包,里面包含了大量各式花的图片。
SAP Leonardo接受的能用于重新训练模型的数据集,必须符合下图所示的层级结构,即training, validation和test三个文件夹下面,分别包含以产品类别命名的子文件夹,且数据规模之比为8:1:1.

有了用于训练的数据后,下一步就是把这些数据上传到SAP Leonardo的模型在线存储平台上。
在 SAP 云平台上创建 Leonardo 机器学习的服务实例,这个实例的 service key 里包含了一个IMAGE_RETRAIN_API_URL,可以用来获取在线存储的url:

向这个url发送一个HTTP get请求,得到在线存储的url:

把这个url粘贴到浏览器里,输入postman里返回的accessKey和secretKey登录,就能以web的方式访问这个在线存储了:

下一步是把本地的训练文件上传到这个部署在AWS上的在线存储上去。
首先用命令行 mc config host
定义一个名为sapjerrys3的远程站点,将上一步从postman获得的AWS在线存储url,accessKey和secret绑定到这个站点上:

然后使用命令行上传文件:
mc.exe cp -r C:\Code\MachineLearningStudy\flowersjerry sapjerrys3\data
大概十几分钟后,文件上传完毕:

此时可以从浏览器里看到AWS在线存储上传完毕的训练文件。

现在可以提交一个后台作业了,让Leonardo去处理这些上传好的文件,ABAP顾问们可以把这个动作理解成在Netweaver事务码SM36里定义一个后台作业并提交。发送一个HTTP post请求,除了下图jobName, dataset和modelName需要自己维护外,其他字段都使用SAP官网上定义的默认值。

这个请求会返回一个后台作业ID,抄下来后把它拼到url末尾,然后重新发送一个HTTP get请求,即可查询到这个作业的执行情况。Jerry重新训练的时候,等待了大概五分钟,作业状态就变为SUCCEEDED了。

因为本文做的练习都是在SAP Cloud Platform的CloudFoundry环境中进行的,因此我们也可以用cf命令行来查询这些作业的执行情况:
cf sapml retraining jobs -m image

如果遇到作业状态为FAILED的情况,去AWS在线存储上查看以作业名称命名的文件夹,里面包含了详细的训练日志,可以用作错误分析:

在这个训练好的模型能正式被使用之前,我们还需要对其进行部署,类似ABAP Netweaver里的“激活”动作。

和提交训练的后台作业类似,模型部署也是一个异步执行的步骤,提交部署请求后,得到一个部署作业ID:ms-26c5a22c-6d07-4164-8222-a4182969162d

根据这个部署作业ID可以查询模型部署状态:

成功部署后,我们就可以用Restful API消费这个模型了,url的格式为:
https://mlfinternalproduction-image-classifier.cfapps.sap.hana.ondemand.com/api/v2/image/classification/models/<model name>/versions/1
我从网络上随便找一张向日葵的照片,

将这张图片作为HTTP POST的参数发给我重新训练并且部署好的模型flowerjerrymodel,得到的结果显示,重新训练后的模型认为这张图片有大约87%的可能性是代表向日葵。

至此,这个机器模型重新训练和消费的步骤就成功完成了。
总结
本文首先介绍了使用 SAP UI5 前端框架开发的 Web 应用如何通过 Restful API 的方式消费 SAP Leonardo 平台提供的机器学习服务,然后以向日葵模型为例,介绍了 Leonardo 平台上机器学习模型重新训练和消费的详细步骤。
网友评论