Module 'tensorflow' has no attribute 'contrib' - tensorflow

I am trying to train my own custom object detector using Tensorflow Object-Detection-API
I installed the tensorflow using "pip install tensorflow" in my google compute engine. Then I followed all the instructions on this site: https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/training.html
When I try to use train.py I am getting this error message:
Traceback (most recent call last):
File "train.py", line 49, in
from object_detection.builders import dataset_builder
File "/usr/local/lib/python3.6/dist-packages/object_detection-0.1->py3.6.egg/object_detection/builders/dataset_builder.py", line 27, in
from object_detection.data_decoders import tf_example_decoder
File "/usr/local/lib/python3.6/dist-packages/object_detection-0.1-py3.6.egg/object_detection/data_decoders/tf_example_decoder.py", line 27, in
slim_example_decoder = tf.contrib.slim.tfexample_decoder
AttributeError: module 'tensorflow' has no attribute 'contrib'
Also I am getting different results when I try to learn version of tensorflow.
python3 -c 'import tensorflow as tf; print(tf.version)' : 2.0.0-dev20190422
and when I use
pip3 show tensorflow:
Name: tensorflow
Version: 1.13.1
Summary: TensorFlow is an open source machine learning framework for everyone.
Home-page: https://www.tensorflow.org/
Author: Google Inc.
Author-email: opensource#google.com
License: Apache 2.0
Location: /usr/local/lib/python3.6/dist-packages
Requires: gast, astor, absl-py, tensorflow-estimator, keras-preprocessing, grpcio, six, keras-applications, wheel, numpy, tensorboard, protobuf, termcolor
Required-by:
sudo python3 train.py --logtostderr --train_dir=training/ --
pipeline_config_path=training/ssd_inception_v2_coco.config
What should I do to solve this problem? I couldn't find anything about this error message except this: tensorflow 'module' object has no attribute 'contrib'

tf.contrib has moved out of TF starting TF 2.0 alpha.
Take a look at these tf 2.0 release notes https://github.com/tensorflow/tensorflow/releases/tag/v2.0.0-alpha0
You can upgrade your TF 1.x code to TF 2.x using the tf_upgrade_v2 script
https://www.tensorflow.org/alpha/guide/upgrade

This issue might be helpful for you, it explains how to achieve TPUStrategy, a popular functionality of tf.contrib in TF<2.0.
So, in TF 1.X you could do the following:
resolver = tf.contrib.cluster_resolver.TPUClusterResolver('grpc://' + os.environ['COLAB_TPU_ADDR'])
tf.contrib.distribute.initialize_tpu_system(resolver)
strategy = tf.contrib.distribute.TPUStrategy(resolver)
And in TF>2.0, where tf.contrib is deprecated, you achieve the same by:
tf.config.experimental_connect_to_host('grpc://' + os.environ['COLAB_TPU_ADDR'])
resolver = tf.distribute.cluster_resolver.TPUClusterResolver('grpc://' + os.environ['COLAB_TPU_ADDR'])
tf.tpu.experimental.initialize_tpu_system(resolver)
strategy = tf.distribute.experimental.TPUStrategy(resolver)

One easy way is you can pass your code written in TensorFlow 1.x to the below code to automatically upgrade it to TensorFlow 2.x.
$tf_upgrade_v2 \
--intree my_project/ \
--outtree my_project_v2/ \
--reportfile report.txt
The above code will replace all the commands which are deprecated in 2.x with the onces that are actually working in 2.x. And then you can run your code in TensorFlow 2.x.
In case if it throws an error and is unable to convert the complete code and then don't panic. Please open the "report.txt" file that is generated by the above code. In this file, you will find commands that are deprecated and their alternative commands that can be used in TensorFlow 2.x.
Taadaa, just replace the commands that are throwing errors with the new ones.
Example:
If the command in TensorFlow 1.x is:
tf.contrib
Then the same command in Tensorflow 2.x is:
tf.compat.v1.estimator
In the above example replace "tf.contrib" with "tf.compat.v1.estimator" and that should solve the problem.

