Open In App

Tensorflow.js tf.layers.depthwiseConv2d() Function

Last Updated : 02 May, 2022
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.depthwiseConv2d() function is used to apply the depthwise separable 2D convolution operation on data.

Syntax:

tf.layers.depthwiseConv2d(args)

Parameters: It accepts the args object which can have the following properties:

  • args: It is an object that accepts the following properties.
    • kernelSize (number|number[]): The convolution window’s dimensions. The convolutional window will be square if kernelSize is a number.
    • depthMultiplier (number): For each input channel, the number of depthwise convolution output channels. FiltersIn * depthMultiplier will equal the entire number of depthwise convolution output channels. 1 is the default value.
    • depthwiseInitializer: The depthwise kernel matrix’s initializer. GlorotNormal is the default value.
    • depthwiseConstraint: The depthwise kernel matrix’s constraint.
    • depthwiseRegularizer: The depthwise kernel matrix’s regularizer function.
    • strides (number|number[]): The convolutional strides in each dimension. Strides in both dimensions are equal if strides are a number.
    • padding: The padding mode.
    • dataFormat: The data format. This specifies the order in which the dimensions in the inputs are ordered. channelsLast is the default value.
    • dilationRate: In each dimension, the dilation rate to utilize for the dilated convolution. It should be an integer or a two- or three-int array.
    • activation: The layer’s activation function.
    • useBias (boolean): If the layer has a bias vector or not. True is the default value.
    • kernelInitializer: The convolutional kernel weights matrix’s initializer.
    • biasInitializer: The bias vector’s initializer.
    • kernelConstraint: The constraint for the convolutional kernel weights.
    • biasConstraint: The constraint for the bias vector.
    • kernelRegularizer: The regularizer function applied to the kernel weights matrix.
    • biasRegularizer: The regularizer function applied to the bias vector.
    • activityRegularizer: The regularizer function applied to the activation.
    • 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.
    • inputDType: It is used for Legacy support.

Returns: It returns an object (DepthwiseConv2D).

Example 1:

Javascript




import * as tf from "@tensorflow/tfjs";
 
const input = tf.input({ shape: [4, 4, 4] });
const depthwiseConv2DLayer = tf.layers.depthwiseConv2d({
    kernelSize: 2,
    depthMultiplier: 2
});
 
const output = depthwiseConv2DLayer.apply(input);
 
const model = tf.model({ inputs: input, outputs: output });
model.predict(tf.ones([1, 4, 4, 4])).print();


Output:

Tensor
   [[[[0.25322, -0.140555, 0.4617286, -0.447692, -0.5744522, -0.0203538, 0.3361098, -0.7265304],
      [0.25322, -0.140555, 0.4617286, -0.447692, -0.5744522, -0.0203538, 0.3361098, -0.7265304],
      [0.25322, -0.140555, 0.4617286, -0.447692, -0.5744522, -0.0203538, 0.3361098, -0.7265304]],
     [[0.25322, -0.140555, 0.4617286, -0.447692, -0.5744522, -0.0203538, 0.3361098, -0.7265304],
      [0.25322, -0.140555, 0.4617286, -0.447692, -0.5744522, -0.0203538, 0.3361098, -0.7265304],
      [0.25322, -0.140555, 0.4617286, -0.447692, -0.5744522, -0.0203538, 0.3361098, -0.7265304]],
     [[0.25322, -0.140555, 0.4617286, -0.447692, -0.5744522, -0.0203538, 0.3361098, -0.7265304],
      [0.25322, -0.140555, 0.4617286, -0.447692, -0.5744522, -0.0203538, 0.3361098, -0.7265304],

Example 2:

Javascript




import * as tf from "@tensorflow/tfjs";
const input = tf.input({ shape: [4, 4, 1] });
const depthwiseConv2DLayer = tf.layers.depthwiseConv2d({
    kernelSize: 3
});
 
const output = depthwiseConv2DLayer.apply(input);
const model = tf.model({ inputs: input, outputs: output });
const x = tf.tensor4d(
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
    [1, 4, 4, 1]
);
 
model.predict(x).print();


Output:

Tensor
   [[[[2.8932226],
      [2.8629632]],
     [[2.7721865],
      [2.7419279]]]]

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads