Keras/TensorFlow 2.15.0无法导入预处理模块函数及工具模块问题
Keras/TensorFlow 2.15.0 导入image_preprocessing及preprocessing_utils问题解决方案
版本适配说明
TensorFlow 2.15.0 内置的 Keras(即tf.keras)与独立 Keras 包的模块结构存在差异,你遇到的导入问题本质是路径不匹配导致的。
正确导入image_preprocessing独立函数
不要使用独立 Keras 的导入路径,改用 TensorFlow 内置 Keras 的完整路径:
# 导入单个函数 from tensorflow.keras.layers.preprocessing.image_preprocessing import random_rotation, random_zoom # 或者通过tf.keras直接调用 import tensorflow as tf augmented_img = tf.keras.layers.preprocessing.image_preprocessing.random_flip_left_right(img)
修复preprocessing_utils导入错误
你之前的导入语句from keras.layers.preprocessing import preprocessing_utils as utils针对的是独立 Keras 包,在 TF 2.15.0 中需要替换为:
from tensorflow.keras.layers.preprocessing import preprocessing_utils as utils
该路径对应 TF 内置 Keras 中的工具模块,能正常访问所需工具函数。
复制函数代码时的注意事项
如果要手动复制image_preprocessing中的函数代码,需将函数内所有依赖的 Keras 模块路径统一替换为tensorflow.keras开头,避免混用独立 Keras 和 TF 内置 Keras 的模块,防止出现导入或运行时错误。
内容的提问来源于stack exchange,提问作者statsman92
相关产品推荐
相关产品推荐

