Hello everyone,

I am trying to implement a face recognition model which uses triplet loss (like facenet). The loss function is not the actual problem, but I’m struggling to develop a proper triplet image generator, which feeds a batch into the neural network. The batch should contain multiple image triplets of respective positve, negative and one anchor identity (image).

I saw, that there is already a Gluon API functionality for the Triplet Loss (https://mxnet.incubator.apache.org/api/python/gluon/loss.html#mxnet.gluon.loss.TripletLoss) , but I did not find a way how to work (feed it) with images. So far, I got the following:

def test_triplet_loss():

np.random.seed(1234)

N = 20

data = mx.random.uniform(-1, 1, shape=(N, 10))

pos = mx.random.uniform(-1, 1, shape=(N, 10))

neg = mx.random.uniform(-1, 1, shape=(N, 10))

data_iter = mx.io.NDArrayIter(data, {‘pos’: pos, ‘neg’: neg}, batch_size=10,

label_name=‘label’, shuffle=True)

output = get_net(10)

pos = mx.symbol.Variable(‘pos’)

neg = mx.symbol.Variable(‘neg’)

Loss = gluon.loss.TripletLoss()

loss = Loss(output, pos, neg)

loss = mx.sym.make_loss(loss)

mod = mx.mod.Module(loss, data_names=(‘data’,), label_names=(‘pos’,‘neg’))

mod.fit(data_iter, num_epoch=200, optimizer_params={‘learning_rate’: 0.01},

initializer=mx.init.Xavier(magnitude=2), eval_metric=mx.metric.Loss(),

optimizer=‘adam’)

But this uses the mx.io.NDArrayIter and no actual images. So how can I feed triplet image batches to the network? There must be a way to select a positve, a negative and an anchor image for a proper triplet and concatenate multiple triplets within batches. I thought, that if we already have a suggestion for the triplet loss API, there might also be way to train a network based on this loss.

Did anyone ever tried to implement an image triplet generator in order to train a neural network within Mxnet? Or do you have an idea, how this could be done?

Thanks in advance and kind regards,

kepoggem