Skip to Content

This blog post shows how to retrain and deploy a SAP Leonardo Machine Learning Image Classification Model on the SAP Cloud Platform trial version. I use Postman for all API calls and Minio to upload the training data based on the Apparel classification with Style dataset.

Thanks to the trained model, I can now for example see the difference between a blouse and a shirt. 😉

At SAP Help you can find some help about Retraining of Customizable Services, and there are also some (older) blog posts and documentations available:

And if you want more details about retraining, have a look at How to Retrain an Image Classifier for New Categories.

What do you need?

Step 1 – SAP Leonardo Machine Learning instance

First, you create a SAP Leonardo Machine Learning instance and a client ID and client secret on a SAP Could Platform (SCP) trial account:

  • Log on or register a free SCP trial account.
  • Click on Cloud Foundry Trial.
  • In the Start Cloud Foundry Trial dialog select Europe (Frankfurt) for the region and click OK.
  • This creates a trial account (e.g. p2000894545trial). Click on Go to Space.
  • SCP opens Home [Europe (Rot) – Trial] / Europe (Frankfurt) / p2000894545trial / trial / dev
  • Click on Service Marketplace (left menu) and search for “ml”.
  • Click on ml-foundation-trial-beta.
  • An Overview page will open. Click on Instances (left menu) to open the Instances page.
  • Click on New Instance.
  • The Create Instance dialog opens. Select standard as Plan and click Next.
  • Click Next on Specify Parameters (Optional) and Assign Application (Optional).
  • On the Confirm page fill in the Instance Name e.g. fashion_data and click Finish.
  • The Instances page shows the new created fashion_data instance. Open the fashion_data Instance with a click.
  • Click on Service Keys (left menu).
  • On the Service Keys page click on Create Service Key.
  • Add e.g. ml_key for the name on the Create Service Key dialog and click Save.

This creates the Service Key with the following content:

{
  "clientid": "sb-42mn3-3z7p-96r-3c79-0x1pm0l!p216|klgco-lag-vitas!d66",
  …
  "clientsecret": "5ghsdYM/Z567N5LoQ7nrXBkZ0BV=",
  "serviceurls": {
    …
    "IMAGE_RETRAIN_API_URL": "https://mlftrial-retrain-image-api.cfapps.eu10.hana.ondemand.com/api/v2/image/retraining",
    …
    "IMAGE_CLASSIFICATION_URL": "https://mlftrial-image-classifier.cfapps.eu10.hana.ondemand.com/api/v2/image/classification",
    …
  },
  "url": "https://p2000894545trial.authentication.eu10.hana.ondemand.com"
}

Step 2 – Storage

Before you can upload data, you need some storage. Generate a Bearer Token and create the storage with Postman. You can find my Postman collection here: SAP Leonardo Machine Learning Postman Collection

Generate Token

URL: {url}/oauth/token
POST
https://p2000482375trial.authentication.eu10.hana.ondemand.com/oauth/token

Header:
  Content-Type: application/x-www-form-urlencoded

Body:
  grant_type: client_credentials
  client_id: <replace it with clientid>
  client_secret: <replace it with clientsecret>

Test:
  pm.test("Set Bearer Token", function () {
    var data = pm.response.json();
    pm.environment.set("Bearer Token", "Bearer " + data.access_token);
  });

This creates a Bearer Token. With the code in the test folder, the token is also stored in the Postman environment as ‘Bearer Token’. Postman replaces the string {{Bearer Token}}} with the most recent value each time it is called.

Storage

URL: IMAGE_RETRAIN_API_URL/storage
Doc: https://api.sap.com/api/retraining_service_imgc_api/resource
POST
https://mlftrial-retrain-image-api.cfapps.eu10.hana.ondemand.com/api/v2/image/retraining/storage

Header:
  Authorization: {{Bearer Token}}

Result:
  {
    "accessKey": "18gXjKdl5YR",
    "endpoint": "j310p.files.trial.eu-central-1.aws.ml.hana.ondemand.com",
    "message": "The endpoint is ready to use.",
    "secretKey": "b47iOAuRYosEtGT",
    "status": "READY"
  }

The endpoint, accessKey and secretKey are required for step 4.

Step 3 – Training data

The following steps are necessary to create the training data:

The script creates the following structure:

