-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nets_1.py
132 lines (107 loc) · 4.59 KB
/
Nets_1.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import torch.nn as nn
import torch
import torch.nn.functional as F
class Block(nn.Module):
'''Depthwise conv + Pointwise conv'''
def __init__(self, in_planes, out_planes, k=3):
super(Block, self).__init__()
self.conv1 = nn.Conv2d(in_planes, in_planes, kernel_size=3, stride=1, padding=1, groups=in_planes, bias=False)
self.bn1 = nn.BatchNorm2d(in_planes)
self.conv2 = nn.Conv2d(in_planes, out_planes, kernel_size=k, stride=1, padding=0, bias=False)
self.bn2 = nn.BatchNorm2d(out_planes)
def forward(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = F.relu(self.bn2(self.conv2(out)))
return out
class PNet(nn.Module):
cfg = [10, 16, 32]
def __init__(self):
super().__init__()
self.layer1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(32)
self.layers = self._make_layers(in_planes=32)
self.conv1 = nn.Sequential(
nn.Conv2d(32, 1, kernel_size=1, stride=1),
nn.Sigmoid(),
)
self.conv2 = nn.Conv2d(32, 4, kernel_size=1, stride=1)
self.conv3 = nn.Conv2d(32, 10, kernel_size=1, stride=1)
def _make_layers(self, in_planes):
layers = []
for x in self.cfg:
out_planes = x if isinstance(x, int) else x[0] # isinstance为内联函数,判断一个对象是否是一个已知的类型。
k = 3 if isinstance(x, int) else x[1]
layers.append(Block(in_planes, out_planes, k))
in_planes = out_planes
return nn.Sequential(*layers)
def forward(self, x):
x = F.relu(self.bn1(self.layer1(x)))
out = self.layers(x)
out = F.max_pool2d(out, 2)
cond = self.conv1(out)
offset = self.conv2(out)
ldmk_off = self.conv3(out)
return cond, offset, ldmk_off
class RNet(nn.Module):
cfg = [28, 48, (64, 2)]
def __init__(self):
super().__init__()
self.layer1 = nn.Conv2d(3, 28, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(28)
self.layers = self._make_layers(in_planes=28)
self.linear1 = nn.Linear(64 * 2 * 2, 128)
self.relu = nn.PReLU()
self.linear2 = nn.Linear(128, 1)
self.linear3 = nn.Linear(128, 4)
self.linear4 = nn.Linear(128, 10)
def _make_layers(self, in_planes):
layers = []
for x in self.cfg:
out_planes = x if isinstance(x, int) else x[0] # isinstance为内联函数,判断一个对象是否是一个已知的类型。
k = 3 if isinstance(x, int) else x[1]
layers.append(Block(in_planes, out_planes, k))
in_planes = out_planes
return nn.Sequential(*layers)
def forward(self, x):
x = F.relu(self.bn1(self.layer1(x)))
x = self.layers(x)
x = F.max_pool2d(x, 2)
x = x.view(x.size(0), -1)
# 讲多维度的图片转换成一维数据,-1是形状自动给,常放在卷积网络和线性网络中间
x = self.linear1(x)
x = self.relu(x)
cond = torch.sigmoid(self.linear2(x))
offset = self.linear3(x)
ldmk_off = self.linear4(x)
return cond, offset, ldmk_off
class ONet(nn.Module):
cfg = [32, 64, 64, (128, 2)]
def __init__(self):
super().__init__()
self.layer1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(32)
self.layers = self._make_layers(in_planes=32)
self.linear1 = nn.Linear(128 * 2 * 2, 256)
self.relu = nn.PReLU()
self.linear2 = nn.Linear(256, 1)
self.linear3 = nn.Linear(256, 4)
self.linear4 = nn.Linear(256, 10)
def _make_layers(self, in_planes):
layers = []
for x in self.cfg:
out_planes = x if isinstance(x, int) else x[0] # isinstance为内联函数,判断一个对象是否是一个已知的类型。
k = 3 if isinstance(x, int) else x[1]
layers.append(Block(in_planes, out_planes, k))
in_planes = out_planes
return nn.Sequential(*layers)
def forward(self, x):
x = F.relu(self.bn1(self.layer1(x)))
x = self.layers(x)
x = F.max_pool2d(x, 2)
x = x.view(x.size(0), -1)
x = self.linear1(x)
x = self.relu(x)
cond = torch.sigmoid(self.linear2(x))
offset = self.linear3(x)
ldmk_off = self.linear4(x)
return cond, offset, ldmk_off