在 tensorflow 中加载 tiff 图像来抑制警告似乎不起作用?

Suppress warnings with loading tiff images in tensorflow doesn't seem to work?

提问人:Jane Sully 提问时间:5/11/2021 最后编辑:InnatJane Sully 更新时间:5/12/2021 访问量:416

问:

使用tfio.experimental.image.decode_tiff时,我不断收到以下警告。我发现它工作正常,但一直发出这些警告,我想抑制这些警告。

TIFFFetchNormalTag: Warning, ASCII value for tag "DateTime" contains null byte in value; value incorrectly truncated during reading due to implementation limitations.

我按如下方式使用它:

string = tf.io.read_file(filename)
image = tfio.experimental.image.decode_tiff(string) # this line produces warning

如果我尝试使用 抑制警告,它似乎不起作用?它不会给我一个错误,但它没有做任何事情。warning

import warnings
warnings.filterwarnings('ignore', message='ASCII value for tag "DateTime" contains null byte in value; value incorrectly truncated during reading due to implementation limitations')

如何禁止显示此警告,或者解决产生警告的问题?

python tensorflow 警告 tensorflow-datasets suppress-warnings

评论

0赞 furas 5/11/2021
有标准模块,您应该在 Google 中找到许多如何使用它的示例。warnings
0赞 Jane Sully 5/11/2021
我确实试过了。在这种情况下它似乎不起作用,但也许我错误地使用了它。我已经更新了我的问题以包括它。
0赞 Amit Sharma 8/29/2023
当我使用 openslide 并尝试打开 TIFF 文件进行read_region时,这对我不起作用。它溢出了 vscode ipynb 内存,内核最终崩溃。我需要禁用它,但没有任何方法可以阻止打印此警告

答:

1赞 furas 5/12/2021 #1

如果你想抑制所有警告,那么你可以使用

  warnings.filterwarnings("ignore")

如果要抑制某些消息,则必须使用消息开头或开头message=....*

  warnings.filterwarnings("ignore", message=".*ASCII value for tag")

最小示例:

import warnings

warnings.filterwarnings("ignore", message=".*ASCII value for tag")

# some tests - it should be supressed by `filterwarnings()`
warnings.warn('TIFFFetchNormalTag: Warning, ASCII value for tag "DateTime" contains null byte in value; value incorrectly truncated during reading due to implementation limitations.')

print("Hello World")

评论

0赞 Jane Sully 5/12/2021
感谢您的帮助!我有一个后续问题。我可以使用相同的格式来指定/禁止显示多个警告吗?我的理解是,我会为每个警告复制最小示例。还有和 和有什么不一样?我试图过滤另一个警告,但这似乎不起作用。filterwarningswarnPNG warning: iCCP: known incorrect sRGB profilemessage=.*known incorrect sRGB profile
0赞 furas 5/12/2021
warn生成警告。我用它来证明它正在工作。如果要过滤所有警告,请使用 .仅当消息由 生成并在之前使用时,所有这些操作才有效。如果你在之后使用,那么它就无法阻止它。如果导入的模块中有警告,则必须在导入之前使用fiter。有些警告可能是正常的,然后你就无法阻止它。filterwarningswarnings.filterwarnings("ignore")warn()filterwarnings()warnfilterwarningswarnprint()