Invalid Argument error while using keras model API inside an estimator model_fn
Asked Answered
B

1

1

The model_fn for custom estimator which I have built is as shown below,

def _model_fn(features, labels, mode):
      """
        Mask RCNN Model function
      """
      self.keras_model = self.build_graph(mode, config)

      outputs = self.keras_model(features) # ERROR STATEMENT
      # outputs = self.keras_model(list(features.values())) # Same ERROR with this statement

      # Predictions
      if mode == tf.estimator.ModeKeys.PREDICT:
        ... # Defining Prediction Spec

      # Training
      if mode == tf.estimator.ModeKeys.TRAIN:
        # Defining Loss and Training Spec
        ...

      # Evaluation
      ...

The _model_fn() receives arguments features and labels from tf.data in form:

features = {
'a' : (batch_size, h, w, 3) # dtype: float
'b' : (batch_size, n) # # dtype: float
}
# And
labels = []

The self.keras_model is built using tensorflow.keras.models.Model API with Input placeholders (defined using layer tensorflow.keras.layers.Input()) of name 'a' and 'b' for respective shapes.

After running the estimator using train_and_evaluate() the _model_fn is running fine. The graph is initialized, but when the training starts I'm facing the following issue:

tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'a' with dtype float and shape [?,128,128,3] [[{{node a}}]]

I have worked with custom estimators before, this the first time using tensorflow.keras.models.Model API inside the _model_fn to compute the graph.

Bemire answered 26/11, 2019 at 8:16 Comment(5)
Please show code for building model.Glazer
I guess you are putting this features dict to input node a directly which should expect a tensor rather than a dict.Forgat
I'm using MaskRCNN model from the official repository with 7 inputs and and 7 outputs. Please refer the build() method in this official repo linkBemire
@zongfan is there a way to feed tensors inside the features dict to the tf.keras.models.Model?Bemire
@SWAPNILMASUREKAR tf.keras.models.Model() only accepts keras Tensor. If you want to input a dict rather than a tensor, wrap a custom model class which inherits this official Model class in this way: tensorflow.org/api_docs/python/tf/keras/….Forgat
B
0

This problem occurs only with this particular model (Mask-RCNN). To overcome this problem slight modifications can be made in method self.build_graph(mode, config) as follows:

def build_graph(mode, config):
    # For Input placeholder definition
    a = KL.Input(tensor=features['a'])
    # Earlier
    # a = KL.Input(shape=[batch_size, h, w, 3], name='a')

    b = KL.Input(tensor=features['b'])
    # Earlier
    # b = KL.Input(shape=[batch_size, n], name='b')
    ...
    ...

These modifications wraps the feature tensor directly into tensorflow.keras.layers.Input(). Which can be later used to define input arguments while defining Model using tensorflow.keras.models.Model.

Bemire answered 30/11, 2019 at 16:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.