Skip to Content
Technical Articles

SAP Leonardo Machine Learning Service – Image Classifier

Hello All,

In this blog post we are going to see how to use SAP Leonardo Machine Learning Service call – Image Classifier .

 

Few Pre-requites

  1. SAP Cloud foundry account from here 
  2. Install CL Plugin on your local machine from here  

 

PS : There are good blog posts on SAP SCN  on how to use “Machine Learning Service”, you can try and check these SAP Experts blog posts as well such as @lars.gregori @Fabian Lehmann ,@divyamunnuru and @alexey.ershov 

 

Let’s Get Started –  We are going to perform Step- by-Step process on how to Use Machine Learning Image classifier API.

  1. Create your SAP CL account and activate the service . (You can find it here how to do that ) . Below are some screenshots

 

From the left hand side Click on Regions and Select FRANKFURT.

Create your Global Account and Sub-Account into your Main Account –

 

Click on your Sub-account (“Development” in my case ) and Create your Space.

 

 

Now , Your Account is ready and Space is also created , Let us go to Service from the left panel and click on Service Marketplace  . 

Click on SAP Leonardo Machine Learning service.

 

We are going to create an Instance of our service . Click on the SAP Leonardo Machine Learning Service and from the left panel click on Instance –

 

Once you create the New Instance you will find a new line item , Click on your instance and from the left panel create the Service Key –

 

 

IMPORTANT – Copy all your Service key and save it on a note pad , we are going to use these key through out this blog. 

 

         2. Install the CL plugin and login in your SAP CL account via command line interface

 

For the installation of SAP CL plugin just click here 

Once you are done –

Go to your command line by pressing “Crtl +R” and enter ” cmd”

 

Now from here , go to the path ( directory ) where you have installed the plugins and login into your SAP CL account –

PS : to change the path just type ” CD (path ) and enter and you will enter your plugin path.

Command – ” cf login” 

 

Give your email ID ( your user name ) and hit enter and enter your password ( password won’t be visible )

 

If you have reached here successfully login into your CF account via Command line interface ,

Great !!!! we have done the setup to use our SAP ML services !!

 

Before we move further ,We need to setup our postman to call our service . Which is very simple and in few steps it will be done

Setup an environment of your post man and enter the below parameters –

Go to Google chrome and type ” Postman Launch ” and enter the below credentials

URL https://development-ml.authentication.eu10.hana.ondemand.com/oauth/token?grant_type=client_credentials
Authorization tab Basic Authorization – ( From the service key which we get from SAP CF account – get the Client ID and Client Secret and enter those two values )
Tests tab postman.setEnvironmentVariable(“token”, “Bearer ” +JSON.parse(responseBody).access_token);

Screenshots for the same –

 

Now test our ML url which we got it from Service key

Done !!

So we have created our SAP CF ( Cloud foundry ) account , Installed the CF plugin and login via cmd command and get the postman ready .

 

Our setup is almost ready to use !!

Let us get into the real part , that is Machine learning Image classifier api  . Before we start, let us get some knowledge on what we are going to do here –

 

MACHINE LEARNING – 

In Machine learning , we divide our data into 2 parts . Train data and test data  with divide ratio of 80% train data and 20% test data .

We put the machine learning regression (From sklearn library and import the classes ) on the train data to learn what the data is all about and later we  deploy this object onto test data and predict the value.  This prediction is later plot on the graph to visualize our data .

 

Here in this blog we are going to divide into 3 parts – Train/ Test / and Validate the data .

Once we have 3 folders on your machine – Train data /Test data/ Validate data . we are going to use ML rest api to understand and learn from the train data folder and later we will change the path to test data to predict what is the value .

Enough theory !! let us do practical 🙂

PS : I got my data from HANA ACADEMY as it was ready-made ( i do not need to divide the folders or anything )

 

STEP 1 . Check for the api available APIs –  Enter this in your CLI

"cf sapml config get"

 

STEP 2 , set the API in-order to use it , with the below commands

cf sapml config set auth_server ( Your Authenticate URL from service key )
cf sapml config set job_api ( job API )
cf samplf config set training_api ( retrain API)

STEP 3.  Initialize your CLI with command –

cf sapml fs init

 

STEP 4.  Uploading the data in SAP Cloud foundry using CLI and S3 – Minio with command –

cf sapml fs config

 

Now – Download the Minio from here

*****Put this downloaded file in the same path were your CLI Plugin is placed and we are good to run it****** 

Now , I change my directory and later i use the minio command ” mc” to check if it working or not

 

Now we need to login into minio so that we can upload the files into your SAP CF . To do this – give the below command with the endpoint acces key and secret key

mc config host add https:// ( Your End point  Access key Secret key ) 

We are ready to upload now !! with the command –

mc cp Brands mlfs3/data --recursive

NOTE – Brand is the folder name – in your case it will be different

In case you get session time out – enter the command –

mc session resume ( your sessionID )

 

Congratulations !! your date is uploaded .. .let us check it now – with command –

cf sapml fs list

 

STEP 5.  Retrain our data which we have uploaded in SAP CF via minio on CLI

Now a bit small coding –  Save the below in .Json format in the same path. ( ModelName and data set name will be yours own data set and any model name

{
	"dataset": "brands",
	"modelName": "brands",
	"learningRate": 0.001
}

and please note – this job will be running on the Instance which we have created on the SAP CF account and not on the local machine .

Command for this –

cf sapml retraining job_submit retraining job_submit retrain.json m-m image

 

Prefect !!! Now let us retrain it with command –

cf sapml retraining models -m image

 

 

STEP 6 Time to test !!!

 

Congratulation !! We have completed the setup and data is train, let us now test it  .

Upload your first image on the non train url –

 

"https://mlftrial-image-classifier.cfapps.eu10.hana.ondemand.com/api/v2/image/classification"

 

We can see our output as “Wool” 

 

Let us upload the image in our train url –

"https://mlftrial-image-classifier.cfapps.eu10.hana.ondemand.com/api/v2/image/classification/models/brands/versions/1"

 

 

We can see our model read the same image as ” Adidas ”

 

Congratulations !!

 

We have learned the below things –

  1. Create SAP CF account
  2. Install CF Plugins
  3. Login into CG via CLI command
  4. Connect to Postman
  5. Uploading the data to file system
  6. Retrain the model using the uploaded data.
  7. Deploy the Retrained model to your service instance.
  8. Testing from postman

 

Next blog post we are going to see how to use other APIs provided by SAP Leonardo Machine Learning .

Till then , Enjoy Machine Learning 🙂

 

 

 

 

 

3 Comments
You must be Logged on to comment or reply to a post.
  • Stellar content, thanks so much!

    Quick question though; when you’re setting up Postman and Manage Environments, there’s a Token – “Bearer” key-value pair. I get that that’s supposed to get populated by the previous request. 

    The previous request is giving me an error : 

    “The subdomain does not map to a valid identity zone.”

     

    Any ideas as to how to resolve this?

    Thanks again!

    • Hello Partick,

       

      Greetings for the day !

       

      Token Bearer is set when you send your Client Key and secret with the Postman request under Authentication tab.

       

      Next time it won’t ask as in your environment you are setting/saving this token.

       

      Thanks and Regards,

      M.Zahid