Hub: Feature request: use Hub modules with Eager execution enabled

Created on 1 Aug 2018  路  23Comments  路  Source: tensorflow/hub

Hi, I guess this is best classified as a support request. I'm trying to use a Hub resnet image model in a transfer learning application. Most of my project takes advantage of tensorflow's eager execution mode. When I add in the image module, I am met with the following error, below, at the line that should download and import the module (is inside of a tfe.Network.__init__() method.).

The main message is pretty straightforward: that no graph exists for eager mode therefore the Hub module graph cannot be imported. Is there a way around this.. or will I have to dump the variables to a npy and use them to initialize eager tensors?

Traceback (most recent call last):
  File "run.py", line 170, in <module>
    main(train_list, val_list, test_list, Model, loss_function, arch_dir, fold_num)
  File "run.py", line 75, in main
    model = Model()
  File "/home/.../script.py", line 32, in __init__
    self.resnet = hub.Module(MODULE_URL, trainable=True, tags={'train'})
  File "/home/ing/envs/tensorflow/local/lib/python2.7/site-packages/tensorflow_hub/module.py", line 126, in __init__
    tags=self._tags)
  File "/home/ing/envs/tensorflow/local/lib/python2.7/site-packages/tensorflow_hub/native_module.py", line 282, in _create_impl
    name=name)
  File "/home/ing/envs/tensorflow/local/lib/python2.7/site-packages/tensorflow_hub/native_module.py", line 333, in __init__
    variable_tensor_map, self._state_map = self._create_state_graph(name)
  File "/home/ing/envs/tensorflow/local/lib/python2.7/site-packages/tensorflow_hub/native_module.py", line 383, in _create_state_graph
    restore_collections_predicate=(lambda key: key in import_collections))
  File "/home/ing/envs/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 1943, in import_meta_graph
    raise RuntimeError("Exporting/importing meta graphs is not supported when "
RuntimeError: Exporting/importing meta graphs is not supported when eager execution is enabled. No graph exists when eager execution is enabled.

Some information about my environment:

$ pip freeze | grep tensorflow
tensorflow==1.8.0
tensorflow-gpu==1.7.0
tensorflow-hub==0.1.0
tensorflow-tensorboard==1.5.0

$ python --version
Python 2.7.12
hub awaiting tensorflower feature

Most helpful comment

+1 for the request

All 23 comments

Hi Nathan, using Hub modules from Eager mode would be a great, but regrettably it is not possible at this point in time. I'll keep this issue open for the feature request.

+1 for the request

+1 for the request

+1 for the request

+1 for the request

@arnoegw considering that Eager would be the default execution mode from TF 2 and upwards, wouldn't this feature request be more than just a "nice thing to have" ? I'm curious if there any timeline for eager support.

@gokul-uf: Agreed. We are working on TF2 support, including Eager mode, in time for the TF2.0 release (not preview).

Thanks for the update @arnoegw For those who can't wait until until TF2, I believe tfe.py_func might be a nice intermediate. Hacky, yes. But it works. See here for an example

+1 for this

Looking forward for this.

I'm a early TF 2.0 adapter but can't figure how to use TF Hub within TF 2.0 preview version.

Closing this Feature Request as we can use Hub Modules with Eager Execution Enabled from Tensorflow 2.0 Alpha version on wards. Please feel free to reopen this issue if your requirement is not implemented.

+1 for this

+1

I checked with the recently released tensorflow 2.0.0 (stable), the issue is still persisting.

I tried it for accessing the ELMo vectors using the following tensorflow-hub line:
elmo_model = hub.Module("https://tfhub.dev/google/elmo/2", trainable=True)

Exception:
RuntimeError: Exporting/importing meta graphs is not supported when eager execution is enabled. No graph exists when eager execution is enabled.

Please use

elmo_model = hub.load("https://tfhub.dev/google/elmo/2")
out = elmo_model.signatures["default"](in)

...optionally with a tf.GradientTape() around the call.

In TF2, hub.load() is the right API to use for both TF1-style Hub modules and TF2-style SavedModels. All it does is fetch the model contents into TFHUB_CACHE_DIR and then call tf.saved_model.load() from there.

Moe detailed documentation on TF Hub will be forthcoming. For TF2-style SavedModels in general, please see https://www.tensorflow.org/guide/saved_model

Hello,
I've just tried the following:

import tensorflow as tf
import tensorflow_hub as hub

elmo = hub.load('https://tfhub.dev/google/elmo/2')
test_input = tf.constant(['the cat is on the table'], dtype=tf.string)

with tf.GradientTape() as tape:
    result = elmo.signatures['default'](test_input)

And it throws AttributeError: 'NoneType' object has no attribute 'outer_context' when calling the loaded ELMo model. On the other hand, removing the tf.GradientTape() wrap up, the code works. Am I missing something?

elmo = hub.load('https://tfhub.dev/google/elmo/2')
test_input = tf.constant(['the cat is on the table'], dtype=tf.string)
result = elmo.signatures['default'](test_input)

Tensorflow config:
tensorflow-estimator==2.0.0
tensorflow-gpu==2.0.0
tensorflow-hub==0.6.0

Python config:
Python 3.5.3

EDIT

I've also tested BERT hub model without reporting any error. I don't know if the problem is exclusively attributable to the ELMo hub model then. Anyone got it working?

+1 for the request
I tried reading BERT vectors using the following line:
bert_module = hub.Module(bert_path)
but got RuntimeError: Exporting/importing meta graphs is not supported when eager execution is enabled. No graph exists when eager execution is enabled.

config:
Python 3.6.9
tensorflow-2.0.0
bert-tensorflow-1.0.1
tensorflow-hub-0.6.0

+1 for the request.
I tried load USE module using these lines:

use_module = hub.load("https://tfhub.dev/google/universal-sentence-encoder-large/3")
def embed_use(self, x):
        return self.use_module(tf.reshape(tf.cast(x, 'string'), [-1]), signature='default', as_dict=True)['default']

but got TypeError: 'AutoTrackable' object is not callable

my config:

  • Python == 3.7.2
  • tensorflow_hub == 0.7.0
  • tensorflow == 2.0.0

@ReemSuwaileh: For TF2 versions of BERT, please see the new models https://tfhub.dev/s?publisher=tensorflow&q=bert published from the reimplementation of BERT from tensorflow/models.

@Carolyn95: To get the TF2 version of google/universal-sentence-encoder-large, please upgrade to https://tfhub.dev/google/universal-sentence-encoder-large/4

For using old models in the hub.Module format with hub.load(), please see https://www.tensorflow.org/hub/migration_tf2 and notice how the signature is picked out from a dict (not passed as an argument).

@federicoruggeri: Thanks for reporting the problem about tf.GradientTape and the elmo model. This is important but distinct enough to be tracked separately. I filed it as https://github.com/tensorflow/hub/issues/416

The original feature request has been fulfilled. Please file separate issues if you encounter any problems besides the ones addressed above. Thank you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bzburr picture bzburr  路  4Comments

truas picture truas  路  5Comments

basroelenga picture basroelenga  路  3Comments

marcoaleixo picture marcoaleixo  路  4Comments

bhack picture bhack  路  3Comments