Score a Mini Batch Containing base64 Encoded String

This example demonstrates how to score a mini batch of images that are converted to base64 encoded strings. Use the mini batch functionality to score multiple images, since it is not possible to upload multiple image files directly. Here, the images must be first converted to some form of text. OML Services supports base64 encoded strings and image tensors (matrices) for image classification.

Note:

OML Services supports batch scoring for regression, classification, clustering, and feature extraction models.

The cognitive image module accepts three different kinds of image inputs - the image file, image tensors, and base64 encoded strings. You then send a POST request to the cognitive-image/classification endpoint. This functionality uses the prebuilt ONNX image model for scoring.

Use the Linux utility jq to parse the JSON output into a readable format. The jq utility is included in all the major Linux distribution repositories. On Oracle Linux and Red Hat systems, you can install this utility by running this command:
$ sudo yum install jq
This example shows how to score a mini batch containing base64 encoded strings. This example uses three images files and a bash script, that are stored locally.
  • The image files are cat.jpg, dog.jpg, flowers.jpg.
  • The bash script test.sh contains the json code.
To score a mini batch:
  1. First, run the following command to view the content of the bash script:
    $ cat test.sh
    #!/bin/bash
    The script returns the following:
    # get the paths to the images
    image1="./cat.jpg"
    image2="./dog.jpg"
    image3="./flowers.jpg"
    # get base64 encoded strings
    base64_1=`base64 -w 0 $image1`
    base64_2=`base64 -w 0 $image2`
    base64_3=`base64 -w 0 $image3`
    json_data=$(mktemp)
    echo "{\"inputRecords\":[
    {\"input\": \"$base64_1\"},
    {\"input\": \"$base64_2\"},
    {\"input\": \"$base64_3\"}], \"topN\":3}" > "$json_data"
    

    In this script:

    • The paths to the images are set here:
      image1="./cat.jpg"
      image2="./dog.jpg"
      image3="./flowers.jpg"
    • Then, the .jpg images are converted to base64 encoded strings.
      
      base64_1=`base64 -w 0 $image1`
      base64_2=`base64 -w 0 $image2`
      base64_3=`base64 -w 0 $image3`
    • In this part of the script, a temporary file json_data is created to which the input records comprising the base64 strings are added. It also specifies the topN class, and uses the @$json_data argument to fetch the content. During image classification, for each image the possible classes that the image belongs to will be sorted based on the probability.
      json_data=$(mktemp)
      echo "{\"inputRecords\":[
      {\"input\": \"$base64_1\"},
      {\"input\": \"$base64_2\"},
      {\"input\": \"$base64_3\"}], \"topN\":3}" > "$json_data"
      
  2. Next, run the following command to make the test.sh file executable:
    $ chmod +x test.sh 
    
  3. Run the script file test.sh:
    ./test.sh
  4. Now, obtain the authentication token, and run the following cURL command to score a mini batch containing base64 encoded strings in the input record:
    $ curl -X POST "${omlservice}/omlmod/v1/cognitive-image/classification" \
    --header "Authorization: Bearer $token" \
    --header 'Content-Type: application/json' --data "@$json_data" | jq

Note:

Here, you use the flag --data to fetch the input records from the temporary file json_data. Since the base64 encoded strings are very long, the application will throw the error Argument list too long. Hence, it is recommended to use the --data flag.
Use the Linux utility jq to parse the JSON output into a readable format. The jq utility is included in all the major Linux distribution repositories. On Oracle Linux and Red Hat systems, you can install this utility by running this command:
$ sudo yum install jq

Note:

Using jq in the cURL command does not return the HTTP response. To return HTTP response, remove jq command and add the -i flag to the curl command.

The command returns the following scoring result:

{
  "scoringResults": [
    {
      "classifications": [
        {
          "label": "Cat",
          "probability": 0.9999594688415527
        },
        {
          "label": "Felidae",
          "probability": 0.9997081756591797
        },
        {
          "label": "Whiskers",
          "probability": 0.9994940757751465
        }
      ]
    },
    {
      "classifications": [
        {
          "label": "Snout",
          "probability": 0.9996676445007324
        },
        {
          "label": "Dog",
          "probability": 0.996991753578186
        },
        {
          "label": "Vertebrate",
          "probability": 0.9954350590705872
        }
      ]
    },
    {
      "classifications": [
        {
          "label": "Pink",
          "probability": 0.9993271827697754
        },
        {
          "label": "Flowering plant",
          "probability": 0.9986602663993835
        },
        {
          "label": "Flower",
          "probability": 0.99815833568573
        }
      ]
    }
  ]
}