OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool` is not allowed in Graph execution. Add Metrics to compile function

Viewed 888

I am using a Convolutional neural Network and after I start running it I get the error stated in my question:

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.

Traceback (most recent call last):

File "/home/CNN1_FOLD1.py", line 452, in main_dense(0.1, DenseNet, error_anal = True, depth=depth)

File "/home/CNN1_FOLD1.py", line 440, in main_dense train(model, x_train, y_train, x_val, y_val, epochs_list = [500, 5000, 300], name = model_name, learning_rate = learning_rate)

File "/home/CNN1_FOLD1.py", line 320, in train 'AUC'])

File "/home/.local/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 75, in symbolic_fn_wrapper return func(*args, **kwargs)

File "/home/.local/lib/python3.7/site-packages/keras/engine/training.py", line 222, in compile masks=masks)

File "/home/.local/lib/python3.7/site-packages/keras/engine/training.py", line 871, in _handle_metrics self._per_output_metrics[i], target, output, output_mask)

File "/home/.local/lib/python3.7/site-packages/keras/engine/training.py", line 842, in _handle_per_output_metrics metric_fn, y_true, y_pred, weights=weights, mask=mask)

File "/home/.local/lib/python3.7/site-packages/keras/engine/training_utils.py", line 1033, in call_metric_function update_ops = metric_fn.update_state(y_true, y_pred, sample_weight=weights)

File "/home/.local/lib/python3.7/site-packages/keras/utils/metrics_utils.py", line 42, in decorated update_op = update_state_fn(*args, **kwargs)

File "/home/.local/lib/python3.7/site-packages/keras/metrics.py", line 318, in update_state matches = self._fn(y_true, y_pred, **self._fn_kwargs)

File "/home/.local/lib/python3.7/site-packages/keras/metrics.py", line 1421, in init thresholds, default_threshold=default_threshold)

File "/home/.local/lib/python3.7/site-packages/keras/utils/metrics_utils.py", line 107, in parse_init_thresholds assert_thresholds_range(to_list(thresholds))

File "/home/.local/lib/python3.7/site-packages/keras/utils/metrics_utils.py", line 98, in assert_thresholds_range invalid_thresholds = [t for t in thresholds if t is None or t < 0 or t > 1]

File "/home/.local/lib/python3.7/site-packages/keras/utils/metrics_utils.py", line 98, in invalid_thresholds = [t for t in thresholds if t is None or t < 0 or t > 1]

File "/home/.local/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 765, in bool self._disallow_bool_casting()

File "/home/.local/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 534, in _disallow_bool_casting self._disallow_in_graph_mode("using a tf.Tensor as a Python bool")

File "/home/.local/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 523, in _disallow_in_graph_mode " this function with @tf.function.".format(task))

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.

I understand it is related to my metrics. I was initially running it as shown below:

   model.compile(loss='categorical_crossentropy', 
                  optimizer=opt, 
                  metrics=['accuracy'])

But I wanted to add some more metrics, so I changed it to:

    model.compile(loss='categorical_crossentropy', 
                  optimizer=opt, 
                  metrics=['accuracy',
                           'Precision',
                           'Recall',
                           'AUC'])

Or:

    model.compile(loss='categorical_crossentropy', 
                  optimizer=opt, 
                  metrics=['accuracy',
                           tf.keras.metrics.Precision(),
                           tf.keras.metrics.Recall(),
                           'AUC'])

Both did not work and gave me the error.

What could I do to solve the issue?

thank you!

2 Answers

I also had a similar problem. Since TensorFlow upgraded to 2.x, to use keras API metrics you should instantiate all of them in order to compile like so:

model.compile(loss='categorical_crossentropy', 
                  optimizer=opt, 
                  metrics=[tf.keras.metrics.Accuracy(),
                           tf.keras.metrics.Precision(),
                           tf.keras.metrics.Recall(),
                           tf.keras.metrics.AUC()])

Hope this helps :)

To give some context on how I got this error, I was trying to convert darknet weights to a TensorFlow model, following this tutorial. 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.

Related