程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了TFRecord 解析 3-D 特征大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决TFRecord 解析 3-D 特征?

开发过程中遇到TFRecord 解析 3-D 特征的问题如何解决?下面主要结合日常开发的经验,给出你关于TFRecord 解析 3-D 特征的解决方法建议,希望对你解决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,请注明来意。