Skip to content

Commit c465681

Browse files
authored
nightly test fix (#8214)
* testing nightly fix * disable mobilenet test * fix converter and e2e tests
1 parent f0f981f commit c465681

21 files changed

+611
-636
lines changed

e2e/integration_tests/convert_predict.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
import numpy as np
3939
import tensorflow as tf
40+
import tf_keras
4041
from tensorflow.python.eager import def_function
4142
from tensorflow.python.framework import constant_op
4243
from tensorflow.python.framework import dtypes
@@ -232,16 +233,16 @@ def _create_saved_model_with_conv2d(save_dir):
232233
save_dir: directory name of where the saved model will be stored.
233234
"""
234235
layers = [
235-
tf.keras.layers.Conv2D(
236+
tf_keras.layers.Conv2D(
236237
16, [3, 3], padding='same', use_bias=False),
237-
tf.keras.layers.BatchNormalization(),
238-
tf.keras.layers.ReLU()
238+
tf_keras.layers.BatchNormalization(),
239+
tf_keras.layers.ReLU()
239240
]
240-
model = tf.keras.Sequential(layers)
241+
model = tf_keras.Sequential(layers)
241242
result = model.predict(tf.ones((1, 24, 24, 3)))
242243
# set the learning phase to avoid keara learning placeholder, which
243244
# will cause error when saving.
244-
tf.keras.backend.set_learning_phase(0)
245+
#tf_keras.backend.set_learning_phase(0)
245246
tf.saved_model.save(model, save_dir)
246247
return {
247248
"async": False,
@@ -263,14 +264,14 @@ def _create_saved_model_with_prelu(save_dir):
263264
# set the bias and alpha intitialize to make them constant and ensure grappler
264265
# be able to fuse the op.
265266
layers = [
266-
tf.keras.layers.Conv2D(
267+
tf_keras.layers.Conv2D(
267268
16, [3, 3], padding='same', use_bias=True,
268269
bias_initializer=tf.initializers.constant(0.25)),
269-
tf.keras.layers.PReLU(alpha_initializer=tf.initializers.constant(0.25))
270+
tf_keras.layers.PReLU(alpha_initializer=tf.initializers.constant(0.25))
270271
]
271-
model = tf.keras.Sequential(layers)
272+
model = tf_keras.Sequential(layers)
272273
result = model.predict(tf.ones((1, 24, 24, 3)))
273-
tf.keras.backend.set_learning_phase(0)
274+
#tf_keras.backend.set_learning_phase(0)
274275
tf.saved_model.save(model, save_dir)
275276
return {
276277
"async": False,
@@ -351,13 +352,13 @@ def _create_saved_model_v2_with_tensorlist_ops(save_dir):
351352
Args:
352353
save_dir: directory name of where the saved model will be stored.
353354
"""
354-
model = tf.keras.Sequential()
355-
model.add(tf.keras.layers.Embedding(100, 20, input_shape=[10]))
356-
model.add(tf.keras.layers.GRU(4))
355+
model = tf_keras.Sequential()
356+
model.add(tf_keras.layers.Embedding(100, 20, input_shape=[10]))
357+
model.add(tf_keras.layers.GRU(4))
357358

358359
result = model.predict(tf.ones([1, 10]))
359360

360-
tf.keras.backend.set_learning_phase(0)
361+
#tf_keras.backend.set_learning_phase(0)
361362
tf.saved_model.save(model, save_dir)
362363

363364
return {
@@ -469,7 +470,7 @@ def lookup(input):
469470
}
470471

471472
def _layers_mobilenet():
472-
model = tf.keras.applications.MobileNetV2()
473+
model = tf_keras.applications.MobileNetV2()
473474
model_path = 'mobilenet'
474475
tfjs.converters.save_keras_model(model, os.path.join(
475476
_tmp_dir, model_path))

remote-execution/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ platform(
99
],
1010
exec_properties = {
1111
# We use the same docker image for remote builds as we use for CI testing.
12-
"container-image": "docker://gcr.io/learnjs-174218/release@sha256:d85abab6146eaf1e01312bdb9e353a5efa0508b913dccf30fc5e505d009026ff",
12+
"container-image": "docker://gcr.io/learnjs-174218/release:latest@sha256:f712eae902a364750727f1bc2e4bfc3f75be846e2277f4e8026f9c03752f00e4",
1313
# By default in Google Cloud Remote build execution, network access is disabled. We explicitly set the
1414
# property in the platform again in case the default ever changes. Network access is not desirable in
1515
# Bazel builds as it is potential source of flaky tests and therefore also breaks hermeticity.

tfjs-converter/python/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ py_wheel(
7171
"jax>=0.4.13",
7272
"jaxlib>=0.4.13",
7373
"tensorflow>=2.13.0,<3",
74+
"tf-keras>=2.13.0",
7475
"tensorflow-decision-forests>=1.5.0",
7576
"six>=1.16.0,<2",
76-
"tensorflow-hub>=0.14.0",
77+
"tensorflow-hub>=0.16.1",
7778
"packaging~=23.1",
7879
],
7980
strip_path_prefixes = [

tfjs-converter/python/requirements-dev_lock.txt

Lines changed: 148 additions & 126 deletions
Large diffs are not rendered by default.

tfjs-converter/python/requirements.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ importlib_resources>=5.9.0
33
jax>=0.4.23
44
jaxlib>=0.4.23
55
tensorflow>=2.13.0,<3
6-
tensorflow-decision-forests>=1.5.0
6+
tf-keras>=2.16.0
7+
tensorflow-decision-forests>=1.9.0
78
six>=1.16.0,<2
8-
tensorflow-hub>=0.14.0
9+
tensorflow-hub>=0.16.1
910
packaging~=23.1

tfjs-converter/python/requirements_lock.txt

Lines changed: 148 additions & 126 deletions
Large diffs are not rendered by default.

tfjs-converter/python/tensorflowjs/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ py_library(
116116
deps = [requirement("debugpy")],
117117
)
118118

119+
py_library(
120+
name = "expect_tf_keras_installed",
121+
# tf-keras is used to provide keras 2.0 support.
122+
deps = [requirement("tf-keras")],
123+
)
124+
119125
py_library(
120126
name = "quantization",
121127
srcs = ["quantization.py"],

tfjs-converter/python/tensorflowjs/converters/BUILD.bazel

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ py_test(
5959
"//tfjs-converter/python/tensorflowjs:expect_h5py_installed",
6060
"//tfjs-converter/python/tensorflowjs:expect_numpy_installed",
6161
"//tfjs-converter/python/tensorflowjs:expect_tensorflow_installed",
62+
"//tfjs-converter/python/tensorflowjs:expect_tf_keras_installed",
6263
"//tfjs-converter/python/tensorflowjs:version",
6364
],
6465
)
@@ -87,6 +88,7 @@ py_test(
8788
":tf_module_mapper",
8889
"//tfjs-converter/python/tensorflowjs:expect_numpy_installed",
8990
"//tfjs-converter/python/tensorflowjs:expect_tensorflow_installed",
91+
"//tfjs-converter/python/tensorflowjs:expect_tf_keras_installed",
9092
],
9193
)
9294

@@ -105,6 +107,7 @@ py_library(
105107
":graph_rewrite_util",
106108
"//tfjs-converter/python/tensorflowjs:expect_numpy_installed",
107109
"//tfjs-converter/python/tensorflowjs:expect_tensorflow_installed",
110+
"//tfjs-converter/python/tensorflowjs:expect_tf_keras_installed",
108111
],
109112
)
110113

@@ -154,6 +157,7 @@ py_library(
154157
":graph_rewrite_util",
155158
"//tfjs-converter/python/tensorflowjs:expect_numpy_installed",
156159
"//tfjs-converter/python/tensorflowjs:expect_tensorflow_installed",
160+
"//tfjs-converter/python/tensorflowjs:expect_tf_keras_installed",
157161
],
158162
)
159163

