简体   繁体   中英

Use tf.keras.layers.concatenate() as custom layer in tensorflow

I want to make U-net using custom layers in tensorflow. I need use tf.keras.layers.concatenate there and that is my problem. Input tensors for all other layers I can add to layer in method call. But syntax for concatenate layer is tf.keras.layers.concatenate(input, axis) and I need something like this tf.keras.layers.concatenate(axis)(input), but it does not work. Can anybody help me please?
Thank you.

My code is something like this:

class MyModel(tf.keras.Model):
  def __init__(self):
    super(MyModel, self).__init__()
    self.block1 = Conv2D(.....)
    self.block2 = BatchNormalization()
    ....etc.....
    self.decoder_concat = tf.keras.layers.concatenate(axis=-1) #that i need but it does not work

  def call(self, inputs):
     x = self.block1(inputs)
     x = self.block2(x)
     ....etc......
     x = self.decoder_concat([x, concatLayer]) #that i need but it does not work

Providing the solution here (Answer Section), even though it is present in the Comment Section, for the benefit of the community.

After changing tf.keras.layers.concatenate to tf.keras.layers.Concatenate has resolved the issue.

tf.keras.layers.Concatenate which is used as a layer that concatenates list of inputs in Tensorflow, where as tf.keras.layers.concatenate acts as functional interface to the Concatenate layer. Please refer more details here

Please refer updated code in below

class MyModel(tf.keras.Model):
  def __init__(self):
    super(MyModel, self).__init__()
    self.block1 = Conv2D(.....)
    self.block2 = BatchNormalization()
    ....etc.....
    self.decoder_concat = tf.keras.layers.Concatenate(axis=-1) #that i need but it does not work

  def call(self, inputs):
     x = self.block1(inputs)
     x = self.block2(x)
     ....etc......
     x = self.decoder_concat([x, concatLayer]) #that i need but it does not work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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