Open In App

Tensorflow.js tf.image.resizeBilinear() 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.resizeBilinear() function is used to bilinearly rescale an individual 3D image or else a heap of 3D images to a different configuration.



Syntax:

tf.image.resizeBilinear(images, size, alignCorners?, halfPixelCenters?)

Parameters:  



Return Value: It returns tf.Tensor3D, or tf.Tensor4D.

Example 1: In this example, we are going to use a 4d tensor and a size parameter inside tf.image.resizeBilinear() function.




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Calling image.resizeBilinear() method and
// Printing output
tf.image.resizeBilinear(tf.tensor4d([[
 
  [[4, 7], [21, 9]],
 
  [[8, 9], [1, 33]]
 
]]), [3, 4]).print();

Output:

Tensor
    [[[[4        , 7        ],
       [12.5     , 8        ],
       [21       , 9        ],
       [21       , 9        ]],

      [[6.666667 , 8.333333 ],
       [7.1666665, 16.666666],
       [7.666666 , 25       ],
       [7.666666 , 25       ]],

      [[8        , 9        ],
       [4.5      , 21       ],
       [1        , 33       ],
       [1        , 33       ]]]]

Example 2:  In this example, we are going to use an array of floats, alignCorners, as well as halfPixelCenters.




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Defining an array of floats
const arr = [[
 
  [[1.1, 1.7, 1.5, 1.1],
  [1.7, 1.9, 8.1, 6.3]],
  [[3.3, 3.4, 3.7, 4.0],
  [5.1, 5.2, 5.3, 5.9]]
 
]];
 
// Calling image.resizeBilinear() method and
// Printing output
tf.image.resizeBilinear(arr, [1, 2], true, false).print();

Output:

Tensor
    [[[[1.1, 1.7, 1.5      , 1.1      ],
       [1.7, 1.9, 8.1000004, 6.3000002]]]]

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


Article Tags :