没有名称' tensorflow.compat'
我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这只是我发生在我身上,但我弄清楚了。您的.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.
您正在使用哪个版本的TensorFlow?在终端上使用此命令查看您正在使用的版本:
或者
尝试安装TensorFlow == 1.15
Which version of TensorFlow you're using? Use this command on terminal to see which version you're using:
Or
Then try to install tensorflow==1.15
您可以将以下代码而不是
导入TensorFlow作为TF
:You can use below code instead of
import tensorflow as tf
: