NameError: name 'tf' is not defined

NameError: name 'tf' is not defined

报错如下:


/opt/conda/lib/python3.6/site-packages/tensorflow/python/keras/layers/core.py in <lambda>(x)
    307       It defaults to the `image_data_format` value found in your
    308       Keras config file at `~/.keras/keras.json`.
--> 309       If you never set it, then it will be "channels_last".
    310 
    311   Call arguments:

NameError: name 'tf' is not defined


这个报错比较奇怪,定位到是执行到如下语句的报错:

tf.keras.models.load_model(MODEL_PATH)

之前遇到过几次,我试了下主要有两种解决方式是:

第一种解决方式是重新拉取MODEL_PATH的文件,可能在拉取过程中发生一些文件损坏。

第二种解决方式是,观察报错,提示 name 'tf' is not defined,说明内部没有将tensorflow这个库传入内部当中,用以下语句进行解决:

tf.keras.models.load_model(MODEL_PATH, custom_objects={'tf': tf})
posted @ 2023-03-29 09:49  NoMornings  阅读(460)  评论(0编辑  收藏  举报