Notes:
这个问题延续了之前的问题我的问题 https://stackoverflow.com/questions/52035692/tensorflow-v1-10-store-images-as-byte-strings-or-per-channel。在那个问题中,我询问将一些虚拟数据存储为的最佳方法Example
and SequenceExample
寻求了解与所提供的虚拟数据类似的数据哪个更好。我提供了两种明确的表述Example
and SequenceExample
构建以及答案中的编程方式。
因为这仍然是很多代码,所以我提供了一个Colab https://colab.research.google.com/drive/1HUGoXfgxp0A_0eSdaCzutOkFvnYZ-egv(由 google 托管的交互式 jupyter 笔记本)文件,您可以在其中亲自尝试代码以获取帮助。所有必要的代码都在那里,并且有大量的注释。
我正在尝试学习如何将我的数据转换为 TF Records,因为所声称的好处对于我的数据来说是值得的。然而,文档还有很多不足之处,而教程/博客(我见过的)试图更深入,实际上只触及表面或重新散列现有的稀疏文档。
对于我的演示数据上一个问题 https://stackoverflow.com/questions/52035692/tensorflow-v1-10-store-images-as-byte-strings-or-per-channel- 以及这里 - 我写了一个不错的课程,需要:
- 具有 n 个通道的序列(在本示例中,它是基于整数、固定长度且具有 n 个通道)
- 软标记类别概率(在本例中,有 n 个类别且基于浮点数)
- 一些元数据(在本例中是一个字符串和两个浮点数)
并可以用 6 种形式之一对数据进行编码:
- 例如,序列通道/类以数字类型分开(
int64
在这种情况下)添加了元数据
- 例如,序列通道/类作为字节字符串分开(通过
numpy.ndarray.tostring()
)加上元数据
例如,将序列/类转储为字节字符串,并附加元数据
SequenceExample,序列通道/类以数字类型和元数据作为上下文分开
- SequenceExample,序列通道作为字节字符串分开,元数据作为上下文
- SequenceExample,序列和类转储为字节字符串,元数据作为上下文
这很好用。
In the Colab https://colab.research.google.com/drive/1HUGoXfgxp0A_0eSdaCzutOkFvnYZ-egv我展示了如何将虚拟数据全部写入同一文件以及单独的文件中。
我的问题是如何恢复这些数据?
我在链接文件中尝试了 4 次。
为什么 TFReader 与 TFWriter 位于不同的子包下?
通过更新特征以包含形状信息并记住这一点来解决SequenceExample
are unnamed FeatureLists
.
context_features = {
'Name' : tf.FixedLenFeature([], dtype=tf.string),
'Val_1': tf.FixedLenFeature([], dtype=tf.float32),
'Val_2': tf.FixedLenFeature([], dtype=tf.float32)
}
sequence_features = {
'sequence': tf.FixedLenSequenceFeature((3,), dtype=tf.int64),
'pclasses' : tf.FixedLenSequenceFeature((3,), dtype=tf.float32),
}
def parse(record):
parsed = tf.parse_single_sequence_example(
record,
context_features=context_features,
sequence_features=sequence_features
)
return parsed
filenames = [os.path.join(os.getcwd(),f"dummy_sequences_{i}.tfrecords") for i in range(3)]
dataset = tf.data.TFRecordDataset(filenames).map(lambda r: parse(r))
iterator = tf.data.Iterator.from_structure(dataset.output_types,
dataset.output_shapes)
next_element = iterator.get_next()
training_init_op = iterator.make_initializer(dataset)
for _ in range(2):
# Initialize an iterator over the training dataset.
sess.run(training_init_op)
for _ in range(3):
ne = sess.run(next_element)
print(ne)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)