0

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?

1 Answer 1

1

I got it working 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)

        return context_data['image'], sequence_data['Box Vectors']

    def format_data(image, labels):
        vecs = tf.sparse.to_dense(labels)
        #print(vecs)
        image = tf.image.decode_jpeg(image)
        image = tf.image.convert_image_dtype(image, dtype=tf.float32) # this should also normalize pixels
        #print(image)
        return image, vecs

    def train():
        dataset = dataset.map(_parse_image_function)
        dataset = dataset.map(format_data)
        dataset = dataset.padded_batch(params.batch_size, padded_shapes=([None, None, 3], [None, None]))
        for epoch in range(params.epochs):
            for x, y in dataset:
                loss_value, grads = grad(model, x, y)
                optimizer.apply_gradients(zip(grads, model.trainable_variables))

I had to call dataset.map twice. My images are (1280, 1920, 3) and labels are (1, 10)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.