I used google colab to run my models and everything was perfect untill i used inline tesorboard. With tensorboard inline, I had the same issue of "Module 'tensorflow' has no attribute 'contrib'".
It was able to run training when rebuild and reinstall the model using setup.py(research folder) after initialising tensorboard.

I used tensorflow 1.8 to train my model and there is no problem for now. Tensorflow 2.0 alpha is not suitable with object detection API

I'm using Google Colab as well. A comment suggested to put
%tensorflow_version 1.x
in the first (code) cell, and it worked!

If you want to use tf.contrib, you need to now copy and paste the source code from github into your script/notebook. It's annoying and doesn't always work. But that's the only workaround I've found. For example, if you wanted to use tf.contrib.opt.AdamWOptimizer, you have to copy and paste from here. https://github.com/tensorflow/tensorflow/blob/590d6eef7e91a6a7392c8ffffb7b58f2e0c8bc6b/tensorflow/contrib/opt/python/training/weight_decay_optimizers.py#L32

I face the same error and solve it by install python version 3.7 then i can install tensorflow 1.15 and it work.

I used tensorflow==2.9 but tensorflow-probability==0.6.0 so I met this error too. tensorflow-probability==0.6.0 seems to be compatible with tf 1
this is solution: pip install tensorflow_probability==0.12.2
This version of TensorFlow Probability requires TensorFlow version >= 2.3
if there are still some errors pip install tensorflow_probability==0.17.0

For me it worked using the latest release of tensorflow: pip install tensorflow==2.2.0

Related

AttributeError: module 'tensorflow._api.v1.config' has no attribute 'set_visible_devices'

First of all, I apologize that my English is not good for you to understand.
Currently, I am doing computer vision using tensorflow version 1.14. In the process, the following problem ocurred in the process of rotating the model using GPU.
AttributeError: module 'tensorflow._api.v1.config' has no attribute 'set_visible_devices'
The current development environment is as follows.
Python: 3.7.9
conda: 4.8.3
tensorflow: 1.14.0
keras: 2.3.1
In addition, I currently have 4 gpu, and i want to use 2 gpu as if it were 1 gpu. Can you give me a good idea for this?
thank you.
It seems you need to upgrade the tensorflow because tf.config.set_visible_devices() function is available in latest version of tensorflow, you can use below code to upgrade the tensorflow:
!pip install --upgrade pip.
!pip install --upgrade tensorflow
You can follow the link to install the CPU/GPU version of tensorflow as per requirement and for tf.config.set_visible_devices() function related details, check here

TensorFlow problem i have the required TF but still get an error

I created an env and imported TensorFlow but I get the error:
Keras requires TensorFlow 2.2 or higher
TensorFlow at this env is 2.3, so I don't know why I get this error.
the way to solve the problem is to create a new virtual environment
python 3.7
tesorflow 1.15.2
or uninstall any previously installed tensorflow and install 1.15.2
i'm not sure why this worked though.

python3 recognizes tensorflow, but doesn't recognize any of its attributes

I am getting the following errors:
AttributeError: module 'tensorflow' has no attribute 'variable_scope'
AttributeError: module 'tensorflow' has no attribute 'squared_difference'
tensorflow is installed:
>> pip3 list | grep tensorflow
tensorflow 2.0.0
tensorflow-estimator 2.0.1
TensorFlow 2.0 cleaned up some of the APIs. Mathematical functions such as squared_difference() are now under tf.math.
There is no tf.variable_scope() in TensorFlow 2.0. I suggest reading this post with examples on how to migrate your code to TF2.
If you want your code to be compatible with older versions of TensorFlow, you can use tf.compat.v1.variable_scope()

How to downgrade tensorflow version in colab?

