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

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

This appears to be a bug with tensorflow 2.6 see https://forums.developer.nvidia.com/t/unable-to-import-keras-models-on-tensorflow-2-6-0-jetpack-v46/191904
I had the same issue and solved it by downgrading to tensorflow 2.5 until the issue is resolved in a future update.
**Edit: 2.7 is out and seems to have fixed the issue.
*Note: I am using tensorflow with my CPU, not a GPU.

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:
CUDA installation missing.
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

Tensorflow 2x - ModuleNotFoundError: No module named 'tensorflow.contrib'

I'm working on a Tensorflow project that needs to be upgraded to use tensorflow==2.2.0, and can no longer work with Tensorflow 1.x, due to comparability with other packages.
However, after upgrading, I'm getting the below error:
File "/opt/app-root/lib/python3.6/site-packages/tflearn/__init__.py", line 4, in <module>
from . import config
File "/opt/app-root/lib/python3.6/site-packages/tflearn/config.py", line 5, in <module>
from .variables import variable
File "/opt/app-root/lib/python3.6/site-packages/tflearn/variables.py", line 7, in <module>
from tensorflow.contrib.framework.python.ops import add_arg_scope as contrib_add_arg_scope
ModuleNotFoundError: No module named 'tensorflow.contrib'
The only help I can find regarding this error is to downgrade.
What, if any, is the recommend approach to migrate up to Tensorflow 2.x when using tflearn?
It is not directly possible. You have to rebuild different routines and several functions need to be altered accordingly. There are not 100% differences between 1.X and 2.X version, but at-least some functions are new and some old options are deprecated. If you upgrade step by step, you can see there are deprecation warnings for next level upgradations.

Tensorflow TypeError: expected bytes, Descriptor found

