简体   繁体   English

Keras 自定义层与自定义 function

[英]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.我想创建带有内置图像处理 function 的自定义图层,例如蒙版,或某种模糊/噪声/颜色变化等。

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"它不能那样工作,因为该操作需要一个 numpy 数组,但得到“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.stack.imgur.com/CCpgr.png picture https://i.stack.imgur.com/CCpgr.png图片

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结果我想制作类似编码器网络的东西,但在它中间有一个将输入向量转换为图片的算法,然后网络的右侧部分将解码此图像并返回相同的值,我将其发送到输入层

Not sure what you are trying to do, but I guess a lambda wrapping layer might be the solution.不确定您要做什么,但我想 lambda 包装层可能是解决方案。 Look at the documentation and a useful blog.查看文档和有用的博客。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM