-
Notifications
You must be signed in to change notification settings - Fork 2
/
nets.py
109 lines (101 loc) · 3.52 KB
/
nets.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
98
99
100
101
102
103
104
105
106
107
108
109
import torch
import torch.nn as nn
class PNet(nn.Module):
def __init__(self):
super(PNet, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(3,10,(3,3),1),
nn.BatchNorm2d(10),
nn.PReLU(),
nn.MaxPool2d(2,2),#N 10 5 5
)
self.conv2 = nn.Sequential(
nn.Conv2d(10, 16,(3,3), 1), #N 16 3 3
nn.BatchNorm2d(16),
nn.PReLU(),
)
self.conv3 = nn.Sequential(
nn.Conv2d(16, 32,(3,3), 1), #N 32 1 1
nn.BatchNorm2d(32),
nn.PReLU(),
)
self.conv4_1 = nn.Conv2d(32, 1, kernel_size=1, stride=1)
self.conv4_2 = nn.Conv2d(32, 4, kernel_size=1, stride=1)
self.conv4_3 = nn.Conv2d(32, 10, kernel_size=1, stride=1)
def forward(self,x):
y = self.conv3(self.conv2(self.conv1(x)))
cls = torch.sigmoid(self.conv4_1(y))
offset = self.conv4_2(y)
# landmark = self.conv4_3(y)
return cls,offset
class RNet(nn.Module):
def __init__(self):
super(RNet, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(3, 28, (3,3), 1),#N 28 48 48
nn.BatchNorm2d(28),
nn.PReLU(),
nn.MaxPool2d(3, 2 ,1),#N 28 11 11
)
self.conv2 = nn.Sequential(
nn.Conv2d(28, 48, (3,3), 1),#N 48 9 9
nn.BatchNorm2d(48),
nn.PReLU(),
nn.MaxPool2d(3,2,0),#N 48 4 4
)
self.conv3 = nn.Sequential(
nn.Conv2d(48, 64, (2,2), 1), #N 64 3 3
nn.BatchNorm2d(64),
nn.PReLU())
self.linear = nn.Linear(64 * 3 * 3, 128)
self.prlu = nn.PReLU()
self.linear_1 = nn.Linear(128, 1)
self.linear_2 = nn.Linear(128, 4)
self.linear_3 = nn.Linear(128, 10)
def forward(self, x):
y = self.conv3(self.conv2(self.conv1(x)))
y = torch.reshape(y, [y.size(0), -1])
y = self.linear(y)
y = self.prlu(y)
cls = torch.sigmoid(self.linear_1(y))
offset = self.linear_2(y)
# landmark = self.linear_3(y)
return cls, offset
class ONet(nn.Module):
def __init__(self):
super(ONet, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(3, 32,(3,3), 1 ),#N 32 48 48
nn.BatchNorm2d(32),
nn.PReLU(),
nn.MaxPool2d(3, 2, 1),#N 32 23 23
)
self.conv2 = nn.Sequential(
nn.Conv2d(32, 64, (3,3), 1),#N 64 21 21
nn.BatchNorm2d(64),
nn.PReLU(),
nn.MaxPool2d(3, 2),#N 64 10 10
)
self.conv3 = nn.Sequential(
nn.Conv2d(64, 64,(3,3), 1),#N 64 8 8
nn.BatchNorm2d(64),
nn.PReLU(),
nn.MaxPool2d(2, 2),#N 64 4 4
)
self.conv4 = nn.Sequential(
nn.Conv2d(64, 128, (2,2), 1),#N 128 3 3
nn.BatchNorm2d(128),
nn.PReLU())
self.linear = nn.Linear(128 * 3 * 3, 256)
self.prlu = nn.PReLU()
self.linear_1 = nn.Linear(256, 1)
self.linear_2 = nn.Linear(256, 4)
self.linear_3 = nn.Linear(256, 10)
def forward(self, x):
y = self.conv4(self.conv3(self.conv2(self.conv1(x))))
y = torch.reshape(y, [y.size(0), -1])
y = self.linear(y)
cls = torch.sigmoid(self.linear_1(y))
offset = self.linear_2(y)
# landmark = self.linear_3(y)
return cls, offset