Open In App

Tensorflow.js tf.image.nonMaxSuppressionWithScoreAsync() Function

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.nonMaxSuppressionWithScoreAsync() function is used to asynchronously execute the non-maximum suppression of the limiting boxes on the basis of iou i.e. intersection over the union. Moreover, this operation also favors a Soft-NMS mode where boxes decrease the stated score of different intersecting boxes, thus supporting various areas of the image besides high scores. In order to enable the aforementioned Soft-NMS mode, we need to set the softNmsSigma parameter to be greater than zero.



Syntax:

tf.image.nonMaxSuppressionWithScoreAsync(boxes, scores, maxOutputSize,
iouThreshold?, scoreThreshold?, softNmsSigma?)

Parameters: This method accepts the following parameters:



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

Example 1: Using a 2d tensor, scores, and maxOutputSize parameters.




// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs")
  
// Calling image.nonMaxSuppressionWithScoreAsync() method
const output = tf.image.nonMaxSuppressionWithScoreAsync(
    tf.tensor2d([1, 2, 3, 4, 2, 4, 6, 7], [2, 4]), [1, 1], 4);
  
// Printing output
console.log(output.then((result) => console.log(result)));

Output:

{
  selectedIndices: Tensor {
    kept: false,
    isDisposedInternal: false,
    shape: [ 2 ],
    dtype: 'int32',
    size: 2,
    strides: [],
    dataId: { id: 2 },
    id: 2,
    rankType: '1'
  },
  selectedScores: Tensor {
    kept: false,
    isDisposedInternal: false,
    shape: [ 2 ],
    dtype: 'float32',
    size: 2,
    strides: [],
    dataId: { id: 3 },
    id: 3,
    rankType: '1'
  }
}

Example 2: Using an array of floats, iouThreshold, scoreThreshold, as well as softNmsSigma.




// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs")
  
// Defining an array of floats
const arr = [[11.1, 2.3, 7.3, 6.4], [3, 6]]
  
// Calling image.nonMaxSuppressionWithScoreAsync() method
const output = tf.image.nonMaxSuppressionWithScoreAsync(
    arr, [2.1, 0], 100, 0.5, 1, 0.5);
  
// Printing output
console.log(output.then((result) => console.log(result)));

Output:

{
  selectedIndices: Tensor {
    kept: false,
    isDisposedInternal: false,
    shape: [ 1 ],
    dtype: 'int32',
    size: 1,
    strides: [],
    dataId: { id: 2 },
    id: 2,
    rankType: '1'
  },
  selectedScores: Tensor {
    kept: false,
    isDisposedInternal: false,
    shape: [ 1 ],
    dtype: 'float32',
    size: 1,
    strides: [],
    dataId: { id: 3 },
    id: 3,
    rankType: '1'
  }
}

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


Article Tags :