【问题标题】:How can I load the hub.KerasLayer offline?如何离线加载 hub.KerasLayer?
【发布时间】:2020-05-29 17:08:43
【问题描述】:
我已经通过 tensorflow hub 构建了一个模型并保存了它。
但是当我加载它时,我必须添加custom_objects={'KerasLayer':hub.KerasLayer}。
它将连接网络。
model = tf.keras.models.load_model('my_model.h5',custom_objects={'KerasLayer':hub.KerasLayer})
如何下载hub.KerasLayer 并离线加载?
【问题讨论】:
标签:
tensorflow2.0
keras-layer
h5py
tensorflow-hub
【解决方案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") 的调用将使用本地副本
【解决方案2】:
如果您有从 TFHub 页面下载的模型,
例如,
import tensorflow as tf
import tensorflow_hub as hub
model_path = "path/to/tfhubmodel/"
hub_layer = hub.KerasLayer(hub.load(model_path))