fashion_data
├── test
│   ├── blouses
│   ├── cloak
│   ├── coat
│   ├── jacket
│   ├── jersey__t-shirt__tee_shirt
│   ├── long_dress
│   ├── polo_shirt__sport_shirt
│   ├── robe
│   ├── shirt
│   ├── short_dress
│   ├── suit__suit_of_clothes
│   ├── sweater
│   ├── undergarment__upper_body
│   ├── uniform
│   └── vest__waistcoat
├── training
│   ├── blouses
│   ├── cloak
│   ├── coat
│   ├── jacket
│   ├── jersey__t-shirt__tee_shirt
│   ├── long_dress
│   ├── polo_shirt__sport_shirt
│   ├── robe
│   ├── shirt
│   ├── short_dress
│   ├── suit__suit_of_clothes
│   ├── sweater
│   ├── undergarment__upper_body
│   ├── uniform
│   └── vest__waistcoat
└── validation
    ├── blouses
    ├── cloak
    ├── coat
    ├── jacket
    ├── jersey__t-shirt__tee_shirt
    ├── long_dress
    ├── polo_shirt__sport_shirt
    ├── robe
    ├── shirt
    ├── short_dress
    ├── suit__suit_of_clothes
    ├── sweater
    ├── undergarment__upper_body
    ├── uniform
    └── vest__waistcoat

Other data sets:

Step 4 – Upload

Minio is almost three times faster than sapml via Cloud Foundry. However, if you are looking for the latest version of sapml, you can find it here: SAP Leonardo Machine Learning foundation plugin for SAP Cloud Platform CLI 

mc config host add saps3 https://<endpoint> <accessKey> <secretKey>

Example:

mc config host add saps3 https://j310p.files.trial.eu-central-1.aws.ml.hana.ondemand.com 18gXjKdl5YR b47iOAuRYosEtGT
  • test (the data folder should exist)
mc ls saps3/
  • upload
mc cp --recursive fashion_data saps3/data

This will take some time :coffee: or :tee:

Step 5 – Training

After uploading the training data, start the test (with Postman).

Training

URL: IMAGE_RETRAIN_API_URL/jobs
Doc: https://api.sap.com/api/retraining_service_imgc_api/resource
POST
https://mlftrial-retrain-image-api.cfapps.eu10.hana.ondemand.com/api/v2/image/retraining/jobs

Headers:
  Authorization: {{Bearer Token}}
  Accept application/json
  Content-Type application/json

Body:
  {
   "dataset": "fashion_data",
   "modelName": "apparel_fashion"
  }

Result:
  {
    "id": "fashiondata-2018-10-31t2118z489249"
  }

This will also take some time as the trial environments are not highly prioritized. You can check the job status with a GET IMAGE_RETRAIN_API_URL/jobs call.

Jobs

URL: IMAGE_RETRAIN_API_URL/jobs
Doc: https://api.sap.com/api/retraining_service_imgc_api/resource
GET
https://mlftrial-retrain-image-api.cfapps.eu10.hana.ondemand.com/api/v2/image/retraining/jobs

Results:
  {
    "jobs": [
        {
            "message": "Requested resources are unavailable.
                        Please wait or cancel your job.",
            "id": "fashiondata-2018-10-31t2118z489249",
            "submissionTime": "2018-10-31T20:55:40+00:00",
            "status": "PENDING"
        }
    ]
  }


  {
    "jobs": [
        {
            "startTime": "2018-10-29T08:46:24+00:00",
            "id": "fashiondata-2018-10-31t2118z489249",
            "finishTime": "2018-10-29T09:31:30+00:00",
            "status": "SUCCEEDED",
            "submissionTime": "2018-10-29T08:38:41+00:00",
            "message": ""
        }
    ]
  }

Logs

In case of failure or success, download the job logs:

mc cp --recursive saps3/data/fashiondata-2018-10-31t2118z489249/ logs

Step 6 – Deploy

The model must be deployed after a successful training.

Deploy Model

URL: IMAGE_RETRAIN_API_URL/deployments
Doc: https://api.sap.com/api/retraining_service_imgc_api/resource
POST
https://mlftrial-retrain-image-api.cfapps.eu10.hana.ondemand.com/api/v2/image/retraining/deployments

Header:
  Authorization {{Bearer Token}}
  Content-Type application/json

Body:
  {
    "modelName": "apparel_fashion",
    "modelVersion": “1”
  }

Result:
  {
    "id": "f6b34f68-6bf0-4fe8-98f5-9f9a4310a9b8"
  }

Now the model is available for an image classification.

Step 7 – Test

You can now use your trained model instead of the sandbox version.

