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

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.

Related

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

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

Run Python, Anaconda, Pandas, Numpy offline on a Server getting dependency error

After installing Anaconda on a Virtual Machine I run a script which is working on my local machine but not on my Virtual Machine.
I'm getting the error Message:
C:\Users\...\python>"C:\ProgramData\Anaconda3\python.exe" "C:\Users\...\reporting.py"
C:\ProgramData\Anaconda3\lib\site-packages\numpy\__init__.py:140: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
from . import _distributor_init
Traceback (most recent call last):
File "C:\Users\...\reporting.py", line 1, in <module>
import pandas as pd
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\__init__.py", line 16, in <module>
raise ImportError(
ImportError: Unable to import required dependencies:
numpy:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
* The Python version is: Python3.8 from "C:\ProgramData\Anaconda3\python.exe"
* The NumPy version is: "1.18.5"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: DLL load failed while importing _multiarray_umath: The specified module could not be found.
The VM has no Internet. Is there any way to install all the required libs and frameworks?
You can package your script and used libraries using something like Pyinstaller. When you use an virtualenv keep in mind that all the packages that you use during pyinstaller execution are stored and may lead to an huge .exe

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"

unittest in tensorflow, python session_test.py, got a exception

I'm trying to run session_test.py from https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/client/session_test.py, So in my anaconda, tensorflow virtual env, I went to the corresponding directory in tensorflow like ../master/tensorflow/python/client/.. and run
python session_test.py
I got exception info like this:
Traceback (most recent call last):
File "session_test.py", line 32, in
from tensorflow.core.protobuf import cluster_pb2
ImportError: cannot import name 'cluster_pb2'
What package I should install or related documents that I could read about it. Is there anyone trying to do the same thing, run those unit test coming along with each module in tensorflow repo?
Please help me, thanks!
That line was added just a couple of weeks ago (see commit), and relates to a new feature being implemented in TensorFlow. Unless you have compiled and installed a very bleeding-edge version of TensorFlow you won't be able to run it. Instead, try to run the script corresponding to the version of TensorFlow that you have installed (for example, this is for version 1.1.0, the latest stable release at the time of writing).