@@ -180,6 +184,7 @@ py_library(
180184
":graph_rewrite_util",
181185
"//tfjs-converter/python/tensorflowjs:expect_numpy_installed",
182186
"//tfjs-converter/python/tensorflowjs:expect_tensorflow_installed",
187+
"//tfjs-converter/python/tensorflowjs:expect_tf_keras_installed",
183188
],
184189
)
185190

@@ -229,6 +234,7 @@ py_library(
229234
"//tfjs-converter/python/tensorflowjs:expect_tensorflow_decision_forests_installed",
230235
"//tfjs-converter/python/tensorflowjs:expect_tensorflow_hub_installed",
231236
"//tfjs-converter/python/tensorflowjs:expect_tensorflow_installed",
237+
"//tfjs-converter/python/tensorflowjs:expect_tf_keras_installed",
232238
"//tfjs-converter/python/tensorflowjs:resource_loader",
233239
"//tfjs-converter/python/tensorflowjs:version",
234240
"//tfjs-converter/python/tensorflowjs:write_weights",
@@ -283,6 +289,7 @@ py_binary(
283289
"//tfjs-converter/python/tensorflowjs:expect_PyInquirer_installed",
284290
"//tfjs-converter/python/tensorflowjs:expect_h5py_installed",
285291
"//tfjs-converter/python/tensorflowjs:expect_tensorflow_installed",
292+
"//tfjs-converter/python/tensorflowjs:expect_tf_keras_installed",
286293
],
287294
)
288295

