How to fix error: Cannot register 2 metrics with the same name: /tensorflow/api/keras/optimizers

Viewed 10918

When I am trying to run a Python code for deep learning utilizing the TensorFlow library, I am getting the following error:

2021-10-24 10:07:13.619481: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2021-10-24 10:07:13.619752: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2021-10-24 10:07:18.797570: E tensorflow/core/lib/monitoring/collection_registry.cc:77] `Cannot register 2 metrics with the same name: /tensorflow/api/keras/optimizers`
Traceback (most recent call last):
  File "C:\Users\Admin\Downloads\Compressed\Face-Mask-Detection-master\detect_mask_image.py", line 5, in <module>
    from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
  File "C:\Python39\lib\site-packages\keras\api\_v2\keras\__init__.py", line 8, in <module>
    from keras import __version__
  File "C:\Python39\lib\site-packages\keras\__init__.py", line 25, in <module>
    from keras import models
  File "C:\Python39\lib\site-packages\keras\models.py", line 20, in <module>
    from keras import metrics as metrics_module
  File "C:\Python39\lib\site-packages\keras\metrics.py", line 26, in <module>
    from keras import activations
  File "C:\Python39\lib\site-packages\keras\activations.py", line 20, in <module>
    from keras.layers import advanced_activations
  File "C:\Python39\lib\site-packages\keras\layers\__init__.py", line 23, in <module>
    from keras.engine.input_layer import Input
  File "C:\Python39\lib\site-packages\keras\engine\input_layer.py", line 21, in <module>
    from keras.engine import base_layer
  File "C:\Python39\lib\site-packages\keras\engine\base_layer.py", line 43, in <module>
    from keras.mixed_precision import loss_scale_optimizer
  File "C:\Python39\lib\site-packages\keras\mixed_precision\loss_scale_optimizer.py", line 18, in <module>
    from keras import optimizers
  File "C:\Python39\lib\site-packages\keras\optimizers.py", line 26, in <module>
    from keras.optimizer_v2 import adadelta as adadelta_v2
  File "C:\Python39\lib\site-packages\keras\optimizer_v2\adadelta.py", line 22, in <module>
    from keras.optimizer_v2 import optimizer_v2
  File "C:\Python39\lib\site-packages\keras\optimizer_v2\optimizer_v2.py", line 36, in <module>
    keras_optimizers_gauge = tf.__internal__.monitoring.BoolGauge(
  File "C:\Python39\lib\site-packages\tensorflow\python\eager\monitoring.py", line 360, in __init__
    super(BoolGauge, self).__init__('BoolGauge', _bool_gauge_methods,
  File "C:\Python39\lib\site-packages\tensorflow\python\eager\monitoring.py", line 135, in __init__
    self._metric = self._metric_methods[self._label_length].create(*args)
tensorflow.python.framework.errors_impl.
AlreadyExistsError: Another metric with the same name already exists.

Environment:

Python version: 3.9

OS: Windows

Library: TensorFlow API

5 Answers

Update: TensorFlow has now released version 2.7.0 (here), so using the latest version will fix your issue. This specific bug was fixed in version 2.6.2 though actually.


I starting having this issue yesterday during a Github CI build job, but the code was running locally fine.

I solved it by temporarily downgrading to TensorFlow 2.5.2 until the issue is resolved in a future update.

Version 2.5.2 is the latest version before version 2.6.0. I confirmed 2.6.0 and 2.6.1 didn't work for me (at the time of writing).

I happen to have encountered about the same problem on a CI build job today, and wasn't occurring yesterday. So I investigated, and the culprit seems to be keras 2.7 and not tensorflow: https://github.com/keras-team/keras/issues/15585

On my end, the solution was to constraint the version index of Keras to <2.7 but I'll report back if a more stable fix is implemented.

Cheers!

EDIT: the patch release of TF 2.6.2 fixes this (the formerly loose version constraint for keras). So either add a constraint <2.7.0 for keras with your existing TF version or upgrade to TF 2.6.2!

Here, you have got two issues:

  1. CUDA installation missing.
  2. TensorFlow library issue.

For the first case, you can follow the procedure described here

For the issue with the TensorFlow, what worked for me is to downgrade Keras to 2.6.0 using pip install keras==2.6.0 (it should be probably 2.6.1 as of now) to same version as TensorFlow (in my case, TensorFlow version is 2.6.0). Please let me know it works for you.

Encountered this while running code on external GPU, resolved it by upgrading TensorFlow to the 2.7 version

Related