I'm using Keras and Tensorflow for neural networks, however Tensorflow is doubling logging from my main logger, which uses a StreamHandler, and its warnings cannot be disabled entirely.
I tried manually disabling the messages and logging from the very beginning of the imports and it did disable a LOT of messages Keras(Using Tensorflow backend
) and Tensorflow were sending but, the doubling issue happens.
This is what I'm currently using.
import os
import logging
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '4'
logging.getLogger('tensorflow').disabled = True
import sys
stderr = sys.stderr
sys.stderr = open(os.devnull, 'w')
import keras
sys.stderr = stderr
from keras.layers import Activation, Dense, Dropout
from keras.models import Sequential, load_model
from keras.optimizers import SGD
from keras.backend.tensorflow_backend import tf
logger = tf.get_logger()
logger.disabled = True
logger.setLevel(logging.FATAL)
That code is still ugly and immense and replacing sys.stderr even if it's temporary is personally very unprofessional. The worst part is, just a part of my issue was fixed, my console still outputs this:
WARNING: Logging before flag parsing goes to stderr.
I0817 17:59:30.386165 5752 social.py:42] Social loaded
INFO | Systems ready
I0817 17:59:30.401529 5752 __init__.py:63] Systems ready
The only message it's supposed to be there is INFO | Systems ready
, everything else should not.
Please, if someone has an idea of how to fix this once and for all, let me know below.
For anyone out there dealing with the same issue, this seems to be an issue with another library. This code right here should do the trick to disable Keras' deprecation warnings and Tensorflow's ugly logging, this is still ugly so if someone has a better way to do it I'd really like to hear about it. This also fixed the double logging.
import os
import sys
import logging
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
# Keras outputs warnings using `print` to stderr so let's direct that to devnull temporarily
stderr = sys.stderr
sys.stderr = open(os.devnull, 'w')
import keras
# we're done
sys.stderr = stderr
from keras.backend.tensorflow_backend import tf
import absl.logging
logging.root.removeHandler(absl.logging._absl_handler)
absl.logging._warn_preinit_stderr = False
logger = tf.get_logger()
logger.setLevel(logging.FATAL)
graph = tf.get_default_graph()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With