Cysu / open-reid

Open source person re-identification library in python
https://cysu.github.io/open-reid/
MIT License
1.34k stars 349 forks source link

Combine two kinds of loss function #45

Closed wang5566 closed 6 years ago

wang5566 commented 6 years ago

I want to train my model using soft max Loss and triplet loss together. How can I combine two loss in a net together? Any reply will be appreciated.

Rizhiy commented 6 years ago

You need to write a new loss, similar to the one reid/loss/triplet.py and use that instead. Usually, you can just add losses together, just make sure to scale them since softmax can be much larger than triplet.

You would probably need to write your own data loader since softmax and triplet use different sampling techniques.

Cysu commented 6 years ago

@Rizhiy Thank you very much for the answer!

wang5566 commented 6 years ago

@Rizhiy Thank you for your reply. But how to scale this two loss? In my network, I got softmax loss 1.5 and triplet loss 1.27, seeming unusual.

Rizhiy commented 6 years ago

To be honest I have never tried to train a network with multi-loss. Perhaps train softmax and triplet loss separately first, then calculate the scale such that they become the same value.

zydou commented 6 years ago

You can add a parameter to control the balance between them. For example:

loss = loss1 + lambda * loss2 
Cysu commented 6 years ago

@Rizhiy @zydou Thanks a lot for your answers!