3

I'm trying to build a model in tensorflow by extending the 'Model' class in tensorflow.keras. I need to pass two arguments in the 'call' function of this class, input images x (224,224,3) and output label y. But I get the following error while building the model:

ValueError: Currently, you cannot build your model if it has positional or keyword arguments that are not input to the model, but are required for its 'call' method.

class myCNN(Model):
  def __init__(self):
    super(myCNN, self).__init__()

    base_model = tf.keras.applications.VGG16(input_shape=(224,224,3), weights='imagenet')
    layer_name = 'block5_conv3'
    self.conv_1 = Model(inputs=base_model.input, outputs=base_model.get_layer(layer_name).output)
    self.flatten = L.Flatten(name='flatten')
    self.fc1 = L.Dense(1000, activation='relu', name='fc1')
    self.final = L.Activation('softmax')

  # The problem is because I need y
  def call(self, x, y):
    x = self.conv_1(x)
    x = self.flatten(x)
    x = self.fc1(x)
    return self.final(x)

model = myCNN()
model.build((None, 224, 224, 3, 1))
4
  • Why do you need y? You are not using it in the current computation inside call Commented Nov 6, 2019 at 8:33
  • 1
    I have not shown the usage. I need to mask x before passing it to flatten layer. The mask is dependant on y. As of now I did a workaround by passing [x,y] as a single argument to call() Commented Nov 6, 2019 at 11:21
  • That's not a workaround, it is how it should be implemented. Commented Nov 6, 2019 at 15:30
  • 1
    Did you get any solution for this? Commented Jan 7, 2020 at 15:00

1 Answer 1

3

Inputs parameter of call method can be an input tensor or list/tuple of input tensors.

You can pass two arguments like this:

def call(self, inputs):
    x = inputs[0]
    y = inputs[1]
    x = self.conv_1(x)
    x = self.flatten(x)
    x = self.fc1(x)
    return self.final(x)  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.