TF DATA API: How to produce tensorflow input to object set recognition

ArthurSeat

Consider this problem: select a random number of samples from a random subject in an image dataset (like ImageNet) as an input element for Tensorflow graph which functions as an object set recognizer. For each batch, each class has a same number of samples to facilitate computation. But a different batch would have a different number of images for one class, i.e. batch_0:num_imgs_per_cls=2; batch_1000:num_imgs_per_cls=3.

If there is existing functionality in Tensorflow, explanation for the whole process from scratch (like from directories of images) will be really appreciated.

Olivier Moindrot

There is a very similar answer by @mrry here.

Sampling balanced batches

In face recognition we often use triplet loss (or similar losses) to train the model. The usual way to sample triplets to compute the loss is to create a balanced batch of images where we have for instance 10 different classes (i.e. 10 different people) with 5 images each. This gives a total batch size of 50 in this example.

More generally the problem is to sample num_classes_per_batch (10 in the example) classes, and then sample num_images_per_class (5 in the example) images for each class. The total batch size is:

batch_size = num_classes_per_batch * num_images_per_class

Have one dataset for each class

The easiest way to deal with a lot of different classes (100,000 in MS-Celeb) is to create one dataset for each class.
For instance you can have one tfrecord for each class and create the datasets like this:

# Build one dataset per class.
filenames = ["class_0.tfrecords", "class_1.tfrecords"...]
per_class_datasets = [tf.data.TFRecordDataset(f).repeat(None) for f in filenames]

Sample from the datasets

Now we would like to be able to sample from these datasets. For instance we want the following labels in our batch:

1 1 1 3 3 3 9 9 9 4 4 4

This corresponds to num_classes_per_batch=4 and num_images_per_class=3.

To do this we will need to use features that will be released in r1.9. The function should be called tf.contrib.data.choose_from_datasets (see here for a discussion on this).
It should look like:

def choose_from_datasets(datasets, selector):
    """Chooses elements with indices from selector among the datasets in `datasets`."""

So we create this selector which will output 1 1 1 3 3 3 9 9 9 4 4 4 and combine it with datasets to obtain our final dataset that will output balanced batches:

def generator(_):
    # Sample `num_classes_per_batch` classes for the batch
    sampled = tf.random_shuffle(tf.range(num_classes))[:num_classes_per_batch]
    # Repeat each element `num_images_per_class` times
    batch_labels = tf.tile(tf.expand_dims(sampled, -1), [1, num_images_per_class])
    return tf.to_int64(tf.reshape(batch_labels, [-1]))

selector = tf.contrib.data.Counter().map(generator)
selector = selector.apply(tf.contrib.data.unbatch())

dataset = tf.contrib.data.choose_from_datasets(datasets, selector)

# Batch
batch_size = num_classes_per_batch * num_images_per_class
dataset = dataset.batch(batch_size)

You can test this with the nightly TensorFlow build and by using DirectedInterleaveDataset as a workaround:

# The working option right now is 
from tensorflow.contrib.data.python.ops.interleave_ops import DirectedInterleaveDataset
dataset = DirectedInterleaveDataset(selector, datasets)

I also wrote about this workaround here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I create padded batches in Tensorflow for tf.train.SequenceExample data using the DataSet API?

How to import an saved Tensorflow model train using tf.estimator and predict on input data

How can I change the default output, input tensor names in tensorflow object detection API?

How to get a position of custom object on image using vision recognition api

How to set the input of a Keras layer with a Tensorflow tensor?

how to set input of Tensorflow Lite C++

Data Augmentation in Tensorflow Object Detection API

How to load pickle files by tensorflow's tf.data API

Tensorflow: FailedPreconditionError: Table not initialized (using tf.data.Dataset API)

How to use the tf.case api of TensorFlow correctly?

Tensorflow tf.data.Dataset API, dataset unzip function?

Use of tf.data.Dataset with Keras input layer on Tensorflow 2.0

use tensorflow object detection API for gender recognition

how to convert a set of values in tensorflow using tf.case ?

TensorFlow tf.data.Dataset API for medical imaging

How to shuffle data at each epoch using tf.data API in TensorFlow 2.0?

How to impove the speed of tf.data.experimental.CsvDataset in tensorflow 1.13.1?

How to save tf.data.Dataset object?

How to set the input of a keras subclass model in tensorflow?

Tensorflow - How to handle sequence input data (Sequence Data Input Layer)

TensorFlow 2-tf.keras: How to train a tf.keras multi-task network like MTCNN using tf.data API & TFRecords

How to use TensorFlow tf.train.string_input_producer to produce several epochs data?

How to expand tf.data.Dataset with additional example transformations in Tensorflow

tensorflow object detection API: generate TF record of custom data set

How to aggregate information from a data-set to produce a result table

How to properly restore a checkpoint in TensorFlow object detection API with TF1?

Using tf.data.Dataset to produce multi-input data

How to set minimum learning rate inside pipeline.config (tensorflow 1.15 + object detection API)

How can I use a tensorflow data set (TDFS) as an input for a tensorflow model?