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

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.

Related

Why I can't import this module?

I'm trying to import img_to_array even though all packages are there. This error still appears:
Traceback (most recent call last):
File "c:\Users\Usuario\Downloads\keras-api\keras-main.py", line 11, in <module> from keras.preprocessing.image import img_to_array
ImportError: cannot import name 'img_to_array' from 'keras.preprocessing.image'
This is how I am calling the packages from the keras package.
from keras.preprocessing.image import img_to_array
On the other hand, this is what I'm trying to do with the code.
image = img_to_array(image)
However, I cannot even continue because I'm not able to import img_to_array
but this is how you import img_to_array if you have installed the keras package via e.g. pip install keras
from keras.utils import img_to_array
img_to_array
check if the keras version is the correct one:
Keras Installation Guide

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.

keras is not running even after installing tensorflow

i have installed "Anaconda3-2020.07-Windows-x86_64" and python "python-3.8.0". after that, i have installed keras and then tensorflow. but each time i am running the code "import keras" in spyder, the problem is showing that:
***Traceback (most recent call last):
File "C:\Users\Asus\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow.py", line 64, in
from tensorflow.python._pywrap_tensorflow_internal import *
ImportError: DLL load failed while importing _pywrap_tensorflow_internal: The specified module could not be found.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Asus\anaconda3\lib\site-packages\keras_init_.py", line 3, in
from tensorflow.keras.layers.experimental.preprocessing import RandomRotation
File "C:\Users\Asus\AppData\Roaming\Python\Python38\site-packages\tensorflow_init_.py", line 41, in
from tensorflow.python.tools import module_util as _module_util
File "C:\Users\Asus\AppData\Roaming\Python\Python38\site-packages\tensorflow\python_init_.py", line 40, in
from tensorflow.python.eager import context
File "C:\Users\Asus\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\eager\context.py", line 35, in
from tensorflow.python import pywrap_tfe
File "C:\Users\Asus\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tfe.py", line 28, in
from tensorflow.python import pywrap_tensorflow
File "C:\Users\Asus\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow.py", line 83, in
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "C:\Users\Asus\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow.py", line 64, in
from tensorflow.python._pywrap_tensorflow_internal import *
ImportError: DLL load failed while importing _pywrap_tensorflow_internal: The specified module could not be found.
Failed to load the native TensorFlow runtime.
See https://www.tensorflow.org/install/errors
for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "", line 1, in
import keras
File "C:\Users\Asus\anaconda3\lib\site-packages\keras_init_.py", line 5, in
raise ImportError(
ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via pip install tensorflow
----------------------------------------------***
now, i dont know what is the problem mainly. because my tensorflow version shows that its 2.3.0..
i dont know what iis the problem. please, any one help.
From comments
I had to create a environment in anaconda, named the environment as
tensorflow, and created the environment based on python 3.6. In that
environment, i imported tensorflow, keras separately and installed the
spyder, jupyter notebook . after all these steps, it worked (paraphrased from kazi fahim lateef)
Steps to create virtual environment in anaconda
# Create environment
conda create --name TF
# When conda asks you to proceed, type y:
proceed ([y]/n)?
# Activate virtual environment
conda activate TF
# Install the TF Version
conda install tensorflow
# Install the Keras Version
conda install Keras
Note: From TF 2.0, onwards keras are integrated with TF. You can refer tf.keras module for more details.

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.

ImportError: cannot import name 'Iterator'

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).

Categories

Resources