0

I'm having trouble reading/understanding Clarifai's API query results. Below is my curl query via php. Everything seems to work but the results I get (see example below) don't correspond with the results when testing on their website. On the website model prediction values range from 0.0 to 1.0 (the closer to 1.0 it is the truer it is). Through the API, however, I'm getting values such as 9.800707E-13. What is the range used in the API?

//initiate curl
$ch = curl_init();

//set up curl options
curl_setopt($ch, CURLOPT_URL, 'https://api.clarifai.com/v2/models/my-model/versions/25cd8d254f9542a2a0473d07c2e02ccf/outputs');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"inputs\": [{\"data\": {\"image\": {\"url\": \"https://live.staticflickr.com/4017/4273847676_9920e12771_b.jpg\"}}}]}");

//set up headers array
$headers = array();
$headers[] = 'Authorization: Key dba0e6dc60414f52af51742cd655cab5';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

//execute curl, check for error and close
$result = curl_exec($ch);
if (curl_errno($ch)) echo 'Error:' . curl_error($ch);
curl_close($ch);

//decode json to an array
$result = json_decode($result, true);
    
//to test
echo "<pre>"; print_r($result); echo "</pre>";

Below is the returned concepts testing from the json result:

                [data] => Array
                    (
                        [concepts] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => Plastic-Straw
                                        [name] => Plastic-Straw
                                        [value] => 0.9999347
                                        [app_id] => 8c51e25474ce4d11a2eb4b390c80203a
                                    )

                                [1] => Array
                                    (
                                        [id] => Straw
                                        [name] => Straw
                                        [value] => 9.800707E-13
                                        [app_id] => 8c51e25474ce4d11a2eb4b390c80203a
                                    )

                            )

                    )

Edit: Below is the unformatted JSON output.

{"status":{"code":10000,"description":"Ok","req_id":"e6a9b93a24854921a1b694bedb0d882a"},"outputs":[{"id":"542ad9f40d704cd19e0b964ab1c34737","status":{"code":10000,"description":"Ok"},"created_at":"2021-01-25T19:43:55.966102577Z","model":{"id":"last-straw-model","name":"Last Straw Model","created_at":"2021-01-06T14:24:36.329385Z","app_id":"8c51e25474ce4d11a2eb4b390c80203a","output_info":{"output_config":{"concepts_mutually_exclusive":false,"closed_environment":true,"max_concepts":0,"min_value":0},"message":"Show output_info with: GET /models/{model_id}/output_info","type":"concept","type_ext":"concept"},"model_version":{"id":"25cd8d254f9542a8a0473d07c2e02bbf","created_at":"2021-01-08T13:34:29.630929Z","status":{"code":21100,"description":"Model is trained and ready"},"total_input_count":77,"completed_at":"2021-01-08T13:34:31.506648Z"},"input_info":{},"train_info":{},"model_type_id":"embedding-classifier"},"input":{"id":"f24405b5483d429381f9e6aeb54d7941","data":{"image":{"url":"https://live.staticflickr.com/4017/4273847676_9920e12771_b.jpg"}}},"data":{"concepts":[{"id":"Plastic-Straw","name":"Plastic-Straw","value":0.9999347,"app_id":"8c51e25474ce4d11a2eb4b390c80203a"},{"id":"Straw","name":"Straw","value":9.800707e-13,"app_id":"8c51e25474ce4d11a2eb4b390c80203a"}]}}]}
garrettlynchirl
  • 790
  • 8
  • 23
  • Can you show what the actual json looks like? I'm guessing that the -13 is a very low close to 0 number, which means that PHP is going have problems with it – aynber Jan 25 '21 at 19:02
  • @aynber I have added an edit with the JSON to the initial question. – garrettlynchirl Jan 25 '21 at 19:46
  • Okay, so you're actually getting the `9.800707E-13` from the API. You'll need to contact their support to find out why it's not sending the proper number – aynber Jan 25 '21 at 19:49

1 Answers1

1

e is appended to the end of numbers to abstract their very large value. 9.800707E-13 is simply used to avoid writing 0.0000000098, etc...

It's used for either very large positive or negative values (e13 or e-13, e5, e-10, etc...)

In the context of prediction, this means that the probability is near null.

Jeremy Faret
  • 316
  • 2
  • 3
  • Got it, also found a function to calculate what the real number is (https://stackoverflow.com/questions/51625341/convert-a-full-string-represented-in-exponential-format-back-in-number-in-php). Many thanks @Jeremy Faret – garrettlynchirl Jan 26 '21 at 12:29