-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathunet.py
202 lines (140 loc) · 5.86 KB
/
unet.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# -*- coding: utf-8 -*-
#Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved.
#This program is free software; you can redistribute it and/or modify it under the terms of the BSD 0-Clause License.
#This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the BSD 0-Clause License for more details.
'''
This is a PyTorch implementation of the CVPR 2020 paper:
"Deep Local Parametric Filters for Image Enhancement": https://arxiv.org/abs/2003.13985
Please cite the paper if you use this code
Tested with Pytorch 1.7.1, Python 3.7.9
Authors: Sean Moran ([email protected]),
Pierre Marza ([email protected])
'''
import torch
import torch.nn as nn
class UNet(nn.Module):
def __init__(self):
"""UNet implementation
:returns: N/A
:rtype: N/A
"""
super().__init__()
self.conv1 = nn.Conv2d(16, 64, 1)
self.conv2 = nn.Conv2d(32, 64, 1)
self.conv3 = nn.Conv2d(64, 64, 1)
self.local_net = LocalNet(16)
self.dconv_down1 = LocalNet(3, 16)
self.dconv_down2 = LocalNet(16, 32)
self.dconv_down3 = LocalNet(32, 64)
self.dconv_down4 = LocalNet(64, 128)
self.dconv_down5 = LocalNet(128, 128)
self.maxpool = nn.MaxPool2d(2, padding=0)
self.upsample = nn.UpsamplingNearest2d(scale_factor=2)
self.up_conv1x1_1 = nn.Conv2d(128, 128, 1)
self.up_conv1x1_2 = nn.Conv2d(128, 128, 1)
self.up_conv1x1_3 = nn.Conv2d(64, 64, 1)
self.up_conv1x1_4 = nn.Conv2d(32, 32, 1)
self.dconv_up4 = LocalNet(256, 128)
self.dconv_up3 = LocalNet(192, 64)
self.dconv_up2 = LocalNet(96, 32)
self.dconv_up1 = LocalNet(48, 16)
self.conv_last = LocalNet(16, 3)
def forward(self, x):
"""UNet implementation
:param x: image
:returns: predicted image
:rtype: Tensor
"""
x_in_tile = x.clone()
conv1 = self.dconv_down1(x)
x = self.maxpool(conv1)
conv2 = self.dconv_down2(x)
x = self.maxpool(conv2)
conv3 = self.dconv_down3(x)
x = self.maxpool(conv3)
conv4 = self.dconv_down4(x)
x = self.maxpool(conv4)
x = self.dconv_down5(x)
x = self.up_conv1x1_1(self.upsample(x))
if x.shape[3] != conv4.shape[3] and x.shape[2] != conv4.shape[2]:
x = torch.nn.functional.pad(x, (1, 0, 0, 1))
elif x.shape[2] != conv4.shape[2]:
x = torch.nn.functional.pad(x, (0, 0, 0, 1))
elif x.shape[3] != conv4.shape[3]:
x = torch.nn.functional.pad(x, (1, 0, 0, 0))
x = torch.cat([x, conv4], dim=1)
x = self.dconv_up4(x)
x = self.up_conv1x1_2(self.upsample(x))
if x.shape[3] != conv3.shape[3] and x.shape[2] != conv3.shape[2]:
x = torch.nn.functional.pad(x, (1, 0, 0, 1))
elif x.shape[2] != conv3.shape[2]:
x = torch.nn.functional.pad(x, (0, 0, 0, 1))
elif x.shape[3] != conv3.shape[3]:
x = torch.nn.functional.pad(x, (1, 0, 0, 0))
x = torch.cat([x, conv3], dim=1)
x = self.dconv_up3(x)
x = self.up_conv1x1_3(self.upsample(x))
del conv3
if x.shape[3] != conv2.shape[3] and x.shape[2] != conv2.shape[2]:
x = torch.nn.functional.pad(x, (1, 0, 0, 1))
elif x.shape[2] != conv2.shape[2]:
x = torch.nn.functional.pad(x, (0, 0, 0, 1))
elif x.shape[3] != conv2.shape[3]:
x = torch.nn.functional.pad(x, (1, 0, 0, 0))
x = torch.cat([x, conv2], dim=1)
x = self.dconv_up2(x)
x = self.up_conv1x1_4(self.upsample(x))
del conv2
if x.shape[3] != conv1.shape[3] and x.shape[2] != conv1.shape[2]:
x = torch.nn.functional.pad(x, (1, 0, 0, 1))
elif x.shape[2] != conv1.shape[2]:
x = torch.nn.functional.pad(x, (0, 0, 0, 1))
elif x.shape[3] != conv1.shape[3]:
x = torch.nn.functional.pad(x, (1, 0, 0, 0))
x = torch.cat([x, conv1], dim=1)
del conv1
x = self.dconv_up1(x)
out = self.conv_last(x)
out = out + x_in_tile
return out
class LocalNet(nn.Module):
def forward(self, x_in):
"""Double convolutional block
:param x_in: image features
:returns: image features
:rtype: Tensor
"""
x = self.lrelu(self.conv1(self.refpad(x_in)))
x = self.lrelu(self.conv2(self.refpad(x)))
return x
def __init__(self, in_channels=16, out_channels=64):
"""Double convolutional block
:param in_channels: number of input channels
:param out_channels: number of output channels
:returns: N/A
:rtype: N/A
"""
super(LocalNet, self).__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels, 3, 1, 0, 1)
self.conv2 = nn.Conv2d(out_channels, out_channels, 3, 1, 0, 1)
self.lrelu = nn.LeakyReLU()
self.refpad = nn.ReflectionPad2d(1)
# Model definition
class UNetModel(nn.Module):
def __init__(self):
"""UNet model definition
:returns: N/A
:rtype: N/A
"""
super(UNetModel, self).__init__()
self.unet = UNet()
self.final_conv = nn.Conv2d(3, 64, 3, 1, 0, 1)
self.refpad = nn.ReflectionPad2d(1)
def forward(self, img):
"""UNet model definition
:param image: input image
:returns: image features
:rtype: Tensor
"""
output_img = self.unet(img)
return self.final_conv(self.refpad(output_img))