@@ -314,6 +321,7 @@ py_binary(
314321
":tf_saved_model_conversion_v2",
315322
"//tfjs-converter/python/tensorflowjs:expect_h5py_installed",
316323
"//tfjs-converter/python/tensorflowjs:expect_tensorflow_installed",
324+
"//tfjs-converter/python/tensorflowjs:expect_tf_keras_installed",
317325
"//tfjs-converter/python/tensorflowjs:version",
318326
],
319327
)
@@ -325,6 +333,7 @@ py_binary(
325333
srcs_version = "PY3",
326334
deps = [
327335
"//tfjs-converter/python/tensorflowjs:expect_tensorflow_installed",
336+
"//tfjs-converter/python/tensorflowjs:expect_tf_keras_installed",
328337
],
329338
)
330339

tfjs-converter/python/tensorflowjs/converters/converter.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
# ==============================================================================
15-
"""Artifact conversion to and from Python TensorFlow and tf.keras."""
15+
"""Artifact conversion to and from Python TensorFlow and tf_keras."""
1616

1717
from __future__ import absolute_import
1818
from __future__ import division
@@ -28,6 +28,7 @@
2828
import h5py
2929
import tensorflow.compat.v1 as tf1
3030
import tensorflow.compat.v2 as tf
31+
import tf_keras
3132

