简体   繁体   中英

Problems in loading a tf.keras model

I'm new to tensorflow and don't know anything much about it. However, with help from the net I was able to train a very simple 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:

/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.

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

However, it still doesn't work on my desktop IDLE pycharm; only on google Colab

EDIT2: Did some more digging in Pycharm as well, and figured out the problem. 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.

However, my imports were incomplete in actual_bot.py. Downloaded all the same things as main.py as well as the extras needed, and now it works like a charm.

Upgrading tensorflow to 2.7 from 2.6 solved the issue. Now I can save the TF's model as ONNX (using 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. But with tf version 2.5 the problem fixed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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