devspartan
devspartan

Reputation: 622

How to use tf-hub models locally

I,ve been trying to use a BERT model from tf-hub https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/2.

import tensorflow_hub as hub
bert_layer = hub.keras_layer('./bert_en_uncased_L-12_H-768_A-12_2', trainable=True)

But problem is that it is downloading data after every run.

So i downloaded the .tar file from tf-hub https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/2

Now i'm trying to use this downloaded tar file(after untar)

I've followed this tutorial https://medium.com/@xianbao.qian/how-to-run-tf-hub-locally-without-internet-connection-4506b850a915

But it didn't work out well and there is no further infromation or script is provided in this blog post

if someone can provide complete script to use the dowloaded model locally(without internet) or can improve the above blog post(Medium).

I've also tried

untarredFilePath = './bert_en_uncased_L-12_H-768_A-12_2'
bert_lyr = hub.load(untarredFilePath)
print(bert_lyr)

Output

<tensorflow.python.saved_model.load.Loader._recreate_base_user_object.<locals>._UserObject object at 0x7f05c46e6a10>

Doesn't seems to work.

or is there any other method to do so..??

Upvotes: 1

Views: 4173

Answers (5)

user941581
user941581

Reputation: 399

I found the modules downloaded to: /tmp/tfhub_modules

Upvotes: 0

arnoegw
arnoegw

Reputation: 1238

The tensorflow_hub library caches downloaded and uncompressed models on disk to avoid repeated uploads. The documentation at tensorflow.org/hub/caching has been expanded to discuss this and other cases.

Upvotes: 1

devspartan
devspartan

Reputation: 622

After getting information from tf-hub team they provide this solution. Let's say you have downloaded the .tar.gz file from official tf-hub model page from download button. You have extracted it. You got a folder which contain assets, variable and model. You put it in your working directory.

In script just add path to that folder:

import tensroflow-hub as hub

model_path ='./bert_en_uncased_L-12_H-768_A-12_2' # in my case
# one thing the path you have to provide is for folder which contain assets, variable and model
# not of the model.pb itself

lyr = hub.KerasLayer(model_path, trainable=True)

Hope it should work for you as well. Give it a try

Upvotes: 1

I wrote this script using this medium article(https://medium.com/@xianbao.qian/how-to-run-tf-hub-locally-without-internet-connection-4506b850a915) as reference. I am creating a cache directory within my project and the tensorflow model is cached locally in this cached directory and I am able to load the model locally. Hope this helps you.

import os
os.environ["TFHUB_CACHE_DIR"] = r'C:\Users\USERX\PycharmProjects\PROJECTX\tf_hub'

import tensorflow as tf
import tensorflow_hub as hub
import hashlib

handle = "https://tfhub.dev/google/universal-sentence-encoder/4"
hashlib.sha1(handle.encode("utf8")).hexdigest()


embed = hub.load("https://tfhub.dev/google/universal-sentence-encoder/4")
def get_sentence_embeddings(paragraph_array):
    embeddings=embed(paragraph_array)
    return embeddings

Upvotes: 1

Frederik Bode
Frederik Bode

Reputation: 2744

Hmm I cannot reproduce your problem. What worked for me:

script.sh

# download the model file using the 'wget' program
wget "https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/2?tf-hub-format=compressed"

# rename the downloaded file name to 'tar_file.tar.gz'
mv 2\?tf-hub-format\=compressed tar_file.tar.gz

# extract tar_file.tar.gz to the local directory 
tar -zxvf tar_file.tar.gz

# turn off internet

# run a test script
python3 test.py

# running the last command prints some tensorflow warnings, and then '<tensorflow_hub.keras_layer.KerasLayer object at 0x7fd702a7d8d0>'

test.py

import tensorflow_hub as hub
print(hub.KerasLayer('.'))

Upvotes: 1

Related Questions