I am using pip3 install tensorflow==1.8.0, but it doesn't have GPU support.
So I am using pip3 install tensorflow-gpu==1.8.0, but it still raises an exception
libcudart.so.VERSION No such file.
Should I use colab to install tensorflow from source?
After pip3 list:
tensorboard 1.10.0
tensorflow 1.10.0
tensorflow-hub 0.1.1
Google recommends you not to do pip installs!!!!
use this instead: %tensorflow_version 1.x
Restart the Runtime and check if its changed:
import tensorflow
print(tensorflow.__version__)
Here is a link to the main article:
https://colab.research.google.com/notebooks/tensorflow_version.ipynb#scrollTo=8UvRkm1JGUrk
You can downgrade Tensorflow to a previous version without GPU support on Google Colab. I ran:
!pip install tensorflow==1.14.0
import tensorflow as tf
print(tf.__version__)
which initially returned
2.0.0-dev20190130
but when I returned to it after a few hours, I got the version I requested:
1.14.0
Trying to downgrade to a version with GPU support:
!pip install tensorflow-gpu==1.14.0
requires restarting the runtime and fails, as importing import tensorflow as tf returns:
ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory
Update
When the import fails you can always downgrade CUDA to version 9.0 using following commands
!wget https://developer.nvidia.com/compute/cuda/9.0/Prod/local_installers/cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64-deb
!dpkg -i cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64-deb
!apt-key add /var/cuda-repo-9-0-local/7fa2af80.pub
!apt-get update
!apt-get install cuda=9.0.176-1
You can check the version of CUDA by running:
!nvcc --version
Second update
This code now seems to fail, see the follow-up question at How to downgrade to tensorflow-gpu version 1.12 in google colab
Google gives quite a simple solution to downgrade to the previously used Colab tf v.1.15.2. Just run the following magic line in Colab:
%tensorflow_version 1.x
Ther recommend "against using pip install to specify a particular TensorFlow version for both GPU and TPU backends. Colab builds TensorFlow from the source to ensure compatibility with our fleet of accelerators. Versions of TensorFlow fetched from PyPI by pip may suffer from performance problems or may not work at all". This means if you need GPU support, use one of the two given TF versions. The other versions will not necessary work I guess even for CPU.
The build process for GPU-enabled tensorflow is involved. In particular, old versions of TensorFlow use (or require) older versions of CUDA, which itself depends on system libraries and configuration beyond the scope of a pip install.
I suspect that downgrading TensorFlow on a VM configured for a newer version is going to be an involved process, perhaps involving downgrades / reinstalls of system libraries.
If it's practical, it might be simpler to update your code to use the latest version of TensorFlow, at least until Colab supports persistent backend enivronments.
It seems that only tensorflow 2 is supported by Colab, but that's not true, you still can use pip to uninstall tensorflow 2 and install a specific version of tf1. !yes|pip uninstall tensorflow, !pip install tensorflow==1.15.5 Maybe you should install other dependencies. So use !pip install -r requirements.txt Attention! You must restart the runtime in order to use newly installed versions.
%tensorflow_version 1.x no longer works.
%tensorflow_version 1.x
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-8d2919c1d33c> in <module>
----> 1 get_ipython().run_line_magic('tensorflow_version', '1.x')
1 frames
/usr/local/lib/python3.8/dist-packages/google/colab/_tensorflow_magics.py in _tensorflow_version(line)
33
34 if line.startswith("1"):
---> 35 raise ValueError(
36 # pylint: disable=line-too-long
37 textwrap.dedent("""\
ValueError: Tensorflow 1 is unsupported in Colab.
Your notebook should be updated to use Tensorflow 2.
See the guide at https://www.tensorflow.org/guide/migrate#migrate-from-tensorflow-1x-to-tensorflow-2.

Tensorflow installation issue on Win 10

I am new to tensorflow and tried to install into Anaconda. I followed the instruction to the dot from tensorflow.org. I encountered no problem encountered but in python shell when i perform import tensorflow as tf, I got this error message, "ModuleNotFoundError: No module named 'tensorflow'".
Please advise what to do to install tensorflow.
I already followed the previous post on conda create -n tensorflow python=3.6 but to no avail.