I have tfrecord file containing images and bounding boxes. There are a variable number of bounding boxes associated with each image. I'm creating my SequenceExamples like
def image_example(image_string, obj_vectors):
box_feature_list = []
for vec in obj_vectors:
box_features = tf.train.Feature(float_list=tf.train.FloatList(value=vec))
box_feature_list.append(box_features)
all_box_features = tf.train.FeatureList(feature=box_feature_list)
box_dict = {
'Box Vectors': all_box_features
}
boxes = tf.train.FeatureLists(feature_list=box_dict)
image = tf.train.Features(feature={
'image': _bytes_feature(image_string),
})
example = tf.train.SequenceExample(
context=image,
feature_lists=boxes
)
return example
Then I'm reading them with
def _parse_image_function(example):
# Create a dictionary describing the features.
context_feature = {
'image': tf.io.FixedLenFeature([], dtype=tf.string)
}
sequence_features = {
'Box Vectors': tf.io.VarLenFeature(dtype=tf.float32)
}
context_data, sequence_data = tf.io.parse_single_sequence_example(serialized=example,
context_features=context_feature, sequence_features=sequence_features)
print(context_data)
image = context_data['image']
print(image)
#tf.io.decode_raw(image, tf.string)
#print(image.numpy())
image = tf.image.decode_jpeg(context_data['image'])
print(image)
print(sequence_data['Box Vectors'])
return context_data, sequence_data
When I print context_data it prints {'image': <tf.Tensor 'ParseSingleSequenceExample/ParseSingleSequenceExample:0' shape=() dtype=string>} and when I print context_data['image'] is prints Tensor("ParseSingleSequenceExample/ParseSingleSequenceExample:0", shape=(), dtype=string). I would expect to get the raw string when I do context_data['image'] but I don't.
I use _parse_image_function as an input to dataset.map like
dataset = tf.data.TFRecordDataset(FILENAME)
dataset = dataset.map(_parse_image_function)
I can then get the raw image string by doing
for x, y in dataset:
vecs = y['Box Vectors']
image = x['image']
image = tf.reshape(image, [])
#print(image)
image = tf.image.decode_jpeg(image)
vecs = tf.sparse.to_dense(vecs)
But I want to convert my data to tensors in my map function _parse_image_function. I want to do this so I can batch my data, I was going to try to use dataset.padded_batch on the mapped dataset. I am going about this the wrong way?