I've been following this tutorial for recognising an object using machine learning:
https://www.youtube.com/watch?v=Rgpfk6eYxJA
I've followed all the instructions on what to install and how, including those in this related tutorial:
https://www.youtube.com/watch?v=RplXYjxgZbw
I tried both with their version and the newest available versions of the software. With the exception that I create the virtual environment like this:
conda create -n tensorflow1 pip python=3.6
Because the tensorflow module isn't yet compatible with python 3.7.
After I install all the packages needed, also described here:
https://github.com/EdjeElectronics/TensorFlow-Object-Detection-API-Tutorial-Train-Multiple-Objects-Windows-10
Under 2d. Set up new Anaconda virtual environment
and go through the code in the video, I run into a error when I run
python generate_tfrecord.py --csv_input=images\train_labels.csv --image_dir=images\train --output_path=train.record
which is working in the video at 19:35.
The error is
2019-12-11 10:13:43.410540: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_100.dll'; dlerror: cudart64_100.dll not found
Traceback (most recent call last):
File "generate_tfrecord.py", line 17, in <module>
import tensorflow as tf
File "C:\Anaconda\envs\tensorflow1\lib\site-packages\tensorflow\__init__.py", line 98, in <module>
from tensorflow_core import *
File "C:\Anaconda\envs\tensorflow1\lib\site-packages\tensorflow_core\__init__.py", line 40, in <module>
from tensorflow.python.tools import module_util as _module_util
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 947, in _find_and_load_unlocked
File "C:\Anaconda\envs\tensorflow1\lib\site-packages\tensorflow\__init__.py", line 50, in __getattr__
module = self._load()
File "C:\Anaconda\envs\tensorflow1\lib\site-packages\tensorflow\__init__.py", line 44, in _load
module = _importlib.import_module(self.__name__)
File "C:\Anaconda\envs\tensorflow1\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "C:\Anaconda\envs\tensorflow1\lib\site-packages\tensorflow_core\python\__init__.py", line 52, in <module>
from tensorflow.core.framework.graph_pb2 import *
File "C:\Anaconda\envs\tensorflow1\lib\site-packages\tensorflow_core\core\framework\graph_pb2.py", line 16, in <module>
from tensorflow.core.framework import node_def_pb2 as tensorflow_dot_core_dot_framework_dot_node__def__pb2
File "C:\Anaconda\envs\tensorflow1\lib\site-packages\tensorflow_core\core\framework\node_def_pb2.py", line 16, in <module>
from tensorflow.core.framework import attr_value_pb2 as tensorflow_dot_core_dot_framework_dot_attr__value__pb2
File "C:\Anaconda\envs\tensorflow1\lib\site-packages\tensorflow_core\core\framework\attr_value_pb2.py", line 16, in <module>
from tensorflow.core.framework import tensor_pb2 as tensorflow_dot_core_dot_framework_dot_tensor__pb2
File "C:\Anaconda\envs\tensorflow1\lib\site-packages\tensorflow_core\core\framework\tensor_pb2.py", line 16, in <module>
from tensorflow.core.framework import resource_handle_pb2 as tensorflow_dot_core_dot_framework_dot_resource__handle__pb2
File "C:\Anaconda\envs\tensorflow1\lib\site-packages\tensorflow_core\core\framework\resource_handle_pb2.py", line 16, in <module>
from tensorflow.core.framework import tensor_shape_pb2 as tensorflow_dot_core_dot_framework_dot_tensor__shape__pb2
File "C:\Anaconda\envs\tensorflow1\lib\site-packages\tensorflow_core\core\framework\tensor_shape_pb2.py", line 112, in <module>
'__module__' : 'tensorflow.core.framework.tensor_shape_pb2'
TypeError: expected bytes, Descriptor found
This problem is the same that appears in the jupyter kernel when I run the imports that appear in the video at 14:25
How do I fix the
TypeError: expected bytes, Descriptor found
Error?
And what's with
Could not load dynamic library 'cudart64_100.dll'; dlerror: cudart64_100.dll not found
That also appears?
I can also share this with you, in the second tutorial, the one just about installing tensorflow-gpu library, after I create an account for cuDNN and download it as inscribed, I only get a cudnn64_7.dll file in C:\cuda\bin which is in my system path environment variable, just as are
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\libnvvp and
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\extras\CUPTI\lib64,
as instructed in the tutorial. As you can see, I have version 10.1 of Cuda and cuDNN and the paths are a bit different. The GPU Driver is also updated.
P.S. in the tensorflow installing tutorial, the test code doesn't work either.
This is all the information I think I have to offer.
I've been trying to solve this problem for 4-5 days at this point (and this is not my first video I watch to get a .record file for an image recognition neural network)
and the solutions for this particular problem offered in TypeError: expected bytes, Descriptor found or any other place on stackoverflow are not useful.
What should I do?
P.S. The tensorflow-gpu version I have is 2.0.0, and it might not be compatible with Cuda and cuDNN. It might be why I only have a cudnn64_7.dll file and not a cudart64_100.dll file. If no one has other solutions, I'll just install tensorflow 1.5 and try the software again.
If someone has another solution however, by all means, post it. I'll post a reply if it works. I'll edit this if it doesn't.
I've followed a different tutorial, however came across the same errors.
In case anyone is still wondering, I've fixed it by updating the tensorflow version from 1.5 originally to 1.15
pip install --ignore-installed --upgrade tensorflow-gpu==1.15.0
This is the official issue where I got the idea from.
As for the second part,
Could not load dynamic library 'cudart64_100.dll'; dlerror: cudart64_100.dll not found
This is an issue with the CUDA drivers. In short, there's compatibility issue between the tensorflow and your GPU. In most cases, don't worry too much, since it will default to using your CPU over GPU for training of a model. In case you really want to use the GPU (for better performance etc) check if it's supported. You can check similarly asked question, or from an official source.
Alternatively, since you've installed CUDA 10.1, as per official documentation, you'll need to upgrade tensorflow 2.1.0 or above to make it work.
Personally, I had to opt to using tensorflow 1.15 over 2.2.0 and installing CUDA 9.0 to make everything run. However, I'm working on a laptop with a mobile 1050 GPU, and no matter what, I couldn't get it to run otherwise.

Tensorflow-gpu, has no attribute "python_io"

I am trying to create a custom TFrecords.
When I called a script that uses the tf.python_io.TFRecordWriter(..) as ...:
this error came up
log:
Traceback (most recent call last):
File "createtfrecordsbyother.py", line 105, in <module>
main(args)
File "createtfrecordsbyother.py", line 85, in main
num_shards=num_shards_train)
File "createtfrecordsbyother.py", line 45, in create_tfrecords
with tf.python_io.TFRecordWriter(output_filename) as tfrecord_writer:
AttributeError: module 'tensorflow' has no attribute 'python_io'
Switched to the tensorflow,ver1.15 .
After switching to that version that error is gone and replaced with a warning that states module is depreciated with a new name (io) instead of (python_io)
Note many modules also changed the name.
and downgrading tensorflow may require a updates of CUDA and Cudnn library.
It's not a complete solution to those who wanted to stay in Tensorflow2.0-gpu
But I will post this as a solution for those who are like me fine with downgrade to 1.15.
For uninstall tf,please refer to tf official documentation under install page.

