首页 > 解决方案 > 如何将功能转换为 Python 对象?

问题描述

我有一些Examples 以二进制格式存储在文件中,我正在阅读如下:

for fp in shard_filepaths:
    record_iterator = tf.python_io.tf_record_iterator(path=fp)
    for record in record_iterator:
         example = tf.train.Example.FromString(record)

有没有办法将其映射example到 Pythondictobject

我知道我可以做类似的事情

data = json.loads(MessageToJson(tf.train.Example.FromString(record)))

但这会产生相当复杂和不方便的输出。

我在这里有什么选择?


此外,这是to_example(dictionary)生成从 adict到a 的记录的方法(参见 tensor2tensor ) example

def to_example(dictionary):
    """Helper: build tf.Example from (string -> int/float/str list) dictionary."""

    features = {}

    for (k, v) in six.iteritems(dictionary):
        if not v:
            raise ValueError('Empty generated field: %s' % str((k, v)))
        if isinstance(v[0], six.integer_types):
            features[k] = tf.train.Feature(int64_list=tf.train.Int64List(value=v))
        elif isinstance(v[0], float):
            features[k] = tf.train.Feature(float_list=tf.train.FloatList(value=v))
        elif isinstance(v[0], six.string_types):
            if not six.PY2:  # Convert in python 3.
                v = [bytes(x, 'utf-8') for x in v]
            features[k] = tf.train.Feature(bytes_list=tf.train.BytesList(value=v))
        elif isinstance(v[0], bytes):
            features[k] = tf.train.Feature(bytes_list=tf.train.BytesList(value=v))
        else:
            raise ValueError('Value for %s is not a recognized type; v: %s type: %s' % (k, str(v[0]), str(type(v[0]))))

    return tf.train.Example(features=tf.train.Features(feature=features))

标签: tensorflow

解决方案


据我所知,这就是您要查找的内容,您需要修改变量名,但这应该可以完成工作。

reader = tf.TFRecordReader()
filename_queue = tf.train.string_input_producer(['filename.tfrecord'])

_, serialized_example = reader.read(filename_queue)

# Define features
read_features = {
    'Feature 1': tf.FixedLenFeature([], dtype=tf.int64),
    'Feature 2 tf.VarLenFeature(dtype=tf.string),
    'Feature 3: tf.VarLenFeature(dtype=tf.float32),
    'Feature 4: tf.FixedLenFeature([], dtype=tf.string),
    'Feature 5: tf.FixedLenFeature([], dtype=tf.float32),
    'Feature 6: tf.FixedLenFeature([], dtype=tf.float32)}

# Extract features from serialized data
read_data = tf.parse_single_example(serialized=serialized_example,
                                    features=read_features)

read_data 将是一个列表输出。


推荐阅读