-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathCompenNetModel.py
75 lines (61 loc) · 2.34 KB
/
CompenNetModel.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
'''
CompenNet CNN structure
'''
import torch
import torch.nn as nn
class CompenNet(nn.Module):
def __init__(self):
super(CompenNet, self).__init__()
self.name = 'CompenNet'
self.relu = nn.ReLU()
# backbone branch
self.conv1 = nn.Conv2d(3, 32, 3, 2, 1)
self.conv2 = nn.Conv2d(32, 64, 3, 2, 1)
self.conv3 = nn.Conv2d(64, 128, 3, 1, 1)
self.conv4 = nn.Conv2d(128, 256, 3, 1, 1)
self.conv5 = nn.Conv2d(256, 128, 3, 1, 1)
# surface image feature extraction branch
self.conv1_s = nn.Conv2d(3, 32, 3, 2, 1)
self.conv2_s = nn.Conv2d(32, 64, 3, 2, 1)
self.conv3_s = nn.Conv2d(64, 128, 3, 1, 1)
self.conv4_s = nn.Conv2d(128, 256, 3, 1, 1)
# transposed conv
self.transConv1 = nn.ConvTranspose2d(128, 64, 2, 2, 0)
self.transConv2 = nn.ConvTranspose2d(64, 32, 2, 2, 0)
self.conv6 = nn.Conv2d(32, 3, 3, 1, 1)
# skip layers
self.skipConv1 = nn.Sequential(
nn.Conv2d(3, 3, 3, 1, 1),
self.relu,
nn.Conv2d(3, 3, 3, 1, 1),
self.relu,
nn.Conv2d(3, 3, 3, 1, 1),
self.relu
)
self.skipConv2 = nn.Conv2d(32, 64, 1, 1, 0)
self.skipConv3 = nn.Conv2d(64, 128, 1, 1, 0)
# initialization function, first checks the module type,
def _initialize_weights(m):
if type(m) == nn.Conv2d:
nn.init.kaiming_normal_(m.weight)
self.apply(_initialize_weights)
# x is the input uncompensated image, s is a 1x3x256x256 surface image
def forward(self, x, s):
# surface feature extraction
res1_s = self.relu(self.conv1_s(s))
res2_s = self.relu(self.conv2_s(res1_s))
res3_s = self.relu(self.conv3_s(res2_s))
res4_s = self.relu(self.conv4_s(res3_s))
# backbone
res1 = self.skipConv1(x)
x = self.relu(self.conv1(x) + res1_s)
res2 = self.skipConv2(x)
x = self.relu(self.conv2(x) + res2_s)
res3 = self.skipConv3(x)
x = self.relu(self.conv3(x) + res3_s)
x = self.relu(self.conv4(x) + res4_s)
x = self.relu(self.conv5(x) + res3)
x = self.relu(self.transConv1(x) + res2)
x = self.relu(self.transConv2(x))
x = torch.clamp(self.relu(self.conv6(x) + res1), max=1)
return x