'Implementing a custom convolution operation in Tensoflow/Keras

I want to create a convolutional layer that goes over an array with a window, grabs the highest and lowest values and returns a value inversely proportional to the range.

This is the code I'm using presently, but I don't think the operation is applying the way I expect.

class StandardizedConv2DWithOverride(layers.Conv2D):
    def convolution_op(self, inputs, kernel):
        ranger = np.ptp(kernel) #this finds the range in the kernel
        return tf.nn.conv2d(
            inputs,
            1/ranger, #This is the inversely proportional value
            padding="VALID",
            strides=list(self.strides),
            name=self.__class__.__name__,
        )
    
conv = StandardizedConv2DWithOverride(filters=1,kernel_size=(5,5))
conv.build(input_shape=(5,5,1))
conv.set_weights([np.ones((5,5,1,1)),np.zeros((1))])

This should then ideally go through and highlight the uniform areas of this image

enter image description here

But instead it seems to just like blur it

enter image description here

Is there some way of creating such a convolution layer?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source