Skip to Content

Use the Similarity Scoring with the Image Feature Extraction API from a REST Client

test
0 %
Use the Similarity Scoring with the Image Feature Extraction API from a REST Client
Details
// Explore More Tutorials

Use the Similarity Scoring with the Image Feature Extraction API from a REST Client

06/11/2019

Discover how to call the Similarity Scoring with the Image Feature Extraction API from a REST Client like Postman

You will learn

  • Call multiple API and reuse the result into the next one from a REST client like Postman
  • The basics about Machine Learning Foundation Service for Similarity Scoring & Image Feature Extraction

Note: The Face Feature Extraction service was in alpha version when this tutorial was released.

Step 1: The Image Feature Extraction Service

Similarly to the Text Feature Extraction or the Face Feature Extraction service, the Image Feature Extraction service extracts a vector of features out of an input image.

This is the list of accepted file extensions:

Name Description
Archive file zip, tar, gz, tgz
Image file jpg, jpe, jpeg, png, gif, bmp

The images should be RGB, or 8-bit gray scale.

If an archive file is provided, no additional files can be provided.

The input file (or the archive file) is provided using form data (as an element named files in the form data).

The service will return a JSON response that includes the vector of features for the associated image.

For more details, you can check the following link:

Log on to answer question
Step 2: The Similarity Scoring Service

The Similarity Scoring service compares vectors of extracted features with respect to cosine similarity.

A vector of features can be represented using the following format [number, number, number, …, number].

The set of feature vectors which should be compared must be provided either using:

  • an archive file which will contain multiple feature vector file
  • plain text that represents a vector of features

With both options, you can provide either a single set of feature vector entries or two sets of feature vector entries which will drive the way entries are processed:

  • with a single feature vector entries, every feature vector entries will be compared to each other ((n-1)!-1 comparison).
  • with two set of feature vector entries, every feature vector entries in the first set will be compared to every feature vector entries from the second set (n*m comparison).

The input content will provided using form data, either as:

  • a single or two element named files in the form data for the archive file
  • a single or two element named texts in the form data for the plain text

This is the list of accepted file extensions for the archive file:

Name Description
Archive file zip, tar, gz, tgz

A required setting must also be provided as part of the form data (named options in the form data) using a JSON string format.

Name Description
numSimilarVectors Number of most similar vectors to return in the response

The service will return a JSON response that includes a series of scores for each comparison.

For more details, you can check the Inference Service for Similarity Scoring on the SAP API Business Hub.

Log on to answer question
Step 3: Call the Image Feature Extraction API

First you will need to select and download a series of pictures to be used with Image Feature Extraction service.

If you are missing some inspiration, you can use the following pictures from the ImageNet database around fruits:

To keep it simple, only one class should be detected from each image.

Open a new tab in Postman.

Make sure that the my-ml-foundation environment is selected.

On the Authorization tab, select Bearer Token, then enter {{OAuthToken}} as value.

Postman

Note:: the OAuthToken environment variable can be retrieved following the Get your OAuth Access Token using a REST Client tutorial.

Fill in the following additional information:

Field Name Value
HTTP Method POST
URL the value for IMAGE_FEATURE_EXTRACTION_URL in your service key

Note As a reminder, the URL depends on you Cloud Platform landscape region but for the trial landscape only Europe (Frankfurt) provide access to the Machine Learning Foundation services.

On the Body tab, keep form-data selected.

Add 6 elements with the key named files and switch it to File instead of Text (default).

Then set the file with the downloaded images in the following order:

The sequence of files is important here as you will be using a script to retrieve the corresponding responses.

Postman

Switch to the Tests tab and insert the following code:

pm.environment.set("Apple 1", decodeURIComponent(pm.response.json().predictions[0].featureVectors))
pm.environment.set("Apple 2", decodeURIComponent(pm.response.json().predictions[1].featureVectors))
pm.environment.set("Banana 1", decodeURIComponent(pm.response.json().predictions[2].featureVectors))
pm.environment.set("Banana 2", decodeURIComponent(pm.response.json().predictions[3].featureVectors))
pm.environment.set("Cherry 1", decodeURIComponent(pm.response.json().predictions[4].featureVectors))
pm.environment.set("Cherry 2", decodeURIComponent(pm.response.json().predictions[4].featureVectors))
Postman

Click on Send.

You should receive a response that includes for each image an entry with the feature vector:

Postman
{
    "featureVectors": [ 0, "..."
    ],
    "name": "Apple 1.jpg"
}

Each entry in the response represents a box that identify one of the face.

Log on to answer question
Step 4: Call the Similarity Scoring API

Open a new tab in Postman.

Make sure that the my-ml-foundation environment is selected.

On the Authorization tab, select Bearer Token, then enter {{OAuthToken}} as value.

Fill in the following additional information:

Field Name Value
HTTP Method POST
URL the value for SIMILARITY_SCORING_URL in your service key

On the Body tab, keep form-data selected.

Add a key named texts and switch it to Text (default).

Paste the following value:

{
	"0":
	[
		{"id": "Apple 1", "vector": [{{Apple 1}}]},
		{"id": "Apple 2", "vector": [{{Apple 2}}]},
		{"id": "Banana 1", "vector": [{{Banana 1}}]},
		{"id": "Banana 2", "vector": [{{Banana 2}}]},
		{"id": "Cherry 1", "vector": [{{Cherry 1}}]},
		{"id": "Cherry 2", "vector": [{{Cherry 2}}]}		
	]
}

Add a key named options and switch it to Text (default).

Paste the following value:

{"numSimilarVectors":2}
Postman

Click on Send.

You should receive a response that includes for each input feature vector (in your case 6 in total) the top 3 most similar feature vectors.

For example here, the image identified as Apple 1 has been matched with Apple 2 with a score of 0.81 and only 0.59 with Cherry 1:

{
    "id": "Apple 1",
    "similarVectors": [
        {
            "id": "Apple 2",
            "score": 0.8198536095383784
        },
        {
            "id": "Cherry 1",
            "score": 0.590942196617069
        }
    ]
}
Log on to answer question
Step 5: Validate your results

Provide an answer to the question below then click on Validate.

Paste the full response returned by the last request.
×

Next Steps

Back to top