没有名称' tensorflow.compat'

发布于 2025-01-24 22:32:28 字数 1368 浏览 2 评论 0原文

我正在尝试使用 Thotable Machine网站

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)

但是运行代码时,我会收到以下错误: modulenotfoundError:no模块名为'tensorflow.compat'

我尝试在两台单独的计算机上运行代码,卸载和重新安装tensorflow,pip,keras,似乎没有任何帮助。

我正在使用Python 3.9和Tensorflow 2.8.0

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

┼── 2025-01-31 22:32:28

这只是我发生在我身上,但我弄清楚了。您的.py脚本文件名与TensorFlow库的一个文件之一相同。您可以重命名您的Python脚本,它可以正常工作。

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.

盗心人 2025-01-31 22:32:28

您正在使用哪个版本的TensorFlow?在终端上使用此命令查看您正在使用的版本:

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

或者

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

尝试安装TensorFlow == 1.15

pip install tensorflow==1.15
import tensorflow.compat.v2 as tf

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
醉生梦死 2025-01-31 22:32:28

您可以将以下代码而不是导入TensorFlow作为TF

from tensorflow import compat as ttf
tf=ttf.v1

You can use below code instead of import tensorflow as tf:

from tensorflow import compat as ttf
tf=ttf.v1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文