3233
from tensorflowjs import quantization
3334
from tensorflowjs import version
@@ -50,7 +51,7 @@ def dispatch_keras_h5_to_tfjs_layers_model_conversion(
5051
- A weights-only HDF5 (e.g., generated with Keras Model's `save_weights()`
5152
method),
5253
- A topology+weights combined HDF5 (e.g., generated with
53-
`tf.keras.model.save_model`).
54+
`tf_keras.model.save_model`).
5455
5556
Args:
5657
h5_path: path to an HDF5 file containing keras model data as a `str`.
@@ -199,7 +200,7 @@ def dispatch_keras_h5_to_tfjs_graph_model_conversion(
199200
200201
Args:
201202
h5_path: Path to the HDF5-format file that contains the model saved from
202-
keras or tf.keras.
203+
keras or tf_keras.
203204
output_dir: The destination to which the tfjs GraphModel artifacts will be
204205
written.
205206
quantization_dtype_map: A mapping from dtype (`uint8`, `uint16`, `float16`)
@@ -223,7 +224,7 @@ def dispatch_keras_h5_to_tfjs_graph_model_conversion(
223224
'directory: %s' % h5_path)
224225

225226
temp_savedmodel_dir = tempfile.mktemp(suffix='.savedmodel')
226-
model = tf.keras.models.load_model(h5_path, compile=False)
227+
model = tf_keras.models.load_model(h5_path, compile=False)
227228
model.save(temp_savedmodel_dir, include_optimizer=False, save_format='tf')
228229

229230
# NOTE(cais): This cannot use `tf.compat.v1` because
@@ -253,13 +254,13 @@ def dispatch_keras_saved_model_to_tensorflowjs_conversion(
253254
"""Converts keras model saved in the SavedModel format to tfjs format.
254255
255256
Note that the SavedModel format exists in keras, but not in
256-
keras-team/tf.keras.
257+
keras-team/tf_keras.
257258
258259
Args:
259260
keras_saved_model_path: path to a folder in which the
260261
assets/saved_model.json can be found. This is usually a subfolder
261262
that is under the folder passed to
262-
`tf.keras.models.save_model()` and has a Unix epoch time
263+
`tf_keras.models.save_model()` and has a Unix epoch time
263264
as its name (e.g., 1542212752).
264265
output_dir: Output directory to which the TensorFlow.js-format model JSON
265266
file and weights files will be written. If the directory does not exist,
@@ -274,7 +275,7 @@ def dispatch_keras_saved_model_to_tensorflowjs_conversion(
274275
metadata: User defined metadata map.
275276
"""
276277
with tf.Graph().as_default(), tf.compat.v1.Session():
277-
model = tf.keras.models.load_model(keras_saved_model_path)
278+
model = tf_keras.models.load_model(keras_saved_model_path)
278279

279280
# Save model temporarily in HDF5 format.
280281
temp_h5_path = tempfile.mktemp(suffix='.h5')
@@ -363,12 +364,12 @@ def dispatch_tensorflowjs_to_keras_keras_conversion(config_json_path, v3_path):
363364
'but cannot read valid JSON content from %s.' % config_json_path)
364365

365366
model = keras_tfjs_loader.load_keras_keras_model(config_json_path)
366-
tf.keras.saving.save_model(model, v3_path, save_format="keras")
367+
tf_keras.saving.save_model(model, v3_path, save_format="keras")
367368

368369

369370
def dispatch_tensorflowjs_to_keras_saved_model_conversion(
370371
config_json_path, keras_saved_model_path):
371-
"""Converts a TensorFlow.js Layers model format to a tf.keras SavedModel.
372+
"""Converts a TensorFlow.js Layers model format to a tf_keras SavedModel.
372373
373374
Args:
374375
config_json_path: Path to the JSON file that includes the model's
@@ -397,7 +398,7 @@ def dispatch_tensorflowjs_to_keras_saved_model_conversion(
397398

398399
with tf.Graph().as_default(), tf.compat.v1.Session():
399400
model = keras_tfjs_loader.load_keras_model(config_json_path)
400-
tf.keras.models.save_model(
401+
tf_keras.models.save_model(
401402
model, keras_saved_model_path, save_format='tf')
402403

403404

@@ -751,7 +752,7 @@ def get_arg_parser():
751752
help='Input format. '
752753
'For "keras", the input path can be one of the two following formats:\n'
753754
' - A topology+weights combined HDF5 (e.g., generated with'
754-
' `tf.keras.model.save_model()` method).\n'
755+
' `tf_keras.model.save_model()` method).\n'
755756
' - A weights-only HDF5 (e.g., generated with Keras Model\'s '
756757
' `save_weights()` method). \n'
757758
'For "keras_saved_model", the input_path must point to a subfolder '
@@ -885,7 +886,7 @@ def convert(arguments):
885886
if args.show_version:
886887
print('\ntensorflowjs %s\n' % version.version)
887888
print('Dependency versions:')
888-
print(' keras %s' % tf.keras.__version__)
889+
print(' keras %s' % tf_keras.__version__)
889890
print(' tensorflow %s' % tf.__version__)
890891
return
891892

0 commit comments

Comments
 (0)