0

I need a (non-trainable) layer in Keras which does a local maximum search for a 2-dim input and sums up all entries in a certain environment around that maximum. The input shape to the layer is something like (None, 1, 24, 16).

To make it more clear let's look at how the input would look like:

Input image example

The desired output of the layer should then be:

Desired output example

So it should find every local maximum and then sum up all entries from the (up to) 8 neighbouring pixels to that maximum.

It's a high energy physics related problem, so the interpretation of these "images" should not be too important I think.

How can I accomplish that using Keras layers?

Any suggestions? Thanks!

Pagna401
  • 31
  • 4
  • I really can't understand those images. – Daniel Möller May 03 '18 at 14:35
  • This question may be related? https://stackoverflow.com/questions/3684484/peak-detection-in-a-2d-array?rq=1 – Daniel Möller May 03 '18 at 14:36
  • Unfortunately not. I already have a python function (using numpy arrays) myself for doing this, but I need a Keras layer doing the exact same thing, so that I can utilise this during training of the NN – Pagna401 May 03 '18 at 14:41
  • But is the problem the same as described in that question? – Daniel Möller May 03 '18 at 14:58
  • No. My problem is actually quite easy. For example for the two pixels in my example image with 13 and 26: The layer detects 26 as a local maximum (in a 3x3 "kernel") and then adds all values of the surrounding pixels (in this case only the 13), which leads to 39 in the final output at the position of the local maximum. As easy as that... but now using a Keras layer – Pagna401 May 03 '18 at 15:19
  • Why only the 13? What does that image mean? Are they actually 2D images? If so, what about all the pixels? – Daniel Möller May 03 '18 at 16:30
  • Because the 13 is the only filled pixel in the direct environment of this local maximum. It should only sum up the neighbours of the local maximum... so nothing crazy here. It's really simple and only a few lines of python code using numpy arrays. But I still need it as a Keras layer unfortunately – Pagna401 May 03 '18 at 16:47
  • Can you share your python code? – Daniel Möller May 03 '18 at 16:50
  • If you have python code for this core function, simply write a custom keras layer. All you need to do is to wrap your python function using `tf.py_func` inside the class method `call()`. – pitfall May 03 '18 at 20:50
  • That's exactly what I tried using tf.py_func. So I wrapped my python code in tf.py_func and then I tried to invoke it using a keras.layers.Lambda layer. But that does not work. What's the proper way of combining tf.py_func with a custom keras layer? – Pagna401 May 04 '18 at 10:42

0 Answers0