在Flask应用中加载Keras模型时遇tree模块无flatten属性错误
AttributeError: module 'tree' has no attribute 'flatten'问题 问题详情
在Flask应用中加载Keras模型时触发错误:
AttributeError: module 'tree' has no attribute 'flatten'
使用的TensorFlow 2版本应为2.16.1(原描述的12.16.1为笔误,TensorFlow 2系列版本号以2.x开头),加载模型的核心代码如下:
from flask import Flask, render_template, request from tensorflow.keras.models import load_model import numpy as np from tensorflow.keras.preprocessing.image import load_img from tensorflow.keras.applications.vgg16 import preprocess_input import os from tensorflow.keras.preprocessing import image app = Flask(__name__) model = load_model('mymodel.h5')
完整报错追踪:
Traceback (most recent call last):
File "C:\Users\ALTARON\Desktop\Project\model\app.py", line 10, in
model = load_model('mymodel.h5')
File "C:\Users\ALTARON\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\saving\saving_api.py", line 183, in load_model
return legacy_h5_format.load_model_from_hdf5(filepath)
File "C:\Users\ALTARON\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\legacy\saving\legacy_h5_format.py", line 133, in load_model_from_hdf5
model = saving_utils.model_from_config(
File "C:\Users\ALTARON\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\legacy\saving\saving_utils.py", line 85, in model_from_config
return serialization.deserialize_keras_object(
File "C:\Users\ALTARON\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\legacy\saving\serialization.py", line 495, in deserialize_keras_object
deserialized_obj = cls.from_config(
File "C:\Users\ALTARON\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\models\sequential.py", line 322, in from_config
model = cls(name=name)
File "C:\Users\ALTARON\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\models\model.py", line 144, in new
return super().new(cls)
File "C:\Users\ALTARON\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\layers\layer.py", line 217, in new
obj = super().new(cls, *args, **kwargs)
File "C:\Users\ALTARON\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\ops\operation.py", line 100, in new
flat_arg_values = tree.flatten(kwargs)
AttributeError: module 'tree' has no attribute 'flatten'
模型结构摘要:
Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d (Conv2D) (None, 256, 256, 32) 896 max_pooling2d (MaxPooling2D) (None, 85, 85, 32) 0 conv2d_1 (Conv2D) (None, 85, 85, 64) 18496 max_pooling2d_1 (MaxPooling2D) (None, 28, 28, 64) 0 conv2d_2 (Conv2D) (None, 28, 28, 128) 73856 conv2d_3 (Conv2D) (None, 28, 28, 128) 147584 max_pooling2d_2 (MaxPooling2D) (None, 9, 9, 128) 0 flatten (Flatten) (None, 10368) 0 dense (Dense) (None, 200) 2073800 dropout (Dropout) (None, 200) 0 dense_1 (Dense) (None, 38) 7638 ================================================================= Total params: 2322270 (8.86 MB) Trainable params: 2322270 (8.86 MB) Non-trainable params: 0 (0.00 Byte)
解决方案
该错误源于第三方tree包与TensorFlow/Keras依赖的dm-tree模块冲突,或dm-tree未正确安装/版本不兼容,以下是具体修复方案:
方案1:替换冲突模块
卸载用于目录遍历的第三方tree包,安装Keras所需的dm-tree:
pip uninstall -y tree pip install dm-tree
方案2:临时强制导入正确模块
若无法卸载tree包,可在加载模型前覆盖全局tree引用:
import sys import tree as dm_tree sys.modules['tree'] = dm_tree # 后续正常导入并加载模型 from tensorflow.keras.models import load_model model = load_model('mymodel.h5')
方案3:降级TensorFlow版本
若前两种方法无效,降级到依赖逻辑更稳定的TensorFlow 2.15.x版本:
pip install tensorflow==2.15.0
验证
完成修复后启动Flask应用,确认模型能正常加载,再测试图片预测流程,确保无报错。
内容的提问来源于stack exchange,提问作者Atul Anand

