Open In App

Tensorflow.js tf.layers.separableConv2d() Function

Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment. It also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or in Node.js.

The tf.layers.separableCov2d() function is separable convolution which is a way to factorize a convolution kernel into two smaller kernels. Separable convolution consists of depthwise convolution and pointwise convolution which mixes together the resulting output channels. The depthMultiplier argument controls how many output channels are generated per input channel in the depthwise step. separableCov2d is Depthwise separable 2D convolution. 



Syntax: 

tf.layers.separableCov2d( args )

Parameters:



Returns: It returns SeparableConv2D.

Example 1:




import * as tf from "@tensorflow/tfjs";
 
// InputShape and Input layer for convLstm2dCell layer
const InputShape = [ 4,  5, 2];
const input = tf.input({ shape: InputShape });
 
// Creating ConvLstm2dCell
const separableConv2d = tf.layers.separableConv2d(
    { filters: 3, kernelSize: 2, batchInputShape: [ 4, 5, 3]});
const output = separableConv2d.apply(input);
 
// Printing summary of layers
const model = tf.model({ inputs: input, outputs: output });
model.summary();

Output:

__________________________________________________________________________________________
Layer (type)                Input Shape               Output shape              Param #   
==========================================================================================
input12 (InputLayer)        [[null,4,5,2]]            [null,4,5,2]              0         
__________________________________________________________________________________________
separable_conv2d_SeparableC [[null,4,5,2]]            [null,3,4,3]              17        
==========================================================================================
Total params: 17
Trainable params: 17
Non-trainable params: 0
__________________________________________________________________________________________

Example 2:




// Import the header file
import * as tf from "@tensorflow/tfjs"
 
// Creating separableConv2d layer
const separableConv2d = tf.layers.separableConv2d({
    filters : 2,
    kernelSize: 3,
    batchInputShape: [2, 3, 5, 5]
});
 
// Create an input with 2 time steps.
const input = tf.input({shape: [3, 4, 5]});
const output = separableConv2d.apply(input);
 
// Printing the Shape of file
console.log(JSON.stringify(output.shape));

Output:

[null,1,2,2]

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


Article Tags :