首页 > 解决方案 > 无法使用更新代码在 tensorflow V2 中加载 TensorFlow v1 模型?

问题描述

我保存了 .ckpt 和 .h5 格式的模型,其中我的文件夹包含 4 个文件。

如果我使用命令加载版本 1

model.load("model.ckpt") or model.load("model.h5")

它在 Tensorflow 版本 1 中加载成功,但在 tensorflow 版本 2 中加载时显示错误

  tf.keras.models.load_model('model.h5') or  tf.keras.models.load_model('model.ckpt')

有时会出错

SavedModel file does not exist at: /model/model.ckpt/{saved_model.pbtxt|saved_model.pb}

或有时给出错误

OSError: Unable to open file (unable to open file: name = 'model.ckpt', errno = 2, error message = 
'No such file or directory', flags = 0, o_flags = 0)

我仔细检查了我在 google colab 中上传的路径并给出了确切的位置,但没有工作。

这是我的代码链接

https://colab.research.google.com/drive/1hf7AwEMJHf4zLZOBSzxejA2ISuTQ3N9K

标签: python-3.xdeep-learningnlptensorflow2.0

解决方案


If the file is inside a folder in your drive then add your folder's name in the path. otherwise use the code as it is.

import os
from google.colab import drive
drive.mount('/content/gdrive')
model_file_name = os.path.join("/content/gdrive/My Drive", 'model.h5')# CHANGE MODEL NAME
model = tf.keras.models.load_model(model_file_name)

On execution it will show a link click on it and choose your drive account where the file is located, then copy paste the key in your program.


推荐阅读