-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Applying Center Loss * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fixed minor typos and errors + Center loss implementation * sphinx version change --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
2371ddf
commit db4f455
Showing
11 changed files
with
126 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
quaterion.loss.center\_loss module | ||
================================== | ||
|
||
.. automodule:: quaterion.loss.center_loss | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
quaterion.loss.circle\_loss module | ||
================================== | ||
|
||
.. automodule:: quaterion.loss.circle_loss | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
quaterion.loss.cos\_face\_loss module | ||
===================================== | ||
|
||
.. automodule:: quaterion.loss.cos_face_loss | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
quaterion.loss.fast\_ap\_loss module | ||
==================================== | ||
|
||
.. automodule:: quaterion.loss.fast_ap_loss | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from typing import Optional | ||
|
||
import torch | ||
import torch.nn as nn | ||
import torch.nn.functional as F | ||
from torch import LongTensor, Tensor | ||
|
||
from quaterion.loss.group_loss import GroupLoss | ||
from quaterion.utils import l2_norm | ||
|
||
|
||
class CenterLoss(GroupLoss): | ||
""" | ||
Center Loss as defined in the paper "A Discriminative Feature Learning Approach | ||
for Deep Face Recognition" (http://ydwen.github.io/papers/WenECCV16.pdf) | ||
It aims to minimize the intra-class variations while keeping the features of | ||
different classes separable. | ||
Args: | ||
embedding_size: Output dimension of the encoder. | ||
num_groups: Number of groups (classes) in the dataset. | ||
lambda_c: A regularization parameter that controls the contribution of the center loss. | ||
""" | ||
|
||
def __init__( | ||
self, embedding_size: int, num_groups: int, lambda_c: Optional[float] = 0.5 | ||
): | ||
super(GroupLoss, self).__init__() | ||
self.num_groups = num_groups | ||
self.centers = nn.Parameter(torch.randn(num_groups, embedding_size)) | ||
self.lambda_c = lambda_c | ||
|
||
nn.init.xavier_uniform_(self.centers) | ||
|
||
def forward(self, embeddings: Tensor, groups: LongTensor) -> Tensor: | ||
""" | ||
Compute the Center Loss value. | ||
Args: | ||
embeddings: shape (batch_size, vector_length) - Output embeddings from the encoder. | ||
groups: shape (batch_size,) - Group (class) ids associated with embeddings. | ||
Returns: | ||
Tensor: loss value. | ||
""" | ||
embeddings = l2_norm(embeddings, 1) | ||
|
||
# Gather the center for each embedding's corresponding group | ||
centers_batch = self.centers.index_select(0, groups) | ||
|
||
# Calculate the distance between embeddings and their respective class centers | ||
loss = F.mse_loss(embeddings, centers_batch) | ||
|
||
# Scale the loss by the regularization parameter | ||
loss *= self.lambda_c | ||
|
||
return loss |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import torch | ||
|
||
from quaterion.loss import CenterLoss | ||
|
||
|
||
class TestCenterLoss: | ||
embeddings = torch.Tensor( | ||
[ | ||
[0.0, -1.0, 0.5], | ||
[0.1, 2.0, 0.5], | ||
[0.0, 0.3, 0.2], | ||
[1.0, 0.0, 0.9], | ||
[1.2, -1.2, 0.01], | ||
[-0.7, 0.0, 1.5], | ||
] | ||
) | ||
groups = torch.LongTensor([1, 2, 0, 0, 2, 1]) | ||
|
||
def test_batch_all(self): | ||
# Initialize the CenterLoss | ||
loss = CenterLoss(embedding_size=self.embeddings.size()[1], num_groups=3) | ||
|
||
# Calculate the loss | ||
loss_res = loss.forward(embeddings=self.embeddings, groups=self.groups) | ||
|
||
# Assertions to check the output shape and type | ||
assert isinstance( | ||
loss_res, torch.Tensor | ||
), "Loss result should be a torch.Tensor" | ||
assert loss_res.shape == torch.Size( | ||
[] | ||
), "Loss result should be a scalar (0-dimension tensor)" |