Image Classification

URL: IMAGE_CLASSIFICATION_URL/models/{model}/versions/{version}
Doc: https://api.sap.com/api/image_classification_api/resource
POST
https://mlftrial-image-classifier.cfapps.eu10.hana.ondemand.com/api/v2/image/classification/models/apparel_fashion/versions/1

Header:
  Authorization {{Bearer Token}}
  Content-Type application/json

Body:
  files: model-1338993_640.jpg

Here is the result for this image:

…. and as you can see it is 44% a blouse and only 8.8% a shirt.

{
    "id": "4ac738e5-2ae7-40b1-7c71-5811ffefd957",
    "predictions": [
        {
            "name": "model-1338993_640.jpg",
            "results": [
                {
                    "label": "blouses",
                    "score": 0.4417305588722229
                },
                {
                    "label": "sweater",
                    "score": 0.17741338908672333
                },
                {
                    "label": "jersey__t-shirt__tee_shirt",
                    "score": 0.09909668564796448
                },
                {
                    "label": "shirt",
                    "score": 0.08796049654483795
                },
                {
                    "label": "jacket",
                    "score": 0.0525900162756443
                },
                {
                    "label": "cloak",
                    "score": 0.05253848433494568
                },
                {
                    "label": "polo_shirt__sport_shirt",
                    "score": 0.035483747720718384
                },
                {
                    "label": "short_dress",
                    "score": 0.03353172168135643
                },
                {
                    "label": "long_dress",
                    "score": 0.011001537553966045
                },
                {
                    "label": "undergarment__upper_body",
                    "score": 0.002974236151203513
                },
                {
                    "label": "coat",
                    "score": 0.0025379941798746586
                },
                {
                    "label": "robe",
                    "score": 0.0012542953481897712
                },
                {
                    "label": "vest__waistcoat",
                    "score": 0.0008026888244785368
                },
                {
                    "label": "uniform",
                    "score": 0.0006318347295746207
                },
                {
                    "label": "suit__suit_of_clothes",
                    "score": 0.0004524094401858747
                }
            ]
        }
    ],
    "processedTime": "2018-10-31T22:02:29.581062+00:00",
    "status": "DONE"
}

And now you are able to test your model. Here are more images (they are not part of the training data):

have fun :goofy face:

To report this post you need to login first.

8 Comments

You must be Logged on to comment or reply to a post.

  1. Tuncer Gorgun

    Hi Lars ,

    I have logged on SCP and when I go to Service Marketplace and search “ml” , I couldnt find “ml foundation-trial-beta”. There are 19 services but there is no machine learning foundation. What could be the problem?

    Thanks.

     

    (0) 
    1. Marius Obert

      Hi Tuncer Gorgun,

      Can you please double check that you are searching for Cloud Foundry (SCP Cockpit -> CF Trial -> Subaccount -> Space -> Service Marketplace) Service and not for Neo Services?

      If so, your subaccount might miss the proper entitlements. You can see this when you navigate to your subaccount and select ‘entitlements’ in the left side panel.

      Regards,

      Marius

      (3) 
  2. Luvpreet Kaur

    can you tell me what type of training data format should be for text classification i can use..i want to retrain the product text classification api..

    (0) 
    1. Lars Gregori
      Post author

      Hi Levpreet Kaur,

      The Uploading Data page (3rd block) on SAP Help describes the structure. Here’s an example:

      sentiment
      ├── test
      │   ├── bad
      │   └── good
      ├── training
      │   ├── bad
      │   └── good
      └── validation
          ├── bad
          └── good

      Zip this structure with:

      cd sentiment
      zip -r sentiment.zip training test validation

      … and upload the zip file:

      mc cp --recursive sentiment.zip saps3/data/sentiment

      It is important that the zip file is uploaded in a folder (e.g. sentiment). Use this folder as dataset for the retraining:

      {
        "dataset": "sentiment",
        "modelName": "sentiment",
        "preprocessingLanguage": "en",
        "completionTime": 24,
        "memory": 8192
      }
      (1) 
      1. Luvpreet Kaur

        I have one query inside bad and good, it will text files representing categories (means what should be the data format)..like in image we given images .jpeg or .jpg

        (0) 
        1. Lars Gregori
          Post author

          I’ve used .txt for each file. Here is an example for good / positive:

          cat training/good/1234505.txt
          Thank you for your humor. That really just made me feel better. 
          
          (0) 

Leave a Reply