Work with Cognitive Image Functionality
Oracle Machine Learning Services supports two cognitive image functions - Image Classification and Image Feature Extraction using a prebuilt ONNX image model.
- By uploading a single image file as input. In this case, the equivalent tensor format of the uploaded image is generated with proper preprocessing for the model. Supported image file formats include:
- Bitmap
(.bmp)
- Portable Bitmap
(.pbm)
- Portable Graymap
(.pgm)
- Portable Pixmap
(.ppm)
- Sun Raster Graphic
(.sr)
- Raster Graphic
(.ras)
- Joint Photographic Expert Group
(.jpeg, .jpg, .jpe, .jp2)
- Tag Image File Format
(.tiff, .tif)
- Portable Network Graphics
(.png)
- Bitmap
- By uploading a collection of scoring input records. Here, mini-batch scoring with ONNX image models is done with
<model_attribute_name, value/array>
pairs. Here,value/array
can be either an encoded image in base64 string or a 4D array storing processed image pixel values. The shape of the 4D array must be 1x3x224x224.
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:
Usingjq
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.
- Get a list of model endpoints
- Get the
topN
categories of an image along with their respective probabilities - Get the feature extraction results for input images
1: Get a List of Model Endpoints
curl -X GET --header "Authorization: Bearer ${token}" "<oml-cloud-service-location-url>/omlmod/v1/cognitive-image"
The command returns the following details:
{
"items": [
{
"name": "classification",
"description": "A cognitive image scoring endpoint that returns the classification of the images.",
"links": [
{
"rel": "classification",
"href": "http://adb.us-sanjose-1.oraclecloud.com/omlmod/v1/cognitive-image/classification"
},
{
"rel": "self",
"href": "http://adb.us-sanjose-1.oraclecloud.com/omlmod/v1/cognitive-image/."
}
]
},
{
"name": "feature-extraction",
"description": "A cognitive image scoring endpoint that extracts features of the images.",
"links": [
{
"rel": "feature-extraction",
"href": "http://adb.us-sanjose-1.oraclecloud.com/omlmod/v1/cognitive-image/feature-extraction"
},
{
"rel": "self",
"href": "http://adb.us-sanjose-1.oraclecloud.com/omlmod/v1/cognitive-image/."
}
]
}
],
"links": [
{
"rel": "self",
"href": "http://adb.us-sanjose-1.oraclecloud.com/omlmod/v1/cognitive-image"
}
]
}
2: Get the Top N Categories of an Image
This example shows how to get the top N categories of an image that is in .jpg
format by sending a POST request to the cognitive-image/classification
endpoint. This example also returns the probabilities of each categories that the input images belong to.
img.jpg
could possibly belong to along with the probabilities:curl -X POST --header "Authorization: Bearer $token" <oml-cloud-service-location-url>/omlmod/v1/cognitive-image/classification \
-F imageData=@img.jpg
-F topN=4
$token
is the environmental variable that is assigned to the token obtained through the Authorization API.img.png
is the path of the image file. In this example, the picture contains a small dog.4
is the number of top classes with the corresponding probabilities returned by the model.topN
is an optional parameter for classification endpoints. Pass this parameter as part of the input to determine how many categories to return for each images sorted by probabilities.topN
must be>=0
. By default, the top5
classes with the highest probabilities are returned. WhentopN=0
, the entire unsorted classification output list is returned.
The command returns the top 4 classes along with their respective probabilities. Here is the scoring result:
{
"scoringResults": [
{
"classifications": [
{
"label": "Dog",
"probability": 0.9995992183685303
},
{
"label": "Puppy",
"probability": 0.9975613355636597
},
{
"label": "Companion dog",
"probability": 0.997296154499054
},
{
"label": "Dog breed",
"probability": 0.9910918474197388
}
]
}
]
}
3: Get the Image Feature Extraction Results
This example demonstrates how to get the image feature extraction results for the input images by sending a POST request to the cognitive-image/feature-extraction
endpoint. This functionality uses the prebuilt ONNX image model for scoring.
Run the following cURL command to get the most relevant keywords:
curl -X POST --header "Authorization: Bearer $token"
<oml-cloud-service-location-url>/omlmod/v1/cognitive-image/feature-extraction \
-F imageData=@img.png
The command returns the weights of all features.
{
"scoringResults": [
{
"weights": [
0.1776999,
0.30117133,
0.0,
0.0,
0.0,
0.06017106,
0.0,
0.007070067,
0.0,
0.0,
0.0
]
}
]
}