- 1.软件环境
- 2.问题描述
- 2.1.`CUDA`信息以及`CPU`指令集不支持`AVX2`
- 2.2.`Tensorflow 1.0`和`Tensorflow 2.0`之间的`API`变更等`Warning`
- 2.3.`Pyinstaller`打包为exe后额外增加的`Warning`
- 3.解决方法
- 3.1.取消打印`CUDA`信息以及`CPU`指令集不支持`AVX2`
- 3.1.1.具体操作
- 3.1.2.效果预览
- 3.2.取消打印`Tensorflow 2.0`API变更以及`Pyinstaller`打包为`exe`后额外增加的`Warning`
- 3.2.1.具体操作
- 3.2.2.效果预览
- 4.总结
1.软件环境TensorFlow其他警告信息如何取消:
Python:设置不显示Using TensorFlow backend及FutureWarning: Passing type, 1 or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as type, 1, / '1,type
2.问题描述Windows10 教育版64位
Tensorflow-GPU 2.3.0
Keras 2.4.3
CUDA 10.1
Pyinstaller 4.10
在使用Tensorflow时,经常会打印一些无用的信息,主要包括如下:
2.1.CUDA信息以及CPU指令集不支持AVX2Tensorflow在成功加载CUDA后,会打印CUDA相关的信息,真的是一片红~~:
即:
2022-05-25 18:32:09.641749: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudart64_101.dll 2022-05-25 18:32:16.164069: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library nvcuda.dll 2022-05-25 18:32:16.186774: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1716] Found device 0 with properties: pciBusID: 0000:73:00.0 name: Quadro RTX 6000 computeCapability: 7.5 coreClock: 1.77GHz coreCount: 72 deviceMemorySize: 24.00GiB deviceMemoryBandwidth: 625.94GiB/s 2022-05-25 18:32:16.187080: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudart64_101.dll 2022-05-25 18:32:16.190454: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cublas64_10.dll 2022-05-25 18:32:16.193652: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cufft64_10.dll 2022-05-25 18:32:16.194809: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library curand64_10.dll 2022-05-25 18:32:16.198553: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cusolver64_10.dll 2022-05-25 18:32:16.200484: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cusparse64_10.dll 2022-05-25 18:32:16.207734: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudnn64_7.dll 2022-05-25 18:32:16.207897: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1858] Adding visible gpu devices: 0 2022-05-25 18:32:17.342370: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations: AVX2 To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. 2022-05-25 18:32:17.351683: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x28cb2077ca0 initialized for platform Host (this does not guarantee that XLA will be used). Devices: 2022-05-25 18:32:17.351949: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version 2022-05-25 18:32:17.352268: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1716] Found device 0 with properties: pciBusID: 0000:73:00.0 name: Quadro RTX 6000 computeCapability: 7.5 coreClock: 1.77GHz coreCount: 72 deviceMemorySize: 24.00GiB deviceMemoryBandwidth: 625.94GiB/s 2022-05-25 18:32:17.352595: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudart64_101.dll 2022-05-25 18:32:17.352746: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cublas64_10.dll 2022-05-25 18:32:17.352907: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cufft64_10.dll 2022-05-25 18:32:17.353069: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library curand64_10.dll 2022-05-25 18:32:17.353242: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cusolver64_10.dll 2022-05-25 18:32:17.353354: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cusparse64_10.dll 2022-05-25 18:32:17.353448: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudnn64_7.dll 2022-05-25 18:32:17.353661: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1858] Adding visible gpu devices: 0 2022-05-25 18:32:17.881639: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1257] Device interconnect StreamExecutor with strength 1 edge matrix: 2022-05-25 18:32:17.881807: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1263] 0 2022-05-25 18:32:17.881871: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1276] 0: N 2022-05-25 18:32:17.882136: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1402] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 21845 MB memory) -> physical GPU (device: 0, name: Quadro RTX 6000, pci bus id: 0000:73:00.0, compute capability: 7.5) 2022-05-25 18:32:17.885199: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x28ce667ab30 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices: 2022-05-25 18:32:17.885412: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Quadro RTX 6000, Compute Capability 7.52.2.Tensorflow 1.0和Tensorflow 2.0之间的API变更等Warning
由于Tensorflow 1.0和Tensorflow 2.0之间的API变化较大,会打印许多Warning信息:
即:
WARNING:tensorflow:From C:UsersJayceAnaconda3envsmap_scale_guilibsite-packagestensorflowpythonutildeprecation.py:574: calling map_fn_v2 (from tensorflow.python.ops.map_fn) with dtype is deprecated and will be removed in a future version. Instructions for updating: Use fn_output_signature instead WARNING:tensorflow:From C:UsersJayceAnaconda3envsmap_scale_guilibsite-packagestensorflowpythonkerasenginetraining_v1.py:2070: Model.state_updates (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version. Instructions for updating: This property should not be used in TensorFlow 2.0, as updates are applied automatically.2.3.Pyinstaller打包为exe后额外增加的Warning
在使用Pyinstaller 等软件打包之后,程序还会额外打印一些新的警告:
即:
WARNING:tensorflow:AutoGraph could not transform> and will run it as-is. Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: Unable to locate the source code of >. Note that functions defined in certain environments, like the interactive Python shell do not expose their source code. If that is the case, you should to define them in a .py source file. If you are certain the code is graph-compatible, wrap the call using @tf.autograph.do_not_convert. Original error: could not get source code To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert WARNING:tensorflow:AutoGraph could not transform > and will run it as-is. Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: Unable to locate the source code of >. Note that functions defined in certain environments, like the interactive Python shell do not expose their source code. If that is the case, you should to define them in a .py source file. If you are certain the code is graph-compatible, wrap the call using @tf.autograph.do_not_convert. Original error: could not get source code To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert WARNING:tensorflow:AutoGraph could not transform > and will run it as-is. Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: Unable to locate the source code of >. Note that functions defined in certain environments, like the interactive Python shell do not expose their source code. If that is the case, you should to define them in a .py source file. If you are certain the code is graph-compatible, wrap the call using @tf.autograph.do_not_convert. Original error: could not get source code To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert WARNING:tensorflow:AutoGraph could not transform > and will run it as-is. Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: Unable to locate the source code of >. Note that functions defined in certain environments, like the interactive Python shell do not expose their source code. If that is the case, you should to define them in a .py source file. If you are certain the code is graph-compatible, wrap the call using @tf.autograph.do_not_convert. Original error: could not get source code To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert WARNING:tensorflow:From tensorflowpythonutildeprecation.py:574: calling map_fn_v2 (from tensorflow.python.ops.map_fn) with dtype is deprecated and will be removed in a future version. Instructions for updating: Use fn_output_signature instead WARNING:tensorflow:AutoGraph could not transform .nms_keep_map at 0x0000021767AD99D8> and will run it as-is. Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: Unable to locate the source code of .nms_keep_map at 0x0000021767AD99D8>. Note that functions defined in certain environments, like the interactive Python shell do not expose their source code. If that is the case, you should to define them in a .py source file. If you are certain the code is graph-compatible, wrap the call using @tf.autograph.do_not_convert. Original error: could not get source code To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
上述信息虽然不会影响我们程序的正常执行,但是有如下缺点:
- 如果你是多进程运行预测的话,上述信息会打印很多遍,这个时候你如果要捕获CMD的输出,就会变得非常繁琐;
- 如果你的程序本身有需要打印的关键信息,上述输出非常容易遮挡你需要查看的关键信息;
- 过多的无用输出可能会影响程序性能
对我们来说,其实上面的信息是无用的。那么,有没有办法可以让我们将上述Tensorflow中的无用信息屏蔽掉呢?
TensorFlow中的Log共有DEBUG、INFO、WARNING、ERROR、FATAL 5种级别,且通过环境变量TF_CPP_MIN_LOG_LEVEL对日志级别进行设置,TF_CPP_MIN_LOG_LEVEL不同值的含义如下:
| Level | Level for Humans | Level Description |
|---|---|---|
| 0 | DEBUG | all messages are logged (Default) |
| 1 | INFO | INFO messages are not printed |
| 2 | WARNING | INFO and WARNING messages are not printed |
| 3 | ERROR | INFO, WARNING, and ERROR messages are not printed |
因此我们可以通过设置TF_CPP_MIN_LOG_LEVEL的值,屏蔽该级别以及更低级别的日志,如:设置为3,表示屏蔽INFO, WARNING, ERROR级别的日志。
3.1.1.具体操作直接在你的主函数(即你运行的.py文件)开头添加如下代码:
import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
记住一定是代码开头,如果你把上面代码放在import tensorflow as tf后面,这个时候tensorflow 都import完了才会运行我们的语句,自然设置就不会有效果了。
3.1.2.效果预览这个时候,我们已经成功取消打印CUDA信息以及CPU指令集不支持AVX2等无用信息,之前界面一大堆红色的警告信息已经消失不见,只剩下API变更的警告:
Tensorflow为了兼容1.x和2.x 的APIs,专门提供了tf.compat模块:
因此,我们只需要在程序里面进行兼容性设置即可。
直接在3.1添加的代码后面继续添加如下代码:
import tensorflow as tf tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)3.2.2.效果预览
这个时候,3.1剩下的API变更的警告也没有了,控制台只打印我们自己定义的提示信息:
运行Pyinstaller打包后的程序也没有多余的警告打印:
如果想完全消除Tensorflow本身打印的警告,需要结合3.1和3.2,即直接在你的主函数(即你运行的.py文件)开头添加代码:
import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' import tensorflow as tf tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
如果只想屏蔽一部分,如只想取消打印CUDA信息以及CPU指令集不支持AVX2,则按3.1操作即可;而如果只想取消打印Tensorflow 2.0的API变更以及Pyinstaller打包为exe后额外增加的Warning,则按3.2操作即可。但一定要注意:是在你即将运行的.py文件的开头添加相应代码。
都看到这里了,还不赶紧点赞,评论,收藏走一波?



