运行图像特征提取代码时遇ModuleNotFoundError: No module named 'tensorflow.compat'
问题解决:ModuleNotFoundError: No module named 'tensorflow.compat'
问题描述
编写基于VGG16的图像特征提取代码,预期输出numpy.ndarray类型的特征结果,运行时触发上述模块找不到的错误。
原代码
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)
错误堆栈
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'
解决方案
原因分析
错误源于Keras版本(大概率是Keras 3.x)与TensorFlow环境不兼容:Keras 3.x默认依赖TensorFlow作为后端,但你的环境中未安装TensorFlow,或安装的版本过低,导致Keras无法找到tensorflow.compat模块。此外原代码还存在类构造函数命名错误(init应改为__init__)。
修复步骤
安装/升级TensorFlow
打开终端执行命令,安装兼容的TensorFlow版本:pip install tensorflow>=2.15.0若已安装,可升级到最新稳定版:
pip install --upgrade tensorflow修正代码并适配Keras 3.x
将导入路径改为tensorflow.keras,同时修复构造函数的命名错误:from tensorflow.keras.preprocessing import image from tensorflow.keras.applications.vgg16 import VGG16 from tensorflow.keras.applications.vgg16 import preprocess_input from tensorflow.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)验证环境
执行以下代码检查TensorFlow和Keras是否正常运行:import tensorflow as tf from tensorflow.keras.applications.vgg16 import VGG16 print(tf.__version__) print(tf.keras.__version__)能正常输出版本号则说明环境修复完成。
内容的提问来源于stack exchange,提问作者tg7
相关产品推荐
相关产品推荐

