Shiro
Shiro

Reputation: 805

tensorflow TFRecord k-hot encoding

I am trying to create my training dataset with the TFRecord format following this tutorial : https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/using_your_own_dataset.md for API detection.

But, instead of using one hot encoding, I would like to use k-hot encoding. For example instead of having [0 0 0 1 0] labels, I can have [0 1 0 1 0] so multi-classification. I was wondering how to do that with the TFRecord format. Do I have to create two tf.train.example if I use 2-hot encoding ? (using two times the same bouding box coordinates) or is there another way ? (like using for example 'image/object/class/text': dataset_util.bytes_list_feature(classes_text), and 'image/object/class/text2': dataset_util.bytes_list_feature(classes_text2)) ?

Upvotes: 4

Views: 1295

Answers (1)

Sharky
Sharky

Reputation: 4533

Given that you have a list of labels like [0, 1, 2] and 10 classes, you need to

def int64_feature(value):
    if type(value) != list:
        value = [value]
    return tf.train.Feature(int64_list=tf.train.Int64List(value=value))

Then you pass label to tf.Example as one of the features

'label': int64_feature(label)

After that, when you parse your dataset during training, you decole label like this:

tf.reduce_max(tf.one_hot(labels, num_classes, dtype=tf.int32), axis=0)

Which gives

[1 1 1 0 0 0 0 0 0 0]

Upvotes: 1

Related Questions