嗨,我正试着让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。据我所知,TFRecord中的所有字节字符串图像都没有损坏。
我找不到关于StatisticsGen和处理图像数据的具体例子。根据文档,Tensorflow数据验证可以处理图像数据。
除了计算默认的数据统计之外,TFDV还可以计算语义域(例如图像、文本)的统计信息。要启用语义域统计信息的计算,可以将一个tfdv.StatsOptions对象( enable_semantic_domain_stats设置为True )传递给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)提前谢谢。
发布于 2020-04-22 12:19:12
嗨,各位,
您正在看到的警告表明,StatisticsGen正在尝试将原始图像功能视为一个分类字符串功能。图像字节正被很好地解码。问题是当统计数据(包括最上面的K例子)被写入时,输出proto需要一个UTF-8有效的字符串,但却得到原始的图像字节。据我所知,你的设置没有什么问题,但这只是一个意料之外的副作用,如果你有一个不能序列化的分类字符串特性的话,这只是一个善意警告的副作用。我们将寻找一个更好的默认值,更优雅地处理图像数据。
同时,要告诉StatisticsGen这个特性实际上是一个不透明的blob,您可以传递一个用户修改的模式,如StatsGen文档中所描述的那样。要生成此模式,您可以运行StatisticsGen和SchemaGen一次(对数据样本),然后修改推断模式以注释该图像特性。以下是来自@tall-josh的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-11 15:54:30
我发现这个解决方案比我想象的要简单得多.
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) https://stackoverflow.com/questions/60577308
复制相似问题