[Python] Error when running app that uses tensorflow and pyinstaller

Discussão em 'Python' iniciado por Stack, Março 6, 2025.

  1. Stack

    Stack Membro Participativo

    very good everyone, thank you for going into my problem, so I'm going to get to the point, it turns out that when I compile with pyinstaller everything works well, tensorflow is imported correctly, I don't see any problems when importing the h5 model either, the problem is when I want to do a prediction with .predict(), when you run the application with the predict line in the application, instead of opening the application an error window opens that says:

    the code is very simple, this is it

    import tensorflow as tf
    modelo = tf.keras.models.load_model("model.h5")
    numeros_variados = tf.random.uniform(shape=(1, 30), minval=2, maxval=11, dtype=tf.int32)
    y_pred= modelo.predict(numeros_variados)
    print(y_pred)


    Traceback (most recent call last):
    File "main.py", line 37, in <module>
    y_pred= modelo.predict(numeros_variados)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "keras\src\utils\traceback_utils.py", line 122, in error_handler
    File "keras\src\utils\traceback_utils.py", line 119, in error_handler
    File "keras\src\backend\tensorflow\trainer.py", line 506, in predict
    File "keras\src\callbacks\callback_list.py", line 126, in on_predict_batch_end
    File "keras\src\callbacks\progbar_logger.py", line 66, in on_predict_batch_end
    File "keras\src\callbacks\progbar_logger.py", line 95, in _update_progbar
    File "keras\src\utils\progbar.py", line 182, in update
    File "keras\src\utils\io_utils.py", line 98, in print_msg
    AttributeError: 'NoneType' object has no attribute 'write'


    I also attach a screenshot of the error, and thank you very much in advance

    I need to be able to make the .predict() predictions without the error I described above appearing

    I don't know if it helps but I get this message in the terminal every time I run the script

    2024-05-02 13:48:11.074234: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable TF_ENABLE_ONEDNN_OPTS=0. 2024-05-02 13:48:14.790349: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable TF_ENABLE_ONEDNN_OPTS=0. 2024-05-02 13:48:22.773016: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations. To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
    WARNING:absl:Compiled the loaded model, but the compiled metrics have yet to be built. model.compile_metrics will be empty until you train or evaluate the model.

    Continue reading...

Compartilhe esta Página