3

嗨,我正试图让 TFX 管道像练习一样运行。我用来从磁盘ImportExampleGen加载。TFRecords每一个都Example包含TFRecord一个字节串、高度、宽度、深度、转向和油门标签形式的 jpg。

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

我找不到有关StatisticsGen和处理图像数据的具体示例。根据文档Tensorflow 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)

提前致谢。

4

2 回答 2

5

来自git 问题回复 感谢Evan Rosen

嗨伙计,

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

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

在 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)

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

于 2020-04-22T12:19:12.893 回答
1

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

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) 
于 2020-04-11T15:54:30.233 回答