AttributeError: module 'tensorflow.estimator' has no attribute 'SessionRunHook'

I'm running TensorFlow version 1.9 on Ubuntu 18. I am trying to use tensorboard visualization .
I have installed tensorboard 1.9 with my anaconda prompt.
I have excuted this command :
tensorboard --logdir=./eval/ --port=8090 --host=127.0.0.1
eval is the file that contains summary data that i have generated when running TensorFlow .
The script fails with:
File "/home/anaconda3/envs/tensorflow_cpu/bin/tensorboard", line 7, in
from tensorboard.main import run_main
File "/home/anaconda3/envs/tensorflow_cpu/lib/python3.6/site-packages/tensorboard/main.py", line 44, in
from tensorboard import default
File "/home/anaconda3/envs/tensorflow_cpu/lib/python3.6/site-packages/tensorboard/default.py", line 36, in
from tensorboard.plugins.beholder import beholder_plugin
File "/home/anaconda3/envs/tensorflow_cpu/lib/python3.6/site-packages/tensorboard/plugins/beholder/__init__.py", line 15, in
from tensorboard.plugins.beholder.beholder import Beholder
File "/home/anaconda3/envs/tensorflow_cpu/lib/python3.6/site-packages/tensorboard/plugins/beholder/beholder.py", line 199, in
class BeholderHook(tf.estimator.SessionRunHook):
AttributeError: module 'tensorflow.estimator' has no attribute 'SessionRunHook'
Please could someone help me find the solution to this error?
Thank you
This might be caused due to 2 reasons:
inconsistency in versions between your Tensorflow and Tensosrboard.
if your tensorflow version is less than 1.10
Try to upgrade the versions of Tensorflow and Tensorboard so that the corresponding versions are greater than 1.12 and make sure the versions are consistent with each other. This should solve your problem.
For more information, you can refer to the following issues [1] and [2]

Anaconda install of Tensorflow missing 'audio_ops' from contrib framework

I'm trying to follow along the Audio Recognition Network tutorial.
I've created an Anaconda environment with python 3.6 and followed the install instruction accordingly for installing the GPU whl.
I can run the 'hello world' TF example.
When I go to run 'train.py' in the Audio Recognition Network tutorial/example, I get:
Traceback (most recent call last):
File "train.py", line 79, in <module>
import input_data
File "/home/philglau/speech_commands/input_data.py", line 35, in <module>
from tensorflow.contrib.framework.python.ops import audio_ops as contrib_audio
ImportError: cannot import name 'audio_ops'
The code in the tutorial that fails is:
from tensorflow.contrib.framework.python.ops import audio_ops as contrib_audio
I then backed up that chain until I could import some part of it:
import tensorflow.contrib.framework as test ==> works
import tensorflow.contrib.framework.python as test --> fail:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'tensorflow.contrib.framework' has no attribute 'python'
Not sure where I'm going wrong on my install.
Details:
Ubuntu 16.04
Anaconda env with python 3.6
Followed the 'anaconda' instruction on the TF install page. (GPU version)
I also tried using a python 2.7 env for anaconda but got the same results.
It looks like they're releasing the audio_ops modules in version 1.4 (https://github.com/tensorflow/tensorflow/issues/11339#issuecomment-327879009).
Until v1.4 is released, an easy way around this is to install the nightly tensorflow build
pip install tf-nightly
or with the docker image linked in the issue comment.
The short answer:
The framework is missing the "audio_ops.py" and the example wont work until the file is released. Or you code the wrappers.
More on this:
If you go to the: tensorflow.contrib.framework.python.ops local folder you can find other *_ops.py files but not the "audio_ops.py".
If you get it from the Master at: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/framework/python/ops
You will find the file is almost empty and with import labels wrong: "audio_ops" vs "gen_audio_ops".
With almost empty I mean that: decode_wav, encode_wav, audio_spectrogram , mfcc are not implemented/wrapped.
So, no working example and no fun.
We need to check again when "audio_ops.py" is released.
Here:
https://github.com/tensorflow/tensorflow/issues/11339
You can find a Developer saying: "we don't actually want to make them public / supported yet. I'm sorry this decision wasn't better documented"