From 44bbf04059e24f18f74a548821e12a774349fc0b Mon Sep 17 00:00:00 2001 From: William Chargin Date: Thu, 15 Aug 2019 18:49:21 -0700 Subject: [PATCH] =?UTF-8?q?Make=20audio-related=20functionality=20and=20te?= =?UTF-8?q?sts=20TF=202.x=E2=80=93compatible?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: These tests used `tf.contrib.ffmpeg.encode_audio`, but they can actually just use `tf.audio.encode_wav`, which has existed since TensorFlow 1.14. This also makes the encoder itself TF 2.x–compatible. Makes progress toward #1705. Test Plan: Tests pass in both TF 1.x and TF 2.x, and the `run_v1_only` decorators have been removed: ``` $ git grep run_v1_only '*encoder*_test.py' '*audio*_test.py' | wc -l 0 ``` wchargin-branch: audio-tf2x --- tensorboard/plugins/audio/BUILD | 1 - .../plugins/audio/audio_plugin_test.py | 65 ++++++++++--------- tensorboard/plugins/audio/summary.py | 5 +- tensorboard/plugins/audio/summary_test.py | 5 +- tensorboard/util/BUILD | 1 - tensorboard/util/encoder.py | 6 +- tensorboard/util/encoder_test.py | 2 - 7 files changed, 37 insertions(+), 48 deletions(-) diff --git a/tensorboard/plugins/audio/BUILD b/tensorboard/plugins/audio/BUILD index d3d69d330e..256b815b96 100644 --- a/tensorboard/plugins/audio/BUILD +++ b/tensorboard/plugins/audio/BUILD @@ -108,7 +108,6 @@ py_test( "//tensorboard:expect_tensorflow_installed", "//tensorboard/compat/proto:protos_all_py_pb2", "//tensorboard/util:tensor_util", - "//tensorboard/util:test_util", ], ) diff --git a/tensorboard/plugins/audio/audio_plugin_test.py b/tensorboard/plugins/audio/audio_plugin_test.py index 93e1967d24..31ebe53403 100644 --- a/tensorboard/plugins/audio/audio_plugin_test.py +++ b/tensorboard/plugins/audio/audio_plugin_test.py @@ -39,7 +39,6 @@ from tensorboard.util import test_util -@test_util.run_v1_only('Uses tf.contrib in setUp via audio.summary') class AudioPluginTest(tf.test.TestCase): def setUp(self): @@ -51,40 +50,42 @@ def setUp(self): # Create old-style audio summaries for run "foo". tf.compat.v1.reset_default_graph() - sess = tf.compat.v1.Session() - placeholder = tf.compat.v1.placeholder(tf.float32) - tf.compat.v1.summary.audio(name="baz", tensor=placeholder, sample_rate=44100) - merged_summary_op = tf.compat.v1.summary.merge_all() - foo_directory = os.path.join(self.log_dir, "foo") - with test_util.FileWriterCache.get(foo_directory) as writer: - writer.add_graph(sess.graph) - for step in xrange(2): - # The floats (sample data) range from -1 to 1. - writer.add_summary(sess.run(merged_summary_op, feed_dict={ - placeholder: numpy.random.rand(42, 22050) * 2 - 1 - }), global_step=step) + with tf.compat.v1.Graph().as_default(): + sess = tf.compat.v1.Session() + placeholder = tf.compat.v1.placeholder(tf.float32) + tf.compat.v1.summary.audio(name="baz", tensor=placeholder, sample_rate=44100) + merged_summary_op = tf.compat.v1.summary.merge_all() + foo_directory = os.path.join(self.log_dir, "foo") + with test_util.FileWriterCache.get(foo_directory) as writer: + writer.add_graph(sess.graph) + for step in xrange(2): + # The floats (sample data) range from -1 to 1. + writer.add_summary(sess.run(merged_summary_op, feed_dict={ + placeholder: numpy.random.rand(42, 22050) * 2 - 1 + }), global_step=step) # Create new-style audio summaries for run "bar". tf.compat.v1.reset_default_graph() - sess = tf.compat.v1.Session() - audio_placeholder = tf.compat.v1.placeholder(tf.float32) - labels_placeholder = tf.compat.v1.placeholder(tf.string) - summary.op("quux", audio_placeholder, sample_rate=44100, - labels=labels_placeholder, - description="how do you pronounce that, anyway?") - merged_summary_op = tf.compat.v1.summary.merge_all() - bar_directory = os.path.join(self.log_dir, "bar") - with test_util.FileWriterCache.get(bar_directory) as writer: - writer.add_graph(sess.graph) - for step in xrange(2): - # The floats (sample data) range from -1 to 1. - writer.add_summary(sess.run(merged_summary_op, feed_dict={ - audio_placeholder: numpy.random.rand(42, 11025, 1) * 2 - 1, - labels_placeholder: [ - tf.compat.as_bytes('step **%s**, sample %s' % (step, sample)) - for sample in xrange(42) - ], - }), global_step=step) + with tf.compat.v1.Graph().as_default(): + sess = tf.compat.v1.Session() + audio_placeholder = tf.compat.v1.placeholder(tf.float32) + labels_placeholder = tf.compat.v1.placeholder(tf.string) + summary.op("quux", audio_placeholder, sample_rate=44100, + labels=labels_placeholder, + description="how do you pronounce that, anyway?") + merged_summary_op = tf.compat.v1.summary.merge_all() + bar_directory = os.path.join(self.log_dir, "bar") + with test_util.FileWriterCache.get(bar_directory) as writer: + writer.add_graph(sess.graph) + for step in xrange(2): + # The floats (sample data) range from -1 to 1. + writer.add_summary(sess.run(merged_summary_op, feed_dict={ + audio_placeholder: numpy.random.rand(42, 11025, 1) * 2 - 1, + labels_placeholder: [ + tf.compat.as_bytes('step **%s**, sample %s' % (step, sample)) + for sample in xrange(42) + ], + }), global_step=step) # Start a server with the plugin. multiplexer = event_multiplexer.EventMultiplexer({ diff --git a/tensorboard/plugins/audio/summary.py b/tensorboard/plugins/audio/summary.py index a0ecf8952d..6498623dde 100644 --- a/tensorboard/plugins/audio/summary.py +++ b/tensorboard/plugins/audio/summary.py @@ -91,7 +91,6 @@ def op(name, file format explicitly. """ # TODO(nickfelt): remove on-demand imports once dep situation is fixed. - import tensorflow # for contrib import tensorflow.compat.v1 as tf if display_name is None: @@ -101,9 +100,7 @@ def op(name, if encoding == 'wav': encoding = metadata.Encoding.Value('WAV') - encoder = functools.partial(tensorflow.contrib.ffmpeg.encode_audio, - samples_per_second=sample_rate, - file_format='wav') + encoder = functools.partial(tf.audio.encode_wav, sample_rate=sample_rate) else: raise ValueError('Unknown encoding: %r' % encoding) diff --git a/tensorboard/plugins/audio/summary_test.py b/tensorboard/plugins/audio/summary_test.py index f1547032b2..25fabdd54e 100644 --- a/tensorboard/plugins/audio/summary_test.py +++ b/tensorboard/plugins/audio/summary_test.py @@ -30,7 +30,6 @@ from tensorboard.plugins.audio import metadata from tensorboard.plugins.audio import summary from tensorboard.util import tensor_util -from tensorboard.util import test_util try: @@ -149,7 +148,6 @@ def test_requires_wav(self): self.audio('k488', data, 44100, encoding='pptx') -@test_util.run_v1_only('Uses tf.contrib') class SummaryV1PbTest(SummaryBaseTest, tf.test.TestCase): def setUp(self): super(SummaryV1PbTest, self).setUp() @@ -168,13 +166,12 @@ def test_requires_nonnegative_max_outputs(self): self.skipTest('summary V1 pb does not actually enforce this') -@test_util.run_v1_only('Uses tf.contrib') class SummaryV1OpTest(SummaryBaseTest, tf.test.TestCase): def setUp(self): super(SummaryV1OpTest, self).setUp() def audio(self, *args, **kwargs): - return tf.Summary.FromString(summary.op(*args, **kwargs).numpy()) + return tf.compat.v1.Summary.FromString(summary.op(*args, **kwargs).numpy()) def test_tag(self): data = np.array(1, np.float32, ndmin=3) diff --git a/tensorboard/util/BUILD b/tensorboard/util/BUILD index e322c1a477..3db15d49f0 100644 --- a/tensorboard/util/BUILD +++ b/tensorboard/util/BUILD @@ -23,7 +23,6 @@ py_test( srcs_version = "PY2AND3", deps = [ ":encoder", - ":test_util", "//tensorboard:expect_numpy_installed", "//tensorboard:expect_tensorflow_installed", "@org_pythonhosted_six", diff --git a/tensorboard/util/encoder.py b/tensorboard/util/encoder.py index 31ba34993d..130d1632d4 100644 --- a/tensorboard/util/encoder.py +++ b/tensorboard/util/encoder.py @@ -84,16 +84,14 @@ def __init__(self): def initialize_graph(self): # TODO(nickfelt): remove on-demand imports once dep situation is fixed. - import tensorflow # for contrib import tensorflow.compat.v1 as tf self._audio_placeholder = tf.placeholder( dtype=tf.float32, name='image_to_encode') self._samples_per_second_placeholder = tf.placeholder( dtype=tf.int32, name='samples_per_second') - self._encode_op = tensorflow.contrib.ffmpeg.encode_audio( + self._encode_op = tf.audio.encode_wav( self._audio_placeholder, - file_format='wav', - samples_per_second=self._samples_per_second_placeholder) + sample_rate=self._samples_per_second_placeholder) def run(self, audio, samples_per_second): # pylint: disable=arguments-differ if not isinstance(audio, np.ndarray): diff --git a/tensorboard/util/encoder_test.py b/tensorboard/util/encoder_test.py index 53c999af7a..c58916e196 100644 --- a/tensorboard/util/encoder_test.py +++ b/tensorboard/util/encoder_test.py @@ -21,7 +21,6 @@ import tensorflow as tf from tensorboard.util import encoder -from tensorboard.util import test_util class TensorFlowPngEncoderTest(tf.test.TestCase): @@ -56,7 +55,6 @@ def test_encodes_png_with_alpha(self): self._check_png(data) -@test_util.run_v1_only('Uses contrib') class TensorFlowWavEncoderTest(tf.test.TestCase): def setUp(self):