Wednesday, July 3, 2024
HomeLanguagesJavascriptTensorflow.js tf.image.resizeNearestNeighbor() Function

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

Syntax:

tf.image.resizeNearestNeighbor(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 will be going to use a 4d tensor and a size parameter.

Javascript




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


Output:

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

      [[4 , 7 ],
       [4 , 7 ],
       [21, 9 ],
       [21, 9 ]],

      [[8 , 9 ],
       [8 , 9 ],
       [1 , 33],
       [1 , 33]]]]

Example 2: In this example, we will be 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.resizeNearestNeighbor() method
// and printing output
tf.image.resizeNearestNeighbor(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.resizeNearestNeighbor

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments