ddd
ddd

Reputation: 5029

How to suppress Keras logs in Python

I am writing a Python application which runs Tensorflow model for classfication. Library Keras is used for simplicity. Here is my logging configuration:

logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
handler = RotatingFileHandler(LOG_DIR + '/' + LOG_FILE_NAME, maxBytes=LOG_FILE_MAX_BYTES,backupCount=LOG_FILE_BACKUP_COUNT)
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
handler.setFormatter(formatter)
logger = logging.getLogger('')
logger.addHandler(handler)
logging.getLogger('boto').setLevel(logging.WARNING)
logging.getLogger('keras').setLevel(logging.CRITICAL)
logging.getLogger('botocore').setLevel(logging.CRITICAL)

Although I set the logging level of keras to critical, it still prints out some kind of warning at the beginning to STDOUT:

UserWarning: Update your `InputLayer` call to the Keras 2 API: `InputLayer(batch_input_shape=[None, 64,..., sparse=False, name="input_1", dtype="float32")`
  return cls(**config)
UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(trainable=True, name="convolution2d_1", activity_regularizer=None, activation="relu", kernel_size=(3, 3), filters=64, strides=[1, 1], padding="same", data_format="channels_last", kernel_initializer="glorot_uniform", kernel_regularizer=None, bias_regularizer=None, kernel_constraint=None, bias_constraint=None, use_bias=True)`
  return cls(**config)
UserWarning: Update your `MaxPooling2D` call to the Keras 2 API: `MaxPooling2D(strides=[2, 2], trainable=True, name="maxpooling2d_1", pool_size=[2, 2], padding="valid", data_format="channels_last")`
  return cls(**config)

Why does this output not being logged to log files? Do I need to create a handler just for keras module and specify the same log file as the rest of the application? CRITICAL is higher than Warning. Why is it still outputting some type of warning?

Upvotes: 0

Views: 1529

Answers (1)

zimmerrol
zimmerrol

Reputation: 4951

You can simply turn off all python warnings, by either using

python -W ignore script.py

or using

import warnings
warnings.filterwarnings("ignore")

according to this SO post. You can find more information about the second method in the official python documentation.

A third way is to use the aforementioned module and use the `catch_warnings context manager

def fxn():
    warnings.warn("deprecated", DeprecationWarning)

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    # the warning will be ignored
    fxn()

Upvotes: 1

Related Questions