2
votes

I'm training a detection model where train and test data are 3D NumPy array. When start train this model found this type of error. Code link is given below

Training_model.py

detection.py

perform_learning.py

model.fit_generator(generator=training_generator,
                        validation_data=validation_generator,
                        use_multiprocessing=True,
                        workers=6,
                        epochs=epochs,
                        callbacks=[checkpoint, tensorboard])

Traceback (most recent call last): File "/content/SpineFinder-master/train_detection_model.py", line 25, in shuffle=True)

File "/content/SpineFinder-master/learning_functions/perform_learning.py", line 57, in perform_learning callbacks=[checkpoint, tensorboard])

File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/deprecation.py", line 324, in new_func return func(*args, **kwargs)

File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py", line 1479, in fit_generator initial_epoch=initial_epoch)

File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py", line 66, in _method_wrapper return method(self, *args, **kwargs)

File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py", line 848, in fit tmp_logs = train_function(iterator)

File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py", line 580, in call result = self._call(*args, **kwds)

File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py", line 627, in _call self._initialize(args, kwds, add_initializers_to=initializers)

File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py", line 506, in _initialize *args, **kwds))

File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py", line 2446, in _get_concrete_function_internal_garbage_collected graph_function, _, _ = self._maybe_define_function(args, kwargs)

File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py", line 2777, in _maybe_define_function graph_function = self._create_graph_function(args, kwargs)

File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py", line 2667, in _create_graph_function capture_by_value=self._capture_by_value),

File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py", line 981, in func_graph_from_py_func func_outputs = python_func(*func_args, **func_kwargs)

File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py", line 441, in wrapped_fn return weak_wrapped_fn().wrapped(*args, **kwds)

File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py", line 968, in wrapper raise e.ag_error_metadata.to_exception(e)

ValueError: in user code:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:571 train_function * outputs = self.distribute_strategy.run( /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:951 run ** return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_replica
    return self._call_for_each_replica(fn, args, kwargs)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_replica
    return fn(*args, **kwargs)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:543 train_step  **
    self.compiled_metrics.update_state(y, y_pred, sample_weight)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:391 update_state
    self._build(y_pred, y_true)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:333 _build
    self._set_metric_names()

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:353 _set_metric_names
    m._name))

ValueError: Found two metrics with the same name: recall

1
Please include some more code, as there is little context as to what those variables represent.ewong
which version of keras do you use?ewong
Keras version - 2.4.3Abu Farhad

1 Answers

1
votes

The error stems from the following in detection.py:

    recall_background = km.binary_recall(label=0)
    recall_vertebrae = km.binary_recall(label=1)

According to [1] and [2], km.binary_recall() instantiates the keras.metrics.recall() class. However, without the name kwarg, both lines uses the same name recall. Therefore, in order to avoid this, it's my understanding that you'd have to specify the name kwarg like so:

    recall_background = km.binary_recall(name="recall_background", label=0)
    recall_vertebrae = km.binary_recall(name="recall_vertebrae", label=1)

[1] - https://github.com/netrack/keras-metrics/blob/master/keras_metrics/\_\_init__.py#L34

[2] - https://github.com/netrack/keras-metrics/blob/master/keras_metrics/metrics.py#L150