tensorflow2.0keras-layerh5pytensorflow-hub

How can I load the hub.KerasLayer offline?


I have build a model by tensorflow hub and save it. But when I load it, I must add custom_objects={'KerasLayer':hub.KerasLayer}. It will connect the network.

model = tf.keras.models.load_model('my_model.h5',custom_objects={'KerasLayer':hub.KerasLayer})

How can I download the hub.KerasLayer and load it offline?


Solution

  • You can also use TFHUB_CACHE_DIR to do this automatically

    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")
    

    Model assets will be downloaded to /tmp/model/ Future calls to hub.KerasLayer("https://tfhub.dev/google/nnlm-id-dim50-with-normalization/2") will use the local copy