Skip to content

Commit

Permalink
Replace hub.Module with TF1 Hub format when referring to the TF1 Hub …
Browse files Browse the repository at this point in the history
…file format.

PiperOrigin-RevId: 320047485
  • Loading branch information
TensorFlow Hub Authors authored and maringeo committed Jul 8, 2020
1 parent 4793e5b commit e888325
Show file tree
Hide file tree
Showing 20 changed files with 128 additions and 130 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ as well as other associated code and documentation.
* The asset types of [tfhub.dev](https://tfhub.dev/)
* [SavedModels for TensorFlow 2](docs/tf2_saved_model.md)
and the [Reusable SavedModel interface](docs/reusable_saved_models.md).
* Deprecated: [hub.Modules for TensorFlow 1](docs/tf1_hub_module.md) and
* Deprecated: [Models in TF1 Hub format](docs/tf1_hub_module.md) and
their [Common Signatures](docs/common_signatures/index.md) collection.
* Using the library
* [Installation](docs/installation.md)
Expand Down
2 changes: 1 addition & 1 deletion docs/_book.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ upper_tabs:
- heading: Asset types
- title: SavedModels for TensorFlow 2
path: /hub/tf2_saved_model
- title: "Deprecated: hub.Modules for TensorFlow 1"
- title: "Deprecated: TF1 Hub format"
path: /hub/tf1_hub_module
status: deprecated
# Contributing to TF-Hub - either models or code
Expand Down
8 changes: 4 additions & 4 deletions docs/common_issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ embed = hub.load('https://tfhub.dev/google/nnlm-en-dim128/1')
embed(['my text', 'batch'])
```

This error frequently arises when loading TF1 hub.Modules with the `hub.load()`
API in TF2. Adding the correct signature should fix this problem. See the
[TF-Hub migration guide for TF2](migration_tf2.md) for more details on moving to
TF2 and the use of TF1 hub.Modules in TF2.
This error frequently arises when loading models in TF1 Hub format with the
`hub.load()` API in TF2. Adding the correct signature should fix this problem.
See the [TF-Hub migration guide for TF2](migration_tf2.md) for more details on
moving to TF2 and the use of models in TF1 Hub format in TF2.

```python

Expand Down
60 changes: 30 additions & 30 deletions docs/migration_tf2.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ migrating your TensorFlow code from TensorFlow 1 to TensorFlow 2.
It complements TensorFlow's general
[migration guide](https://www.tensorflow.org/guide/migrate).

For TF2, TF Hub has switched away from the
[custom hub.Module format](tf1_hub_module.md) and its `hub.Module` API
to the native [SavedModel format of TF2](tf2_saved_model.md)
and its associated API of `hub.load()` and `hub.KerasLayer`.
For TF2, TF Hub has switched away from the [TF1 Hub format](tf1_hub_module.md)
and its `hub.Module` API to the native
[SavedModel format of TF2](tf2_saved_model.md) and its associated API of
`hub.load()` and `hub.KerasLayer`.

The `hub.Module` API remains available in the `tensorflow_hub` library
for use in TF1 and in the TF1 compatibility mode of TF2.
It can only load assets in the hub.Module format.
The `hub.Module` API remains available in the `tensorflow_hub` library for use
in TF1 and in the TF1 compatibility mode of TF2. It can only load models in the
TF1 Hub format.

The new API of `hub.load()` (and `hub.KerasLayer`, which wraps it for Keras)
works for TensorFlow 1.15 (in eager and graph mode) and in TensorFlow 2.
This new API can load the new TF2 SavedModel assets, and, with
the restrictions laid out below, for the legacy hub.Module assets.
works for TensorFlow 1.15 (in eager and graph mode) and in TensorFlow 2. This
new API can load the new TF2 SavedModel assets, and, with the restrictions laid
out below, for the legacy models in TF1 Hub format.

In general, it is recommended to use new API wherever possible.

Expand Down Expand Up @@ -48,8 +48,9 @@ Many tutorials show these APIs in action. See in particular
* [Text classification example notebook](https://github.com/tensorflow/hub/blob/master/examples/colab/tf2_text_classification.ipynb)
* [Image classification example notebook](https://github.com/tensorflow/hub/blob/master/examples/colab/tf2_image_retraining.ipynb)

If the hub.Module you use has a newer version that comes in the TF2 SavedModel
format, we recommend to switch the API and the module version at the same time.
If the model in TF1 Hub format you use has a newer version that comes in the TF2
SavedModel format, we recommend to switch the API and the module version at the
same time.

### Using the new API in Estimator training

Expand All @@ -72,29 +73,28 @@ estimator = tf.estimator.Estimator(..., config=run_config)
Starting with TF2.2, this option is no longer experimental, and
the `.experimental` piece can be dropped.

## Loading legacy models in TF1 Hub format

## Loading legacy hub.Modules

It can happen that a new TF2 SavedModel is not yet available for your
use-case and you need to load an legacy hub.Module. Starting in `tensorflow_hub`
release 0.7, you can use legacy hub.Modules together with `hub.KerasLayer` as
shown below:
It can happen that a new TF2 SavedModel is not yet available for your use-case
and you need to load an legacy model in TF1 Hub format. Starting in
`tensorflow_hub` release 0.7, you can use legacy model in TF1 Hub format
together with `hub.KerasLayer` as shown below:

```python
m = hub.KerasLayer(handle)
tensor_out = m(tensor_in)
```

Additionally `KerasLayer` exposes the ability to specify `tags`, `signature`,
`output_key` and `signature_outputs_as_dict` for more specific usages of
legacy hub.Modules and legacy SavedModels.

Note: `trainable=True` is NOT supported when loading old hub.Modules.
`output_key` and `signature_outputs_as_dict` for more specific usages of legacy
models in TF1 Hub format and legacy SavedModels.

Note: `trainable=True` is NOT supported when loading legacy models in TF1 Hub
format.

## Using lower level APIs

Old hub.Modules can be loaded via `tf.saved_model.load`. Instead of
Legacy TF1 Hub format models can be loaded via `tf.saved_model.load`. Instead of

```python
# DEPRECATED: TensorFlow 1
Expand All @@ -115,13 +115,13 @@ keyed by signature names. Calling such a function computes all its outputs,
even if unused. (This is different from the lazy evaluation of TF1's
graph mode.)

## Retraining legacy hub.Modules
## Retraining legacy models in TF1 Hub format

Retraining legacy hub.Modules with the new APIs is not supported. This is due to
them depending on `tf.saved_model.load` converting a `flat graph view` into
an `object view` and dropping important details. Such as: trainable variables
are imported as such, but update ops (for batch normalization etc.),
Retraining legacy models in TF1 Hub format with the new APIs is not supported.
This is due to them depending on `tf.saved_model.load` converting a `flat graph
view` into an `object view` and dropping important details. Such as: trainable
variables are imported as such, but update ops (for batch normalization etc.),
regularization losses and cond/while contexts for differentiation are dropped.

If you need to retrain legacy hub.Modules you will need to keep using the
1.x APIs.
If you need to retrain legacy models in TF1 Hub format you will need to keep
using the 1.x APIs.
47 changes: 21 additions & 26 deletions docs/tf1_hub_module.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
# hub.Modules in TensorFlow 1
# TF1 Hub format

At its launch in 2018, TensorFlow Hub offered a single type of asset:
hub.Modules for import into TensorFlow 1 programs.
At its launch in 2018, TensorFlow Hub offered a single type of asset: TF1 Hub
format for import into TensorFlow 1 programs.

This page explains how to use hub.Modules in TensorFlow 1
(or the TF1 compatibility mode of TF2) with the `hub.Module` class
and associated APIs. (The typical use is to build a `tf.Graph`,
possibly inside a TF1 `Estimator`, by combining one or more `hub.Modules`
with `tf.compat.layers` or `tf.layers`).
This page explains how to use TF1 Hub format in TF1 (or the TF1 compatibility
mode of TF2) with the `hub.Module` class and associated APIs. (The typical use
is to build a `tf.Graph`, possibly inside a TF1 `Estimator`, by combining one or
more models in TF1 Hub format with `tf.compat.layers` or `tf.layers`).

Users of TensorFlow 2 (outside TF1 compatibility mode) must use
[the new API with `hub.load()` or `hub.KerasLayer`](tf2_saved_model.md).
The new API loads the new TF2 SavedModel asset type, but also has
limited [support for loading hub.Modules into TF2](migration_tf2.md).
[the new API with `hub.load()` or `hub.KerasLayer`](tf2_saved_model.md). The new
API loads the new TF2 SavedModel asset type, but also has limited
[support for loading TF1 Hub format into TF2](migration_tf2.md).

## Using a model in TF1 Hub format

## Using a Module
### Instantiating a model in TF1 Hub format

### Instantiating a Module

A hub.Module is imported into a TensorFlow program by
creating a `hub.Module` object from a string with its URL or filesystem path,
such as:
A model in TF1 Hub format is imported into a TensorFlow program by creating a
`hub.Module` object from a string with its URL or filesystem path, such as:

```python
m = hub.Module("path/to/a/module_dir")
Expand Down Expand Up @@ -93,18 +90,16 @@ To this end, we maintain a collection of recommended

### Compatibility note

The hub.Module format is geared towards TensorFlow 1.
It is only partially supported by TF Hub in TensorFlow 2.
Please do consider publishing in the new
The TF1 Hub format is geared towards TensorFlow 1. It is only partially
supported by TF Hub in TensorFlow 2. Please do consider publishing in the new
[TF2 SavedModel](tf2_saved_model.md) format instead.

The hub.Module format is similar to the SavedModel format of TensorFlow 1
on a syntactic level (same file names and protocol messages) but semantically
The TF1 Hub format is similar to the SavedModel format of TensorFlow 1 on a
syntactic level (same file names and protocol messages) but semantically
different to allow for module reuse, composition and re-training (e.g.,
different storage of resource initializers, different tagging conventions
for metagraphs). The easiest way to tell them apart on disk is the
presence or absence of the `tfhub_module.pb` file.

different storage of resource initializers, different tagging conventions for
metagraphs). The easiest way to tell them apart on disk is the presence or
absence of the `tfhub_module.pb` file.

### General approach

Expand Down
24 changes: 12 additions & 12 deletions docs/tf2_saved_model.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# SavedModels from TF Hub in TensorFlow 2

The [SavedModel format of
TensorFlow 2](https://www.tensorflow.org/guide/saved_model)
is the recommended way to share pre-trained models and model pieces
on TensorFlow Hub. It replaces the older [hub.Module format for
TensorFlow 1](tf1_hub_module.md) and comes with a new set of APIs.

This page explains how to reuse TF2 SavedModels in a TensorFlow 2
program with the low-level `hub.load()` API and its `hub.KerasLayer`
wrapper. (Typically, `hub.KerasLayer` is combined with other `tf.keras.layers`
to build a Keras model or the `model_fn` of a TF2 Estimator.)
These APIs can also load the older hub.Modules for TF1, within limits,
see the [migration guide](migration_tf2.md).
The
[SavedModel format of TensorFlow 2](https://www.tensorflow.org/guide/saved_model)
is the recommended way to share pre-trained models and model pieces on
TensorFlow Hub. It replaces the older [TF1 Hub format](tf1_hub_module.md) and
comes with a new set of APIs.

This page explains how to reuse TF2 SavedModels in a TensorFlow 2 program with
the low-level `hub.load()` API and its `hub.KerasLayer` wrapper. (Typically,
`hub.KerasLayer` is combined with other `tf.keras.layers` to build a Keras model
or the `model_fn` of a TF2 Estimator.) These APIs can also load the legacy
models in TF1 Hub format, within limits, see the
[migration guide](migration_tf2.md).

Users of TensorFlow 1 can update to TF 1.15 and then use the same APIs.
Older versions of TF1 do not work.
Expand Down
4 changes: 2 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ embeddings for real-time similarity matching and retrieval.

#### [`colab/semantic_approximate_nearest_neighbors`](colab/semantic_approximate_nearest_neighbors.ipynb)

This tutorial illustrates how to generate embeddings from a
[TF1 legacy hub.Module](https://www.tensorflow.org/hub/tf1_hub_module) given
This tutorial illustrates how to generate embeddings from a model in the legacy
[TF1 Hub format](https://www.tensorflow.org/hub/tf1_hub_module) given
input data and build an approximate nearest neighbours (ANN) index using the
extracted embeddings for real-time similarity matching and retrieval.

Expand Down
2 changes: 1 addition & 1 deletion examples/colab/tf2_image_retraining.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
"source": [
"## Select the TF2 SavedModel module to use\n",
"\n",
"For starters, use https://tfhub.dev/google/imagenet/mobilenet_v2_100_224/feature_vector/4. The same URL can be used in code to identify the SavedModel and in your browser to show its documentation. (Note that `hub.Modules` for TF 1.x won't work here.)"
"For starters, use https://tfhub.dev/google/imagenet/mobilenet_v2_100_224/feature_vector/4. The same URL can be used in code to identify the SavedModel and in your browser to show its documentation. (Note that models in TF1 Hub format won't work here.)"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion tensorflow_hub/e2e_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def test_load(self):
if not hasattr(tf_v1.saved_model, "load_v2"):
try:
hub.load("@my/tf2_module/2")
self.fail("Failure expected. hub.module() not support in TF 1.x")
self.fail("Failure expected. hub.load() not supported in TF 1.x")
except NotImplementedError:
pass
elif tf_v1.executing_eagerly():
Expand Down
4 changes: 2 additions & 2 deletions tensorflow_hub/estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
def register_module_for_export(module, export_name):
"""Register a Module to be exported under `export_name`.
DEPRECATION NOTE: This belongs to the hub.Module API and file format for TF1.
DEPRECATION NOTE: This belongs to the hub.Module API and TF1 Hub format.
This function registers `module` to be exported by `LatestModuleExporter`
under a subdirectory named `export_name`.
Expand All @@ -64,7 +64,7 @@ def register_module_for_export(module, export_name):
class LatestModuleExporter(tf_v1.estimator.Exporter):
"""Regularly exports registered modules into timestamped directories.
DEPRECATION NOTE: This belongs to the hub.Module API and file format for TF1.
DEPRECATION NOTE: This belongs to the hub.Module API and TF1 Hub format.
Modules can be registered to be exported by this class by calling
`register_module_for_export` when constructing the graph. The
Expand Down
8 changes: 4 additions & 4 deletions tensorflow_hub/image_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
from tensorflow_hub import native_module


# hub.Modules for images can provide further information for the utilities
# in this file by attaching an ImageModuleInfo message under this key.
# Models in TF1 Hub format for images can provide further information for the
# utilities in this file by attaching an ImageModuleInfo message under this key.
IMAGE_MODULE_INFO_KEY = "image_module_info"


Expand All @@ -34,7 +34,7 @@
def attach_image_module_info(image_module_info):
"""Attaches an ImageModuleInfo message from within a module_fn.
DEPRECATION NOTE: This belongs to the hub.Module API and file format for TF1.
DEPRECATION NOTE: This belongs to the hub.Module API and TF1 Hub format.
Args:
image_module_info: an ImageModuleInfo message.
Expand All @@ -45,7 +45,7 @@ def attach_image_module_info(image_module_info):
def get_image_module_info(module_or_spec, required=False):
"""Returns the module's attached ImageModuleInfo message, or None if missing.
DEPRECATION NOTE: This belongs to the hub.Module API and file format for TF1.
DEPRECATION NOTE: This belongs to the hub.Module API and TF1 Hub format
Args:
module_or_spec: a hub.Module or module_spec object.
Expand Down
Loading

0 comments on commit e888325

Please sign in to comment.