AttributeError : module 'tensorflow' has no attribute 'contrib' [duplicate] - tensorflow

This question already has answers here:
Module 'tensorflow' has no attribute 'contrib'
(10 answers)
Closed 2 years ago.
I followed instructions (https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/training.html) to install the Tensorflow Object Detection API. I use Anaconda on ubuntu18.4 and all of the steps in the instructions seemed to complete OK.
When I train to train my model with the following command:
python model_main_tf2.py --model_dir=models/my_ssd_resnet50_v1_fpn --pipeline_config_path=models/my_ssd_resnet50_v1_fpn/pipeline.config
I get the following error:
Instructions for updating:
non-resource variables are not supported in the long term
Traceback (most recent call last):
File "model_main_tf2.py", line 34, in <module>
from object_detection import model_lib_v2
File "/home/chanitda/Desktop/Tensorflow/workspace/training_demo/object_detection/model_lib_v2.py", line 28, in <module>
from object_detection import eval_util
File "/home/chanitda/Desktop/Tensorflow/workspace/training_demo/object_detection/eval_util.py", line 30, in <module>
slim = tf.contrib.slim
AttributeError: module 'tensorflow' has no attribute 'contrib'
I get the error mentioned in the headline. I would be very thankful if someone could help me with a code example to solve the problem.

It looks like you are using the tensorflow 2.0 version, but the code you use needs tf 1.14. The module contrib is not available in tf 2.0 anymore, thats why you are seeing this error. So you should set up a new environment and install tf 1.14 there, so you can run the code properly without ruining your tf 2.0 environment

Related

Tensorflow installation issues on RaspberryPi 3 B+

Tried installing Tensorflow on RaspberryPi 3b+ model which gives the following error:
Traceback (most recent call last):
File "/home/infant-cry-detector/infant-cry-detector/_cnn__real_time_audio.py", line 22, in <module>
from tensorflow.keras import losses, models, optimizers
ModuleNotFoundError: No module named 'tensorflow.keras'
Tried to install Tensorflow versions<1.6 on RaspberryPi 3 b+ model but while using the same it shows module not found and unable to import tensorflow.keras which also returns module not found error.
We are complete beginners in coding with RaspberryPi, hence requesting detailed help that would help us get through the error.

ModuleNotFoundError: No module named 'official.modeling.optimization'

Please give solution on the following error. The tricky part is that the verification and testing is running "OK" but when I run command for training it throws following error.
The system details are as follows
Win10 - python 3.7.6
cuda 10.1 cudnn - 7.6.5
opencv - 4.5.1.48
tensorflow / tensorflow-gpu ==2.2.0
object_detection-0.1
protoc - 3.17.3
tf-models-official
tensorflow-model-optimization
Paths are already created
(Working on jupyter notebook on TFOD-API)
Traceback (most recent call last): File
"Tensorflow\models\research\object_detection\model_main_tf2.py", line
32, in
from object_detection import model_lib_v2 File "D:\Code_kida\ANPR\Tensorflow\models\research\object_detection\model_lib_v2.py",
line 31, in
from object_detection import model_lib File "D:\Code_kida\ANPR\Tensorflow\models\research\object_detection\model_lib.py",
line 34, in
from object_detection.builders import optimizer_builder File "D:\Code_kida\ANPR\Tensorflow\models\research\object_detection\builders\optimizer_builder.py",
line 25, in
from official.modeling.optimization import ema_optimizer ModuleNotFoundError: No module named 'official.modeling.optimization'
Make sure that the folder "D:\Code_kida\ANPR\Tensorflow\models\official" is exist and it has "modeling/optimization/ema_optimizer.py" inside.
I had the same error and that was the fix for me. might work for you too. or whoever comes here in the future

No module named 'tensorflow_datasets.image.cityscapes'

