python - TensorFlow 解析和重塑 Dataset.map() 中的浮点列表
问题描述
我正在尝试将 3D 浮点列表写入 TFrecord,所以我成功地先将其压平,然后解析它,但在重塑它时会引发错误。
错误:ValueError: Shapes () and (8,) are not compatible
这就是我编写 TFrecord 文件的方式
def _floats_feature(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=value.flatten()))
def write(output_path, data_rgb, data_depth, data_decalib):
with tf.python_io.TFRecordWriter(output_path) as writer:
feature = {'data_rgb': _floats_feature(data_rgb),
'data_depth': _floats_feature(data_depth),
'data_decalib': _floats_feature(data_decalib)}
sample = tf.train.Example(features=tf.train.Features(feature=feature))
writer.write(sample.SerializeToString())
这就是我读取 TFrecord 文件的方式
def get_batches(date, drives, batch_size=1):
"""
Create a generator that returns batches of tuples
rgb, depth and calibration
:param date: date of the drive
:param drives: array of the drive_numbers within the drive date
:return: batch generator
"""
filenames = get_paths_drives(date, drives)
dataset = tf.data.TFRecordDataset(filenames)
dataset = dataset.map(input_parser) # Parse the record into tensors.
dataset = dataset.repeat() # Repeat the input indefinitely.
dataset = dataset.batch(batch_size)
return dataset
config = configparser.ConfigParser()
config.read(path_helpers.get_config_file_path())
IMAGE_WIDTH = int(config['DATA_INFORMATION']['IMAGE_WIDTH'])
IMAGE_HEIGHT = int(config['DATA_INFORMATION']['IMAGE_HEIGHT'])
INPUT_RGB_SHAPE = [IMAGE_HEIGHT, IMAGE_WIDTH, 3]
INPUT_DEPTH_SHAPE = [IMAGE_HEIGHT, IMAGE_WIDTH, 1]
LABEL_CALIB_SHAPE = [8]
def input_parser(example_proto):
features = {'data_rgb': tf.FixedLenFeature([], tf.float32),
'data_depth': tf.FixedLenFeature([], tf.float32),
'data_decalib': tf.FixedLenFeature([], tf.float32)}
parsed_features = tf.parse_single_example(example_proto, features)
data_rgb = parsed_features['data_rgb']
data_rgb.set_shape(np.prod(INPUT_RGB_SHAPE))
img_rgb = tf.reshape(data_rgb, INPUT_RGB_SHAPE)
data_depth = parsed_features['data_depth']
data_depth.set_shape(np.prod(INPUT_DEPTH_SHAPE))
img_depth = tf.reshape(data_depth, INPUT_DEPTH_SHAPE)
data_decalib = parsed_features['data_decalib']
data_decalib.set_shape(LABEL_CALIB_SHAPE)
return img_rgb, img_depth, data_decalib
解决方案
原来我需要改变我的输入解析器如下:
def input_parser(example_proto):
features = {'data_rgb': tf.FixedLenFeature(shape=[np.prod(INPUT_RGB_SHAPE)], dtype=tf.float32),
'data_depth': tf.FixedLenFeature(shape=[np.prod(INPUT_DEPTH_SHAPE)], dtype=tf.float32),
'data_decalib': tf.FixedLenFeature(shape=LABEL_CALIB_SHAPE, dtype=tf.float32)}
parsed_features = tf.parse_single_example(example_proto, features)
正如 tf.FixedLenFeature (现为tf.io.FixedLenFeature)的文档所指示的那样。第一个参数是shape
,我将其设置为[]
error ValueError: Shapes () and (8,) are not compatible
。将其设置为它们的真实值是可行的。
推荐阅读
- r - 自动识别变量列中包含的“日期”一词并将条目更改为日期格式的功能(尽管存在异质性)
- javascript - 可访问性 - 分组复选框,其中一个复选框具有相关的文本区域
- javascript - 如何将对象的对象递归地展平为数组
- flutter - Navigator.pop(context) 不返回上一屏 Flutter
- google-analytics - 为什么 shopify 向 GA 发送带有 # 和不带 # 的重复交易 ID
- r - 在 pdf 上标记化以进行定量分析
- c++ - 如何测试一个类是否满足 C++ 中的概念?
- javascript - 为什么我的保存到本地存储功能会多次保存相同的内容?
- r - R - Shiny - 如何在 Wellpanel 的单行中对齐按钮/图标?
- python - ValueError:logits 和标签必须具有相同的形状 ((None, 1) vs (None, 2))