-
Notifications
You must be signed in to change notification settings - Fork 0
/
classifiers.py
98 lines (83 loc) · 3.69 KB
/
classifiers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import math
import torch
import torch.nn as nn
from torch.nn import Parameter
import torch.nn.functional as F
# Based on https://github.com/ronghuaiyang/arcface-pytorch/blob/master/models/metrics.py#L10
class AAMC(nn.Module):
def __init__(self, in_features, out_features, s=30.0, m=0.50):
"""Implementation of Additive Angular Margin Classifier:
Args:
in_features: size of each input sample
out_features: size of each output sample
s: norm of input feature
m: margin
"""
super().__init__()
self.in_features = in_features
self.out_features = out_features
self.s = s
self.m = m
self.weight = Parameter(torch.FloatTensor(out_features, in_features))
nn.init.xavier_uniform_(self.weight)
self.cos_m = math.cos(m)
self.sin_m = math.sin(m)
def forward(self, input, label):
# --------------------------- cos(theta) & phi(theta) ---------------------------
cosine = F.linear(F.normalize(input), F.normalize(self.weight))
sine = torch.sqrt((1.0 - torch.pow(cosine, 2)).clamp(0, 1))
phi = cosine * self.cos_m - sine * self.sin_m
phi = torch.where(cosine > 0, phi, cosine)
# --------------------------- convert label to one-hot ---------------------------
# one_hot = torch.zeros(cosine.size(), requires_grad=True, device='cuda')
one_hot = torch.zeros_like(cosine)
one_hot.scatter_(1, label.view(-1, 1).long(), 1)
# -------------torch.where(out_i = {x_i if condition_i else y_i) -------------
output = (one_hot * phi) + ((1.0 - one_hot) * cosine) # you can use torch.where if your torch.__version__ is 0.4
output *= self.s
# print(output)
return output, cosine
# Based on https://github.com/MuggleWang/CosFace_pytorch/blob/master/layer.py
class LMCC(nn.Module):
def __init__(self, in_features, out_features, s=30.0, m=0.40):
"""Implementation of Large Margin Cosine Classifier:
Args:
in_features: size of each input sample
out_features: size of each output sample
s: norm of input feature
m: margin
"""
super().__init__()
self.in_features = in_features
self.out_features = out_features
self.s = s
self.m = m
self.weight = Parameter(torch.Tensor(out_features, in_features))
nn.init.xavier_uniform_(self.weight)
@staticmethod
def cosine_sim(x1, x2, dim=1, eps=1e-8):
ip = torch.mm(x1, x2.t())
w1 = torch.norm(x1, 2, dim)
w2 = torch.norm(x2, 2, dim)
return ip / torch.ger(w1,w2).clamp(min=eps), ip
def forward(self, input, label):
cosine, prod = self.cosine_sim(input, self.weight)
one_hot = torch.zeros_like(cosine)
one_hot.scatter_(1, label.view(-1, 1), 1.0)
output = self.s * (cosine - one_hot * self.m)
return output, prod
def __repr__(self):
return self.__class__.__name__ + '(' \
+ 'in_features=' + str(self.in_features) \
+ ', out_features=' + str(self.out_features) \
+ ', s=' + str(self.s) \
+ ', m=' + str(self.m) + ')'
class LinearLayer(nn.Module):
def __init__(self, in_features, out_features):
super().__init__()
self.linear = torch.nn.Linear(in_features, out_features)
def forward(self, inputs, label=None):
"""param 'label' is not used, but having it makes it easier to replace LMCC with
LinearLayer in other parts of the code, instead of having lots of if-else"""
output = self.linear(inputs)
return output, output