Disabling keras or tensorflow outputs

Viewed 304

I know that there are already some similar questions here on stackoverflow but none of them solved my problem. In a python script, I have to train a keras model multiple times and I want to do this with GPU support. Each time I get a bunch of lines in the output console which is disturbing because I can not see the useful information anymore. Here is a part of the output.

2021-04-08 19:43:40.804324: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check.
2021-04-08 19:43:40.804368: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
2021-04-08 19:43:40.804409: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1159] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-04-08 19:43:40.804418: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1165]      0 
2021-04-08 19:43:40.804424: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 0:   N 
2021-04-08 19:43:40.804495: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1304] Created TensorFlow device (/device:GPU:0 with 1356 MB memory) -> physical GPU (device: 0, name: NVIDIA GeForce MX250, pci bus id: 0000:06:00.0, compute capability: 6.1)
2021-04-08 19:45:27.918402: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties: 
name: NVIDIA GeForce MX250 major: 6 minor: 1 memoryClockRate(GHz): 1.582
pciBusID: 0000:06:00.0

I tried some methods I found on the internet including the following, but nothing worked for me.

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

import logging
logging.getLogger("tensorflow").setLevel(logging.ERROR)
logging.getLogger("tensorflow").addHandler(logging.NullHandler(logging.ERROR))

Every suggestion is welcome.

About my setup:

 - python 3.6.8 
 - keras 2.3.1 
 - tensorflow 2.0.0 
 - cudatoolkit 10.0.130
2 Answers

I use this simple approach, works for me.

# tacke future warning, deprecated warning, bla bla 
import warnings

# ------------ tackle some noisy warning
def warn(*args, **kwargs):
    pass
warnings.warn = warn
warnings.simplefilter(action="ignore", category=FutureWarning)
warnings.filterwarnings("ignore", category = DeprecationWarning)

'''
TF_CPP_MIN_LOG_LEVEL = 0 to all logs .
TF_CPP_MIN_LOG_LEVEL = 1 to filter out INFO logs 
TF_CPP_MIN_LOG_LEVEL = 2 to additionall filter out WARNING 
TF_CPP_MIN_LOG_LEVEL = 3 to additionally filter out ERROR.
'''
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

# now import tf 
import tensorflow as tf

This tutorial in the Tensorflow Documenation discusses using metadata DB(MLMD) APIs to prevent the printing of outputs. This might apply to your application.

Related