How to define custom sampler?

Hello guys!

I am trying to implement the paper In defense of triplet loss for person re-identification with fastai.

As per this paper, I want to be able to build a dataloader which samples k images per class and p classes per batch. In order to do this, I will have to define a custom sampler for my usecase.

How can I define a custom sampler in fastaiv2? I think I need to use the get_idxs function as mentioned by Jeremy here but what is the input to this function? This function will probably be an argument to the get_idxs parameter when creating a dataloader but I don’t know what the parameter arguments to the get_idxs function which I want to define would be…

Can anyone please guide me in this issue?

Thanks & Regards,
Vinayak.

Solved this!

In my particular context, I just had to pass in the dataframe which contained all the examples to the get_idxs function; then I simply got the indices of training samples and then created my custom sampler accordingly.

Thanks! Closing this thread.

3 Likes