0

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?

ddd
  • 4,665
  • 14
  • 69
  • 125

1 Answers1

1

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()
zimmerrol
  • 4,872
  • 3
  • 22
  • 41
  • Wouldn't this turn off warnings for all other things which I do want to log? – ddd Aug 21 '18 at 12:26
  • @ddd Yes, somehow. So if you are using the third method, all warnings inside the with block will be ignored. This means, that you can just wrap this block around the `keras` code which generates the warnings. Then only these will be ignored. – zimmerrol Aug 21 '18 at 14:49
  • I see your point. However the keras starts printing out this stuff while being imported not when some function is called. That's why these userwarnings are printed at the beginning when the application is just launched. – ddd Aug 21 '18 at 15:19
  • This is not correct, as the warnings you posted above are generated only when you call one of the functions. But even if this is the case, why not just wrap the imports in such a block? – zimmerrol Aug 21 '18 at 15:34