Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce usage of tf.compat.v1 APIs #1718

Open
16 of 19 tasks
stephanwlee opened this issue Dec 22, 2018 · 12 comments
Open
16 of 19 tasks

Reduce usage of tf.compat.v1 APIs #1718

stephanwlee opened this issue Dec 22, 2018 · 12 comments

Comments

@stephanwlee
Copy link
Contributor

stephanwlee commented Dec 22, 2018

TensorBoard have migrated to TF 2.0 APIs but there are some usages of V1 APIs due to not-so-compatible changes. Since we are interested in moving off of tf.compat.v1, we will either have to find a replacement or introduce some changes in behavior.

Do note that this list exclude v1 API usages in tests (they are mainly written in graph style and is tracked in #1705) and TB v1 summary.

@nfelt
Copy link
Contributor

nfelt commented Dec 22, 2018

Edited to note that I think tf.compat.v1.train.summary_iterator should also be tracked in #1711. tf.train.summary_iterator should go away in TF 2.0, and we should replace it with whatever we resolve #1711 with.

stephanwlee added a commit that referenced this issue Dec 26, 2018
Now that TensorBoard hosts all proto definitions required to run it, we
use the TensorBoard version of protos. #1678 has missed few usages of
constants of proto, SessionLog.[NAME], and this change fixes that.

Related: #1718.
stephanwlee added a commit that referenced this issue Dec 26, 2018
TF 2.0 is removing tf.app. Instead of staying at tf.compat.v1.app,
we are now using absl.app.

This change also deprecates the usages of tf.app.run without py_binary build target (event_file_inspector and event_file_loader).

Have manually tested all the impacted targets by executing them.

Relates to #1718.
stephanwlee added a commit to stephanwlee/tensorboard that referenced this issue Dec 28, 2018
TF 2.0 is removing symbols for GraphDef and MetaGraphDef protos.
We are converting it to TB proto definitions instead.

Relates to tensorflow#1718.
stephanwlee added a commit that referenced this issue Dec 28, 2018
TF is deprecating tf.flags. Migrate to absl.
Note that absl.flags by itself does not parse sys.argv like [1]
indicates. However, it is passed as part of `absl.app.run` [2] which we use,
so the change should have no behavioral difference.

Confirmed that the flags are parsed as expected by manually running the
demos and script/tools.

[1]: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/platform/flags.py#L66
[2]: https://github.com/abseil/abseil-py/blob/master/absl/app.py#L292

Related to #1718.
stephanwlee added a commit that referenced this issue Dec 28, 2018
TF 2.0 is removing symbols for GraphDef and MetaGraphDef protos.
We are converting it to TB proto definitions instead.

Relates to #1718.
stephanwlee added a commit that referenced this issue Dec 29, 2018
GFile was omitted from V2 API by mistake and the migration script
misbehaved. Now that TF has fixed it, we now want to use the V2 API.

Relates to #1718.
stephanwlee added a commit that referenced this issue Dec 29, 2018
TF 2.0 image API has changed tiny bit in API signature in a way that does not
impact us. We convert to the new 2.0 API and adopted the suggestion from
TF deprecation message[1].

Confirmed correctness by running the beholder (based on demo data) and
What-If tool (by supplying example file).

Relates to #1718.

[1]: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/image_ops_impl.py#L2987
@thealphacod3r
Copy link
Contributor

@stephanwlee @nfelt can I work on the remaining ones, the two mentioned
tf.compat.v1.Session -> Use eager mode
tf.contrib.ffmpeg -> Have to find the replacement ??

@stephanwlee
Copy link
Contributor Author

I recently learned about https://github.com/tensorflow/io which now includes ffmpeg.

@nfelt
Copy link
Contributor

nfelt commented Apr 8, 2019

I think tensorflow/io's ffmpeg support is only for VideoDataset, i.e. it's only for reading videos.

For the v2 audio summary op we can just use tf.audio.encode_wav() now that it exists after tensorflow/tensorflow@b30d3f5.

For the v1 audio summary op and the v1 audio_pb function (which uses contrib ffmpeg via PersistentOpEvaluator), we could either backport the use of tf.audio.encode_wav(), or just leave them as only working with TF 1.x.

For the v2 equivalent of audio_pb, we should just use the python standard library's wave module: https://docs.python.org/2/library/wave.html

Lastly, the Beholder plugin uses ffmpeg to encode videos, but it literally just shells out to ffmpeg so there's no usage of tf.contrib.ffmpeg there.

@amanp592 if you'd like to work on any of this, this could also be a good place to start.

@wchargin
Copy link
Contributor

fwiw, I have noted extremely poor performance of the wave module in
the past. The following blog post describes a way to improve the default
performance from 10m24s to 51s (for 5 minutes of audio output):

https://soledadpenades.com/posts/2009/fastest-way-to-generate-wav-files-in-python-using-the-wave-module/

By contrast, tf.audio.encode_audio completes in 0.271s (hundreds of
times faster than even the “fast” version above):

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import tempfile
import time


class Timer(object):
  def __init__(self):
    self._last = time.time()

  def log(self, msg):
    now = time.time()
    print("[%0.3f] %s" % (now - self._last, msg))
    self._last = now


SAMPLE_LEN = 13230000


def main():
  t = Timer()
  import tensorflow as tf
  t.log("Loaded TensorFlow.")
  audio = tf.random.uniform(shape=(SAMPLE_LEN, 2)) * 2 - 1
  t.log("Generated audio (%d samples)." % SAMPLE_LEN)
  encoded = tf.audio.encode_wav(audio, sample_rate=44100).numpy()
  t.log("Encoded audio.")
  filename = os.path.join(tempfile.mkdtemp(), "noise.wav")
  with open(filename, "w") as outfile:
    outfile.write(encoded)
  t.log("Wrote to: %s" % filename)


if __name__ == "__main__":
  main()
[0.729] Loaded TensorFlow.
[0.102] Generated audio (13230000 samples).
[0.148] Encoded audio.
[0.021] Wrote to: /tmp/tmp0Jqp0u/noise.wav

All this to say: we should benchmark, at least roughly, before and after
any such change.

@nfelt
Copy link
Contributor

nfelt commented May 2, 2019

A belated response to the comment about WAV encoding - I agree that we should benchmark.

That said, I am not sure the above article really establishes that the wave module itself is a bottleneck. They aren't precise about where the timing was done, but it doesn't look like they're differentiating between the loop that generates audio values and the actual call to write out the data. I'm not terribly surprised that python is much slower than C++ if you ask it to generate a random value and preprocess it in a python for loop 13230000 times.

Looking at the wave source, if you ignore big-endian data conversion, writeframesraw(data) essentially just boils down to self._file.write(data) - there's actually no real processing happening. I think the wave module mostly just takes care of writing out the header and doing bookkeeping.
https://github.com/python/cpython/blob/7c2c01f02a1821298a62dd16ecc3a12da663e14b/Lib/wave.py#L421-L440

So I don't see any particular reason why wave would have to be slower than writing to a regular file in python, if the logic that we write to transform the input into bytes can be done efficiently. And this transformation is pretty simple - the article above just uses int16s and then struct.pack('h', value), and equivalently in numpy we can do np.array(values, dtype=np.int16).tobytes().

@HebaGamalElDin
Copy link

what for this line of code : 'tf.compat.v1.summary.image'
gets this error : 'AttributeError: 'module' object has no attribute 'v1' .

@wchargin
Copy link
Contributor

wchargin commented Jun 3, 2019

@HebaGamalElDin: The tf.compat.v1 module was introduced in TensorFlow
version 1.13, and the tf.compat.v2 module will be in TensorFlow 1.14
(RC currently available).

If you’re seeing that error when running TensorBoard, please open a
separate bug report and be sure to include the requested environment
information.

@HebaGamalElDin
Copy link

@wchargin : what for python 2.7 , as almost Tensorrflow version 1.13 , 1.14 isn't applicable for python 2.7 .

@wchargin
Copy link
Contributor

wchargin commented Jun 3, 2019

@HebaGamalElDin: TensorFlow versions 1.13 and 1.14 work just fine on
Python 2.7:

>>> import tensorflow, sys
>>> sys.version
'2.7.16 (default, Apr  6 2019, 01:42:57) \n[GCC 7.3.0]'
>>> tensorflow.__version__
'1.13.1'
>>> import tensorflow, sys
>>> sys.version
'2.7.16 (default, Apr  6 2019, 01:42:57) \n[GCC 7.3.0]'
>>> tensorflow.__version__
'1.14.0-rc0'

TensorFlow will support Python 2.7 at least until its end of life,
which is still seven months out.

@HebaGamalElDin
Copy link

@wchargin : I have searched more for that version of tensorflow for windows but I just found the applicable versions for Linux and mac OS only .

@wchargin
Copy link
Contributor

wchargin commented Jun 4, 2019

@HebaGamalElDin: I was able to install TensorFlow 1.13 on Windows
without difficulty:

Screenshot of TF 1.13.1 on Windows

If you continue to have problems installing TensorFlow, please consult
Stack Overflow or open an issue on the TensorFlow repository:
https://github.com/tensorflow/tensorflow/issues/new?template=10-build-installation-issue.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants