Supercooldude283
Supercooldude283

Reputation: 91

How to add custom "filter" to Conv2D layer in Keras

My research project requires me to add few custom "filters" to the Conv2D layer in Keras (Apart from the filters that Conv2D trained itself). How can I achieve this? Can I achieve this by building any "custom layer"? If yes, can anyone point me towards resources that help me achieve this?

I tried understanding the Conv2D code in github but failed to understand where and how it is handling it's filters.

I am expecting to find a way to add my custom filter say .. [[1 0 0][0 1 0][0 0 1]] to a conv2d layer so that during prediction it convolves the image with the filter that I supplied.

Upvotes: 2

Views: 1325

Answers (1)

Uzzal Podder
Uzzal Podder

Reputation: 3205

In keras it is simple.

Let's have an example. Suppose we want to apply our custom filter onto an input matrix(image)-

enter image description here

Necessary import

import keras.backend as K
import numpy as np
from keras import Input, layers
from keras.models import Model

Definition of the custom filter

enter image description here

# custom filter
def my_filter(shape, dtype=None):

    f = np.array([
            [[[1]], [[0]], [[-1]]],
            [[[1]], [[0]], [[-1]]],
            [[[1]], [[0]], [[-1]]]
        ])
    assert f.shape == shape
    return K.variable(f, dtype='float32')

Dummy example input image (1 channel)

input_mat = np.array([
    [ [4], [9], [2], [5], [8], [3] ],
    [ [3], [6], [2], [4], [0], [3] ],
    [ [2], [4], [5], [4], [5], [2] ],
    [ [5], [6], [5], [4], [7], [8] ],
    [ [5], [7], [7], [9], [2], [1] ],
    [ [5], [8], [5], [3], [8], [4] ]
])
input_mat = input_mat.reshape((1, 6, 6, 1))

Dummy conv model where we will use our custom filter

def build_model():
    input_tensor = Input(shape=(6,6,1))
    x = layers.Conv2D(1, kernel_size = 3,
                      kernel_initializer=my_filter,
                      strides=2, padding='valid') (input_tensor)
    model = Model(inputs=input_tensor, outputs=x)
    return model

Testing

model = build_model()
out = model.predict(input_mat)
print(out)

Output

[[[[ 0.]
   [-4.]]

  [[-5.]
   [ 3.]]]]

Upvotes: 1

Related Questions