EmotiEffLib
Python implementation of Emotion Efficient Library
Loading...
Searching...
No Matches
mobilefacenet.py
Go to the documentation of this file.
1"""
2Adapted from https://github.com/cavalleria/cavaface.pytorch/blob/master/backbone/mobilefacenet.py
3Original author cavalleria
4"""
5
6import torch
7import torch.nn as nn
8from torch.nn import BatchNorm1d, BatchNorm2d, Conv2d, Linear, Module, PReLU, Sequential
9
10
12 def forward(self, x):
13 return x.view(x.size(0), -1)
14
15
17 def __init__(self, in_c, out_c, kernel=(1, 1), stride=(1, 1), padding=(0, 0), groups=1):
18 super(ConvBlock, self).__init__()
19 self.layers = nn.Sequential(
20 Conv2d(in_c, out_c, kernel, groups=groups, stride=stride, padding=padding, bias=False),
21 BatchNorm2d(num_features=out_c),
22 PReLU(num_parameters=out_c),
23 )
24
25 def forward(self, x):
26 return self.layers(x)
27
28
30 def __init__(self, in_c, out_c, kernel=(1, 1), stride=(1, 1), padding=(0, 0), groups=1):
31 super(LinearBlock, self).__init__()
32 self.layers = nn.Sequential(
33 Conv2d(in_c, out_c, kernel, stride, padding, groups=groups, bias=False),
34 BatchNorm2d(num_features=out_c),
35 )
36
37 def forward(self, x):
38 return self.layers(x)
39
40
43 self, in_c, out_c, residual=False, kernel=(3, 3), stride=(2, 2), padding=(1, 1), groups=1
44 ):
45 super(DepthWise, self).__init__()
46 self.residual = residual
47 self.layers = nn.Sequential(
48 ConvBlock(in_c, out_c=groups, kernel=(1, 1), padding=(0, 0), stride=(1, 1)),
49 ConvBlock(groups, groups, groups=groups, kernel=kernel, padding=padding, stride=stride),
50 LinearBlock(groups, out_c, kernel=(1, 1), padding=(0, 0), stride=(1, 1)),
51 )
52
53 def forward(self, x):
54 short_cut = None
55 if self.residual:
56 short_cut = x
57 x = self.layers(x)
58 if self.residual:
59 output = short_cut + x
60 else:
61 output = x
62 return output
63
64
66 def __init__(self, c, num_block, groups, kernel=(3, 3), stride=(1, 1), padding=(1, 1)):
67 super(Residual, self).__init__()
68 modules = []
69 for _ in range(num_block):
70 modules.append(DepthWise(c, c, True, kernel, stride, padding, groups))
71 self.layers = Sequential(*modules)
72
73 def forward(self, x):
74 return self.layers(x)
75
76
77class GDC(Module):
78 def __init__(self, embedding_size):
79 super(GDC, self).__init__()
80 self.layers = nn.Sequential(
81 LinearBlock(512, 512, groups=512, kernel=(7, 7), stride=(1, 1), padding=(0, 0)),
82 Flatten(),
83 Linear(512, embedding_size, bias=False),
84 BatchNorm1d(embedding_size),
85 )
86
87 def forward(self, x):
88 return self.layers(x)
89
90
92 def __init__(self, fp16=False, num_features=512, blocks=(1, 4, 6, 2), scale=2):
93 super(MobileFaceNet, self).__init__()
94 self.scale = scale
95 self.fp16 = fp16
96 self.layers = nn.ModuleList()
97 self.layers.append(
98 ConvBlock(3, 64 * self.scale, kernel=(3, 3), stride=(2, 2), padding=(1, 1))
99 )
100 if blocks[0] == 1:
101 self.layers.append(
102 ConvBlock(
103 64 * self.scale,
104 64 * self.scale,
105 kernel=(3, 3),
106 stride=(1, 1),
107 padding=(1, 1),
108 groups=64,
109 )
110 )
111 else:
112 self.layers.append(
113 Residual(
114 64 * self.scale,
115 num_block=blocks[0],
116 groups=128,
117 kernel=(3, 3),
118 stride=(1, 1),
119 padding=(1, 1),
120 ),
121 )
122
123 self.layers.extend(
124 [
125 DepthWise(
126 64 * self.scale,
127 64 * self.scale,
128 kernel=(3, 3),
129 stride=(2, 2),
130 padding=(1, 1),
131 groups=128,
132 ),
133 Residual(
134 64 * self.scale,
135 num_block=blocks[1],
136 groups=128,
137 kernel=(3, 3),
138 stride=(1, 1),
139 padding=(1, 1),
140 ),
141 DepthWise(
142 64 * self.scale,
143 128 * self.scale,
144 kernel=(3, 3),
145 stride=(2, 2),
146 padding=(1, 1),
147 groups=256,
148 ),
149 Residual(
150 128 * self.scale,
151 num_block=blocks[2],
152 groups=256,
153 kernel=(3, 3),
154 stride=(1, 1),
155 padding=(1, 1),
156 ),
157 DepthWise(
158 128 * self.scale,
159 128 * self.scale,
160 kernel=(3, 3),
161 stride=(2, 2),
162 padding=(1, 1),
163 groups=512,
164 ),
165 Residual(
166 128 * self.scale,
167 num_block=blocks[3],
168 groups=256,
169 kernel=(3, 3),
170 stride=(1, 1),
171 padding=(1, 1),
172 ),
173 ]
174 )
175
177 128 * self.scale, 512, kernel=(1, 1), stride=(1, 1), padding=(0, 0)
178 )
179 self.features = GDC(num_features)
181
183 for m in self.modules():
184 if isinstance(m, nn.Conv2d):
185 nn.init.kaiming_normal_(m.weight, mode="fan_out", nonlinearity="relu")
186 if m.bias is not None:
187 m.bias.data.zero_()
188 elif isinstance(m, nn.BatchNorm2d):
189 m.weight.data.fill_(1)
190 m.bias.data.zero_()
191 elif isinstance(m, nn.Linear):
192 nn.init.kaiming_normal_(m.weight, mode="fan_out", nonlinearity="relu")
193 if m.bias is not None:
194 m.bias.data.zero_()
195
196 def forward(self, x):
197 with torch.cuda.amp.autocast(self.fp16):
198 for func in self.layers:
199 x = func(x)
200 x = self.conv_sep(x.float() if self.fp16 else x)
201 x = self.features(x)
202 return x
203
204
205def get_mbf(fp16, num_features, blocks=(1, 4, 6, 2), scale=2):
206 return MobileFaceNet(fp16, num_features, blocks, scale=scale)
207
208
209def get_mbf_large(fp16, num_features, blocks=(2, 8, 12, 4), scale=4):
210 return MobileFaceNet(fp16, num_features, blocks, scale=scale)
__init__(self, in_c, out_c, kernel=(1, 1), stride=(1, 1), padding=(0, 0), groups=1)
__init__(self, in_c, out_c, residual=False, kernel=(3, 3), stride=(2, 2), padding=(1, 1), groups=1)
__init__(self, in_c, out_c, kernel=(1, 1), stride=(1, 1), padding=(0, 0), groups=1)
__init__(self, fp16=False, num_features=512, blocks=(1, 4, 6, 2), scale=2)
__init__(self, c, num_block, groups, kernel=(3, 3), stride=(1, 1), padding=(1, 1))
get_mbf_large(fp16, num_features, blocks=(2, 8, 12, 4), scale=4)
get_mbf(fp16, num_features, blocks=(1, 4, 6, 2), scale=2)