Open In App

Tensorflow.js tf.image.resizeBilinear() Function

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

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:  

  • images: The stated images of rank 4 or else rank 3, which is of configuration [batch, height, width, inChannels]. Moreover, in case its of rank 3, then the batch of one is presumed. It can be of type tf.Tensor3D, tf.Tensor4D, TypedArray, or Array.
  • size: The different stated configuration [newHeight, newWidth] in order to rescale the images. Every channel is rescaled one by one. It is of type [number, number].
  • alignCorners: It is the optional parameter whose by default value is false. In case its true, the input is resized by (new_height – 1) / (height – 1), that absolutely queues the four corners of the stated images as well as rescaled images. However, if its false then it is rescaled by new_height / height. It deals in the same manner with the width dimension. It is of type Boolean.
  • halfPixelCenters: It is the optional parameter whose by default value is false. It is of type Boolean.

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.

Javascript




// 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.

Javascript




// 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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads