Offer to come, dig friends take it! I am participating in the 2022 Spring Recruitment series -Debug recording task, click to view the details of the activity.

The author has worked in SAP Chengdu Research Institute for more than 15 years, and has participated in the project development and prototype verification of AI services based on SAP Business Technology Platform(Hereinafter referred to as SAP BTP). This article will share the experience of these AI projects, hoping that AI experts in the community will not hesitate to give advice.

SAP BTP integrates intelligent enterprise applications with database and data management, analytics, integration and extension capabilities into one platform for both cloud and hybrid environments, including hundreds of pre-built integrations for SAP and third-party applications. One of them is SAP AI Business Services.

How do I consume SAP AI services in a Java application

The background of the project is as follows: Develop a Java program that allows a user to upload an image. The Java program calls the SAP AI API, which recognizes the image using a pre-trained machine learning model and returns a text response telling the user the result.

The following are the concrete implementation steps.

Visit api.sap.com and click on API:

Select SAP Leonardo Machine Learning – Functional Services:

Functional Services category select Image recognition AI service, namely Product Image Classification API:

The API model was trained by SAP based on about 50,000 Icecat images and can distinguish between 29 different product categories, which can be found on official documentation, such as computer monitors, digital cameras, external storage devices, keyboards, LCD TVS, phone chargers, laptops and other peripherals.

You can open the technical specification page of the image recognition API, including the API Model Schema introduction, that is, after calling the API, the returned response structure contains the field names and data types.

The interface also includes a small API call console, where you can select to upload a local graphics file and click the Try It Out button to experience the EFFECTS of the API.

Use the image below to test:

Upon seeing the request processed successfully in the console, the API determines that there is a 97% chance that the image is a Notebook.

After the API test passes, the next step is to consume it in a Java program. In the upper right corner of the API console, there is a button to Download the SDK. Click to Download the SDK locally. You can see that the SDK is a Java project based on Gradle. So we need to download Maven and Gradle and complete the environment variable configuration.

With the Java development environment in place, import the SDK into Eclipse using Eclipse’s import function. After the import is completed, it is shown as follows: the red area is the code of AI Service SDK, and the blue area is the code manually created by the author, which is used to call API and print results.

Edit pom. XML in the root directory to maintain the following dependencies:

Com.sap.apibhub. SDK, version 1.0.0

Run the Maven command MVN install in the root directory of the project to ensure that the project is successfully built. At this point, you are ready to write Java code to invoke the AI API using the SAP AI SDK.

The SDK encapsulates the details of sending the underlying HTTP request and parsing the response, making it very simple to use. The inferenceSyncPost function on line 15 receives a local File object and then sends an HTTP request to the SAP AI Service Endpoint.

The API key in line 8 is available from the API console:

Execute the Java application, print the output on the Eclipse console, and the AI Service determines that there is a 97% chance that the image is a Notbook:

How to consume SAP AI services in Web applications

Log in to SAP cloud platform and open WebIDE:

In order to avoid cross-domain problems, you need to create a Destination in the cloud platform, which is similar to the Destination created in ABAP Netweaver transaction code SM59. All HTTP requests and responses go through this Destination.

The properties are shown in the figure above, and the URL is maintained for the corresponding Sandbox environment: sandbox.api.sap.com/ml

Make a note of the name of the Destination sapUI5ml-api because it will be used later in the JavaScript code for your Web application.

Maintain the additional WebIDEnabled attribute to true so that the Destination can be used in the WebIDE application environment. Click Check Connection to ensure that a green light is displayed indicating that the Connection between SAP cloud platform Destination and AI Service Endpoint is available.

Open settings.json in WebIDE and paste the API Key you copied from the API console here:

Sapui5ml-api = sapUI5ml-api = sapUI5ml-api = sapUI5ml-api = sapUI5ml-api = sapUI5ml-api

Run the Web application and you will see the following interface:

To do a simple test of the Web application, upload the following image to the Web application:

The SAP AI API identified Tablets 74.7% of the time, 13.8% of the time, and 13.3% of the time.

Click the “View JSON” button of the Web application to see the technical details returned by the AI service.

How to retrain the machine learning model on THE SAP cloud platform

If the categories of images uploaded by the user through the Java application or Web application described above are not supported by the pre-trained model of the SAP AI service, we can retrain the machine learning model of the AI service by ourselves.

