我已经通过tensorflow hub建立了一个模型并保存了。
但是,当我加载它时,我必须添加custom_objects={'KerasLayer':hub.KerasLayer}
。
它将连接网络。
model = tf.keras.models.load_model('my_model.h5',custom_objects={'KerasLayer':hub.KerasLayer})
如何下载hub.KerasLayer
并离线加载?
答案 0 :(得分:1)
您还可以使用TFHUB_CACHE_DIR自动执行此操作
import tensorflow_hub as hub
import os
os.environ["TFHUB_CACHE_DIR"] = "/tmp/model"
hub.KerasLayer("https://tfhub.dev/google/nnlm-id-dim50-with-normalization/2")
模型资产将下载到/ tmp / model /
以后对hub.KerasLayer("https://tfhub.dev/google/nnlm-id-dim50-with-normalization/2")
的调用将使用本地副本
答案 1 :(得分:0)
其中有些取决于您是否具有正确保存的模型。在这种情况下,您可以
import tensorflow as tf
import tensorflow_hub as hub
k_layer = hub.KerasLayer("some/file/path")
有关更多详细信息,请参见https://www.tensorflow.org/hub/tf2_saved_model。
答案 2 :(得分:0)
如果您从TFHub页面下载了模型,
例如,
import tensorflow as tf
import tensorflow_hub as hub
model_path = "path/to/tfhubmodel/"
hub_layer = hub.KerasLayer(hub.load(model_path))