Open In App

Tensorflow.js tf.maxPoolWithArgmax() Function

Last Updated : 07 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

The .maxPoolWithArgmax() function is used to determine the 2D max pooling of an image along with argmax list i.e. index. Where, the indices in argmax are leveled, in order that a peak value at position [b, y, x, c] turns compressed index: (y * width + x) * channels + c in case, include_batch_in_index is false and if include_batch_in_index is true then it is ((b * height + y) * width + x) * channels +c. Moreover, the indices returned are consistently in [0, height) x [0, width) prior to flattening.

Syntax:

tf.maxPoolWithArgmax(x, filterSize, 
    strides, pad, includeBatchInIndex?)

Parameters:

  • x: The stated input tensor which is either of rank 4 or else rank 3 and of shape: [batch, height, width, inChannels]. Moreover, in case the rank is 3, then the batch of size 1 is presumed. It can be of type tf.Tensor4D, TypedArray, or Array.
  • filterSize: The stated filter size of shape: [filterHeight, filterWidth]. In case, filter size is a singular number, then filterHeight == filterWidth. It can be of type [number, number], or number.
  • strides: The stated strides of the pooling of shape: [strideHeight, strideWidth]. In case, strides is a singular number, then strideHeight == strideWidth. It can be of type [number, number], or number.
  • pad: The stated type of algorithm for padding. It can be of type valid, same, or number.
    • Here, for same and stride 1, the output would have an identical size as input, irrespective of the filter size.
    • For, ‘valid’ the output shall be smaller than the input in case, the filter size is larger than 1*1×1.
  • includeBatchInIndex: It is optional and is of type boolean.

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

Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Defining input tensor
const x = tf.tensor4d([1, 2, 3, 4], [2, 2, 1, 1]);
 
// Calling maxPoolWithArgmax() method
const result = tf.maxPoolWithArgmax(x, 3, 2, 'same');
 
// Printing output
console.log(result)


Output:    

{
  "result": {
    "kept": false,
    "isDisposedInternal": false,
    "shape": [
      2,
      1,
      1,
      1
    ],
    "dtype": "float32",
    "size": 2,
    "strides": [
      1,
      1,
      1
    ],
    "dataId": {
      "id": 20
    },
    "id": 20,
    "rankType": "4",
    "scopeId": 14
  },
  "indexes": {
    "kept": false,
    "isDisposedInternal": false,
    "shape": [
      2,
      1,
      1,
      1
    ],
    "dtype": "float32",
    "size": 2,
    "strides": [
      1,
      1,
      1
    ],
    "dataId": {
      "id": 21
    },
    "id": 21,
    "rankType": "4",
    "scopeId": 14
  }
}

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Calling maxPoolWithArgmax() method
console.log(tf.maxPoolWithArgmax(
    tf.tensor4d([1.1, 2.1, 3.1, 4.1],
    [1, 2, 2, 1]), [1, 2], [1, 1],
    'valid', true
));


Output:

{
  "result": {
    "kept": false,
    "isDisposedInternal": false,
    "shape": [
      1,
      2,
      1,
      1
    ],
    "dtype": "float32",
    "size": 2,
    "strides": [
      2,
      1,
      1
    ],
    "dataId": {
      "id": 80
    },
    "id": 80,
    "rankType": "4",
    "scopeId": 54
  },
  "indexes": {
    "kept": false,
    "isDisposedInternal": false,
    "shape": [
      1,
      2,
      1,
      1
    ],
    "dtype": "float32",
    "size": 2,
    "strides": [
      2,
      1,
      1
    ],
    "dataId": {
      "id": 81
    },
    "id": 81,
    "rankType": "4",
    "scopeId": 54
  }
}

Reference: https://js.tensorflow.org/api/latest/#maxPoolWithArgmax



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads