ImportError: cannot import name 'Iterator' - python

I want to run a segmentation code (with this link in github: https://github.com/taigw/brats17) on Google Colab and I'm faced with this error:
/usr/local/lib/python3.6/dist-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
Traceback (most recent call last):
File "test.py", line 18, in <module>
from tensorflow.contrib.data import Iterator
ImportError: cannot import name 'Iterator'

Try this:
import tensorflow as tf
Iterator = tf.data.Iterator

I suspect this is a version conflict for tensorflow. The data package has been upgraded from tf.contrib.data to a core package tf.data. You are probably using a newer version of tensorflow than that code was developed for. You can install an older version of tensorflow with pip install tensorflow==1.5 (that would install version 1.5 for example).

Related

Tensorflow error TF error: module compiled against API version 0xe but this version of numpy is 0xd

RuntimeError: module compiled against API version 0xe but this version of numpy is 0xd
Traceback (most recent call last):
File "Tensorflow/scripts/generate_tfrecord.py", line 27, in <module>
import tensorflow.compat.v1 as tf
File "C:\Users\ateyu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\__init__.py", line 37, in <module>
from tensorflow.python.tools import module_util as _module_util
File "C:\Users\ateyu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\__init__.py", line 37, in <module>
from tensorflow.python.eager import context
File "C:\Users\ateyu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\eager\context.py", line 35, in <module>
from tensorflow.python.client import pywrap_tf_session
File "C:\Users\ateyu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\client\pywrap_tf_session.py", line 19, in <module>
from tensorflow.python.client._pywrap_tf_session import *
ImportError: SystemError: <built-in method __contains__ of dict object at 0x000002901E7A0630> returned a result with an error set
RuntimeError: module compiled against API version 0xe but this version of numpy is 0xd
Traceback (most recent call last):
File "Tensorflow/scripts/generate_tfrecord.py", line 27, in <module>
import tensorflow.compat.v1 as tf
File "C:\Users\ateyu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\__init__.py", line 37, in <module>
from tensorflow.python.tools import module_util as _module_util
File "C:\Users\ateyu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\__init__.py", line 37, in <module>
from tensorflow.python.eager import context
File "C:\Users\ateyu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\eager\context.py", line 35, in <module>
from tensorflow.python.client import pywrap_tf_session
File "C:\Users\ateyu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\client\pywrap_tf_session.py", line 19, in <module>
from tensorflow.python.client._pywrap_tf_session import *
ImportError: SystemError: <built-in method __contains__ of dict object at 0x000001B8DA1705E8> returned a result with an error set
Kindly check the C-API version and Numpy version from here.
The error message suggests that you have a version of Numpy that provides an older version of the C-API. Based on this, you should upgrade Numpy to 1.20.x or 1.21.x
Please use the following code to upgrade Numpy;
pip install numpy --upgrade --ignore-installed
or you can use the following;
pip install numpy==1.21.6
According to the docs:
A workaround that usually works is to upgrade the NumPy version:
pip install numpy --upgrade
And possible reasons are:
A bad extension "wheel" (binary install) that should use oldest-support-numpy ( with manual constraints if necessary) to build their binary packages.
An environment issue messing with package versions.
Incompatible package versions somehow enforced manually.
An extension module compiled locally against a very recent version followed by a NumPy downgrade.
A compiled extension copied to a different computer with an older NumPy version.
If the reason of this problem was - like in my case - custom compilation of a reverse dependency of numpy (shap in my case) against the latest version of numpy (pre-installed first, before the compilation), followed by numpy downgrade by pip (due to some other reverse dependency such as scipy having a <= pin on the version of numpy... is a bug), then the solution is to: reverse the order:
first install numpy with all its pre-compiled reverse-dependencies (e.g. scipy), and only then
perform the compilation of reverse dependency (against the downgraded, i.e. final numpy version).
Downgrading python from 3.10 to 3.9 worked for me

ModuleNotFoundError: No module named 'six.moves.collections_abc'

i am just starting with machine learning i am following this tutorial from Weights&Bias where they gave us some code and asked to run it i am unable to run the code
First I was getting the error
Keras requires TensorFlow 2.2 or higher
for which I tried this method
Following the advice given here, downgrading Keras did the trick for me without having to touch any other packages. Just do:
pip install keras==2.3.0
from this link
Error "Keras requires TensorFlow 2.2 or higher"
then I started getting the error
Using TensorFlow backend.
Traceback (most recent call last):
File "perceptron-single.py", line 6, in <module>
import wandb
File "C:\Users\hp\AppData\Local\Programs\Python\Python37\lib\site-packages\wandb\__init__.py", line 37, in <module>
from wandb import sdk as wandb_sdk
File "C:\Users\hp\AppData\Local\Programs\Python\Python37\lib\site-packages\wandb\sdk\__init__.py", line 9, in <module>
from .wandb_config import Config # noqa: F401
File "C:\Users\hp\AppData\Local\Programs\Python\Python37\lib\site-packages\wandb\sdk\wandb_config.py", line 10, in <module>
from six.moves.collections_abc import Sequence
*ModuleNotFoundError: No module named 'six.moves.collections_abc'
for which I tried this method
easy_install six
from this link
ImportError: No module named six
but it did not solve my issue
Using this method
pip install --ignore-installed six
From the following link solved the problem for me.
ImportError: No Module named six; six already installed
Although I still don't understand about what was actually wrong and how it got fixed.

cannot import name 'CenterCrop' from 'tensorflow.keras.layers.experimental.preprocessing'

I am using anaconda env.
Python 3.7
keras : 2.3.1
tensorflow: 2.1.0
when i want to use CenterCrop and Rescaling modules, pycharm gives me error.
from tensorflow.keras.layers.experimental.preprocessing import CenterCrop
from tensorflow.keras.layers.experimental.preprocessing import Rescaling
error messages is:
D:\NewAnaconda\envs\Tensor_Turkcell\python.exe "C:/Users/Burak Ekincioğlu/Dekstop/TENSORFLOW/tensor_intro.py"
Traceback (most recent call last):
File "C:/Users/Burak Ekincioğlu/Dekstop/TENSORFLOW/tensor_intro.py", line 5, in <module>
from tensorflow.keras.layers.experimental.preprocessing import CenterCrop
ImportError: cannot import name 'CenterCrop' from 'tensorflow.keras.layers.experimental.preprocessing' (D:\NewAnaconda\envs\Tensor_Turkcell\lib\site-packages\tensorflow_core\python\keras\api\_v2\keras\layers\experimental\preprocessing\__init__.py)
I've tried the import with tensorflow 2.1.0 (keras 2.2.4 by default) and it gave me the same error you are encountering.
Using Tensorflow 2.2.0 with keras 2.3.0 works fine.
So you just need to upgrade tensorflow.

Unable to import tensorflowjs successfully

I have tensorflow 1.5 and everything is working fine with tensorflow.
Now i want to convert my tensorflow keras model to be used in web app so i installed tensorflowjs successfully but when i import i get the error.
I have tried changing the following versions:
numpy from 1.16.4 to 1.15.1,
tensorflow 1.5.0 to 1.8.0 and 1.12.0,
tensorflowjs 1.1.2 to 1.0.0
tensorflow test:
import tensorflow as tf
print(tf.__version__)
hello = tf.constant("Hello Tensorflow")
with tf.Session() as sesh:
print(sesh.run(hello))
Output:
1.12.0
2019-06-11 12:02:11.140827: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
b'Hello Tensorflow'
So i guess everything is fine with tensorflow but still i cant import tensorflowjs due to some files missing in my tensorflow installation.
i got this error first:
Traceback (most recent call last):
File "C:/Users/Uxama/PycharmProjects/untitled1/test.py", line 2, in <module>
import tensorflowjs as tfjs
File "C:\Users\Uxama\PycharmProjects\untitled1\venv\lib\site-packages\tensorflowjs\__init__.py", line 21, in <module>
from tensorflowjs import converters
File "C:\Users\Uxama\PycharmProjects\untitled1\venv\lib\site-packages\tensorflowjs\converters\__init__.py", line 24, in <module>
from tensorflowjs.converters.tf_saved_model_conversion_v2 import convert_tf_saved_model
File "C:\Users\Uxama\PycharmProjects\untitled1\venv\lib\site-packages\tensorflowjs\converters\tf_saved_model_conversion_v2.py", line 29, in <module>
from tensorflow.python.framework import convert_to_constants
ImportError: cannot import name 'convert_to_constants'
Then i placed the file (convert_to_constants) manually because changing the versions doesn't seems to work and change anything.
Then i got this error:
Traceback (most recent call last):
File "C:/Users/Uxama/PycharmProjects/untitled1/test.py", line 2, in <module>
import tensorflowjs as tfjs
File "C:\Users\Uxama\PycharmProjects\untitled1\venv\lib\site-packages\tensorflowjs\__init__.py", line 21, in <module>
from tensorflowjs import converters
File "C:\Users\Uxama\PycharmProjects\untitled1\venv\lib\site-packages\tensorflowjs\converters\__init__.py", line 24, in <module>
from tensorflowjs.converters.tf_saved_model_conversion_v2 import convert_tf_saved_model
File "C:\Users\Uxama\PycharmProjects\untitled1\venv\lib\site-packages\tensorflowjs\converters\tf_saved_model_conversion_v2.py", line 29, in <module>
from tensorflow.python.framework import convert_to_constants
File "C:\Users\Uxama\PycharmProjects\untitled1\venv\lib\site-packages\tensorflow\python\framework\convert_to_constants.py", line 26, in <module>
from tensorflow.python.eager import wrap_function
ImportError: cannot import name 'wrap_function'
I did the same thing again and placed the file but the errors seems to kept coming.
I don't know what exactly causing the problem i installed tensorflow with pip and also tried installing it with wheels as well.
I just want to successfully install tensorflowjs so that i can convert my keras model to use in web app.
Tensorflow 1.12 doesn't define a function called wrap_function(). Consider upgrading to API r1.13. Here are the functions defined in API r1.12, which you are currently using, and here is the wrap_function() method which is defined in API r1.13.

How to make keras in R use the tensorflow installed by Python

I have already performed the tensor flow installation with the following command:
pip install --ignore-installed https://github.com/mind/wheels/releases/download/tf1.5-gpu-cuda91-nomkl/tensorflow-1.5.0-cp27-cp27mu-linux_x86_64.whl
This is the latest tensorflow wheel catered for CUDA 9.1. (3x faster than CUDA 8.0)
And I can call it successfully in my python code.
How can I make the keras in R to call the tensorflow installed by python above? The reason I asked that because I the default installation method
keras::install_keras(method="conda", tensorflow = "gpu")
It failed to recognize the cuda-9.1 library.
> conv_base <- keras::application_vgg16(
+ weights = "imagenet",
+ include_top = FALSE,
+ input_shape = c(150, 150, 3)
+ )
/home/ubuntu/anaconda2/envs/r-tensorflow/lib/python2.7/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
Using TensorFlow backend.
Error: ImportError: Traceback (most recent call last):
File "/home/ubuntu/anaconda2/envs/r-tensorflow/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
File "/home/ubuntu/anaconda2/envs/r-tensorflow/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
_pywrap_tensorflow_internal = swig_import_helper()
File "/home/ubuntu/anaconda2/envs/r-tensorflow/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory
This is because R installation method calling for tensorflow version 1.5.0 that is still not catered for CUDA 9.1.
Try to put this in your .bashrc:
export KERAS_BACKEND='tensorflow'
Or based on this instruction, you can do:
tensorflow::install_tensorflow(version = "https://github.com/mind/wheels/releases/download/tf1.5-gpu-cuda91-nomkl/tensorflow-1.5.0-cp27-cp27mu-linux_x86_64.whl")
Then keras will automatically identify the correct tensorflow
R is looking for the CUDA 9.0 version, rather than the latest 9.1. You should be able to symlink your system so it ends up at the 9.1 folder instead; something like:
ln -s [path to cuda 9.0 it's looking for] [cuda 9.1]
Alternatively you may be able to uninstall 9.1 and install 9.0. I believe you will also need cudnn version 7.

Categories

Resources