title: Implementing a custom convolution operation in Tensoflow/Keras
body: <p>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.</p>
  <p>This is the code I'm using presently, but I don't think the operation is applying the way I expect.</p>
  <pre><code>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=&quot;VALID&quot;,
              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))])
  </code></pre>
  <p>This should then ideally go through and highlight the uniform areas of this image</p>
  <p><a href="https://i.stack.imgur.com/O49fS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/O49fS.png" alt="enter image description here" /></a></p>
  <p>But instead it seems to just like blur it</p>
  <p><a href="https://i.stack.imgur.com/FXfeQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FXfeQ.png" alt="enter image description here" /></a></p>
  <p>Is there some way of creating such a convolution layer?</p>