user7115784
user7115784

Reputation:

Is there a way to suppress the messages TensorFlow prints?

I think that those messages are really important for the first few times but then it is just useless. It is actually making things worse to read and debug.

I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcublas.so.8.0 locally I tensorflow/stream_executor/dso_loader.cc:119] Couldn't open CUDA library libcudnn.so. LD_LIBRARY_PATH: I tensorflow/stream_executor/cuda/cuda_dnn.cc:3459] Unable to load cuDNN DSO I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcufft.so.8.0 locally I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcuda.so.1 locally I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcurand.so.8.0 locally

Is there a way to suppress the ones that just say it was successful?

Upvotes: 66

Views: 77752

Answers (10)

mark jay
mark jay

Reputation: 1284

If you would like to just see the output upon import and suppress the rest, you could make a context manager like this

import os
import contextlib

@contextlib.contextmanager
def suppress_tf_after_import():
    try:
        yield
    finally:
        os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
        
        
with suppress_tf_after_import():
    import tensorflow as tf

Upvotes: 0

alex
alex

Reputation: 11400

This is what worked for me:

import logging
logging.getLogger('tensorflow').setLevel(logging.ERROR)
os.environ["KMP_AFFINITY"] = "noverbose"
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import tensorflow as tf
tf.autograph.set_verbosity(3)

Upvotes: 7

Adam Wallner
Adam Wallner

Reputation: 2412

I created a function which shuts TF up. I call it on start of my programs. Some messages are very annoying and I cannot do anything about them...

def tensorflow_shutup():
    """
    Make Tensorflow less verbose
    """
    try:
        # noinspection PyPackageRequirements
        import os
        from tensorflow import logging
        logging.set_verbosity(logging.ERROR)
        os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

        # Monkey patching deprecation utils to shut it up! Maybe good idea to disable this once after upgrade
        # noinspection PyUnusedLocal
        def deprecated(date, instructions, warn_once=True):
            def deprecated_wrapper(func):
                return func
            return deprecated_wrapper

        from tensorflow.python.util import deprecation
        deprecation.deprecated = deprecated

    except ImportError:
        pass

Edit: This is for TF 2.0 and up:

def tensorflow_shutup():
    """
    Make Tensorflow less verbose
    """
    try:
        os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

        # noinspection PyPackageRequirements
        import tensorflow as tf
        from tensorflow.python.util import deprecation

        tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)

        # Monkey patching deprecation utils to shut it up! Maybe good idea to disable this once after upgrade
        # noinspection PyUnusedLocal
        def deprecated(date, instructions, warn_once=True):  # pylint: disable=unused-argument
            def deprecated_wrapper(func):
                return func
            return deprecated_wrapper

        deprecation.deprecated = deprecated

    except ImportError:
        pass

Upvotes: 19

Li-Pin Juan
Li-Pin Juan

Reputation: 1245

This works for me without introducing any packages other than TensorFlow itself:

import tensorflow as tf

tf.autograph.set_verbosity(0) # "0" means no logging.

For more details, check this TensorFlow API documentation.

Upvotes: 0

craymichael
craymichael

Reputation: 4821

UPDATE (beyond 1.14): see my more thorough answer here (this is a dupe question anyway): https://stackoverflow.com/a/38645250/6557588

In addition to Wintro's answer, you can also disable/suppress TensorFlow logs from the C side (i.e. the uglier ones starting with single characters: I, E, etc.); the issue open regarding logging has been updated to state that you can now control logging via an environmental variable. You can now change the level by setting the environmental variable called TF_CPP_MIN_LOG_LEVEL; it defaults to 0 (all logs shown), but can be set to 1 to filter out INFO logs, 2 to additionally filter out WARNING logs, and 3 to additionally filter out ERROR logs. It appears to be in master now, and will likely be a part of future version (i.e. versions after r0.11). See this page for more information. Here is an example of changing the verbosity using Python:

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'  # or any {'0', '1', '2'}
import tensorflow as tf

You can set this environmental variable in the environment that you run your script in. For example, with bash this can be in the file ~/.bashrc, /etc/environment, /etc/profile, or in the actual shell as:

TF_CPP_MIN_LOG_LEVEL=2 python my_tf_script.py

Upvotes: 63

Matt_Haythornthwaite
Matt_Haythornthwaite

Reputation: 753

To anyone still struggling to get the os.environ solution to work as I was, check that this is placed before you import tensorflow in your script, just like craymichael's answer:

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'  # or any {'0', '1', '2'}
import tensorflow as tf

Upvotes: 14

Wilmar van Ommeren
Wilmar van Ommeren

Reputation: 7689

For tensorflow 2.2 you can disable the logging with the following lines:

import tensorflow as tf   
tf.get_logger().setLevel('ERROR')

Upvotes: 1

Kris
Kris

Reputation: 23569

As of Tensorflow v1.14 (yes, including version 2.x) you can use the native logging module to silence Tensorflow:

import logging
import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'  # FATAL
logging.getLogger('tensorflow').setLevel(logging.FATAL)

I personally use this in my projects:

def set_tf_loglevel(level):
    if level >= logging.FATAL:
        os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    if level >= logging.ERROR:
        os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
    if level >= logging.WARNING:
        os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'
    else:
        os.environ['TF_CPP_MIN_LOG_LEVEL'] = '0'
    logging.getLogger('tensorflow').setLevel(level)

so that I can disable tf logging by running:

set_tf_loglevel(logging.FATAL)

and I can re-enable with

set_tf_loglevel(logging.INFO)

Upvotes: 32

Ziyad Edher
Ziyad Edher

Reputation: 2150

You can set the verbosity levels of TensorFlow's logging using

tf.logging.set_verbosity(tf.logging.ERROR)

where ERROR can be any of DEBUG, INFO, WARN, ERROR, or FATAL. See the logging module.

However, setting this to ERROR does not always completely block all INFO logs, to completely block them you have two main choices in my opinion.

  • If you are using Linux, you can just grep out all output strings beginning with I tensorflow/.
  • Otherwise, you can completely rebuild TensorFlow with some modified files. See this answer.

If you're using TensorFlow version 1 (1.X), you can use

tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)

Upvotes: 38

Danibix
Danibix

Reputation: 235

Considering previous answers, in Tensorflow 1.14 is actually possible to eliminate all the message generated by the library by including in the code the following two lines:

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)

This method exploits both the environment variable approach and Tensorflow logging module.

Note: the compatibility version is necessary to avoids further warnings given by the library, since the standard one is now deprecated.

Upvotes: 1

Related Questions