My ArcFace loss:
import math
class ArcFaceLoss(nn.Module):
def __init__(self, num_classes, embedding_size=64, margin=0.5, scale=64):
super(ArcFaceLoss, self).__init__()
self.num_classes = num_classes
self.embedding_size = embedding_size
self.margin = margin
self.scale = scale
self.cos_m = math.cos(margin)
self.sin_m = math.sin(margin)
self.threshold = math.cos(math.pi - margin)
self.mm = self.sin_m * margin
self.weight = nn.Parameter(torch.FloatTensor(num_classes, embedding_size))
nn.init.xavier_uniform_(self.weight)
def forward(self, x, labels):
cosine = F.linear(F.normalize(x), F.normalize(self.weight))
sine = torch.sqrt(1.0 - torch.pow(cosine, 2))
phi = cosine * self.cos_m - sine * self.sin_m
phi = torch.where(cosine > self.threshold, phi, cosine - self.mm)
one_hot = torch.zeros(cosine.size(), device=x.device)
one_hot.scatter_(1, labels.view(-1, 1).long(), 1)
output = (one_hot * phi) + ((1.0 - one_hot) * cosine)
output *= self.scale
return output
Getting this error:
Input In [89], in ArcFaceLoss.forward(self, x, labels)
17 def forward(self, x, labels):
—> 18 cosine = F.linear(F.normalize(x), F.normalize(self.weight))
19 sine = torch.sqrt(1.0 - torch.pow(cosine, 2))
20 phi = cosine * self.cos_m - sine * self.sin_mRuntimeError: mat1 and mat2 shapes cannot be multiplied (64x10171 and 64x10171)
mat1 and mat2 have same shape. If I use transpose it work, but I dont know is it right and after this I get this error:
Input In [136], in ArcFaceLoss.forward(self, x, labels)
21 phi = torch.where(cosine > self.threshold, phi, cosine - self.mm)
22 one_hot = torch.zeros(cosine.size(), device=x.device)
—> 23 one_hot.scatter_(1, labels.view(-1, 1).long(), 1)
24 output = (one_hot * phi) + ((1.0 - one_hot) * cosine)
25 output *= self.scaleRuntimeError: index 4601 is out of bounds for dimension 1 with size 64
I know how to fix it, but i’m afraid that it’s somehow ruind my ArcFace loss.
When writing the function I relied on this notebook pytorch-losses