25

anyone knows if there is a method to prevent tensorflow from polluting standard error with gpus' memory allocation log?. I noted that when the following command is executed:

with tf.Session() as sess:

tensorflow prints on standard error a log about memory and gpu resources allocation. Something like:

I tensorflow/core/common_runtime/local_device.cc:25] Local device intra op parallelism threads: 48
Graphics Device pciBusID 0000:02:00.0
Free memory: 11.75GiB
...

For important reasons, I wanna avoid this printing.

4 Answers4

41

This was recently fixed, and should be available if you upgrade to TensorFlow 0.12 or later.

To disable all logging output from TensorFlow, set the following environment variable before launching Python:

$ export TF_CPP_MIN_LOG_LEVEL=3
$ python ...

You can also adjust the verbosity by changing the value of TF_CPP_MIN_LOG_LEVEL:

  • 0 = all messages are logged (default behavior)
  • 1 = INFO messages are not printed
  • 2 = INFO and WARNING messages are not printed
  • 3 = INFO, WARNING, and ERROR messages are not printed
mrry
  • 125,488
  • 26
  • 399
  • 400
9

You can set an environment variable before launching Python as described in the first answer, or you can add the following lines to your Python code:

import os  
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'  

Change 3 to values (0, 1, 2, 3) according to the messages you want avoid.

If using TensorFlow 2.0+, make sure to put those lines before import tensorflow to be effective.

Janosh
  • 3,392
  • 2
  • 27
  • 35
singrium
  • 2,746
  • 5
  • 32
  • 45
1

Defaults to 0, so all logs are shown. Set TF_CPP_MIN_LOG_LEVEL to 1 to filter out INFO logs, 2 to additionall filter out WARNING, 3 to additionally filter out ERROR.

Lerner Zhang
  • 6,184
  • 2
  • 49
  • 66
1

In TensorFlow 2.0, this does not work for all logging messages (I still get tf.function retracing warnings, for instance).

To make sure everything is turned off, do this

import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"

import logging
import tensorflow as tf
logger = tf.get_logger()
logger.setLevel(logging.ERROR) # or logging.INFO, logging.WARNING, etc.

Note 1: If you leave out the os.environ["TF_CPP_MIN_LOG_LEVEL"] line, some info messages are still printed (like the This TensorFlow binary is optimized with... that runs on startup.)

Note 2: in TF 1.0 you can also manually set the verbosity with tf.logging.set_verbosity(tf.logging.ERROR) but TF 2.0 does not have tf.logging.

codeananda
  • 939
  • 1
  • 10
  • 16