【问题标题】:Suppress warnings with loading tiff images in tensorflow doesn't seem to work?在 tensorflow 中加载 tiff 图像来抑制警告似乎不起作用?
【发布时间】:2021-05-11 06:46:14
【问题描述】:

我在使用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')

如何抑制此警告,或者解决产生警告的问题?

【问题讨论】:

  • 有标准模块warnings,你应该在谷歌上找到很多使用它的例子。
  • 我试过了。在这种情况下它似乎不起作用,但也许我使用不正确。我已经更新了我的问题以包含它。

标签: python tensorflow warnings tensorflow-datasets suppress-warnings


【解决方案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")

【讨论】:

  • 感谢您的帮助!我有一个后续问题。我可以使用相同的格式来指定/抑制多个警告吗?我的理解是,我会复制每个警告的最小示例。 filterwarningswarn 之间还有什么区别?我尝试使用message=.*known incorrect sRGB profile 过滤另一个警告PNG warning: iCCP: known incorrect sRGB profile,但这似乎不起作用。
  • warn 生成警告。我用它来表明filterwarnings 正在工作。如果要过滤所有警告,请使用warnings.filterwarnings("ignore")。所有这些只有在warn() 生成消息并且filterwarnings()warn 之前使用时才有效。如果您在warn 之后使用filterwarnings,则它无法阻止它。如果您在导入的模块中有警告,那么您必须在导入前使用 fiter。还有一些警告可以正常print()然后就停不下来了。
猜你喜欢
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 2016-12-26
  • 1970-01-01
  • 1970-01-01
  • 2011-12-28
  • 1970-01-01
  • 2011-09-08
相关资源
最近更新 更多