How to save/load a tensorflow hub module to/from a custom path?
Asked Answered
M

6

11

The tensorflow_hub library maintainers has made it every easy for users to download and use the pre-trained tensorflow modules, e.g.:

import tensorflow_hub as hub

embed = hub.Module("https://tfhub.dev/google/universal-sentence-encoder/1")

But from the sys.stderr it seemed like it was saving the module locally to a temporary directory, i.e.

INFO:tensorflow:Using /var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules to cache modules. INFO:tensorflow:Initialize variable module/Embeddings_en/sharded_0:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with Embeddings_en/sharded_0 INFO:tensorflow:Initialize variable module/Embeddings_en/sharded_1:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with Embeddings_en/sharded_1 INFO:tensorflow:Initialize variable module/Embeddings_en/sharded_10:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with Embeddings_en/sharded_10 INFO:tensorflow:Initialize variable module/Embeddings_en/sharded_11:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with Embeddings_en/sharded_11 INFO:tensorflow:Initialize variable module/Embeddings_en/sharded_12:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with Embeddings_en/sharded_12 INFO:tensorflow:Initialize variable module/Embeddings_en/sharded_13:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with Embeddings_en/sharded_13 INFO:tensorflow:Initialize variable module/Embeddings_en/sharded_14:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with Embeddings_en/sharded_14 INFO:tensorflow:Initialize variable module/Embeddings_en/sharded_15:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with Embeddings_en/sharded_15 INFO:tensorflow:Initialize variable module/Embeddings_en/sharded_16:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with Embeddings_en/sharded_16 INFO:tensorflow:Initialize variable module/Embeddings_en/sharded_2:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with Embeddings_en/sharded_2 INFO:tensorflow:Initialize variable module/Embeddings_en/sharded_3:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with Embeddings_en/sharded_3 INFO:tensorflow:Initialize variable module/Embeddings_en/sharded_4:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with Embeddings_en/sharded_4 INFO:tensorflow:Initialize variable module/Embeddings_en/sharded_5:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with Embeddings_en/sharded_5 INFO:tensorflow:Initialize variable module/Embeddings_en/sharded_6:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with Embeddings_en/sharded_6 INFO:tensorflow:Initialize variable module/Embeddings_en/sharded_7:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with Embeddings_en/sharded_7 INFO:tensorflow:Initialize variable module/Embeddings_en/sharded_8:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with Embeddings_en/sharded_8 INFO:tensorflow:Initialize variable module/Embeddings_en/sharded_9:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with Embeddings_en/sharded_9 INFO:tensorflow:Initialize variable module/Encoder_en/DNN/ResidualHidden_0/weights:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with Encoder_en/DNN/ResidualHidden_0/weights INFO:tensorflow:Initialize variable module/Encoder_en/DNN/ResidualHidden_1/weights:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with Encoder_en/DNN/ResidualHidden_1/weights INFO:tensorflow:Initialize variable module/Encoder_en/DNN/ResidualHidden_2/weights:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with Encoder_en/DNN/ResidualHidden_2/weights INFO:tensorflow:Initialize variable module/Encoder_en/DNN/ResidualHidden_3/projection:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with Encoder_en/DNN/ResidualHidden_3/projection INFO:tensorflow:Initialize variable module/Encoder_en/DNN/ResidualHidden_3/weights:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with Encoder_en/DNN/ResidualHidden_3/weights INFO:tensorflow:Initialize variable module/SHARED_RANK_ANSWER/response_encoder_0/tanh_layer_0/bias:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with SHARED_RANK_ANSWER/response_encoder_0/tanh_layer_0/bias INFO:tensorflow:Initialize variable module/SHARED_RANK_ANSWER/response_encoder_0/tanh_layer_0/weights:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with SHARED_RANK_ANSWER/response_encoder_0/tanh_layer_0/weights INFO:tensorflow:Initialize variable module/SHARED_RANK_ANSWER/response_encoder_0/tanh_layer_1/bias:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with SHARED_RANK_ANSWER/response_encoder_0/tanh_layer_1/bias INFO:tensorflow:Initialize variable module/SHARED_RANK_ANSWER/response_encoder_0/tanh_layer_1/weights:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with SHARED_RANK_ANSWER/response_encoder_0/tanh_layer_1/weights INFO:tensorflow:Initialize variable module/SHARED_RANK_ANSWER/response_encoder_0/tanh_layer_2/bias:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with SHARED_RANK_ANSWER/response_encoder_0/tanh_layer_2/bias INFO:tensorflow:Initialize variable module/SHARED_RANK_ANSWER/response_encoder_0/tanh_layer_2/weights:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with SHARED_RANK_ANSWER/response_encoder_0/tanh_layer_2/weights INFO:tensorflow:Initialize variable module/SNLI/Classifier/LinearLayer/bias:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with SNLI/Classifier/LinearLayer/bias INFO:tensorflow:Initialize variable module/SNLI/Classifier/LinearLayer/weights:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with SNLI/Classifier/LinearLayer/weights INFO:tensorflow:Initialize variable module/SNLI/Classifier/tanh_layer_0/bias:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with SNLI/Classifier/tanh_layer_0/bias INFO:tensorflow:Initialize variable module/SNLI/Classifier/tanh_layer_0/weights:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with SNLI/Classifier/tanh_layer_0/weights INFO:tensorflow:Initialize variable module/global_step:0 from checkpoint b'/var/folders/j6/xczfl75n3sbfwpg4190gpb104vnlxt/T/tfhub_modules/c6f5954ffa065cdb2f2e604e740e8838bf21a2d3/variables/variables' with global_step

