-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmobileFaceNet.py
More file actions
169 lines (140 loc) · 5.42 KB
/
Copy pathmobileFaceNet.py
File metadata and controls
169 lines (140 loc) · 5.42 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# import torch
# import torch.nn as nn
# class Flatten(nn.Module):
# def forward(self, x):
# return x.view(x.size(0), -1)
# class MobileFaceNet(nn.Module):
# def __init__(self, embedding_size=128):
# super(MobileFaceNet, self).__init__()
# self.features = nn.Sequential(
# nn.Conv2d(3, 64, 3, 2, 1), nn.PReLU(),
# nn.Conv2d(64, 64, 3, 1, 1), nn.BatchNorm2d(64), nn.PReLU(),
# nn.Conv2d(64, 128, 3, 2, 1), nn.BatchNorm2d(128), nn.PReLU(),
# nn.AdaptiveAvgPool1d(1),
# Flatten(),
# )
# self.embedding = nn.Linear(128, embedding_size)
# def forward(self, x):
# x = self.features(x)
# x = self.embedding(x)
# return x/x.norm(dim=1, keepdim=True)
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import Parameter
import math
class Bottleneck(nn.Module):
def __init__(self, inp, oup, stride, expansion):
super(Bottleneck, self).__init__()
self.connect = stride == 1 and inp == oup
self.conv = nn.Sequential(
nn.Conv2d(inp, inp * expansion, 1, 1, 0, bias=False),
nn.BatchNorm2d(inp * expansion),
nn.PReLU(inp * expansion),
nn.Conv2d(inp * expansion, inp * expansion, 3, stride, 1, groups=inp * expansion, bias=False),
nn.BatchNorm2d(inp * expansion),
nn.PReLU(inp * expansion),
nn.Conv2d(inp * expansion, oup, 1, 1, 0, bias=False),
nn.BatchNorm2d(oup),
)
def forward(self, x):
if self.connect:
return x + self.conv(x)
else:
return self.conv(x)
class ConvBlock(nn.Module):
def __init__(self, inp, oup, k, s, p, dw=False, linear=False):
super(ConvBlock, self).__init__()
self.linear = linear
if dw:
self.conv = nn.Conv2d(inp, oup, k, s, p, groups=inp, bias=False)
else:
self.conv = nn.Conv2d(inp, oup, k, s, p, bias=False)
self.bn = nn.BatchNorm2d(oup)
if not linear:
self.prelu = nn.PReLU(oup)
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
if self.linear:
return x
else:
return self.prelu(x)
Mobilefacenet_bottleneck_setting = [
[2, 64, 5, 2],
[4, 128, 1, 2],
[2, 128, 6, 1],
[4, 128, 1, 2],
[2, 128, 2, 1]
]
class MobileFaceNet(nn.Module):
def __init__(self, bottleneck_setting=Mobilefacenet_bottleneck_setting, embedding_size=128):
super(MobileFaceNet, self).__init__()
self.conv1 = ConvBlock(3, 64, 3, 2, 1)
self.dw_conv1 = ConvBlock(64, 64, 3, 1, 1, dw=True)
self.inplanes = 64
block = Bottleneck
self.blocks = self._make_layer(block, bottleneck_setting)
self.conv2 = ConvBlock(128, 512, 1, 1, 0)
self.linear7 = ConvBlock(512, 512, (7, 6), 1, 0, dw=True, linear=True)
self.linear1 = ConvBlock(512, embedding_size, 1, 1, 0, linear=True)
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def _make_layer(self, block, setting):
layers = []
for t, c, n, s in setting:
for i in range(n):
stride = s if i == 0 else 1
layers.append(block(self.inplanes, c, stride, t))
self.inplanes = c
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.dw_conv1(x)
x = self.blocks(x)
x = self.conv2(x)
x = self.linear7(x)
x = self.linear1(x)
x = x.view(x.size(0), -1)
return x / x.norm(dim=1, keepdim=True)
class ArcMarginProduct(nn.Module):
def __init__(self, in_features=128, out_features=200, s=32.0, m=0.50, easy_margin=False):
super(ArcMarginProduct, self).__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)
self.easy_margin = easy_margin
self.cos_m = math.cos(m)
self.sin_m = math.sin(m)
self.th = math.cos(math.pi - m)
self.mm = math.sin(math.pi - m) * m
def forward(self, x, label):
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
if self.easy_margin:
phi = torch.where(cosine > 0, phi, cosine)
else:
phi = torch.where((cosine - self.th) > 0, phi, cosine - self.mm)
one_hot = torch.zeros(cosine.size(), device=x.device)
one_hot.scatter_(1, label.view(-1, 1).long(), 1)
output = (one_hot * phi) + ((1.0 - one_hot) * cosine)
output *= self.s
return output
if __name__ == "__main__":
input = torch.randn(2, 3, 112, 96)
model = MobileFaceNet()
arcface = ArcMarginProduct(in_features=128, out_features=200)
feats = model(input) # embeddings
labels = torch.tensor([0, 1])
logits = arcface(feats, labels)
print("Embeddings shape:", feats.shape)
print("Logits shape:", logits.shape)