Open In App

Tensorflow.js tf.image.nonMaxSuppressionAsync() 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.nonMaxSuppressionAsync() function is used to execute the non maximum suppression of the limiting boxes on the basis of iou i.e. intersection over union. Moreover, its the async interpretation of nonMaxSuppression() method.



Syntax:

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

Parameters:  



Return Value: It returns Promise of tf.Tensor1D.

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




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

Output:

Tensor
    [0, 1]

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




// 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.nonMaxSuppressionAsync() method
const res = await tf.image.nonMaxSuppressionAsync(
    arr, [2.1, 0], 100, 0.5, 1);
  
// Printing output
res.print();

Output:

Tensor
    [0]

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


Article Tags :