I'm having problem with tensorflow. I want to use ImageDataGenerator, but I'm receiving error ModuleNotFoundError: No module named 'tf'. Not sure what is the problem. I added this tf.version to test will it work, and it shows the version of tensorflow.
import tensorflow as tf
from tensorflow import keras
print(tf.__version__)
from tf.keras.preprocessing.image import ImageDataGenerator
When I run this code, I get this:
2.1.0
Traceback (most recent call last):
File "q:/TF/Kamen papir maaze/rks.py", line 14, in <module>
from tf.keras.preprocessing.image import ImageDataGenerator
ModuleNotFoundError: No module named 'tf'
The line
import tensorflow as tf
means you are importing tensorflow with an alias as tf to call it modules/functions.
You cannot use the alias to import other modules.
For your case, if you call directly
tf.keras.preprocessing.image.ImageDataGenerator(...)
then it will work.
or
you need to import the module with the right module name. i.e.
from tensorflow.keras.preprocessing.image import ImageDataGenerator
This works, tested on kaggle with tf_v2.6 and tf_v2.7
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
...
in Tensorflow 2.0+, to use keras instead of tf use tensorflow always-
import tensorflow
from tensorflow.keras.preprocessing.image import ImageDataGenerator
Related
I was trying to run this code, first time using Tensorflow API in Python. Donwloaded latest version of tensorflow and pip through terminal. However, when I attempt to import from keras subpackage, an error is returned
from keras.models import Model
from keras.layers import Dense
from keras.layers import Input
ImportError: cannot import name 'pywrap_tensorflow' from partially initialized module 'tensorflow.python' (most likely due to a circular import) (/Users/macbook/Library/Python/3.9/lib/python/site-packages/tensorflow/python/__init__.py) ```
I'm try learning TensorFlow but i have a problem. I'm importing TensorFlow like in official website but i take a error.
import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
Traceback (most recent call last):
File "C:\Users\Koray\Desktop\Python\main.py", line 4, in
from tensorflow import keras
ImportError: cannot import name 'keras' from 'tensorflow'
(C:\Users\Koray\Desktop\Python\tensorflow.py)
For community benefit, adding #John Gordon answer here in the answer section.
Don't name your script tensorflow.py. Your import finds that script instead of the real one.
Instead you can name any other for example learning.py. Now all your imports work as expected.
import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
I am trying to do several imports from the keras library. I am doing this on a Jupyter notebook using an Anaconda installed Python. I have used keras before fine, and I just checked for updates but found none. Here are the import statements.
from keras.preprocessing.text import Tokenizer
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import SimpleRNN
from keras.layers import Embedding
I get a warning when I run this that says 'Using TensorFlow backend. Then I get a long error output that ends with 'NoneType' object has no attribute 'message_types_by_name'. Any ideas on what is wrong?
Since Tensorflow supports Keras as High level API, suggested way to import Keras libraries/modules is as below.
import tensorflow as tf
from tf.keras.preprocessing.text import Tokenizer
from tf.keras.utils import to_categorical
from tf.keras.models import Sequential
from tf.keras.layers import Dense
from tf.keras.layers import SimpleRNN
from tf.keras.layers import Embedding
Check the document from Keras here for reference.
I have imported the following libraries for my machine learning project but have problem when I try to run my model in the command prompt...
from tensorflow.python.keras import Model
from tensorflow.python.keras.layers import Layer, Input, Conv2D, MaxPooling2D, Conv2DTranspose, concatenate, Lambda
from tensorflow.python.keras.initializers import TruncatedNormal
from keras.optimizers import Adam
from tensorflow.python.keras.callbacks import ModelCheckpoint, LearningRateScheduler, CSVLogger, Callback
from tensorflow.python.keras.models import load_model
from tensorflow.python.keras.utils import Sequence
This is the error message which I get when trying to run the model in the command prompt.
ImportError: cannot import name '__version__' from partially initialized module 'keras' (most likely due to a circular import) (C:\Users\gurun\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\__init__.py)
It is always recommend to use tensorflow.keras.* instead of tensorflow.python.*.
This is because anything under tensorflow.python.* is private, intended for development only, rather than for public use.
import tensorflow as tf
print(tf.__version__)
from tensorflow.keras import Model
from tensorflow.keras.layers import Layer, Input, Conv2D, MaxPooling2D, Conv2DTranspose, concatenate, Lambda
from tensorflow.keras.initializers import TruncatedNormal
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import ModelCheckpoint, LearningRateScheduler, CSVLogger, Callback
from tensorflow.keras.models import load_model
from tensorflow.keras.utils import Sequence
Output:
2.5.0
You should be consistent and import keras only from one source and the recommended way, as #TFer2 said, is from tensorflow.keras . Your editor maybe can complain that tensorflow.keras cannot be resolved but usually that warning can be ignored and all works fine. If not try to uninstall keras and reinstall it accordingly to the version of your tensorflow installation.
This is the error it gives me when I try to run it:
Traceback (most recent call last):
File "/Users/kids/Library/Mail/V5/E60CBF1C-9021-4A10-8D60-06C96C141AF1/Outbox.mbox/E7C72E99-E3DB-4CDC-B1C9-15116F3478D8/Data/Attachments/405/2/ASL-Finger-Spelling-Recognition-master/main.py", line 10, in <module>
from keras.layers.convolutional import Convolution3D, MaxPooling3D
This is the code error which it is showing:
from keras.layers.convolutional import Convolution3D, MaxPooling3D
Keras version is 2.3.1. Python version is Python 3.8. Running on MacOS.
Your import statement
from keras.layers.convolutional import Convolution3D, MaxPooling3D
attempts to import MaxPooling3D from keras.layers.convolutional. MaxPooling3D is actually part of the pooling module, i.e. keras.layers.pooling.
According to the keras source and docs, the best way to import these two classes in a single statement would be
from keras.layers import Convolution3D, MaxPooling3D
Alternatively, you could import them separately:
from keras.layers.convolutional import Convolution3D
from keras.layers.pooling import MaxPooling3D
but in general, you should pay attention to what is purposefully exposed via the package; that is, the classes accessible from keras.layers.