'using a `tf.Tensor` as a Python `bool` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function

Currently I faced this error, can anyone help solve it?

---------------------------------------------------------------------------
OperatorNotAllowedInGraphError            Traceback (most recent call last)
<ipython-input-24-0211c82920d0> in <module>
      7 warnings.filterwarnings("ignore")
      8 model.train(dataset_train,dataset_val, learning_rate=config.LEARNING_RATE,epochs=5,
----> 9             layers='heads')
/kaggle/working/maskrcnn/Mask_RCNN-master/mrcnn/model.py in train(self, train_dataset, val_dataset, learning_rate, epochs, layers, augmentation, custom_callbacks, no_augmentation_sources)
   2355         log("Checkpoint Path: {}".format(self.checkpoint_path))
   2356         self.set_trainable(layers)
-> 2357         self.compile(learning_rate, self.config.LEARNING_MOMENTUM)
   2358
   2359         # Work-around for Windows: Keras fails on Windows when using
/kaggle/working/maskrcnn/Mask_RCNN-master/mrcnn/model.py in compile(self, learning_rate, momentum)
   2168         for name in loss_names:
   2169             layer = self.keras_model.get_layer(name)
-> 2170             if layer.output in self.keras_model.losses:
   2171                 continue
   2172             loss = (
/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in __bool__(self)
    763       `TypeError`.
    764     """
--> 765     self._disallow_bool_casting()
    766
    767   def __nonzero__(self):

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in _disallow_bool_casting(self)
    532     else:
    533       # Default: V1-style Graph execution.
--> 534       self._disallow_in_graph_mode("using a `tf.Tensor` as a Python `bool`")
    535
    536   def _disallow_iteration(self):

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in _disallow_in_graph_mode(self, task)
    521     raise errors.OperatorNotAllowedInGraphError(
    522         "{} is not allowed in Graph execution. Use Eager execution or decorate"
--> 523         " this function with @tf.function.".format(task))
    524
    525   def _disallow_bool_casting(self):

OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.


Solution 1:[1]

As the error message explain, you try to use a tf.Tensor as a Python bool. This happens generally where condition are expected like in:

if layer.output in self.keras_model.losses:

The part layer.output in self.keras_model.losses should evaluate to a tensor that Python try to use as a bool to check the if condition. This is allowed in eager execution only.

You must either convert the if construct with tf.cond, or rely on @tf.function to make the job for you.

Without more code, it is hard to help you more...

Solution 2:[2]

I have stumble over this also hence i am leaving my solution to this problem to help anyone.

There is a catch when you are in eager execution mode since tf upgraded to 2.x, if you are using keras API loss and metrics you should instantiate them in order to compile.
See the example below:

model.compile(optimizer="...", 
              loss=keras.losses.AnyLoss, 
              metrics=[keras.metrics.AnyMetric])

Above code will give OperatorNotAllowedInGraphError. To overcome do this;

my_loss = keras.losses.AnyLoss(lr, *args, **kwargs)
my_metric = keras.metrics.AnyMetric(*args, **kwargs)

model.compile(optimizer,
              loss = my_loss
              metrics = [my_metric_1, my_metric_2...]

That should do the trick

Solution 3:[3]

To give some context on how I got this error, I was trying to convert darknet weights to a TensorFlow model. I got this error due to the following piece of code:

conv = tf.keras.layers.Conv2D(filters=filters_shape[-1], kernel_size = filters_shape[0], strides=strides, padding=padding,
                              use_bias=not bn, kernel_regularizer=tf.keras.regularizers.l2(0.0005),
                              kernel_initializer=tf.random_normal_initializer(stddev=0.01),
                              bias_initializer=tf.constant_initializer(0.))(input_layer)

if bn: conv = BatchNormalization(dynamic=True)(conv)        ### added dynamic=True

Once I added the argument dynamic=True I was able to resolve this issue.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AlexisBRENON
Solution 2 Cristian Davide Conte
Solution 3 Singh