大佬教程收集整理的这篇文章主要介绍了TFRecord 解析 3-D 特征,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我对 this 有类似的问题,但如果我的特征形状是 3-D 呢?例如,它不是价格 (1,288)
,而是 (1,288,3)
。 tf.io.FixedLenFeature()
的形状我应该怎么写?是 tf.io.FixedLenFeature(shape=[288,3],tf.float32)
或 tf.io.FixedLenFeature(shape=[864],tf.float32)
还是其他什么?谢谢!
有几种方法可以做到这一点。一种是使用 BytesList 功能
def _bytes_feature(value):
return tf.Train.Feature(
bytes_list=tf.Train.bytesList(value=[value]))
另一个正在使用 FloatList 功能
def _float_feature(value):
return tf.Train.Feature(
float_list=tf.Train.FloatList(value=value))
示例:
import numpy as np
import tensorflow as tf
# make some data
img = np.random.normal(size=(5,3))
img = img.astype(np.float32)
writer = tf.io.TFRecordWriter("/tmp/data.tfrec")
example = tf.Train.Example(
features=tf.Train.Features(
feature = {
"img_b": _bytes_feature(img.tobytes()),"img_f": _float_feature(img.flatten()),}))
writer.write(example.serializeToString())
writer.close()
def parse_fn(examplE):
features = {
"img_b": tf.io.FixedLenFeature([],tf.String),"img_f": tf.io.FixedLenFeature([5,3],tf.float32),}
parsed_example = tf.io.parse_single_example(example,features)
img_b = tf.io.decode_raw(
parsed_example['img_b'],out_type=tf.float32)
img_b = tf.reshape(img_b,(5,3))
img_f = parsed_example['img_f']
return img_b,img_f
让我们导入数据,看看它是否有效
dataset = tf.data.TFRecordDataset(["/tmp/data.tfrec"])
dataset = dataset.map(parse_fn).batch(1)
arr_b,arr_f = next(iter(dataset))
np.tesTing.assert_almost_equal(arr_b.numpy(),arr_f.numpy())
# passes
这假设您知道图像的形状并且它们都是相同的形状。
以上是大佬教程为你收集整理的TFRecord 解析 3-D 特征全部内容,希望文章能够帮你解决TFRecord 解析 3-D 特征所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。