Open In App

Tensorflow.js tf.layers.cropping2D() Function

Tensorflow.js is a Google-developed open-source toolkit for executing machine learning models and deep learning neural networks in the browser or on the node platform. It also enables developers to create machine learning models in JavaScript and utilize them directly in the browser or with Node.js.

The tf.layers.cropping2D() function is used to crop an input at the top, bottom, left, and right side of an image tensor.



Syntax:

tf.layers.cropping2D(args)

Input Shape: 4D tensor with shape:



Output Shape: 4D with shape:

Parameters:

Returns: It returns an object (Cropping2D).

Example 1:




import * as tf from "@tensorflow/tfjs";
  
const input = tf.input({ shape: [10, 10, 4] });
  
const cropping2DLayer = tf.layers
    .cropping2D({ cropping: [[2, 2],[2, 2]] });
      
const output = cropping2DLayer.apply(input);
  
console.log(output.shape)

Output:

[ null, 6, 6, 4 ]

Example 2:




import * as tf from "@tensorflow/tfjs";
  
const input = tf.input({ shape: [10, 10, 6] });
const cropping2DLayer = tf.layers
    .cropping2D({ 
    cropping: [[3, 2],[2, 3]], 
    dataFormat: 'channelsFirst' 
});
  
const output = cropping2DLayer.apply(input);
  
console.log(output.shape)

Output:

[ null, 10, 5, 1 ]

Reference: https://js.tensorflow.org/api/latest/#layers.cropping2D


Article Tags :