Open In App

Tensorflow.js tf.layers.cropping2D() Function

Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • If dataFormat is channelsLast: [batch, rows, cols, channels]
  • If data_format is channelsFirst: [batch, channels, rows, cols].

Output Shape: 4D with shape:

  • If dataFormat is channelsLast: [batch, croppedRows, croppedCols, channels]
  • If dataFormat is channelsFirst: [batch, channels, croppedRows, croppedCols].

Parameters:

  • args: It is an object type that accepts the following properties:
    • cropping (number|[number, number]|[[number, number], [number, number]]): The dimension of the cropping along the width and the height.
    • dataFormat: The data format. This specifies the order in which the dimensions in the inputs are ordered. channelsLast is the default value.
    • inputShape: If this property is set, it will be utilized to construct an input layer that will be inserted before this layer. 
    • batchInputShape: If this property is set, an input layer will be created and inserted before this layer. 
    • batchSize: If batchInputShape isn’t supplied and inputShape is, batchSize is utilized to build the batchInputShape.
    • dtype: It is the kind of data type for this layer. float32 is the default value. This parameter applies exclusively to input layers.
    • name: This is the layer’s name and is of string type.
    • trainable: If the weights of this layer may be changed by fit. True is the default value.
    • weights: The layer’s initial weight values.

Returns: It returns an object (Cropping2D).

Example 1:

Javascript




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:

Javascript




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



Last Updated : 25 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads