【问题标题】:TFX StatisticsGen for image data用于图像数据的 TFX StatisticsGen
【发布时间】:2020-03-07 11:56:06
【问题描述】:

您好,我正在尝试让 TFX 流水线运行起来,就像真正的练习一样。我正在使用ImportExampleGen 从磁盘加载TFRecordsTFRecord 中的每个Example 都包含一个字节串形式的jpg,高度、宽度、深度、转向和油门标签。

我正在尝试使用StatisticsGen,但收到此警告; WARNING:root:Feature "image_raw" has bytes value "None" which cannot be decoded as a UTF-8 string. 让我的 Colab Notebook 崩溃。据我所知,TFRecord 中的所有字节字符串图像都没有损坏。

我在StatisticsGen 和处理图像数据方面找不到具体示例。根据docsTensorflow Data Validation 可以处理图片数据。

除了计算一组默认的数据统计信息外,TFDV 还可以计算语义域(例如图像、文本)的统计信息。要启用语义域统计信息的计算,请将 enable_semantic_domain_stats 设置为 True 的 tfdv.StatsOptions 对象传递给 tfdv.generate_statistics_from_tfrecord。

但我不确定这是否适合 StatisticsGen

这是实例化 ImportExampleGen 然后是 StatisticsGen 的代码

from tfx.utils.dsl_utils import tfrecord_input
from tfx.components.example_gen.import_example_gen.component import ImportExampleGen
from  tfx.proto import example_gen_pb2

examples = tfrecord_input(_tf_record_dir)
# https://www.tensorflow.org/tfx/guide/examplegen#custom_inputoutput_split
# has a good explanation of splitting the data the 'output_config' param

# Input train split is _tf_record_dir/*'
# Output 2 splits: train:eval=8:2.
train_ratio = 8
eval_ratio  = 10-train_ratio
output = example_gen_pb2.Output(
             split_config=example_gen_pb2.SplitConfig(splits=[
                 example_gen_pb2.SplitConfig.Split(name='train',
                                                   hash_buckets=train_ratio),
                 example_gen_pb2.SplitConfig.Split(name='eval',
                                                   hash_buckets=eval_ratio)
             ]))
example_gen = ImportExampleGen(input=examples,
                               output_config=output)
context.run(example_gen)

statistics_gen = StatisticsGen(
    examples=example_gen.outputs['examples'])
context.run(statistics_gen)

提前致谢。

【问题讨论】:

  • 更新:我一直在做一些挖掘工作。 TFX StatisticsGen Docs 靠在 tfx.data_validation 上,这让我尝试了这个; stats = tfdv.generate_statistics_from_tfrecord(data_location=tfrecords_filename) 导致相同的警告和 Colab 崩溃。我猜想找到问题的根源。
  • 嗯,好的,所以我找到了一个 CIFAR 10 example,其中已经创建了一个 tfrecord。当我使用它来创建 StatisticsGen 时,我收到了同样的警告,并且我的 Google Colab 崩溃了。也许只是 Colab 对文本输出感到不知所措。也许我可以更改日志级别。看看有没有帮助。
  • 您找到解决此问题的方法了吗?我有同样的错误信息。
  • 有一个类似的问题,修复是更新到 tfx 21.2,让事情在上面的笔记本中以交互方式工作。更新到 21.2 也可以在 kubeflow 上运行(确保更新您的 dockerfile,我已经将这个示例与上面类似地调整为使用 ImportExampleGen github.com/tensorflow/tfx/blob/master/tfx/examples/…
  • 谢谢@DarrenBrien。我会试一试,然后回复你。

标签: tensorflow tfx


【解决方案1】:

来自git issue response 谢谢Evan Rosen

大家好,

您看到的警告表明 StatisticsGen 正在尝试将您的原始图像特征视为分类字符串特征。图像字节被解码得很好。问题是,当正在写入统计信息(包括前 K 个示例)时,输出原型需要一个 UTF-8 有效字符串,而是获取原始图像字节。据我所知,您的设置没有任何问题,但这只是善意警告的意外副作用,如果您有无法序列化的分类字符串功能。我们将寻找一个更好的默认值来更优雅地处理图像数据。

同时,要告诉 StatisticsGen 此功能实际上是一个不透明的 blob,您可以传入用户修改的架构,如 StatsGen 文档中所述。要生成此模式,您可以运行 StatisticsGen 和 SchemaGen 一次(在数据样本上),然后修改推断的模式以注释该图像特征。这是来自@tall-josh 的 colab 的修改版本:

Open In Colab

额外的步骤有点冗长,但出于其他原因,拥有一个精心策划的架构通常是一种很好的做法。这是我添加到笔记本中的单元格:

from google.protobuf import text_format
from tensorflow.python.lib.io import file_io
from tensorflow_metadata.proto.v0 import schema_pb2

# Load autogenerated schema (using stats from small batch)

schema = tfx.utils.io_utils.SchemaReader().read(
    tfx.utils.io_utils.get_only_uri_in_dir(
        tfx.types.artifact_utils.get_single_uri(schema_gen.outputs['schema'].get())))

# Modify schema to indicate which string features are images.
# Ideally you would persist a golden version of this schema somewhere rather
# than regenerating it on every run.
for feature in schema.feature:
  if feature.name == 'image/raw':
    feature.image_domain.SetInParent()

# Write modified schema to local file
user_schema_dir ='/tmp/user-schema/'
tfx.utils.io_utils.write_pbtxt_file(
    os.path.join(user_schema_dir, 'schema.pbtxt'), schema)

# Create ImportNode to make modified schema available to other components
user_schema_importer = tfx.components.ImporterNode(
    instance_name='import_user_schema',
    source_uri=user_schema_dir,
    artifact_type=tfx.types.standard_artifacts.Schema)

# Run the user schema ImportNode
context.run(user_schema_importer)

希望您发现此解决方法很有用。同时,我们将看看图像价值特征的更好的默认体验。

【讨论】:

    【解决方案2】:

    摸索了一下,发现解决方案比我想象的要简单得多......

    from tfx.orchestration.experimental.interactive.interactive_context import InteractiveContext
    import logging
    ...
    logger = logging.getLogger()
    logger.setLevel(logging.CRITICAL)
    ...
    context = InteractiveContext(pipeline_name='my_pipe')
    ...
    c = StatisticsGen(...)
    ...
    context.run(c) 
    

    【讨论】:

    • 谢谢@DarrenBrien。从某种意义上说,我可以继续运行笔记本,而不会用警告消息压倒单元输出。理想情况下,我们将弄清楚如何正确编码/解码图像数据,以便永远不会触发警告。我已经发布了一个 issue 围绕如何正确编码/解码图像数据以便 StatisticsGen 实际工作。
    • 对于任何有兴趣的人Run in Colab。将所有内容运行到 StatisticeGen 单元格
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2019-11-29
    • 2022-06-21
    • 2021-07-25
    • 2021-01-09
    • 1970-01-01
    • 2020-02-08
    相关资源
    最近更新 更多