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(ndimage.correlate(input=x1, weights=w1, mode='constant')[1:-1, 1:-1])