我收到“ModuleNotFoundError:没有名为”tensorflow.compat“的模块

I get 'ModuleNotFoundError: No module named 'tensorflow.compat'

提问人:tg7 提问时间:7/24/2023 最后编辑:John Gordontg7 更新时间:7/24/2023 访问量:86

问:

这是代码,

from keras.preprocessing import image
from keras.applications.vgg16 import VGG16 
from keras.applications.vgg16 import preprocess_input 
from keras.models import Model import numpy as np

class FeatureExtractor: def init(self): base_model = VGG16(weights="imagenet") self.model = Model(inputs=base_model.input, outputs=base_model.get_layer("fc1").output)
def extract(self, img):
    img = img.resize((224, 224)).convert("RGB")
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    feature = self.model.predict(x)[0]
    return feature / np.linalg.norm(feature)

我期待numpy.ndarray,但我得到ModuleNotFoundError:没有名为“tensorflow.compat”的模块

以下是错误堆栈

Traceback (most recent call last):
  File "C:\Users\Ronte\OneDrive\Documents\Image Search Platfoem\static\offline.py", line 4, in <module>
    from featureextractor import FeatureExtractor
  File "C:\Users\Ronte\OneDrive\Documents\Image Search Platfoem\static\featureextractor.py", line 1, in <module>
    from keras.preprocessing import image
  File "C:\Users\Ronte\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\__init__.py", line 3, in <module>
    from keras import __internal__
  File "C:\Users\Ronte\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\__internal__\__init__.py", line 3, in <module>
    from keras.__internal__ import backend
  File "C:\Users\Ronte\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\__internal__\backend\__init__.py", line 3, in <module>
    from keras.src.backend import _initialize_variables as initialize_variables
  File "C:\Users\Ronte\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\src\__init__.py", line 21, in <module>
    from keras.src import models
  File "C:\Users\Ronte\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\src\models\__init__.py", line 18, in <module>
    from keras.src.engine.functional import Functional
  File "C:\Users\Ronte\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\src\engine\functional.py", line 23, in <module>
    import tensorflow.compat.v2 as tf
ModuleNotFoundError: No module named 'tensorflow.compat'
Python TensorFlow Keras 图像识别

评论

0赞 John Gordon 7/24/2023
此代码根本没有提及,因此错误一定发生在其他地方。请向我们展示完整的错误回溯过程,以便我们了解错误实际发生的位置。tensorflow.compat
0赞 John Gordon 7/24/2023
很难阅读注释中的格式化代码/错误。请编辑问题并将错误放在那里。
0赞 John Gordon 7/24/2023
此外,这不可能是完整的错误,因为它不是以代码中的一行开头的。它从库模块中的一行开始。
0赞 tg7 7/24/2023
是的,谢谢
0赞 tg7 7/24/2023
您需要@JohnGordon更多信息

答: 暂无答案