Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module 'tensorflow' has no attribute 'contrib'

Tags:

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: [email protected] 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'

like image 432
Ömer Çiftci Avatar asked Apr 26 '19 14:04

Ömer Çiftci


People also ask

How do I fix module Tensorflow has no attribute contrib?

v1' has no attribute 'contrib' You can solve this in one of two ways: - you can entirely remove Tensorflow v1. x and install Tensorflow v2. x - Or Install Tensorflow v2.

What is Tensorflow contrib?

contrib contains contributed code. It is meant to contain features and contributions that eventually should get merged into core TensorFlow, but whose interfaces may still change, or which require some testing to see whether they can find broader acceptance. The code in tf.

What is TF compat v1?

tf. compat allows you to write code that works both in TensorFlow 1.


4 Answers

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

like image 199
mlneural03 Avatar answered Oct 16 '22 08:10

mlneural03


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) 
like image 13
NeStack Avatar answered Oct 16 '22 09:10

NeStack


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.

like image 7
Ankit Jain Avatar answered Oct 16 '22 08:10

Ankit Jain


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.

like image 5
Pawan Kumar Avatar answered Oct 16 '22 08:10

Pawan Kumar