Unverified Commit 1e2ceffd authored by Ayushman Kumar's avatar Ayushman Kumar Committed by GitHub
Browse files

Merge pull request #4 from tensorflow/master

Updating 
parents 51e60bab c7adbbe4
...@@ -301,5 +301,4 @@ def main(unused_argv): ...@@ -301,5 +301,4 @@ def main(unused_argv):
if __name__ == "__main__": if __name__ == "__main__":
assert tf.version.VERSION.startswith('2.')
app.run(main) app.run(main)
...@@ -49,5 +49,4 @@ class PositionalEmbeddingLayerTest(tf.test.TestCase): ...@@ -49,5 +49,4 @@ class PositionalEmbeddingLayerTest(tf.test.TestCase):
self.assertAllClose(pos_emb, target) self.assertAllClose(pos_emb, target)
if __name__ == "__main__": if __name__ == "__main__":
assert tf.version.VERSION.startswith('2.')
tf.test.main() tf.test.main()
...@@ -28,7 +28,7 @@ import sys ...@@ -28,7 +28,7 @@ import sys
# pylint: disable=g-bad-import-order # pylint: disable=g-bad-import-order
from absl import app as absl_app # pylint: disable=unused-import from absl import app as absl_app # pylint: disable=unused-import
import tensorflow as tf import tensorflow.compat.v1 as tf
# pylint: enable=g-bad-import-order # pylint: enable=g-bad-import-order
# For open source environment, add grandparent directory for import # For open source environment, add grandparent directory for import
...@@ -98,7 +98,7 @@ def model_fn(features, labels, mode, params): ...@@ -98,7 +98,7 @@ def model_fn(features, labels, mode, params):
'class_ids': tf.argmax(logits, axis=1), 'class_ids': tf.argmax(logits, axis=1),
'probabilities': tf.nn.softmax(logits), 'probabilities': tf.nn.softmax(logits),
} }
return tf.compat.v1.estimator.tpu.TPUEstimatorSpec(mode, predictions=predictions) return tf.estimator.tpu.TPUEstimatorSpec(mode, predictions=predictions)
logits = model(image, training=(mode == tf.estimator.ModeKeys.TRAIN)) logits = model(image, training=(mode == tf.estimator.ModeKeys.TRAIN))
loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits) loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)
...@@ -111,14 +111,14 @@ def model_fn(features, labels, mode, params): ...@@ -111,14 +111,14 @@ def model_fn(features, labels, mode, params):
decay_rate=0.96) decay_rate=0.96)
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate) optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
if FLAGS.use_tpu: if FLAGS.use_tpu:
optimizer = tf.compat.v1.tpu.CrossShardOptimizer(optimizer) optimizer = tf.tpu.CrossShardOptimizer(optimizer)
return tf.compat.v1.estimator.tpu.TPUEstimatorSpec( return tf.estimator.tpu.TPUEstimatorSpec(
mode=mode, mode=mode,
loss=loss, loss=loss,
train_op=optimizer.minimize(loss, tf.train.get_global_step())) train_op=optimizer.minimize(loss, tf.train.get_global_step()))
if mode == tf.estimator.ModeKeys.EVAL: if mode == tf.estimator.ModeKeys.EVAL:
return tf.compat.v1.estimator.tpu.TPUEstimatorSpec( return tf.estimator.tpu.TPUEstimatorSpec(
mode=mode, loss=loss, eval_metrics=(metric_fn, [labels, logits])) mode=mode, loss=loss, eval_metrics=(metric_fn, [labels, logits]))
...@@ -128,7 +128,7 @@ def train_input_fn(params): ...@@ -128,7 +128,7 @@ def train_input_fn(params):
data_dir = params["data_dir"] data_dir = params["data_dir"]
# Retrieves the batch size for the current shard. The # of shards is # Retrieves the batch size for the current shard. The # of shards is
# computed according to the input pipeline deployment. See # computed according to the input pipeline deployment. See
# `tf.compat.v1.estimator.tpu.RunConfig` for details. # `tf.estimator.tpu.RunConfig` for details.
ds = dataset.train(data_dir).cache().repeat().shuffle( ds = dataset.train(data_dir).cache().repeat().shuffle(
buffer_size=50000).batch(batch_size, drop_remainder=True) buffer_size=50000).batch(batch_size, drop_remainder=True)
return ds return ds
...@@ -159,16 +159,15 @@ def main(argv): ...@@ -159,16 +159,15 @@ def main(argv):
project=FLAGS.gcp_project project=FLAGS.gcp_project
) )
run_config = tf.compat.v1.estimator.tpu.RunConfig( run_config = tf.estimator.tpu.RunConfig(
cluster=tpu_cluster_resolver, cluster=tpu_cluster_resolver,
model_dir=FLAGS.model_dir, model_dir=FLAGS.model_dir,
session_config=tf.ConfigProto( session_config=tf.ConfigProto(
allow_soft_placement=True, log_device_placement=True), allow_soft_placement=True, log_device_placement=True),
tpu_config=tf.compat.v1.estimator.tpu.TPUConfig( tpu_config=tf.estimator.tpu.TPUConfig(FLAGS.iterations, FLAGS.num_shards),
FLAGS.iterations, FLAGS.num_shards),
) )
estimator = tf.compat.v1.estimator.tpu.TPUEstimator( estimator = tf.estimator.tpu.TPUEstimator(
model_fn=model_fn, model_fn=model_fn,
use_tpu=FLAGS.use_tpu, use_tpu=FLAGS.use_tpu,
train_batch_size=FLAGS.batch_size, train_batch_size=FLAGS.batch_size,
...@@ -199,4 +198,5 @@ def main(argv): ...@@ -199,4 +198,5 @@ def main(argv):
if __name__ == "__main__": if __name__ == "__main__":
tf.disable_v2_behavior()
absl_app.run(main) absl_app.run(main)
...@@ -22,6 +22,7 @@ import os ...@@ -22,6 +22,7 @@ import os
from absl import app as absl_app from absl import app as absl_app
from absl import flags from absl import flags
from six.moves import range
import tensorflow as tf # pylint: disable=g-bad-import-order import tensorflow as tf # pylint: disable=g-bad-import-order
from official.r1.resnet import resnet_model from official.r1.resnet import resnet_model
......
...@@ -22,6 +22,7 @@ import os ...@@ -22,6 +22,7 @@ import os
from absl import app as absl_app from absl import app as absl_app
from absl import flags from absl import flags
from six.moves import range
import tensorflow as tf import tensorflow as tf
from official.r1.resnet import imagenet_preprocessing from official.r1.resnet import imagenet_preprocessing
...@@ -307,7 +308,7 @@ def _get_block_sizes(resnet_size): ...@@ -307,7 +308,7 @@ def _get_block_sizes(resnet_size):
except KeyError: except KeyError:
err = ('Could not find layers for selected Resnet size.\n' err = ('Could not find layers for selected Resnet size.\n'
'Size received: {}; sizes allowed: {}.'.format( 'Size received: {}; sizes allowed: {}.'.format(
resnet_size, choices.keys())) resnet_size, list(choices.keys())))
raise ValueError(err) raise ValueError(err)
......
...@@ -18,7 +18,7 @@ from __future__ import absolute_import ...@@ -18,7 +18,7 @@ from __future__ import absolute_import
from __future__ import division from __future__ import division
from __future__ import print_function from __future__ import print_function
import tensorflow as tf import tensorflow.compat.v1 as tf
class Attention(tf.layers.Layer): class Attention(tf.layers.Layer):
......
...@@ -54,7 +54,7 @@ from __future__ import print_function ...@@ -54,7 +54,7 @@ from __future__ import print_function
import math import math
import os import os
import tensorflow as tf import tensorflow.compat.v1 as tf
from official.utils.misc import model_helpers from official.utils.misc import model_helpers
......
...@@ -18,7 +18,7 @@ from __future__ import absolute_import ...@@ -18,7 +18,7 @@ from __future__ import absolute_import
from __future__ import division from __future__ import division
from __future__ import print_function from __future__ import print_function
import tensorflow as tf # pylint: disable=g-bad-import-order import tensorflow.compat.v1 as tf # pylint: disable=g-bad-import-order
from official.r1.utils import tpu as tpu_utils from official.r1.utils import tpu as tpu_utils
......
...@@ -18,7 +18,7 @@ from __future__ import absolute_import ...@@ -18,7 +18,7 @@ from __future__ import absolute_import
from __future__ import division from __future__ import division
from __future__ import print_function from __future__ import print_function
import tensorflow as tf import tensorflow.compat.v1 as tf
class FeedFowardNetwork(tf.layers.Layer): class FeedFowardNetwork(tf.layers.Layer):
......
...@@ -20,7 +20,7 @@ from __future__ import print_function ...@@ -20,7 +20,7 @@ from __future__ import print_function
import math import math
import tensorflow as tf import tensorflow.compat.v1 as tf
_TRAIN, _EVAL = tf.estimator.ModeKeys.TRAIN, tf.estimator.ModeKeys.EVAL _TRAIN, _EVAL = tf.estimator.ModeKeys.TRAIN, tf.estimator.ModeKeys.EVAL
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
# ============================================================================== # ==============================================================================
"""Test Transformer's schedule manager.""" """Test Transformer's schedule manager."""
import tensorflow as tf import tensorflow.compat.v1 as tf
from official.r1.transformer import schedule from official.r1.transformer import schedule
......
...@@ -22,7 +22,7 @@ from __future__ import absolute_import ...@@ -22,7 +22,7 @@ from __future__ import absolute_import
from __future__ import division from __future__ import division
from __future__ import print_function from __future__ import print_function
import tensorflow as tf import tensorflow.compat.v1 as tf
from official.nlp.transformer import beam_search_v1 as beam_search from official.nlp.transformer import beam_search_v1 as beam_search
from official.nlp.transformer import model_utils from official.nlp.transformer import model_utils
......
...@@ -29,7 +29,7 @@ import tempfile ...@@ -29,7 +29,7 @@ import tempfile
from six.moves import xrange # pylint: disable=redefined-builtin from six.moves import xrange # pylint: disable=redefined-builtin
from absl import app as absl_app from absl import app as absl_app
from absl import flags from absl import flags
import tensorflow as tf import tensorflow.compat.v1 as tf
# pylint: enable=g-bad-import-order # pylint: enable=g-bad-import-order
from official.nlp.transformer import model_params from official.nlp.transformer import model_params
......
...@@ -23,7 +23,7 @@ import os ...@@ -23,7 +23,7 @@ import os
# pylint: disable=g-bad-import-order # pylint: disable=g-bad-import-order
from absl import app as absl_app from absl import app as absl_app
from absl import flags from absl import flags
import tensorflow as tf import tensorflow.compat.v1 as tf
# pylint: enable=g-bad-import-order # pylint: enable=g-bad-import-order
from official.nlp.transformer.utils import tokenizer from official.nlp.transformer.utils import tokenizer
......
...@@ -94,13 +94,15 @@ class Controller(object): ...@@ -94,13 +94,15 @@ class Controller(object):
# TODO(rxsang): Support training until exhaustion by passing # TODO(rxsang): Support training until exhaustion by passing
# `train_steps=-1`. Currently it cannot be supported with a host training # `train_steps=-1`. Currently it cannot be supported with a host training
# loop because break statements are not supported with distributed dataset. # loop because break statements are not supported with distributed dataset.
if train_fn is not None and train_steps is None: if train_fn is not None:
raise ValueError("`train_steps` is required when `train_fn` is provided.") if train_steps is None:
if train_fn is not None and steps_per_loop is None: raise ValueError("`train_steps` is required when `train_fn` is "
raise ValueError("`steps_per_loop` is required when `train_fn is " "provided.")
"provided.") if steps_per_loop is None:
if not isinstance(steps_per_loop, int) or steps_per_loop < 1: raise ValueError("`steps_per_loop` is required when `train_fn is "
raise ValueError("`steps_per_loop` should be a positive integer") "provided.")
if not isinstance(steps_per_loop, int) or steps_per_loop < 1:
raise ValueError("`steps_per_loop` should be a positive integer")
if summary_interval is not None and summary_interval <= 0: if summary_interval is not None and summary_interval <= 0:
raise ValueError("`summary_interval` should be larger than 0") raise ValueError("`summary_interval` should be larger than 0")
......
...@@ -53,13 +53,17 @@ def create_loop_fn(step_fn): ...@@ -53,13 +53,17 @@ def create_loop_fn(step_fn):
""" """
try: try:
step = 0 step = 0
while (num_steps == -1 or step < num_steps): # To make sure the OutOfRangeError exception can be handled well with
outputs = step_fn(iterator) # async remote eager, we need to wrap the loop body in a `async_scope`.
if reduce_fn is not None: with tf.experimental.async_scope():
state = reduce_fn(state, outputs) while (num_steps == -1 or step < num_steps):
step += 1 outputs = step_fn(iterator)
return state if reduce_fn is not None:
state = reduce_fn(state, outputs)
step += 1
return state
except (StopIteration, tf.errors.OutOfRangeError): except (StopIteration, tf.errors.OutOfRangeError):
tf.experimental.async_clear_error()
return state return state
return loop_fn return loop_fn
......
...@@ -117,8 +117,9 @@ class TimeHistory(tf.keras.callbacks.Callback): ...@@ -117,8 +117,9 @@ class TimeHistory(tf.keras.callbacks.Callback):
self.timestamp_log.append(BatchTimestamp(self.global_steps, now)) self.timestamp_log.append(BatchTimestamp(self.global_steps, now))
logging.info( logging.info(
'TimeHistory: %.2f examples/second between steps %d and %d', 'TimeHistory: %.2f seconds, %.2f examples/second between steps %d '
examples_per_second, self.last_log_step, self.global_steps) 'and %d', elapsed_time, examples_per_second, self.last_log_step,
self.global_steps)
if self.summary_writer: if self.summary_writer:
with self.summary_writer.as_default(): with self.summary_writer.as_default():
......
...@@ -465,7 +465,9 @@ class RetinanetBoxLoss(object): ...@@ -465,7 +465,9 @@ class RetinanetBoxLoss(object):
# for instances, the regression targets of 512x512 input with 6 anchors on # for instances, the regression targets of 512x512 input with 6 anchors on
# P3-P7 pyramid is about [0.1, 0.1, 0.2, 0.2]. # P3-P7 pyramid is about [0.1, 0.1, 0.2, 0.2].
normalizer = num_positives * 4.0 normalizer = num_positives * 4.0
mask = tf.not_equal(box_targets, 0.0) mask = tf.cast(tf.not_equal(box_targets, 0.0), dtype=tf.float32)
box_targets = tf.expand_dims(box_targets, axis=-1)
box_outputs = tf.expand_dims(box_outputs, axis=-1)
box_loss = self._huber_loss(box_targets, box_outputs, sample_weight=mask) box_loss = self._huber_loss(box_targets, box_outputs, sample_weight=mask)
box_loss /= normalizer box_loss /= normalizer
return box_loss return box_loss
......
...@@ -275,12 +275,6 @@ def define_keras_flags( ...@@ -275,12 +275,6 @@ def define_keras_flags(
help='Whether to build a tf.while_loop inside the training loop on the ' help='Whether to build a tf.while_loop inside the training loop on the '
'host. Setting it to True is critical to have peak performance on ' 'host. Setting it to True is critical to have peak performance on '
'TPU.') 'TPU.')
flags.DEFINE_boolean(
name='use_tf_keras_layers', default=False,
help='Whether to use tf.keras.layers instead of tf.python.keras.layers.'
'It only changes imagenet resnet model layers for now. This flag is '
'a temporal flag during transition to tf.keras.layers. Do not use this '
'flag for external usage. this will be removed shortly.')
if model: if model:
flags.DEFINE_string('model', 'resnet50_v1.5', flags.DEFINE_string('model', 'resnet50_v1.5',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment