Skip to main content

Posts

Showing posts from July, 2022

Separable Convolutions

 https://towardsdatascience.com/a-basic-introduction-to-separable-convolutions-b99ec3102728 Idea of separating one convolution into two. Types: Spatial Separable Convolution Depthwise Separable Convolution Spatial Separable Convolution: Example: Dividing 3x3 kernel into 3x1 and 1x3 kernel. Advantage: Instead of doing one convolution with 9 multiplications, we do 2 convolutions with 3 multiplications each, so 6 in total, to achieve same effect. Limitations: All convolution kernels are not separable - so only limited number of kernels are allowed Not used much Depthwise Separable Convolution: We do depthwise convolution first and do pointwise colvolutions x num of times we want output It’s worth noting that in both Keras and Tensorflow, there is a argument called the “depth multiplier”. It is set to 1 at default. By changing this argument, we can change the number of output channels in the depthwise convolution. For example, if we set the depth multiplier to 2, each 5x5x1 kernel will giv

Depthwise Convolution and Depthwise Separable Convolutions

 Depthwise Convolution: We apply single convolutional filter for each input channel. Keras Convolution https://stackoverflow.com/questions/51930312/how-to-include-a-custom-filter-in-a-keras-based-cnn import numpy as np from keras.layers import Input, Conv2D from keras.models import Model import keras.backend as K import tensorflow as tf from scipy import ndimage w1 = np.array([[1, -2, 1],                [-2, 4, -2],                [1, -2, 1]]) def filter_init(shape, dtype=None):     w = w1[:, :, np.newaxis, np.newaxis]     assert w.shape == shape     return K.variable(w, dtype='float32') x1 = np.array([[3, 0, 1, 2, 7, 4],               [1, 5, 8, 9, 3, 1],               [2, 7, 2, 5, 1, 3],               [0, 1, 3, 1, 7, 8],               [4, 2, 1, 6, 2, 8],               [2, 4, 5, 2, 3, 9]]) x = K.variable(x1[np.newaxis, :, :, np.newaxis], dtype='float32') print(Conv2D(filters=1, kernel_size=3, kernel_initializer=filter_init, strides=1, padding='valid')(x)) print(