我正在嘗試解析我的 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_traindef 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我的錯誤是什么?
1 回答

繁華開滿天機
TA貢獻1816條經驗 獲得超4個贊
問題是labels具有形狀(),即零維(它是標量),而您嘗試堆疊的所有稀疏張量都是一維的。您應該制作一個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)
除此之外,由于您正在解析單個示例,因此您的所有稀疏張量都應該是一維且連續的,因此您可以將轉換保存為密集并只采用它們.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)
添加回答
舉報
0/150
提交
取消