After a machine reboot, the module gets deleted and running the hub.Module('...') code again would re-download the module.

Is is possible to save the module to a custom directory and afterwards load from the custom directory?

If it's possible, how to save/load a tensorflow hub module to/from a custom path?

Macur answered 14/5, 2018 at 1:7 Comment(0)
L
-3

It is indeed possible to specify/use a custom directory :) For instructions, please see the section called "Caching Modules" at https://www.tensorflow.org/hub/basics

Linearity answered 14/5, 2018 at 9:42 Comment(2)
This answer should be more specific with at least one example.Syntax
the link is also broken! :(Brewington
E
21

You can download your model need from url + '?tf-hub-format=compressed'

i tried downloading elmo and it worked

url = https://tfhub.dev/google/elmo/2 + '?tf-hub-format=compressed'

eg: https://tfhub.dev/google/elmo/2?tf-hub-format=compressed

the model will be downloaded as a tarfile to your machine.

once you untar the file, it will have tfhub_module.pb

Excitable answered 14/9, 2018 at 10:25 Comment(2)
How do i use this downloaded model ?Organology
Sundeep: https://mcmap.net/q/702519/-how-to-load-tf-hub-model-from-local-system/854477Incogitable
C
10
  • Take the URL and change it from:

https://tfhub.dev/google/universal-sentence-encoder/1

To:

https://storage.googleapis.com/tfhub-modules/google/universal-sentence-encoder/1.tar.gz

  • Download using Curl or browser.
  • Extract to a prefered location (e.g. /home/admin/embed/)
  • Change your code to:

.

import tensorflow_hub as hub 
embed = hub.load('/home/admin/embed/')
Citrine answered 7/11, 2019 at 17:3 Comment(0)
N
6

1) Find your model: e.g. https://tfhub.dev/google/imagenet/inception_v1/feature_vector/1

2) Get the real download path:

replace tfhub.dev with storage.googleapis.com/tfhub-modules and append .tar.gz as the suffix.

3) Prepare the cache:

On some platforms, TF hub will log the cache directory, but some won’t. Specifying the cache location in the code is much more reliable. Just put the following code in your file before calling tfhub to do so.

os.environ["TFHUB_CACHE_DIR"] = '/tmp/tfhub'

reference: How to run TF hub locally without internet connection

Nailbiting answered 10/1, 2020 at 15:45 Comment(0)
P
3

For those still looking for an answer, specifically for ELMO, as @harikris said, you can use

url = https://tfhub.dev/google/elmo/2 + '?tf-hub-format=compressed'

format to download a module, e.g. this link

After downloading, extract the contents into a folder and then in the hub.Module code simply replace your URL with the path to the directory, as stated in

https://www.tensorflow.org/hub/basics

Punctuation answered 7/6, 2019 at 15:2 Comment(0)
S
1

If you didn't set the TFHUB_CACHE_DIR environment variable value, the default cache directory is:

cache_dir = os.path.join(tempfile.gettempdir(), "tfhub_modules")

And the downloaded hub is inside it.

Submerse answered 18/10, 2019 at 7:53 Comment(1)
Adding reference to the answer: tensorflow.org/hub/cachingPredation
L
-3

It is indeed possible to specify/use a custom directory :) For instructions, please see the section called "Caching Modules" at https://www.tensorflow.org/hub/basics

Linearity answered 14/5, 2018 at 9:42 Comment(2)
This answer should be more specific with at least one example.Syntax
the link is also broken! :(Brewington

© 2022 - 2024 — McMap. All rights reserved.