Suppose that we expect the Product Image Classfication machine learning model to be able to identify all kinds of flowers. First you have to prepare lots of pictures of different types of flowers. Tensorflow’s website has helpfully provided an exercise package for learners who want to retrain their AI models, complete with pictures of various flowers:

Download.tensorflow.org/example_ima…

The data set used to retrain the AI model must follow the hierarchical structure shown below, with sub-folders named after the product category under the training, Validation, and Test folders, and the data size ratio is 8:1:1.

The SAP cloud platform service Key contains an IMAGE_RETRAIN_API_URL, which can be used to obtain the online storage URL of the data set that needs to be uploaded for the retraining AI model:

Send an HTTP Get request to this URL to Get the URL for online storage:

Paste the URL into your browser, enter the accessKey and secretKey returned from Postman, and access the online store from the Web:

The next step is to upload the local training files to the online store deployed on AWS.

First, define a remote site named sapJerrys3 with the command line MC config host and bind the AWS online storage URL, accessKey and secret obtained from Postman in the previous step to this site:

Then use the command line to upload the file:

mc.exe cp -r C:\Code\MachineLearningStudy\flowersjerry sapjerrys3\data

About ten minutes later, the file was uploaded:

At this point, you can view the training files uploaded by AWS online storage from the browser.

With the retrained data set in place, the next step is to submit a model retraining request through a background job. Send an HTTP Post request using Postman to initiate a background job for model retraining.

Run the cli to check the background job status. If the status becomes SUCCEEDED, the model training is complete.

After the model is retrained, it is consumed in the format of URL:

Mlfinternalproduction-image-classifier.cfapps.sap.hana.ondemand.com/api/v2/imag… /versions/1

Use an image of a sunflower:

Sent to the retrained FlowerJerryModel as a parameter to the HTTP Post, the AI Service gave the picture an approximate 87% chance of being a Sunflower.

Finally, we look at the specific application of an AI service in the Intelligent Service Scenario.

A maintenance engineer to accept the customer’s maintenance request, home maintenance of some equipment, found that some parts of the equipment is damaged. Suppose the technician, for one reason or another, fails to identify the type of part empirically. At this point, the technician takes out a mobile phone, takes a picture of the part, and automatically identifies the exact model of the part through THE SAP intelligent service solution installed on the phone (such as Java program and Web application described before in this paper) and AI API, and returns it to the maintenance engineer.

After receiving the picture uploaded by the maintenance engineer, THE AI service of SAP cloud platform extracts the feature vector of the picture and identifies the accurate model through the model trained on the platform based on massive data sets.

The extraction of feature vectors, mathematically speaking, is the process of converting the input binary stream of images into a vector (one-dimensional matrix) through some algorithm.

In the following figure, the trapezoid and circle are taken as examples. The graph is evenly divided into 9 regions, and the gradient direction of the graph unit in each region is observed in the center of the graph to achieve dimensionality reduction. The two-dimensional image is represented by a one-dimensional matrix.

For programmers developing applications based on SAP AI Service, there is no need to know the specific algorithm of image feature vector extraction. They only need to push the image to be extracted feature vector to the AI Service Endpoint through Restful API invocation, and then feature vector output can be obtained.

Enter the test console and help document of the image feature vector extraction Service in SAP AI Service through the following URL:

Api.sap.com/api/img_fea…

Upload a local image from the console and click the Execute button:

The output eigenvector is obtained:

With eigenvectors in hand, let’s return to the enterprise intelligence services scenario we are discussing. We utilized another ARTIFICIAL intelligence Service related to image processing in SAP AI Service: Inference Service for Similarity Scoring.

This image similarity score AI API input requires two compression packages. The content of the first compressed package is a series of feature vectors, which are from the image files uploaded by the maintenance engineer. The second compression package is stored in the model A, B, C… And other parts of the picture of the feature vector.

As shown in the figure above, for the sake of simplicity, I only store two files in the second compression package, which store the feature vectors of part model A and model B respectively.

Call the API using Postman and pass in these two zip packages:

The results show that the similarity coefficient between the picture uploaded by the maintenance engineer and model A is almost 1, so obviously, the picture represents model A.

Of course, in addition to directly uploading compressed files containing image feature vectors, it is also a common way to input the feature vector content contained in these files in the form of JSON string, and call API. The format of JSON string is explained in detail on the OFFICIAL website of SAP, which will not be repeated here.

I hope you found these SAP AI API usage scenarios helpful. Thanks for reading.