Search code examples
kerasdeep-learningneural-networksiamese-network

How to prepare dataset for siamese neural network?


I am little confused how to create dataset for siamese neural networks. I was following this tutorial on how to implement siamese networks with contrastive loss. They are creating positive and negative pairs, but that is relatively easy in their case because they have equal amount of images for each class and same digits will be positive pair, different digits will be negative.

There is also an example with triplet loss, they use dataset called Totally Looks Like, but in that dataset there are two folders left and right and each image in folder left has it's positive pair in folder right. Again, equal amount of positive and negative pairs.

But in my case, I have around 1000 unlabelled images (I will manually make pairs). One image can be similar to N number of other images, N can be 0 to len(dataset) (theoretically). So image can be unique or have any number of other similar images. How do I create pairs in that case? I don't have any kind of balance between classes. If five images are similar, do I create all possible pairs/combinations (by combinations I mean, for example, pairs 1,5 and 5,1 are two different pairs)? If image is unique, do I even use it in dataset?


Solution

  • If I understand you correctly, I can suggest what I did in the past. Take a set of unique images. These will be your anchor images. Let's say, they're 1000 in number. Then, create another set of images from the original set by adding slight distortions such as rotating, brightening, etc. but not too much. So they are the same objects but don't look like exactly. They will be your positive images. Then, create a third set of images by reversing the first set, so 1000th image in the last set will correspond to the first image in the original (anchor) set. These will be your negative images. This way, you can create all image pairs.