Spaces:
Sleeping
Sleeping
Delete SimSwap/models/models.py
Browse files- SimSwap/models/models.py +0 -177
SimSwap/models/models.py
DELETED
@@ -1,177 +0,0 @@
|
|
1 |
-
import math
|
2 |
-
import torch
|
3 |
-
import torch.nn.functional as F
|
4 |
-
from torch import nn
|
5 |
-
from torch.nn import Parameter
|
6 |
-
from .config import device, num_classes
|
7 |
-
|
8 |
-
|
9 |
-
def create_model(opt):
|
10 |
-
#from .pix2pixHD_model import Pix2PixHDModel, InferenceModel
|
11 |
-
from .fs_model import fsModel
|
12 |
-
model = fsModel()
|
13 |
-
|
14 |
-
model.initialize(opt)
|
15 |
-
if opt.verbose:
|
16 |
-
print("model [%s] was created" % (model.name()))
|
17 |
-
|
18 |
-
if opt.isTrain and len(opt.gpu_ids) and not opt.fp16:
|
19 |
-
model = torch.nn.DataParallel(model, device_ids=opt.gpu_ids)
|
20 |
-
|
21 |
-
return model
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
class SEBlock(nn.Module):
|
26 |
-
def __init__(self, channel, reduction=16):
|
27 |
-
super(SEBlock, self).__init__()
|
28 |
-
self.avg_pool = nn.AdaptiveAvgPool2d(1)
|
29 |
-
self.fc = nn.Sequential(
|
30 |
-
nn.Linear(channel, channel // reduction),
|
31 |
-
nn.PReLU(),
|
32 |
-
nn.Linear(channel // reduction, channel),
|
33 |
-
nn.Sigmoid()
|
34 |
-
)
|
35 |
-
|
36 |
-
def forward(self, x):
|
37 |
-
b, c, _, _ = x.size()
|
38 |
-
y = self.avg_pool(x).view(b, c)
|
39 |
-
y = self.fc(y).view(b, c, 1, 1)
|
40 |
-
return x * y
|
41 |
-
|
42 |
-
|
43 |
-
class IRBlock(nn.Module):
|
44 |
-
expansion = 1
|
45 |
-
|
46 |
-
def __init__(self, inplanes, planes, stride=1, downsample=None, use_se=True):
|
47 |
-
super(IRBlock, self).__init__()
|
48 |
-
self.bn0 = nn.BatchNorm2d(inplanes)
|
49 |
-
self.conv1 = conv3x3(inplanes, inplanes)
|
50 |
-
self.bn1 = nn.BatchNorm2d(inplanes)
|
51 |
-
self.prelu = nn.PReLU()
|
52 |
-
self.conv2 = conv3x3(inplanes, planes, stride)
|
53 |
-
self.bn2 = nn.BatchNorm2d(planes)
|
54 |
-
self.downsample = downsample
|
55 |
-
self.stride = stride
|
56 |
-
self.use_se = use_se
|
57 |
-
if self.use_se:
|
58 |
-
self.se = SEBlock(planes)
|
59 |
-
|
60 |
-
def forward(self, x):
|
61 |
-
residual = x
|
62 |
-
out = self.bn0(x)
|
63 |
-
out = self.conv1(out)
|
64 |
-
out = self.bn1(out)
|
65 |
-
out = self.prelu(out)
|
66 |
-
|
67 |
-
out = self.conv2(out)
|
68 |
-
out = self.bn2(out)
|
69 |
-
if self.use_se:
|
70 |
-
out = self.se(out)
|
71 |
-
|
72 |
-
if self.downsample is not None:
|
73 |
-
residual = self.downsample(x)
|
74 |
-
|
75 |
-
out += residual
|
76 |
-
out = self.prelu(out)
|
77 |
-
|
78 |
-
return out
|
79 |
-
|
80 |
-
|
81 |
-
class ResNet(nn.Module):
|
82 |
-
|
83 |
-
def __init__(self, block, layers, use_se=True):
|
84 |
-
self.inplanes = 64
|
85 |
-
self.use_se = use_se
|
86 |
-
super(ResNet, self).__init__()
|
87 |
-
self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, bias=False)
|
88 |
-
self.bn1 = nn.BatchNorm2d(64)
|
89 |
-
self.prelu = nn.PReLU()
|
90 |
-
self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2)
|
91 |
-
self.layer1 = self._make_layer(block, 64, layers[0])
|
92 |
-
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
|
93 |
-
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
|
94 |
-
self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
|
95 |
-
self.bn2 = nn.BatchNorm2d(512)
|
96 |
-
self.dropout = nn.Dropout()
|
97 |
-
self.fc = nn.Linear(512 * 7 * 7, 512)
|
98 |
-
self.bn3 = nn.BatchNorm1d(512)
|
99 |
-
|
100 |
-
for m in self.modules():
|
101 |
-
if isinstance(m, nn.Conv2d):
|
102 |
-
nn.init.xavier_normal_(m.weight)
|
103 |
-
elif isinstance(m, nn.BatchNorm2d) or isinstance(m, nn.BatchNorm1d):
|
104 |
-
nn.init.constant_(m.weight, 1)
|
105 |
-
nn.init.constant_(m.bias, 0)
|
106 |
-
elif isinstance(m, nn.Linear):
|
107 |
-
nn.init.xavier_normal_(m.weight)
|
108 |
-
nn.init.constant_(m.bias, 0)
|
109 |
-
|
110 |
-
def _make_layer(self, block, planes, blocks, stride=1):
|
111 |
-
downsample = None
|
112 |
-
if stride != 1 or self.inplanes != planes * block.expansion:
|
113 |
-
downsample = nn.Sequential(
|
114 |
-
nn.Conv2d(self.inplanes, planes * block.expansion,
|
115 |
-
kernel_size=1, stride=stride, bias=False),
|
116 |
-
nn.BatchNorm2d(planes * block.expansion),
|
117 |
-
)
|
118 |
-
|
119 |
-
layers = []
|
120 |
-
layers.append(block(self.inplanes, planes, stride, downsample, use_se=self.use_se))
|
121 |
-
self.inplanes = planes
|
122 |
-
for i in range(1, blocks):
|
123 |
-
layers.append(block(self.inplanes, planes, use_se=self.use_se))
|
124 |
-
|
125 |
-
return nn.Sequential(*layers)
|
126 |
-
|
127 |
-
def forward(self, x):
|
128 |
-
x = self.conv1(x)
|
129 |
-
x = self.bn1(x)
|
130 |
-
x = self.prelu(x)
|
131 |
-
x = self.maxpool(x)
|
132 |
-
|
133 |
-
x = self.layer1(x)
|
134 |
-
x = self.layer2(x)
|
135 |
-
x = self.layer3(x)
|
136 |
-
x = self.layer4(x)
|
137 |
-
|
138 |
-
x = self.bn2(x)
|
139 |
-
x = self.dropout(x)
|
140 |
-
x = x.view(x.size(0), -1)
|
141 |
-
x = self.fc(x)
|
142 |
-
x = self.bn3(x)
|
143 |
-
|
144 |
-
return x
|
145 |
-
|
146 |
-
|
147 |
-
class ArcMarginModel(nn.Module):
|
148 |
-
def __init__(self, args):
|
149 |
-
super(ArcMarginModel, self).__init__()
|
150 |
-
|
151 |
-
self.weight = Parameter(torch.FloatTensor(num_classes, args.emb_size))
|
152 |
-
nn.init.xavier_uniform_(self.weight)
|
153 |
-
|
154 |
-
self.easy_margin = args.easy_margin
|
155 |
-
self.m = args.margin_m
|
156 |
-
self.s = args.margin_s
|
157 |
-
|
158 |
-
self.cos_m = math.cos(self.m)
|
159 |
-
self.sin_m = math.sin(self.m)
|
160 |
-
self.th = math.cos(math.pi - self.m)
|
161 |
-
self.mm = math.sin(math.pi - self.m) * self.m
|
162 |
-
|
163 |
-
def forward(self, input, label):
|
164 |
-
x = F.normalize(input)
|
165 |
-
W = F.normalize(self.weight)
|
166 |
-
cosine = F.linear(x, W)
|
167 |
-
sine = torch.sqrt(1.0 - torch.pow(cosine, 2))
|
168 |
-
phi = cosine * self.cos_m - sine * self.sin_m # cos(theta + m)
|
169 |
-
if self.easy_margin:
|
170 |
-
phi = torch.where(cosine > 0, phi, cosine)
|
171 |
-
else:
|
172 |
-
phi = torch.where(cosine > self.th, phi, cosine - self.mm)
|
173 |
-
one_hot = torch.zeros(cosine.size(), device=device)
|
174 |
-
one_hot.scatter_(1, label.view(-1, 1).long(), 1)
|
175 |
-
output = (one_hot * phi) + ((1.0 - one_hot) * cosine)
|
176 |
-
output *= self.s
|
177 |
-
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|