-
Notifications
You must be signed in to change notification settings - Fork 126
/
models.py
186 lines (157 loc) · 5.4 KB
/
models.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
"""
Definition of the FastDVDnet model
Copyright (C) 2019, Matias Tassano <matias.tassano@parisdescartes.fr>
This program is free software: you can use, modify and/or
redistribute it under the terms of the GNU General Public
License as published by the Free Software Foundation, either
version 3 of the License, or (at your option) any later
version. You should have received a copy of this license along
this program. If not, see <http://www.gnu.org/licenses/>.
"""
import torch
import torch.nn as nn
class CvBlock(nn.Module):
'''(Conv2d => BN => ReLU) x 2'''
def __init__(self, in_ch, out_ch):
super(CvBlock, self).__init__()
self.convblock = nn.Sequential(
nn.Conv2d(in_ch, out_ch, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=True),
nn.Conv2d(out_ch, out_ch, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=True)
)
def forward(self, x):
return self.convblock(x)
class InputCvBlock(nn.Module):
'''(Conv with num_in_frames groups => BN => ReLU) + (Conv => BN => ReLU)'''
def __init__(self, num_in_frames, out_ch):
super(InputCvBlock, self).__init__()
self.interm_ch = 30
self.convblock = nn.Sequential(
nn.Conv2d(num_in_frames*(3+1), num_in_frames*self.interm_ch, \
kernel_size=3, padding=1, groups=num_in_frames, bias=False),
nn.BatchNorm2d(num_in_frames*self.interm_ch),
nn.ReLU(inplace=True),
nn.Conv2d(num_in_frames*self.interm_ch, out_ch, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=True)
)
def forward(self, x):
return self.convblock(x)
class DownBlock(nn.Module):
'''Downscale + (Conv2d => BN => ReLU)*2'''
def __init__(self, in_ch, out_ch):
super(DownBlock, self).__init__()
self.convblock = nn.Sequential(
nn.Conv2d(in_ch, out_ch, kernel_size=3, padding=1, stride=2, bias=False),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=True),
CvBlock(out_ch, out_ch)
)
def forward(self, x):
return self.convblock(x)
class UpBlock(nn.Module):
'''(Conv2d => BN => ReLU)*2 + Upscale'''
def __init__(self, in_ch, out_ch):
super(UpBlock, self).__init__()
self.convblock = nn.Sequential(
CvBlock(in_ch, in_ch),
nn.Conv2d(in_ch, out_ch*4, kernel_size=3, padding=1, bias=False),
nn.PixelShuffle(2)
)
def forward(self, x):
return self.convblock(x)
class OutputCvBlock(nn.Module):
'''Conv2d => BN => ReLU => Conv2d'''
def __init__(self, in_ch, out_ch):
super(OutputCvBlock, self).__init__()
self.convblock = nn.Sequential(
nn.Conv2d(in_ch, in_ch, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(in_ch),
nn.ReLU(inplace=True),
nn.Conv2d(in_ch, out_ch, kernel_size=3, padding=1, bias=False)
)
def forward(self, x):
return self.convblock(x)
class DenBlock(nn.Module):
""" Definition of the denosing block of FastDVDnet.
Inputs of constructor:
num_input_frames: int. number of input frames
Inputs of forward():
xn: input frames of dim [N, C, H, W], (C=3 RGB)
noise_map: array with noise map of dim [N, 1, H, W]
"""
def __init__(self, num_input_frames=3):
super(DenBlock, self).__init__()
self.chs_lyr0 = 32
self.chs_lyr1 = 64
self.chs_lyr2 = 128
self.inc = InputCvBlock(num_in_frames=num_input_frames, out_ch=self.chs_lyr0)
self.downc0 = DownBlock(in_ch=self.chs_lyr0, out_ch=self.chs_lyr1)
self.downc1 = DownBlock(in_ch=self.chs_lyr1, out_ch=self.chs_lyr2)
self.upc2 = UpBlock(in_ch=self.chs_lyr2, out_ch=self.chs_lyr1)
self.upc1 = UpBlock(in_ch=self.chs_lyr1, out_ch=self.chs_lyr0)
self.outc = OutputCvBlock(in_ch=self.chs_lyr0, out_ch=3)
self.reset_params()
@staticmethod
def weight_init(m):
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, nonlinearity='relu')
def reset_params(self):
for _, m in enumerate(self.modules()):
self.weight_init(m)
def forward(self, in0, in1, in2, noise_map):
'''Args:
inX: Tensor, [N, C, H, W] in the [0., 1.] range
noise_map: Tensor [N, 1, H, W] in the [0., 1.] range
'''
# Input convolution block
x0 = self.inc(torch.cat((in0, noise_map, in1, noise_map, in2, noise_map), dim=1))
# Downsampling
x1 = self.downc0(x0)
x2 = self.downc1(x1)
# Upsampling
x2 = self.upc2(x2)
x1 = self.upc1(x1+x2)
# Estimation
x = self.outc(x0+x1)
# Residual
x = in1 - x
return x
class FastDVDnet(nn.Module):
""" Definition of the FastDVDnet model.
Inputs of forward():
xn: input frames of dim [N, C, H, W], (C=3 RGB)
noise_map: array with noise map of dim [N, 1, H, W]
"""
def __init__(self, num_input_frames=5):
super(FastDVDnet, self).__init__()
self.num_input_frames = num_input_frames
# Define models of each denoising stage
self.temp1 = DenBlock(num_input_frames=3)
self.temp2 = DenBlock(num_input_frames=3)
# Init weights
self.reset_params()
@staticmethod
def weight_init(m):
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, nonlinearity='relu')
def reset_params(self):
for _, m in enumerate(self.modules()):
self.weight_init(m)
def forward(self, x, noise_map):
'''Args:
x: Tensor, [N, num_frames*C, H, W] in the [0., 1.] range
noise_map: Tensor [N, 1, H, W] in the [0., 1.] range
'''
# Unpack inputs
(x0, x1, x2, x3, x4) = tuple(x[:, 3*m:3*m+3, :, :] for m in range(self.num_input_frames))
# First stage
x20 = self.temp1(x0, x1, x2, noise_map)
x21 = self.temp1(x1, x2, x3, noise_map)
x22 = self.temp1(x2, x3, x4, noise_map)
#Second stage
x = self.temp2(x20, x21, x22, noise_map)
return x