10

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'
Ademir Omercehajic
  • 135
  • 1
  • 1
  • 8
  • 1
    just change it to tensorflow.keras.preprocessing.image – venkata krishnan Apr 08 '20 at 15:30
  • Yes, the module is called tensorflow, not tf – Dr. Snoopy Apr 08 '20 at 15:31
  • Does this answer your question? [How do I get around the " No module named 'Crypto' " error after doing "pip install pycrypto"?](https://stackoverflow.com/questions/43994590/how-do-i-get-around-the-no-module-named-crypto-error-after-doing-pip-inst) – Red May 20 '20 at 18:27

3 Answers3

18

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
venkata krishnan
  • 1,961
  • 1
  • 13
  • 20
0

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
...
Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
-2

in Tensorflow 2.0+, to use keras instead of tf use tensorflow always-

import tensorflow
from tensorflow.keras.preprocessing.image import ImageDataGenerator
Ajeet
  • 151
  • 1
  • 6