Technical Articles
Retrain an Image Classification Model with Postman and SAP Leonardo Machine Learning Foundation on SAP Cloud Platform Trial
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:
- SAP Leonardo ML Foundation – Retraining part 1
- EXERCISE 08 – ML Foundation Re-trainable services
- Retrain and Deploy the Image Classification Model through SAP Leonardo Machine Learning Platform
And if you want more details about retraining, have a look at How to Retrain an Image Classifier for New Categories.
About text retraining, see Retrain a Text Classification with Postman and SAP Leonardo Machine Learning Foundation on SAP Cloud Platform Trial – Sentiment Analysis
What do you need?
- A trial account on SAP Cloud Platform.
- Postman
- Minio
- Image Dataset: Apparel classification with Style
- A script to create the training data: Apparel Dataset Creator
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-new 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:
- download Apparel classification with Style
- run the Apparel Dataset Creator script (needs Node.js)
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
- Install Minio: Minio Client Quickstart Guide
- config the host (don’t forget the https)
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 training (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):
- https://pixabay.com/en/girl-fashion-skirt-tshirt-2625460/
- https://pixabay.com/en/vest-waistcoat-fashion-jacket-male-1031127/
- https://pixabay.com/en/man-human-person-portrait-vintage-66941/
- https://pixabay.com/en/streetstyle-street-fashion-cloak-3750053/
- https://pixabay.com/en/judge-court-justice-law-right-67715/
- https://pixabay.com/en/portrait-woman-fashion-people-3190849/
- https://pixabay.com/en/woman-autumn-model-fashion-sweater-1245840/
have fun :goofy face:
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.
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
Hi Marius Obert ,
You are right , I have activated “ml foundation-trial-beta” under entitlements and after I created a new subaccount it was there.Thanks.
Best Regards.
Tuncer.
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..
Hi Levpreet Kaur,
The Uploading Data page (3rd block) on SAP Help describes the structure. Here's an example:
Zip this structure with:
... and upload the zip file:
It is important that the zip file is uploaded in a folder (e.g. sentiment). Use this folder as dataset for the retraining:
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
I've used .txt for each file. Here is an example for good / positive:
thank you...
Could you please tell me what type of training data format should be for "Customizable similarity search"? I saw that we can retrain the similarity scoring model in product CF now, however I can not find the data structure for it. Thanks in advance
Dear Expert @Lars ,
Thanks for this beautiful blog .
I have tried the similar thing , Via CLI command interface and now I am getting error as -
Server error, status code: 400, error code: 310006, message: You have exceeded t he total routes for your organization's quota.
Can you please help me out here.
thanks and regards
Hi Mohammed zahid
the error is a Cloud Foundry error (and I have no idea to solve this).