Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added CosFace Loss #202

Merged
merged 8 commits into from
Apr 3, 2023
Merged

Added CosFace Loss #202

merged 8 commits into from
Apr 3, 2023

Conversation

crsdvaibhav
Copy link
Contributor

  • Added CosFace Loss, with tests and documentation.
  • Moved l2_norm from ArcFace Loss to quaterion.utils and updated the imports and __init__.py

@netlify
Copy link

netlify bot commented Mar 29, 2023

Deploy Preview for capable-unicorn-d5e336 ready!

Name Link
🔨 Latest commit 2908def
🔍 Latest deploy log https://app.netlify.com/sites/capable-unicorn-d5e336/deploys/642acc02405d61000886c068
😎 Deploy Preview https://deploy-preview-202--capable-unicorn-d5e336.netlify.app/_modules/quaterion/loss/arcface_loss
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@crsdvaibhav
Copy link
Contributor Author

@monatis All checks have passed. Please could you review it? Thanks!

Comment on lines +252 to +264
def l2_norm(inputs: torch.Tensor, dim: int = 0) -> torch.Tensor:
"""Apply L2 normalization to tensor

Args:
inputs: Input tensor.
dim: Dimension to operate on.

Returns:
torch.Tensor: L2-normalized tensor
"""
outputs = inputs / torch.norm(inputs, 2, dim, True)

return outputs
Copy link
Contributor

@KarahanS KarahanS Mar 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the documentation, torch.norm is deprecated and may be removed in the future. That would be better to use torch.linalg.norm instead here: outputs = inputs / torch.linalg.norm(inputs, dim=dim, ord=2, keepdim=True).

This can also be used: return torch.nn.functional.normalize(inputs, p=2, dim=dim)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I think it is better to create a separate issue and a consequent PR for it.

tests/eval/losses/test_cos_face_loss.py Outdated Show resolved Hide resolved
Co-authored-by: M. Yusuf Sarıgöz <yusufsarigoz@gmail.com>
@crsdvaibhav
Copy link
Contributor Author

crsdvaibhav commented Mar 30, 2023

@monatis Sorry, I must have missed changing it after reverting my commits. Thank you! Is there anything more before merging? Sorry for the late update. My health hasn't been keeping up for the past couple of days.

Copy link
Contributor

@monatis monatis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get well soon! Hope you feel better now.

The tests didn't pass again. See why in the review comments below. Also let's add that assertion to ArcFaceLoss as well, just to the beginning of .forward() method as in CosFaceLoss. Finally, please run tests locally with the pytest command.

tests/eval/losses/test_cos_face_loss.py Outdated Show resolved Hide resolved
quaterion/loss/cos_face_loss.py Show resolved Hide resolved
crsdvaibhav and others added 5 commits March 31, 2023 16:51
Co-authored-by: M. Yusuf Sarıgöz <yusufsarigoz@gmail.com>
Co-authored-by: M. Yusuf Sarıgöz <yusufsarigoz@gmail.com>
@crsdvaibhav
Copy link
Contributor Author

@monatis Ran the tests locally. Made the requested changes. Anything more, to be done? Also, thankyou for the help!
test_cos_face_loss

@monatis
Copy link
Contributor

monatis commented Apr 3, 2023

@crsdvaibhav ArcFace has the assertion missing, and CosFace has it twice. See also #203 (thanks @KarahanS)

@monatis monatis merged commit c588fe4 into qdrant:master Apr 3, 2023
4 checks passed
@crsdvaibhav
Copy link
Contributor Author

@crsdvaibhav ArcFace has the assertion missing, and CosFace has it twice. See also #203 (thanks @KarahanS)

I changed it. Thanks for the merge! I will move to #203

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants