21

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.
2
  • 1
    can you show your code? Commented Dec 12, 2019 at 15:50
  • In case anyone reads this comment, I got this error because I used Accuracy instead of accuracy(case sensitivity) as my metric. Changed that and everything was fine.
    – NelsonGon
    Commented Sep 8, 2020 at 3:14

3 Answers 3

9

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

8

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...

5
  • Hi Sir,this is my github model.py code, u can have a look to it github.com/vincent0924/maskrcnn/blob/master/Mask_RCNN-master/…
    – Vincent
    Commented Dec 14, 2019 at 3:37
  • Sir, can u provide detailed info and code from my model.py as I am beginner. Hardly to understand
    – Vincent
    Commented Dec 14, 2019 at 4:02
  • 1
    "This is allowed in eager execution only". However, I encounter it in Tensorflow 2.1.0. As per documentation, eager execution should be enabled by default. Am I missing something?
    – Aloha
    Commented Feb 28, 2020 at 2:05
  • @PNDA You're right, in TF2.0, eager execution is enabled by default. By the way, there is exceptions (like in input pipeline) and the question did not state which TF version is used. Do you have a link to where you encountered it, maybe I can complete the answer. Commented Feb 28, 2020 at 7:54
  • 2
    How to use tf.cond or @tf.function in this specific case (if layer.output in self.keras_model.losses:) Commented Sep 24, 2020 at 2:54
1

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.

1
  • 1
    I had to follow this answer and also implement compute_output_shape() to make this error go away. The accepted answer by Mert Beşiktepe was not necessary (I'm using tf 2.13.1) Commented Apr 23 at 15:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.