ImportError: No module named 'tensorflow.contrib.data' - tensorflow

My tensorflow version is 1.1.0
I try to import some file:
strong textfrom tensorflow.contrib.data import Dataset, Iterator
and got error :
ImportError: No module named 'tensorflow.contrib.data'
So, what is solution of this?

tf.contrib.data has been deprecated and been removed (Check here). So, in order to import "Dataset" and "iterator", follow the following steps:
You need to upgrade the tensorflow version using:
sudo pip3 install --upgrade tensorflow.
Check the installation guide here
Open the python terminal and type
import tensorflow as tf
dataset = tf.data.Dataset
Hope it will help.

Related

Importing TensorFlow "async" syntax error

I am trying to use the module imageai for a project and ran the line "from imageai.Detection import ObjectDetection". However, when I do so, this error appears:
File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/tensorflow/python/pywrap_tensorflow_internal.py:114
def TFE_ContextOptionsSetAsync(arg1, async):
^
SyntaxError: invalid syntax
I found someone who had the same issue here: https://github.com/tensorflow/tensorflow/issues/20690 , but I'm not quite sure how to edit the last file of the trace where the error occurs. Does anyone have any tips on how to do this? Thanks!
I have tried looking at the above GitHub error but am not sure how to approach it.
ImageAI uses Pytorch as backend. So you need to install all the required libraries before installing and importing the imageai module.
Please use the code below to install imageai in your system:
pip install cython pillow>=7.0.0 numpy>=1.18.1 opencv-python>=4.1.2 torch>=1.9.0 --extra-index-url https://download.pytorch.org/whl/cpu torchvision>=0.10.0 --extra-index-url https://download.pytorch.org/whl/cpu pytest==7.1.3 tqdm==4.64.1 scipy>=1.7.3 matplotlib>=3.4.3 mock==4.0.3
pip install imageai --upgrade
Now, import the Object detection from imageai:
from imageai.Detection import ObjectDetection
Please refer this link for more details.
Note: You can easily install imageai in Google Colab with this code
!pip install imageai
from imageai.Detection import ObjectDetection
Hints: Please use below code to install, import and check the TensorFlow version
pip install tensorflow
import tensorflow as tf
tf.__version__

Why can't I access the keras.initializers class?

I'm trying to use the HeUniform() method from the keras.initializers class, bu when I go to run my code, I get the following output:
AttributeError: module 'tensorflow_core.keras.initializers' has no attribute 'HeUniform'
The code that I ran is the following:
init = tf.keras.initializers.HeUniform()
My imports are:
import gym
import tensorflow as tf
import numpy as np
from tensorflow import keras
from collections import deque
import time
import random
I'm using tensorflow version 2.0.0 installed from conda. I tried to upgrade the version, but I was getting further errors saying that version 2.2.0 and 2.4.1 does not exist for conda. Please advise.
Take a look at the tensorflow api documentation for version 2.0. There is no class tf.keras.initializers.HeUniform. Instead, there is a function tf.keras.initializers.he_uniform. You can find the source code for the function on GitHub.
TensorFlow recommends installing with pip. In the past, I have created a new conda environment specifically for tensorflow. It is bad practice to mix conda and pip packages, so that's why I create a new environment just for the specific version of tensorflow.
conda create -n tf2 python=3.8
conda activate tf2
python -m pip install --no-cache-dir tensorflow==2.4.1
Just keep in mind that the GPU versions depend on specific versions of CUDA and cuDNN. See https://www.tensorflow.org/install/source#gpu for more information about the requirements.

Module 'tensorflow' has no attribute 'contrib'

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

tensorflow.keras can't import Activation

just installed tensorflow-gpu via:
conda install --yes tensorflow-gpu==1.12.0
Now when I run from tensorflow.keras import layers into the error:
ImportError: cannot import name 'Activation'
I tried removing tf and keras then reinstalling tf, but hasn't helped.
This is due to a change in 1.12.0
As seen below; in 1.11, tensorflow uses tensorflow.python.keras.activations
https://github.com/tensorflow/tensorflow/blob/r1.11/tensorflow/python/keras/layers/advanced_activations.py
However in 1.12, it doesn't exist anymore;
https://github.com/tensorflow/tensorflow/blob/r1.12/tensorflow/python/keras/layers/advanced_activations.py
So, I think you can directly call the activation function as;
keras.layers.{activation_function}
e.g. keras.layers.LeakyReLU
Alternatively, you can downgrade.
As #Amir replied, use tensorflow.python.keras. That worked for me!

Dataset of MNIST not found from Tensorflow

I have a python script whose initial lines read
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
When I run the script it gives this error
'no module such as tensorflow.tutorials.examples.mnist'.
I have already pip installed Tensorflow, what do i do now?
From your comment I can see that you are using Anaconda. Have you tried to install it with conda install tensorflow? Also, you could try to create an environment first, and install tensorflow afterwards with conda create -n tensorflow pip python=2.7 (or 3.x depending on your system) within it.