【问题标题】: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))
      

      【讨论】:

        【解决方案3】:

        其中一些取决于您是否保存了正确的模型。在这种情况下,您可以

        import tensorflow as tf
        import tensorflow_hub as hub
        
        
        k_layer = hub.KerasLayer("some/file/path")
        

        请参阅https://www.tensorflow.org/hub/tf2_saved_model 了解更多详情。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-06-26
          • 1970-01-01
          • 1970-01-01
          • 2014-07-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多