[Python] AttributeError: module 'tree' has no attribute 'flatten' while loading Keras model in...

Discussão em 'Python' iniciado por Stack, Setembro 3, 2025.

  1. Stack

    Stack Membro Participativo

    I'm encountering an


    AttributeError: module 'tree' has no attribute 'flatten'
    ​

    when trying to load a Keras model in my Flask application. I'm using TensorFlow 2 (version: [12.16.1]), and the code for loading the model looks like this:

    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')


    Error:


    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 summary:

    Model: "sequential"

    _________________________________________________________________
    Layer (type) Output Shape Param #
    =================================================================
    conv2d (Conv2D) (None, 256, 256, 32) 896

    max_pooling2d (MaxPooling2 (None, 85, 85, 32) 0
    D)

    conv2d_1 (Conv2D) (None, 85, 85, 64) 18496

    max_pooling2d_1 (MaxPoolin (None, 28, 28, 64) 0
    g2D)

    conv2d_2 (Conv2D) (None, 28, 28, 128) 73856

    conv2d_3 (Conv2D) (None, 28, 28, 128) 147584

    max_pooling2d_2 (MaxPoolin (None, 9, 9, 128) 0
    g2D)

    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)

    Continue reading...

Compartilhe esta Página