7

I'm trying to use the code from the Teachable Machine website:

from keras.models import load_model
from PIL import Image, ImageOps
import numpy as np

# Load the model
model = load_model('keras_model.h5')

# Create the array of the right shape to feed into the keras model
# The 'length' or number of images you can put into the array is
# determined by the first position in the shape tuple, in this case 1.
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
# Replace this with the path to your image
image = Image.open('<IMAGE_PATH>')
#resize the image to a 224x224 with the same strategy as in TM2:
#resizing the image to be at least 224x224 and then cropping from the center
size = (224, 224)
image = ImageOps.fit(image, size, Image.ANTIALIAS)

#turn the image into a numpy array
image_array = np.asarray(image)
# Normalize the image
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
# Load the image into the array
data[0] = normalized_image_array

# run the inference
prediction = model.predict(data)
print(prediction)

but when running the code, I get the following error: ModuleNotFoundError: No module named 'tensorflow.compat'

I tried running the code on two separate machines, uninstalling and re-installing tensorflow, pip, keras, nothing seemed to help.

I'm using Python 3.9 and tensorflow 2.8.0

laviRZ
  • 316
  • 1
  • 3
  • 11
  • Can you edit in the complete traceback? How are you running this code (did you write a script which you run with `python scriptname` or from jupyter notebook...) – FlyingTeller Apr 27 '22 at 14:29
  • I'm using IntelliJ, but I also tried running the code directly from the command line and got the same error. – laviRZ Apr 27 '22 at 15:06
  • Did you call your own script or another script that you ahve created `tensorflow` by any chance? – FlyingTeller Apr 27 '22 at 15:59
  • What do you mean? I made a script called code.py with the code posted above inside it – laviRZ Apr 27 '22 at 16:07

2 Answers2

7

This just happened to me but I figured it out. Your .py script filename is the same with one of the files of the tensorflow library. You can just rename your python script and it will work fine.

mail_liw
  • 106
  • 1
  • 9
3

Which version of TensorFlow you're using? Use this command on terminal to see which version you're using:

python -c 'import tensorflow as tf; print(tf.__version__)'  # for Python 2
python3 -c 'import tensorflow as tf; print(tf.__version__)'  # for Python 3

Or

>>> import tensorflow as tf
>>> print(tf.__version__)
2.4.1

Then try to install tensorflow==1.15

pip install tensorflow==1.15
import tensorflow.compat.v2 as tf
  • Edited in the version numbers – laviRZ Apr 27 '22 at 13:50
  • I already tried using 1.15, but that didn't help. it required me to also move to an older version of python. – laviRZ Apr 27 '22 at 13:54
  • Try `import tensorflow.compat.v2` or `import tensorflow.compat` these worked for me. Also your Python version is 3.9. I'm using 3.7.9. Maybe that's why you're getting error. Try to lower your Python version to 3.7.9 – Mustafa Uğur Baskın Apr 27 '22 at 14:03
  • 3
    The error _is_ trying to use `tensorflow.compat`. And as I said I already tried using older versions of tensorflow and Python – laviRZ Apr 27 '22 at 15:05