Skip to content
Related Articles

Related Articles

Improve Article

Tensorflow.js tf.image.nonMaxSuppressionPaddedAsync() Function

  • Last Updated : 03 Aug, 2021

Tensorflow.js is an open-source library developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The .image.nonMaxSuppressionPaddedAsync() function is used to asynchronously execute the non-maximum suppression of the limiting boxes on the basis of iou i.e. intersection over union along with a chance to pad outcomes.

Syntax:

tf.image.nonMaxSuppressionPaddedAsync(boxes, scores, maxOutputSize, 
    iouThreshold?, scoreThreshold?, padToMaxOutputSize?)

Parameters:  

  • boxes: The stated 2d tensor, which is of configuration [numBoxes, 4]. And every access is [y1, x1, y2, x2], allowing that (y1, x1) and (y2, x2) are the edges of the restricting box. It can be of type tf.Tensor2D, TypedArray, or Array.
  • scores: The stated 1d tensor, provided that the box scores is of configuration [numBoxes]. It is of type tf.Tensor2D, TypedArray, or Array.
  • maxOutputSize: It is the maximum count of the stated boxes that is to be picked. It is of type number.
  • iouThreshold: It is the stated float signifying the threshold in order to decide if the stated boxes intersect too much with reference to the IOU. It should be in the midst of [0, 1]. The by default value is 0.5 i.e. 50 percent of the box intersects. It is optional and is of type number.
  • scoreThreshold: It is the stated threshold in order to decide at which time boxes are to be removed on the basis of the stated score. The by default value is -inf, i.e. every single score is allowed. It is optional and is of type number.
  • padToMaxOutputSize: It is an optional parameter of type Boolean. The by default value is false. In case, it’s true, then the dimension of the result selectedIndices is padded to the maxOutputSize.

Return Value: It returns promise of {[name: string]: tf.Tensor}.



Example 1: In this example, we will be going to use a 2d tensor, scores, and maxOutputSize parameters.

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs";
  
// Calling image.nonMaxSuppressionPaddedAsync() method
const output = await tf.image.nonMaxSuppressionPaddedAsync(
  tf.tensor2d([1, 2, 3, 4, 2, 4, 6, 7], [2, 4]), [1, 1], 4 );
  
// Printing output
console.log(output);

Output:

{
  "selectedIndices": {
    "kept": false,
    "isDisposedInternal": false,
    "shape": [2],
    "dtype": "int32",
    "size": 2,
    "strides": [],
    "dataId": {
      "id": 54
    },
    "id": 54,
    "rankType": "1",
    "scopeId": 27
  },
  "validOutputs": {
    "kept": false,
    "isDisposedInternal": false,
    "shape": [],
    "dtype": "int32",
    "size": 1,
    "strides": [],
    "dataId": {
      "id": 55
    },
    "id": 55,
    "rankType": "0",
    "scopeId": 27
  }
}

Example 2: In this example, we will be going to use an array of floats, iouThreshold, scoreThreshold, as well as padToMaxOutputSize.

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs";
  
// Defining an array of floats
const arr = [
  [11.1, 2.3, 7.3, 6.4], [3, 6]]
  
// Calling image.nonMaxSuppressionPaddedAsync() method
const res = await tf.image.nonMaxSuppressionPaddedAsync(
  arr, [2.1, 0], 100, 0.5, 1, true);
  
// Printing output
console.log(res);

Output:

{
  "selectedIndices": {
    "kept": false,
    "isDisposedInternal": false,
    "shape": [2],
    "dtype": "int32",
    "size": 2,
    "strides": [],
    "dataId": {
      "id": 62
    },
    "id": 62,
    "rankType": "1",
    "scopeId": 29
  },
  "validOutputs": {
    "kept": false,
    "isDisposedInternal": false,
    "shape": [],
    "dtype": "int32",
    "size": 1,
    "strides": [],
    "dataId": {
      "id": 63
    },
    "id": 63,
    "rankType": "0",
    "scopeId": 29
  }
}

Reference: https://js.tensorflow.org/api/latest/#image.nonMaxSuppressionPaddedAsync




My Personal Notes arrow_drop_up
Recommended Articles
Page :