我正在为每个图像写入带有多个标签的数据,在这种情况下为边界框和分类标签,并且正在使用以下功能将数据写入tfrecord:
def tfr_write_sr(data_split_name,save_dir, label_array, data_array):
filename = os.path.join(save_dir, data_split_name + '.tfrecords')
writer = tf.python_io.TFRecordWriter(filename)
for index in range(data_array.shape[0]):
image = data_array[index].tostring()
example = tf.train.Example(features=tf.train.Features(
feature={
'height': tf.train.Feature(
int64_list=tf.train.Int64List(
value=[data_array.shape[1]])),
'width': tf.train.Feature(
int64_list=tf.train.Int64List(
value=[data_array.shape[2]])),
'depth': tf.train.Feature(
int64_list=tf.train.Int64List(
value=[data_array.shape[3]])),
'shape_type': tf.train.Feature(
int64_list=tf.train.Int64List(
value=[int(label_array[index][3])])),
'bbtl_x': tf.train.Feature(
int64_list=tf.train.Int64List(
value=[int(label_array[index][1][0])])),
'bbtl_y': tf.train.Feature(
int64_list=tf.train.Int64List(
value=[int(label_array[index][1][1])])),
'bbbr_x': tf.train.Feature(
int64_list=tf.train.Int64List(
value=[int(label_array[index][0][0])])),
'bbbr_y': tf.train.Feature(
int64_list=tf.train.Int64List(
value=[int(label_array[index][0][1])])),
'image_raw': tf.train.Feature(
bytes_list=tf.train.BytesList(
value=[image]))}))
writer.write(example.SerializeToString())
writer.close()
我已验证记录是否正确写入,但是我之前看到的所有示例每个图像仅读取一个标签,我如何读取多个标签?
答案 0 :(得分:0)
首先,我们阅读tfrecord并获取其功能:
reader = tf.TFRecordReader()
_ , serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(serialized_example,
features={
'image_raw': tf.FixedLenFeature([],tf.string),
'shape_type' : tf.FixedLenFeature([], tf.int64),
'bbtl_x' : tf.FixedLenFeature([], tf.int64),
'bbtl_y' : tf.FixedLenFeature([], tf.int64),
'bbbr_x' : tf.FixedLenFeature([], tf.int64),
'bbbr_y' : tf.FixedLenFeature([], tf.int64)
})
现在我们有了我们的功能,可以使用tf.stack()为多标签建立张量并将其添加到图形中:
label = tf.stack([features['shape_type'],
features['bbtl_x'],
features['bbtl_y'],
features['bbbr_x'],
features['bbbr_y'] ], axis=0 )
image = tf.decode_raw(features['image_raw'], tf.uint8)
images_batch, labels_batch = tf.train.shuffle_batch([image,label],
batch_size=128,
capacity=2000,
min_after_dequeue=1000)