python – Tensorflow VarLenFeature与FixedLenFeature
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – Tensorflow VarLenFeature与FixedLenFeature,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2361字,纯文字阅读大概需要4分钟。
内容图文
![python – Tensorflow VarLenFeature与FixedLenFeature](/upload/InfoBanner/zyjiaocheng/706/6fb43c7803934c41ac815e69c227c5b0.jpg)
我试图将不同大小的图像保存到tf记录中.我发现即使图像大小不同,我仍然可以使用FixedLenFeature加载它们.
通过检查FixedLenFeature和VarLenFeature上的文档,我发现差异似乎是VarLenFeauture返回稀疏张量.
任何人都可以说明一些应该使用FixedLenFeature或VarLenFeature的情况吗?
解决方法:
您可以加载图像,因为您使用特征类型tf.train.BytesList()保存它们,整个图像数据是列表中的一个大字节值.
如果我是对的,你使用tf.decode_raw从TFRecord加载的图像中获取数据.
关于示例用例:
我使用VarLenFeature保存对象检测任务的数据集:
每个图像的边界框数量可变(等于图像中的对象),因此我需要另一个特征objects_number来跟踪对象(和bbox)的数量.
每个边界框本身都是4个浮点坐标的列表
我正在使用以下代码加载它:
features = tf.parse_single_example(
serialized_example,
features={
# We know the length of both fields. If not the
# tf.VarLenFeature could be used
'height': tf.FixedLenFeature([], tf.int64),
'width': tf.FixedLenFeature([], tf.int64),
'depth': tf.FixedLenFeature([], tf.int64),
# Label part
'objects_number': tf.FixedLenFeature([], tf.int64),
'bboxes': tf.VarLenFeature(tf.float32),
'labels': tf.VarLenFeature(tf.int64),
# Dense data
'image_raw': tf.FixedLenFeature([],tf.string)
})
# Get metadata
objects_number = tf.cast(features['objects_number'], tf.int32)
height = tf.cast(features['height'], tf.int32)
width = tf.cast(features['width'], tf.int32)
depth = tf.cast(features['depth'], tf.int32)
# Actual data
image_shape = tf.parallel_stack([height, width, depth])
bboxes_shape = tf.parallel_stack([objects_number, 4])
# BBOX data is actually dense convert it to dense tensor
bboxes = tf.sparse_tensor_to_dense(features['bboxes'], default_value=0)
# Since information about shape is lost reshape it
bboxes = tf.reshape(bboxes, bboxes_shape)
image = tf.decode_raw(features['image_raw'], tf.uint8)
image = tf.reshape(image, image_shape)
请注意,“image_raw”是固定长度的Feature(有一个元素)并保存“bytes”类型的值,但是“bytes”类型的值本身可以具有可变大小(它是一个字节串,并且其中可以包含许多符号) ).
所以“image_raw”是一个列表,其中包含一个“bytes”类型的元素,它可以超大.
进一步阐述它的工作原理:
功能是值列表,这些值具有特定的“类型”.
功能的数据类型是张量的数据类型的子集,您有:
> int64(内存中64位空间)
> bytes(在内存中占用尽可能多的字节)
> float(在内存占用32-64位idk多少)
您可以检查here张张数据类型.
所以你可以存储没有VarLenFeatures的可变长度数据(实际上你做得很好),但首先你需要将它转换成字节/字符串功能,然后解码它.
这是最常用的方法.
内容总结
以上是互联网集市为您收集整理的python – Tensorflow VarLenFeature与FixedLenFeature全部内容,希望文章能够帮你解决python – Tensorflow VarLenFeature与FixedLenFeature所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。