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

Prevent reimports for TF #1713

Merged
merged 1 commit into from
Jun 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions datumaro/datumaro/util/tf_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,23 @@ def check_import():
def import_tf(check=True):
import sys

tf = sys.modules.get('tensorflow', None)
if tf is not None:
not_found = object()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it performance optimisation? Could you please add a comment?

Copy link
Contributor Author

@zhiltsov-max zhiltsov-max Jun 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not entirely. It is a way to be sure about the result of get() call. The problem with default None is that it is a valid value for a module, and, moreover, we need it later. Using some specific object() we can be sure about existence of a key and its value.

tf = sys.modules.get('tensorflow', not_found)
if tf is None:
import tensorflow as tf # emit default error
elif tf is not not_found:
return tf

# Reduce output noise, https://stackoverflow.com/questions/38073432/how-to-suppress-verbose-tensorflow-logging
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

if check:
check_import()
try:
check_import()
except Exception:
sys.modules['tensorflow'] = None # prevent further import
raise

import tensorflow as tf

Expand Down