I installed TensorFlow Datasets using conda, as:
conda install -c anaconda tensorflow-datasets
I've tried importing Cityscapes:
from tensorflow_datasets.image.cityscapes import Cityscapes
but this raises the following error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
ModuleNotFoundError: No module named 'tensorflow_datasets.image.cityscapes'
How can I fix it?
PS: I can import the following without errors:
import tensorflow_datasets as tfds
Edit:
I've already downloaded Cityscapes data, as recommended in the official guide.

tf-1.5 or tf-2.1 when running object detection training

When I tried to run:
From the tensorflow/models/research/ directory
PIPELINE_CONFIG_PATH=/home/paul/objdetect/models/research/object_detection/samples/configs/ssdlite_mobilenet_v3_small_320x320_kitti.config
MODEL_DIR=/home/paul/objdetect/models/research/object_detection/samples/sample_model_ckpt/
NUM_TRAIN_STEPS=5000
SAMPLE_1_OF_N_EVAL_EXAMPLES=1
python object_detection/model_main.py \
--pipeline_config_path=${PIPELINE_CONFIG_PATH} \
--model_dir=${MODEL_DIR} \
--num_train_steps=${NUM_TRAIN_STEPS} \
--sample_1_of_n_eval_examples=$SAMPLE_1_OF_N_EVAL_EXAMPLES \
--alsologtostderr
if I use tensorflow-gpu==1.5.0 environment, I got following error:
from nets import inception_resnet_v2
File "/home/paul/objdetect/models/research/slim/nets/inception_resnet_v2.py", line 375, in <module>
batch_norm_updates_collections=tf.compat.v1.GraphKeys.UPDATE_OPS,
AttributeError: 'module' object has no attribute 'v1'
Then I switch to tensorflow-gpu==2.1.0 environment, and run it again, I got the following error:
from object_detection import eval_util
File "/home/paul/objdetect/models/research/object_detection/eval_util.py", line 40, in <module>
slim = tf.contrib.slim
AttributeError: module 'tensorflow' has no attribute 'contrib'
So is there a way to run it at all? what do I need to fix to get it running either in tf-1.5 or tf-2.1?
Anyone could help? Thank you very much in advance.
Sincerely yours,
-Paul
tf.contrib has been deprecated in tf2.x. Similarly tf.compat was introduced in the 1.14.x and 1.15.x for solving compatability between tf 1.x and tf2.x. Try to install tensorflow version 1.15.x and run
from object_detection import eval_util
File "/home/paul/objdetect/models/research/object_detection/eval_util.py", line 40, in <module>
slim = tf.contrib.slim
AttributeError: module 'tensorflow' has no attribute 'contrib'
For this error if you have tensorflow >=2.0 you need to replace slim = tf.contrib.slim with import tf_slim as slim.
This will solve your error

Tensorflow - import error in CIFAR tutorial

Recently, I installed tensorflow and got python import error in CIFAR tutorial.
I'm using Mac OS X, CPU only, Python 2.7.
$ python cifar10_train.py
Filling queue with 20000 CIFAR images before starting to train. This will take a few minutes.
Traceback (most recent call last):
File "cifar10_train.py", line 120, in
tf.app.run()
File "/Users/sunwoo/tensorflow/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 30, in run
sys.exit(main(sys.argv[:1] + flags_passthrough))
File "cifar10_train.py", line 116, in main
train()
File "cifar10_train.py", line 76, in train
class _LoggerHook(tf.train.SessionRunHook):
AttributeError: 'module' object has no attribute 'SessionRunHook'
How can I import tf.train.SessionRunHook?
It looks like you are using the master branch of cifar10_train.py, with an older installed version of TensorFlow (0.11 or earlier). The master branch was recently modified to use a new API, which wasn't available in TensorFlow 0.11 or earlier.
There are two ways to fix this problem. Either upgrade TensorFlow to version 0.12 or later, or check out the r0.11 branch of the TensorFlow source, and use the version of cifar10_train.py from that branch.