简体   繁体   English

加载 tf.keras model 时出现问题

[英]Problems in loading a tf.keras model

I'm new to tensorflow and don't know anything much about it.我是 tensorflow 的新手,对此一无所知。 However, with help from the net I was able to train a very simple model.但是,在网络的帮助下,我能够训练出一个非常简单的 model。 But now, whenever I try to load it I get this error:但是现在,每当我尝试加载它时,我都会收到此错误:

Traceback (most recent call last):
  File "C:/Users/Sabyasachi/PycharmProjects/AI_chatbot/actual_bot.py", line 21, in <module>
    n_model = keras.models.load_model('chatbot')
  File "C:\Users\Sabyasachi\PycharmProjects\AI_chatbot\venv\lib\site-packages\tensorflow\python\keras\saving\save.py", line 206, in load_model
    return saved_model_load.load(filepath, compile, options)
  File "C:\Users\Sabyasachi\PycharmProjects\AI_chatbot\venv\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 152, in load
    loaded = tf_load.load_partial(path, nodes_to_load, options=options)
  File "C:\Users\Sabyasachi\PycharmProjects\AI_chatbot\venv\lib\site-packages\tensorflow\python\saved_model\load.py", line 775, in load_partial
    return load_internal(export_dir, tags, options, filters=filters)
  File "C:\Users\Sabyasachi\PycharmProjects\AI_chatbot\venv\lib\site-packages\tensorflow\python\saved_model\load.py", line 905, in load_internal
    loader = loader_cls(object_graph_proto, saved_model_proto, export_dir,
  File "C:\Users\Sabyasachi\PycharmProjects\AI_chatbot\venv\lib\site-packages\tensorflow\python\saved_model\load.py", line 162, in __init__
    self._load_all()
  File "C:\Users\Sabyasachi\PycharmProjects\AI_chatbot\venv\lib\site-packages\tensorflow\python\saved_model\load.py", line 258, in _load_all
    self._load_nodes()
  File "C:\Users\Sabyasachi\PycharmProjects\AI_chatbot\venv\lib\site-packages\tensorflow\python\saved_model\load.py", line 447, in _load_nodes
    slot_variable = optimizer_object.add_slot(
AttributeError: '_UserObject' object has no attribute 'add_slot'

I tried colab as well, but that returns a completely different error:我也尝试了 colab,但返回了一个完全不同的错误:

/usr/local/lib/python3.7/dist-packages/tensorflow/python/saved_model/loader_impl.py in parse_saved_model(export_dir)
    114         "SavedModel file does not exist at: %s%s{%s|%s}" %
    115         (export_dir, os.path.sep, constants.SAVED_MODEL_FILENAME_PBTXT,
--> 116          constants.SAVED_MODEL_FILENAME_PB))
    117 
    118 

OSError: SavedModel file does not exist at: chat_model/{saved_model.pbtxt|saved_model.pb}

Does anyone have any idea how to solve this?有谁知道如何解决这个问题? PS: I just used model.save(whateverfoldername) to save it and keras.models.load_model(whateverfoldername) to load it back again. PS:我刚刚使用 model.save(whateverfoldername) 保存它并使用 keras.models.load_model(whateverfoldername) 再次加载它。

EDIT: I did a bit of digging around, and checked up this page in colab - https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/keras/save_and_load.ipynb#scrollTo=uWwgNaz19TH2 Followed what they did and save the model's folder inside another folder called saved_model and then called the entire path while loading, and now it works perfectly.编辑:我做了一些挖掘,并在colab中检查了这个页面 - https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/keras/save_and_load。 ipynb#scrollTo=uWwgNaz19TH2 按照他们的做法,将模型的文件夹保存在另一个名为 saved_model 的文件夹中,然后在加载时调用整个路径,现在它可以完美运行了。 Save it like this:像这样保存它:

!mkdir -p saved_model
model.save('saved_model/chatbot')

and loading is as such: model = keras.models.load_model('saved_model/chatbot')并且加载是这样的: model = keras.models.load_model('saved_model/chatbot')

However, it still doesn't work on my desktop IDLE pycharm;但是,它仍然无法在我的桌面 IDLE pycharm 上运行; only on google Colab仅在谷歌 Colab 上

EDIT2: Did some more digging in Pycharm as well, and figured out the problem. EDIT2:在 Pycharm 中进行了更多挖掘,并找出了问题所在。 Apparently, what was wrong was that my imports were incorrect.显然,问题在于我的导入不正确。 My main.py included the code to create and train the model, and in the same directory I made actual_bot.py in which I'd create the actual bot.我的 main.py 包含创建和训练 model 的代码,并且在同一个目录中我创建了actual_bot.py,我将在其中创建实际的机器人。

However, my imports were incomplete in actual_bot.py.但是,我在 actual_bot.py 中的导入不完整。 Downloaded all the same things as main.py as well as the extras needed, and now it works like a charm.下载了与 main.py 相同的所有内容以及所需的附加内容,现在它就像一个魅力。

Upgrading tensorflow to 2.7 from 2.6 solved the issue.将 tensorflow 从 2.6 升级到 2.7 解决了这个问题。 Now I can save the TF's model as ONNX (using opset 15)现在我可以将 TF 的 model 保存为 ONNX(使用 opset 15)

/opt/conda/lib/python3.9/runpy.py:127: RuntimeWarning: 'tf2onnx.convert' found in sys.modules after import of package 'tf2onnx', but prior to execution of 'tf2onnx.convert'; this may result in unpredictable behaviour
  warn(RuntimeWarning(msg))
2021-11-25 20:33:30,584 - WARNING - '--tag' not specified for saved_model. Using --tag serve
2021-11-25 20:33:42,054 - INFO - Signatures found in model: [serving_default].
2021-11-25 20:33:42,055 - WARNING - '--signature_def' not specified, using first signature: serving_default
2021-11-25 20:33:42,057 - INFO - Output names: ['activation_2']
WARNING:tensorflow:From /opt/conda/lib/python3.9/site-packages/tf2onnx/tf_loader.py:706: extract_sub_graph (from tensorflow.python.framework.graph_util_impl) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.compat.v1.graph_util.extract_sub_graph`
2021-11-25 20:33:47,030 - WARNING - From /opt/conda/lib/python3.9/site-packages/tf2onnx/tf_loader.py:706: extract_sub_graph (from tensorflow.python.framework.graph_util_impl) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.compat.v1.graph_util.extract_sub_graph`
2021-11-25 20:33:52,639 - INFO - Using tensorflow=2.7.0, onnx=1.10.2, tf2onnx=1.9.3/1190aa
2021-11-25 20:33:52,640 - INFO - Using opset <onnx, 15>
2021-11-25 20:34:00,768 - INFO - Computed 0 values for constant folding
2021-11-25 20:34:05,944 - INFO - Optimizing ONNX model
2021-11-25 20:34:31,232 - INFO - After optimization: Add -1 (6->5), BatchNormalization -4 (20->16), Cast -8 (10->2), Concat -5 (14->9), Const -100 (272->172), Gather +1 (0->1), Identity -6 (6->0), Reshape +2 (4->6), Shape -5 (6->1), Slice -5 (6->1), Squeeze -6 (6->0), Transpose -91 (124->33), Unsqueeze -24 (24->0)
2021-11-25 20:34:31,990 - INFO - 
2021-11-25 20:34:31,990 - INFO - Successfully converted TensorFlow model gen_saved_model to ONNX
2021-11-25 20:34:31,990 - INFO - Model inputs: ['input_2', 'input_3']
2021-11-25 20:34:31,991 - INFO - Model outputs: ['activation_2']
2021-11-25 20:34:31,991 - INFO - ONNX model is saved at gen.onnx

I encountered to the same error with tensorflow version 2.7 and 2.6.我在使用 tensorflow 版本 2.7 和 2.6 时遇到了同样的错误。 But with tf version 2.5 the problem fixed.但是使用 tf 版本 2.5 解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM