使用 tf.sparse.to_dense 函数时出错
error while using tf.sparse.to_dense function
我正在尝试解析我的 tfrecord 数据集以将其用于对象检测。当我尝试将我的稀疏张量更改为密集张量时,出现以下我无法理解的错误:
ValueError: Shapes must be equal rank, but are 1 and 0
From merging shape 3 with other shapes. for '{{node stack}} = Pack[N=5, T=DT_FLOAT, axis=1](SparseToDense, SparseToDense_1, SparseToDense_2, SparseToDense_3, Cast)' with input shapes: [?], [?], [?], [?], [].
我的 feature_description 是:
feature_description = {
'image/filename': tf.io.FixedLenFeature([], tf.string),
'image/encoded': tf.io.FixedLenFeature([], tf.string),
'image/object/bbox/xmin': tf.io.VarLenFeature(tf.float32),
'image/object/bbox/ymin': tf.io.VarLenFeature(tf.float32),
'image/object/bbox/xmax': tf.io.VarLenFeature(tf.float32),
'image/object/bbox/ymax': tf.io.VarLenFeature(tf.float32),
'image/object/class/label': tf.io.VarLenFeature(tf.int64),
}
我的解析代码:
def _parse_image_function(example_proto):
# Parse the input tf.Example proto using the dictionary above.
return tf.io.parse_single_example(example_proto, feature_description)
def _parse_tfrecord(x):
x_train = tf.image.decode_jpeg(x['image/encoded'], channels=3)
x_train = tf.image.resize(x_train, (416, 416))
labels = tf.cast(1, tf.float32)
# print(type(x['image/object/bbox/xmin']))
tf.print(x['image/object/bbox/xmin'])
y_train = tf.stack([tf.sparse.to_dense(x['image/object/bbox/xmin']),
tf.sparse.to_dense(x['image/object/bbox/ymin']),
tf.sparse.to_dense(x['image/object/bbox/xmax']),
tf.sparse.to_dense(x['image/object/bbox/ymax']),
labels], axis=1)
paddings = [[0, 100 - tf.shape(y_train)[0]], [0, 0]]
y_train = tf.pad(y_train, paddings)
return x_train, y_train
def load_tfrecord_dataset(train_record_file, size=416):
dataset=tf.data.TFRecordDataset(train_record_file)
parsed_dataset = dataset.map(_parse_image_function)
final = parsed_dataset.map(_parse_tfrecord)
return final
load_tfrecord_dataset(train_record_file,416)
我使用 for
循环来查看我的数据是否有问题并且 tf.sparse.to_dense 使用 for 循环完美地完成了它的工作,但是当我使用 .map(_parse_tfrecord)
它给了我我上面写的错误。
在 _parse_tfrecord(x) 中打印 x['image/object/bbox/xmin'] 的结果:
SparseTensor(indices=Tensor("DeserializeSparse_1:0", shape=(None, 1), dtype=int64), values=Tensor("DeserializeSparse_1:1", shape=(None,), dtype=float32)
在for循环中打印x['image/object/bbox/xmin']的结果:
SparseTensor(indices=[[0]
[1]
[2]
...
[4]
[5]
[6]], values=[0.115384616 0.432692319 0.75 ... 0.581730783 0.0817307681 0.276442319], shape=[7])
我的 for
循环:
for x in parsed_dataset:
tf.print(x['image/object/bbox/xmin'])
break
我这里的错误是什么?
问题是 labels
的形状为 ()
,即零维(它是标量),而您尝试堆叠的所有稀疏张量都是 one-dimensional.您应该制作一个 label
张量,其形状与框数据张量相同:
# Assuming all box data tensors have the same shape
box_data_shape = tf.shape(x['image/object/bbox/xmin'])
# Make label data
labels = tf.ones(box_data_shape, dtype=tf.float32)
除此之外,由于您正在解析单个示例,因此所有稀疏张量都应该是 one-dimensional 并且是连续的,因此您可以将转换保存为密集并只采用它们的 .values
:
y_train = tf.stack([x['image/object/bbox/xmin'].values,
x['image/object/bbox/ymin'].values,
x['image/object/bbox/xmax'].values,
x['image/object/bbox/ymax'].values,
labels], axis=1)
我正在尝试解析我的 tfrecord 数据集以将其用于对象检测。当我尝试将我的稀疏张量更改为密集张量时,出现以下我无法理解的错误:
ValueError: Shapes must be equal rank, but are 1 and 0
From merging shape 3 with other shapes. for '{{node stack}} = Pack[N=5, T=DT_FLOAT, axis=1](SparseToDense, SparseToDense_1, SparseToDense_2, SparseToDense_3, Cast)' with input shapes: [?], [?], [?], [?], [].
我的 feature_description 是:
feature_description = {
'image/filename': tf.io.FixedLenFeature([], tf.string),
'image/encoded': tf.io.FixedLenFeature([], tf.string),
'image/object/bbox/xmin': tf.io.VarLenFeature(tf.float32),
'image/object/bbox/ymin': tf.io.VarLenFeature(tf.float32),
'image/object/bbox/xmax': tf.io.VarLenFeature(tf.float32),
'image/object/bbox/ymax': tf.io.VarLenFeature(tf.float32),
'image/object/class/label': tf.io.VarLenFeature(tf.int64),
}
我的解析代码:
def _parse_image_function(example_proto):
# Parse the input tf.Example proto using the dictionary above.
return tf.io.parse_single_example(example_proto, feature_description)
def _parse_tfrecord(x):
x_train = tf.image.decode_jpeg(x['image/encoded'], channels=3)
x_train = tf.image.resize(x_train, (416, 416))
labels = tf.cast(1, tf.float32)
# print(type(x['image/object/bbox/xmin']))
tf.print(x['image/object/bbox/xmin'])
y_train = tf.stack([tf.sparse.to_dense(x['image/object/bbox/xmin']),
tf.sparse.to_dense(x['image/object/bbox/ymin']),
tf.sparse.to_dense(x['image/object/bbox/xmax']),
tf.sparse.to_dense(x['image/object/bbox/ymax']),
labels], axis=1)
paddings = [[0, 100 - tf.shape(y_train)[0]], [0, 0]]
y_train = tf.pad(y_train, paddings)
return x_train, y_train
def load_tfrecord_dataset(train_record_file, size=416):
dataset=tf.data.TFRecordDataset(train_record_file)
parsed_dataset = dataset.map(_parse_image_function)
final = parsed_dataset.map(_parse_tfrecord)
return final
load_tfrecord_dataset(train_record_file,416)
我使用 for
循环来查看我的数据是否有问题并且 tf.sparse.to_dense 使用 for 循环完美地完成了它的工作,但是当我使用 .map(_parse_tfrecord)
它给了我我上面写的错误。
在 _parse_tfrecord(x) 中打印 x['image/object/bbox/xmin'] 的结果:
SparseTensor(indices=Tensor("DeserializeSparse_1:0", shape=(None, 1), dtype=int64), values=Tensor("DeserializeSparse_1:1", shape=(None,), dtype=float32)
在for循环中打印x['image/object/bbox/xmin']的结果:
SparseTensor(indices=[[0]
[1]
[2]
...
[4]
[5]
[6]], values=[0.115384616 0.432692319 0.75 ... 0.581730783 0.0817307681 0.276442319], shape=[7])
我的 for
循环:
for x in parsed_dataset:
tf.print(x['image/object/bbox/xmin'])
break
我这里的错误是什么?
问题是 labels
的形状为 ()
,即零维(它是标量),而您尝试堆叠的所有稀疏张量都是 one-dimensional.您应该制作一个 label
张量,其形状与框数据张量相同:
# Assuming all box data tensors have the same shape
box_data_shape = tf.shape(x['image/object/bbox/xmin'])
# Make label data
labels = tf.ones(box_data_shape, dtype=tf.float32)
除此之外,由于您正在解析单个示例,因此所有稀疏张量都应该是 one-dimensional 并且是连续的,因此您可以将转换保存为密集并只采用它们的 .values
:
y_train = tf.stack([x['image/object/bbox/xmin'].values,
x['image/object/bbox/ymin'].values,
x['image/object/bbox/xmax'].values,
x['image/object/bbox/ymax'].values,
labels], axis=1)