当前我遇到此错误,有人可以解决吗?
---------------------------------------------------------------------------
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.
答案 0 :(得分:3)
我对此也感到迷迷糊糊,因此我将解决这个问题的方法留给任何人使用。
由于tf升级到2.x,因此在急切的执行模式中会遇到一个问题,如果使用的是keras API损失和指标,则应实例化它们以进行编译。请参见下面的示例;
model.compile(optimizer="...",
loss=keras.losses.AnyLoss,
metrics=[keras.metrics.AnyMetric])
以上代码将给出OperatorNotAllowedInGraphError
。为了克服这一点;
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...]
应该可以解决问题
答案 1 :(得分:0)
如错误消息所述,您尝试使用tf.Tensor
作为Python bool
。这种情况通常发生在需要条件的地方,例如:
if layer.output in self.keras_model.losses:
layer.output in self.keras_model.losses
部分应评估为一个张量,Python会尝试使用该张量来检查if
条件。
仅在急切执行时才允许这样做。
您必须将if
构造转换为tf.cond
,或依靠@tf.function
为您完成工作。
没有更多代码,很难帮助您更多...
答案 2 :(得分:0)
请参阅我的githut model.py,我已关注问题发布: https://github.com/matterport/Mask_RCNN/pull/1896/commits/8c36bf594c48c6a604231b0f91ebca9e9f386b64
,我的代码已更新: https://github.com/vincent0924/maskrcnn/blob/master/Mask_RCNN-master/mrcnn/model.py
但目前在Kaggle培训中遇到此问题:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-18-091d3220b55c> in <module>
3 warnings.filterwarnings("ignore")
4 model.train(dataset_train,dataset_val, learning_rate=config.LEARNING_RATE,epochs=5,
----> 5 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)
2357 log("Checkpoint Path: {}".format(self.checkpoint_path))
2358 self.set_trainable(layers)
-> 2359 self.compile(learning_rate, self.config.LEARNING_MOMENTUM)
2360
2361 # 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)
2175 tf.reduce_mean(layer.output, keepdims=True)
2176 * self.config.LOSS_WEIGHTS.get(name, 1.))
-> 2177 self.keras_model.add_loss(loss)
2178
2179 # Add L2 Regularization
/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/base_layer.py in add_loss(self, losses, inputs)
1079 for symbolic_loss in symbolic_losses:
1080 if getattr(self, '_is_graph_network', False):
-> 1081 self._graph_network_add_loss(symbolic_loss)
1082 else:
1083 # Possible a loss was added in a Layer's `build`.
/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/network.py in _graph_network_add_loss(self, symbolic_loss)
1482 new_nodes.extend(add_loss_layer.inbound_nodes)
1483 new_layers.append(add_loss_layer)
-> 1484 self._insert_layers(new_layers, new_nodes)
1485
1486 def _graph_network_add_metric(self, value, aggregation, name):
/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/network.py in _insert_layers(self, layers, relevant_nodes)
1437
1438 # Insert layers and update other layer attrs.
-> 1439 layer_set = set(self._layers)
1440 for layer in layers:
1441 if layer not in layer_set:
/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/training/tracking/data_structures.py in __hash__(self)
596 # List wrappers need to compare like regular lists, and so like regular
597 # lists they don't belong in hash tables.
--> 598 raise TypeError("unhashable type: 'ListWrapper'")
599
600 def insert(self, index, obj):
TypeError: unhashable type: 'ListWrapper'