omoindrot / tensorflow-triplet-loss

Implementation of triplet loss in TensorFlow
https://omoindrot.github.io/triplet-loss
MIT License
1.12k stars 283 forks source link

Issues about label dimension #22

Closed stoneyang closed 6 years ago

stoneyang commented 6 years ago

Hi, @omoindrot

Thanks for sharing your code! And I am right now crafting a small project using triplet loss.

I've cloned the code, run it, and all fine. But when applying to my code, in a mini-batch context, code went crack at this line. The shapes of tensors were examined and compared to this mnist example. I found dimension of label is the most possible reason.

Hope you could shed me some light. I am really stucked.... Thanks in advance.

Following is the error message I got:

ValueError: Dimensions must be equal, but are 82398 and 100 for 'Mul' (op: 'Mul') with input shapes: [100,100,100,82398], [100,100,100].

Next the shapes of tensors: In my code (batch_size is 100, number of classes is 82398):

NFO:tensorflow:pairwise_dist: (100, 100)
INFO:tensorflow:labels: (100, 82398)
INFO:tensorflow:embeddings: (100, 512)
INFO:tensorflow:anchor_p_dist: (100, 100, 1)
INFO:tensorflow:anchor_n_dist: (100, 1, 100)
INFO:tensorflow:indices_equal: (?, ?)
INFO:tensorflow:indices_not_equal: (?, ?)
INFO:tensorflow:i_not_equal_j: (?, ?, 1)
INFO:tensorflow:i_not_equal_k: (?, 1, ?)
INFO:tensorflow:j_not_equal_k: (1, ?, ?)
INFO:tensorflow:label_equal: (100, 100, 82398)
INFO:tensorflow:i_equal_j: (100, 100, 1, 82398)
INFO:tensorflow:i_equal_k: (100, 1, 100, 82398)
INFO:tensorflow:distinct_indices: (?, ?, ?)
INFO:tensorflow:valid_labels: (100, 100, 100, 82398)
INFO:tensorflow:mask: (100, 100, 100, 82398)
INFO:tensorflow:triplet_loss: (100, 100, 100)

In your code:

INFO:tensorflow:pairwise_dist: (?, ?)
INFO:tensorflow:labels: (?,)
INFO:tensorflow:embeddings: (?, 64)
INFO:tensorflow:anchor_p_dist: (?, ?, 1)
INFO:tensorflow:anchor_n_dist: (?, 1, ?)
INFO:tensorflow:indices_equal: (?, ?)
INFO:tensorflow:indices_not_equal: (?, ?)
INFO:tensorflow:i_not_equal_j: (?, ?, 1)
INFO:tensorflow:i_not_equal_k: (?, 1, ?)
INFO:tensorflow:j_not_equal_k: (1, ?, ?)
INFO:tensorflow:label_equal: (?, ?)
INFO:tensorflow:i_equal_j: (?, ?, 1)
INFO:tensorflow:i_equal_k: (?, 1, ?)
INFO:tensorflow:distinct_indices: (?, ?, ?)
INFO:tensorflow:valid_labels: (?, ?, ?)
INFO:tensorflow:mask: (?, ?, ?)
INFO:tensorflow:triplet_loss: (?, ?, ?)
omoindrot commented 6 years ago

I guess your labels are in one-hot format, with shape (100, 82398).

In my code the labels are in integer format like this:

labels = [1, 9, 3, 2, 4, 5, ...]

Also if you have a high number of classes you need to make sure to have multiple instances of the same class in your batch (otherwise no triplet will be found).

omoindrot commented 6 years ago

cf. issue #7 for the idea of balanced batches

stoneyang commented 6 years ago

I guess your labels are in one-hot format, with shape (100, 82398).

Yes, you are right, labels in my code is in one-hot format. In my code the labels are in integer format like this:

labels = [1, 9, 3, 2, 4, 5, ...]

I get it. :) Also if you have a high number of classes you need to make sure to have multiple instances of the same class in your batch (otherwise no triplet will be found).

I generated sample list and load them every batch_size line. Thanks for your reminder. :)

stoneyang commented 6 years ago

cf. issue #7 for the idea of balanced batches

Thanks for your pointer and I will be back once this issue get solved.

stoneyang commented 6 years ago

@omoindrot , I'd solved this problem in my code and all seems fine right now. Just before one_hot_encoding, the labels vector is in original form, which is required from triplet loss functions.

This issue seems finished and I will close it right now.