-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
186 lines (139 loc) · 6.79 KB
/
model.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import torch
import torch.optim as optim
from torch.autograd import Variable
import math
import sepconv
import sys
from torch.nn import functional as F
def to_variable(x):
if torch.cuda.is_available():
x = x.cuda()
return Variable(x)
class KernelEstimation(torch.nn.Module):
def __init__(self, kernel_size):
super(KernelEstimation, self).__init__()
self.kernel_size = kernel_size
def Basic(input_channel, output_channel):
return torch.nn.Sequential(
torch.nn.Conv2d(in_channels=input_channel, out_channels=output_channel, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=output_channel, out_channels=output_channel, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=output_channel, out_channels=output_channel, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False)
)
def Upsample(channel):
return torch.nn.Sequential(
torch.nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True),
torch.nn.Conv2d(in_channels=channel, out_channels=channel, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False)
)
def Subnet(ks):
return torch.nn.Sequential(
torch.nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=64, out_channels=ks, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False),
torch.nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True),
torch.nn.Conv2d(in_channels=ks, out_channels=ks, kernel_size=3, stride=1, padding=1)
)
self.moduleConv1 = Basic(6, 32)
self.modulePool1 = torch.nn.AvgPool2d(kernel_size=2, stride=2)
self.moduleConv2 = Basic(32, 64)
self.modulePool2 = torch.nn.AvgPool2d(kernel_size=2, stride=2)
self.moduleConv3 = Basic(64, 128)
self.modulePool3 = torch.nn.AvgPool2d(kernel_size=2, stride=2)
self.moduleConv4 = Basic(128, 256)
self.modulePool4 = torch.nn.AvgPool2d(kernel_size=2, stride=2)
self.moduleConv5 = Basic(256, 512)
self.modulePool5 = torch.nn.AvgPool2d(kernel_size=2, stride=2)
self.moduleDeconv5 = Basic(512, 512)
self.moduleUpsample5 = Upsample(512)
self.moduleDeconv4 = Basic(512, 256)
self.moduleUpsample4 = Upsample(256)
self.moduleDeconv3 = Basic(256, 128)
self.moduleUpsample3 = Upsample(128)
self.moduleDeconv2 = Basic(128, 64)
self.moduleUpsample2 = Upsample(64)
self.moduleVertical1 = Subnet(self.kernel_size)
self.moduleVertical2 = Subnet(self.kernel_size)
self.moduleHorizontal1 = Subnet(self.kernel_size)
self.moduleHorizontal2 = Subnet(self.kernel_size)
def forward(self, rfield0, rfield2):
tensorJoin = torch.cat([rfield0, rfield2], 1)
tensorConv1 = self.moduleConv1(tensorJoin)
tensorPool1 = self.modulePool1(tensorConv1)
tensorConv2 = self.moduleConv2(tensorPool1)
tensorPool2 = self.modulePool2(tensorConv2)
tensorConv3 = self.moduleConv3(tensorPool2)
tensorPool3 = self.modulePool3(tensorConv3)
tensorConv4 = self.moduleConv4(tensorPool3)
tensorPool4 = self.modulePool4(tensorConv4)
tensorConv5 = self.moduleConv5(tensorPool4)
tensorPool5 = self.modulePool5(tensorConv5)
tensorDeconv5 = self.moduleDeconv5(tensorPool5)
tensorUpsample5 = self.moduleUpsample5(tensorDeconv5)
tensorCombine = tensorUpsample5 + tensorConv5
tensorDeconv4 = self.moduleDeconv4(tensorCombine)
tensorUpsample4 = self.moduleUpsample4(tensorDeconv4)
tensorCombine = tensorUpsample4 + tensorConv4
tensorDeconv3 = self.moduleDeconv3(tensorCombine)
tensorUpsample3 = self.moduleUpsample3(tensorDeconv3)
tensorCombine = tensorUpsample3 + tensorConv3
tensorDeconv2 = self.moduleDeconv2(tensorCombine)
tensorUpsample2 = self.moduleUpsample2(tensorDeconv2)
tensorCombine = tensorUpsample2 + tensorConv2
Vertical1 = self.moduleVertical1(tensorCombine)
Vertical2 = self.moduleVertical2(tensorCombine)
Horizontal1 = self.moduleHorizontal1(tensorCombine)
Horizontal2 = self.moduleHorizontal2(tensorCombine)
return Vertical1, Horizontal1, Vertical2, Horizontal2
class SepConvNet(torch.nn.Module):
def __init__(self, kernel_size):
super(SepConvNet, self).__init__()
self.kernel_size = kernel_size
self.kernel_pad = int(math.floor(kernel_size / 2.0))
self.epoch = Variable(torch.tensor(0, requires_grad=False))
self.get_kernel = KernelEstimation(self.kernel_size)
self.optimizer = optim.Adam(self.parameters(), lr=0.001)
self.criterion = torch.nn.MSELoss()
self.modulePad = torch.nn.ReplicationPad2d([self.kernel_pad, self.kernel_pad, self.kernel_pad, self.kernel_pad])
def forward(self, frame0, frame2):
h0 = int(list(frame0.size())[2])
w0 = int(list(frame0.size())[3])
h2 = int(list(frame2.size())[2])
w2 = int(list(frame2.size())[3])
if h0 != h2 or w0 != w2:
sys.exit('Frame sizes do not match')
h_padded = False
w_padded = False
if h0 % 32 != 0:
pad_h = 32 - (h0 % 32)
frame0 = F.pad(frame0, (0, 0, 0, pad_h))
frame2 = F.pad(frame2, (0, 0, 0, pad_h))
h_padded = True
if w0 % 32 != 0:
pad_w = 32 - (w0 % 32)
frame0 = F.pad(frame0, (0, pad_w, 0, 0))
frame2 = F.pad(frame2, (0, pad_w, 0, 0))
w_padded = True
Vertical1, Horizontal1, Vertical2, Horizontal2 = self.get_kernel(frame0, frame2)
tensorDot1 = sepconv.FunctionSepconv.apply(self.modulePad(frame0), Vertical1, Horizontal1)
tensorDot2 = sepconv.FunctionSepconv.apply(self.modulePad(frame2), Vertical2, Horizontal2)
frame1 = tensorDot1 + tensorDot2
if h_padded:
frame1 = frame1[:, :, 0:h0, :]
if w_padded:
frame1 = frame1[:, :, :, 0:w0]
return frame1
def train_model(self, frame0, frame2, frame1):
self.optimizer.zero_grad()
output = self.forward(frame0, frame2)
loss = self.criterion(output, frame1)
loss.backward()
self.optimizer.step()
return loss
def increase_epoch(self):
self.epoch += 1