Search code examples
pythontensorflowimage-processingkeraskeras-layer

Keras custom layer with custom function


I want to create custom layer with built in an image processing function, for example mask, or some kind of blur/noise/color changing etc.

I wrote this code, but I don't know what should I do with

__return input* mask[[1,0,1,0][0,1,0,1][1,0,1,0][0,1,0,1]]__

its not working like that because that operation requires a numpy array, but get "tensorflow.python.framework.ops.Tensor"

class MyLayer(tf.keras.layers.Layer):
  def __init__(self, num_outputs):
    super(MyLayer, self).__init__()
    self.num_outputs = num_outputs

  def build(self, input_shape):
    self.kernel = self.add_weight("kernel",
                                  shape=[int(input_shape[-1]),
                                         self.num_outputs])
  def call(self, input):

    return input* mask[[1,0,1,0][0,1,0,1][1,0,1,0][0,1,0,1]]
    
    ##return tf.matmul(input, self.kernel)

layer = MyLayer(4,4)

https://i.sstatic.net/CCpgr.png picture

in the result I want to make something like encoder network but in the middle of it an algorithm which transform input vectors to picture, then the right part of network will decode this image and will return same values, which I send to input layer


Solution

  • Not sure what you are trying to do, but I guess a lambda wrapping layer might be the solution. Look at the documentation and a useful blog.

    https://blog.paperspace.com/working-with-the-lambda-layer-in-keras/

    https://www.tensorflow.org/api_docs/python/tf/keras/layers/Lambda?hl=en