解码和重新编码TFR记录

2024-04-18 19:31:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我的目标是制作我自己的.tfrecord文件,用一些土壤运动数据来训练我自己的模型。我一直在尝试对DeepMind提供的train.tfrecord进行解码和重新编码,以检查所需的数据,以便将来准备我自己的数据

当我这么做的时候,我有两个问题:一个来自解码,另一个来自编码。如果需要我的.ipynb文件,请告诉我

问题1:解码


我能够用以下两种方法解码DeepMind制作的trian.tfrecord为什么在使用第二种方法解码.tfrecord时找不到position数据?相应的代码时钟附在下面。

第一种解码方法

# I ommitted some code lines above because all the codes are copied from
# the those provided in the DeepMind GitHub repository

ds = ds.map(functools.partial(reading_utils.parse_serialized_simulation_example, metadata=metadata))
ds
# Result

<MapDataset shapes: ({particle_type: (None,), key: ()}, {position: (601, None, 2)}), 
types: ({particle_type: tf.int64, key: tf.int64}, {position: tf.float32})>

并通过迭代ds

for data_dictionary in ds:
    print(data_dictionary)

我得到的数据集包含关于particle_typekeyposition的信息

# Result

({'particle_type': <tf.Tensor: shape=(1444,), dtype=int64, numpy=array([5, 5, 5, ..., 5, 5, 5], dtype=int64)>, 'key': <tf.Tensor: shape=(), dtype=int64, numpy=0>}, {'position': <tf.Tensor: shape=(601, 1444, 2), dtype=float32, numpy=
array([[[0.46111006, 0.6144427 ],
        [0.44826514, 0.6171301 ],
        [0.44778627, 0.6080082 ],
        ...,
        [0.6258534 , 0.7680327 ],
        [0.53640765, 0.7583775 ],
        [0.574302  , 0.65714085]],

       [[0.46392077, 0.620525  ],
        [0.45107582, 0.62321246],
        [0.450597  , 0.6140905 ],
        ...,
        [0.62866354, 0.7741145 ],
        [0.539218  , 0.7644595 ],
        [0.5771124 , 0.66322297]],

第二种解码方法

data_type = "WaterRamps"
raw_dataset = tf.data.TFRecordDataset(f'C:/tmp/datasets/{material_type}/{data_type}.tfrecord')

for raw_record in raw_dataset:
    example = tf.train.Example()
    example.ParseFromString(raw_record.numpy())
    print(example)

我得到了以下信息。但是position数据丢失

features {
  feature {
    key: "key"
    value {
      int64_list {
        value: 0
      }
    }
  }
  feature {
    key: "particle_type"
    value {
      bytes_list {
        value: "\005\000\000\000\000\000\000\000\005\000\000\000\000\000\000\000\005\000\000\000\000\000\000\000\005\000\000\000\000\000\000\000\005\000\000\000\000\000\000\000\005\000\000\000\000\000\000\000\005\000\000\000\000\000\000\000\005\000\000\000\000\000\000\000\005\000\000\000\000\000\000\000\005\000\000\000\000\000\000\000\005\000\000\000\000\000\000\000\005\000\000\000\000\000\000\000\005\000\000\000\000\000\000\000\005\000\000\000\000\000\000\000\005\000\000\000\000\000\000\000\005\000\000\000\000\00

问题2:重新编码


我能够将particle_typekeyposition转换为Numpy数组,但是我无法将它们重新编码为train.tfrecord。我重新编码的.tfrecordposition数据,但原始的trian.tfrecord没有我如何才能使.tfrecord与原始版本完全相同?

以下是我尝试过的:

# Function to make an example proto from Numpy arrays. 
# The functions, `_int64_feature`, `_bytes_feature`, `_bytes_feature`
#   are copied from the TensorFlow website.
def serialize_example(feature0, feature1, feature2):
    feature = {
        'key': _int64_feature(feature0),
        'particle_type': _bytes_feature(tf.io.serialize_tensor(feature1)),
        'position': _bytes_feature(tf.io.serialize_tensor(feature2)),
    }

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

# Write my own tfrecord
with tf.io.TFRecordWriter("./try1.tfrecord") as writer:
    for key_value in range(len(key_value)):
        serialized_example = serialize_example(key_nparray[key_value], particleType_nparray[key_value], position_nparray[key_value])
        writer.write(serialized_example)

以下是我的记录。但问题是它包含了position数据,这与我从上面提到的第一个问题中得到的不同

# Result

features {
  feature {
    key: "key"
    value {
      int64_list {
        value: 0
      }
    }
  }
  feature {
    key: "particle_type"
    value {
      bytes_list {
        value: "\010\t\022\005\022\003\010\244\013\"\240Z\005\000
                            ..."
                    }
                }
            }
        feuature {
            key: "position"
            value {
                bytes_list {value: "\010\001\022\016\022\003\010\331\004\022\003\
                            ..."


Tags: 数据key编码bytesvalueexampletftype