Introduction: Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.
The .depthwiseConv2d() function is used to determine Depthwise 2D convolution.
Moreover, for a given 4D input array as well as a filter array of shape: [filterHeight, filterWidth, inChannels, channelMultiplier] comprising inChannels convolutional filters of depth 1, this method operates a distinct filter to all the input channel (expanding out of 1 channel to channelMultiplier channels for each), then concatenates the results jointly. However, the output has inChannels * channelMultiplier channels.
Syntax:
tf.depthwiseConv2d(x, filter, strides, pad, dataFormat?, dilations?, dimRoundingMode?)
Parameters:
- x: The stated input tensor which is either of rank 3 or else rank 4 and of shape: [batch, height, width, inChannels]. Moreover, in case the rank is 3, then the batch of size 1 is presumed. It can be of type tf.Tensor3D, tf.Tensor4D, TypedArray, or Array.
- filter: The stated filter tensor of rank 4 and shape: [filterHeight, filterWidth, inChannels, channelMultiplier]. It can be of type tf.Tensor4D, TypedArray, or Array.
- strides: The stated strides of the convolution: [strideHeight, strideWidth]. In case, stated strides is a single number, then strideHeight == strideWidth. It can be of type [number, number], or number.
- pad: The stated type of algorithm for padding. It can be of type valid, same, number, or ExplicitPadding.
- Here, for same and stride 1, the output would have identical size as input, irrespective of the filter size.
- For, ‘valid’ the output shall be smaller than the input in case, the filter size is larger than 1*1×1.
- dataFormat: The elective string from “NHWC”, or “NCHW”. It specifies the data format of the stated input as well as output data. The by default value is ‘NHWC’. Moreover, the data here is stored in the order of: [batch, height, width, channels]. It is optional and can be of type ‘NHWC’, or ‘NCHW’ but only ‘NHWC’ is currently favored.
- dilations: The stated dilation rates: [dilationHeight, dilationWidth] in that the input values are sampled over the height as well as width dimensions in favor of atrous convolution. The by default value is [1, 1]. Moreover, in case rate is a single number, then dilationHeight == dilationWidth. And if its greater than 1, then all the values of the strides should be 1. It is optional and is of type [number, number], number.
- dimRoundingMode: The stated string out of ‘ceil’, ’round’, or ‘floor’. In case, nothing is stated, then it defaults to truncate. It is optional and can be of type floor, round, or ceil.
Return Value: It returns tf.Tensor3D or tf.Tensor4D.
Example 1:
Javascript
// Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Defining input tensor const x = tf.tensor4d([2, 2, 3, 4], [2, 1, 1, 2]); // Defining filter tensor const y = tf.tensor4d([2, 1, 4, 4], [1, 1, 2, 2]); // Calling depthwiseConv2d() method const result = tf.depthwiseConv2d(x, y, 2, 'same' ); // Printing output result.print(); |
Output:
Tensor [ [ [[4, 2, 8 , 8 ],]], [ [[6, 3, 16, 16],]]]
Example 2:
Javascript
// Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Calling depthwiseConv2d() method tf.tensor4d([2.1, 2.2, 3.4, 4.1], [2, 1, 1, 2]).depthwiseConv2d( tf.tensor4d([2.1, 1.2, 4.2, 1.3], [1, 1, 2, 2]), 1, 1, 'NHWC' , [1, 1], 'round' ).print(); |
Output:
Tensor [[[[0 , 0 , 0 , 0 ], [0 , 0 , 0 , 0 ], [0 , 0 , 0 , 0 ]], [[0 , 0 , 0 , 0 ], [4.4099994, 2.52 , 9.2399998 , 2.8599999], [0 , 0 , 0 , 0 ]], [[0 , 0 , 0 , 0 ], [0 , 0 , 0 , 0 ], [0 , 0 , 0 , 0 ]]], [[[0 , 0 , 0 , 0 ], [0 , 0 , 0 , 0 ], [0 , 0 , 0 , 0 ]], [[0 , 0 , 0 , 0 ], [7.1399999, 4.0800004, 17.2199993, 5.3299994], [0 , 0 , 0 , 0 ]], [[0 , 0 , 0 , 0 ], [0 , 0 , 0 , 0 ], [0 , 0 , 0 , 0 ]]]]
Reference: https://js.tensorflow.org/api/latest/#depthwiseConv2d