如何屏蔽TensorFlow中INFO:tensorflow:Assets written to日志信息?
屏蔽ModelCheckpoint非H5格式保存的日志信息
方法1:调整TensorFlow/Keras全局日志级别
直接降低TensorFlow的日志输出等级,过滤掉无关的INFO、WARNING类日志:
import tensorflow as tf tf.get_logger().setLevel('ERROR')
如果是命令行运行脚本,也可以提前设置环境变量:
export TF_CPP_MIN_LOG_LEVEL=3
方法2:自定义无冗余日志的ModelCheckpoint回调
不想全局屏蔽日志的话,可以自定义回调类,只保留关键的保存提示:
from tensorflow.keras.callbacks import ModelCheckpoint class QuietModelCheckpoint(ModelCheckpoint): def on_epoch_end(self, epoch, logs=None): current = logs.get(self.monitor) if current is None: return if self.monitor_op(current, self.best): self.best = current print(f"Epoch {epoch+1}: 保存最佳模型") self.save_weights(self.filepath, overwrite=True) # 替换原回调使用自定义版本 callbacks = [ QuietModelCheckpoint('DeeplabV3Plus', save_best_only=True), keras.callbacks.EarlyStopping(patience=30, restore_best_weights=True) ]
方法3:通过logging模块定向过滤
利用Python标准logging模块,单独过滤TensorFlow的日志:
import logging logging.getLogger('tensorflow').setLevel(logging.ERROR)
内容的提问来源于stack exchange,提问作者MCPMH
相关产品推荐
相关产品推荐

