forked from kocchop/depth-completion-gan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
248 lines (196 loc) · 8.54 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
"""
Model and loss description file
"""
import torch.nn as nn
import torch.nn.functional as F
import torch
from torchvision.models import vgg19
import numpy as np
import math
def imgrad(img):
img = torch.mean(img, 1, True)
fx = np.array([[1,0,-1],[2,0,-2],[1,0,-1]])
conv1 = nn.Conv2d(1, 1, kernel_size=3, stride=1, padding=1, bias=False)
weight = torch.from_numpy(fx).float().unsqueeze(0).unsqueeze(0)
# if img.is_cuda:
weight = weight.cuda()
conv1.weight = nn.Parameter(weight)
grad_x = conv1(img)
fy = np.array([[1,2,1],[0,0,0],[-1,-2,-1]])
conv2 = nn.Conv2d(1, 1, kernel_size=3, stride=1, padding=1, bias=False)
weight = torch.from_numpy(fy).float().unsqueeze(0).unsqueeze(0)
# if img.is_cuda:
weight = weight.cuda()
conv2.weight = nn.Parameter(weight)
grad_y = conv2(img)
# grad = torch.sqrt(torch.pow(grad_x,2) + torch.pow(grad_y,2))
return grad_y, grad_x
def imgrad_yx(img):
N,C,_,_ = img.size()
grad_y, grad_x = imgrad(img)
return torch.cat((grad_y.view(N,C,-1), grad_x.view(N,C,-1)), dim=1)
class NormalLoss(nn.Module):
def __init__(self):
super(NormalLoss, self).__init__()
def forward(self, grad_fake, grad_real):
prod = ( grad_fake[:,:,None,:] @ grad_real[:,:,:,None] ).squeeze(-1).squeeze(-1)
fake_norm = torch.sqrt( torch.sum( grad_fake**2, dim=-1 ) )
real_norm = torch.sqrt( torch.sum( grad_real**2, dim=-1 ) )
return 1 - torch.mean( prod/(fake_norm*real_norm) )
class FeatureExtractor(nn.Module):
def __init__(self):
super(FeatureExtractor, self).__init__()
vgg19_model = vgg19(pretrained=True)
self.vgg19_54 = nn.Sequential(*list(vgg19_model.features.children())[:35])
def forward(self, img):
return self.vgg19_54(img)
class HoGLayer(nn.Module):
def __init__(self, nbins=10, pool=8, max_angle=math.pi, stride=1, padding=1, dilation=1, max_out=False):
super(HoGLayer, self).__init__()
self.nbins = nbins
self.stride = stride
self.padding = padding
self.dilation = dilation
self.pool = pool
self.max_angle = max_angle
self.max_out = max_out
mat = torch.FloatTensor([[1, 0, -1],
[2, 0, -2],
[1, 0, -1]])
mat = torch.cat((mat[None], mat.t()[None]), dim=0)
self.register_buffer("weight", mat[:,None,:,:])
self.pooler = nn.AvgPool2d(pool, stride=pool, padding=0, ceil_mode=False, count_include_pad=True)
def forward(self, x):
if x.size(1) > 1:
x = x.mean(dim=1)[:,None,:,:]
gxy = F.conv2d(x, self.weight, None, self.stride,
self.padding, self.dilation, 1)
# 2. Mag/ Phase
mag = gxy.norm(dim=1)
norm = mag[:, None, :, :]
phase = torch.atan2(gxy[:, 0, :, :], gxy[:, 1, :, :])
# 3. Binning Mag with linear interpolation
phase_int = phase / self.max_angle * self.nbins
phase_int = phase_int[:, None, :, :]
n, c, h, w = gxy.shape
out = torch.zeros((n, self.nbins, h, w), dtype=torch.float, device=gxy.device)
out.scatter_(1, phase_int.floor().long() % self.nbins, norm)
out.scatter_add_(1, phase_int.ceil().long() % self.nbins, 1 - norm)
return self.pooler(out)
class Hourglass(nn.Module):
def __init__(self, filters, res_scale=0.4):
super(Hourglass, self).__init__()
self.up1 = DenseResidualBlock(filters, res_scale)
# Lower branch
self.pool1 = nn.MaxPool2d(2, 2)
self.low1 = DenseResidualBlock(filters, res_scale)
self.low2 = DenseResidualBlock(filters, res_scale)
self.low3 = DenseResidualBlock(filters, res_scale)
self.up2 = nn.Upsample(scale_factor=2, mode='nearest')
def forward(self, x):
up1 = self.up1(x)
pool1 = self.pool1(x)
low1 = self.low1(pool1)
low2 = self.low2(low1)
low3 = self.low3(low2)
up2 = self.up2(low3)
return up1 + up2
class DenseResidualBlock(nn.Module):
"""
The core module of paper: (Residual Dense Network for Image Super-Resolution, CVPR 18)
"""
def __init__(self, filters, res_scale=0.2):
super(DenseResidualBlock, self).__init__()
self.res_scale = res_scale
def block(in_features, non_linearity=True):
layers = [nn.Conv2d(in_features, filters, 3, 1, 1, bias=True)]
if non_linearity:
layers += [nn.LeakyReLU()]
return nn.Sequential(*layers)
self.b1 = block(in_features=1 * filters)
self.b2 = block(in_features=2 * filters)
self.b3 = block(in_features=3 * filters)
self.b4 = block(in_features=4 * filters)
self.b5 = block(in_features=5 * filters, non_linearity=False)
def forward(self, x):
inputs = x
out = self.b1(inputs)
inputs = torch.cat([inputs, out], 1)
out = self.b2(inputs)
inputs = torch.cat([inputs, out], 1)
out = self.b3(inputs)
inputs = torch.cat([inputs, out], 1)
out = self.b4(inputs)
inputs = torch.cat([inputs, out], 1)
out = self.b5(inputs)
return out.mul(self.res_scale) + x
class ResidualInResidualDenseBlock(nn.Module):
def __init__(self, filters, res_scale=0.2):
super(ResidualInResidualDenseBlock, self).__init__()
self.res_scale = res_scale
self.dense_blocks = nn.Sequential(
DenseResidualBlock(filters), DenseResidualBlock(filters), DenseResidualBlock(filters)
)
def forward(self, x):
return self.dense_blocks(x).mul(self.res_scale) + x
class GeneratorRRDB(nn.Module):
def __init__(self, channels, filters=64, num_res_blocks=16, num_upsample=2):
super(GeneratorRRDB, self).__init__()
# First layer
self.conv1 = nn.Conv2d(channels, filters, kernel_size=3, stride=1, padding=1)
# Attention Module
self.attention = Hourglass(filters)
# Residual blocks
self.res_blocks = nn.Sequential(*[ResidualInResidualDenseBlock(filters) for _ in range(num_res_blocks)])
# Second conv layer post residual blocks
self.conv2 = nn.Conv2d(filters, filters, kernel_size=3, stride=1, padding=1)
# Upsampling layers
# upsample_layers = []
# for _ in range(num_upsample):
# upsample_layers += [
# nn.Conv2d(filters, filters * 4, kernel_size=3, stride=1, padding=1),
# nn.LeakyReLU(),
# nn.PixelShuffle(upscale_factor=2),
# ]
# self.upsampling = nn.Sequential(*upsample_layers)
# Final output block
self.conv3 = nn.Sequential(
nn.Conv2d(filters, filters, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(),
nn.Conv2d(filters, channels, kernel_size=3, stride=1, padding=1),
)
def forward(self, x):
out0 = self.conv1(x)
out1 = self.attention(out0)
out = self.res_blocks(out1)
out2 = self.conv2(out)
out = torch.add(out1, out2)
# out = self.upsampling(out) #Removed upsampling ffk
out = self.conv3(out)
return out
class Discriminator(nn.Module):
def __init__(self, input_shape):
super(Discriminator, self).__init__()
self.input_shape = input_shape
in_channels, in_height, in_width = self.input_shape
patch_h, patch_w = int(in_height / 2 ** 4), int(in_width / 2 ** 4)
self.output_shape = (1, patch_h, patch_w)
def discriminator_block(in_filters, out_filters, first_block=False):
layers = []
layers.append(nn.Conv2d(in_filters, out_filters, kernel_size=3, stride=1, padding=1))
if not first_block:
layers.append(nn.BatchNorm2d(out_filters))
layers.append(nn.LeakyReLU(0.2, inplace=True))
layers.append(nn.Conv2d(out_filters, out_filters, kernel_size=3, stride=2, padding=1))
layers.append(nn.BatchNorm2d(out_filters))
layers.append(nn.LeakyReLU(0.2, inplace=True))
return layers
layers = []
in_filters = in_channels
for i, out_filters in enumerate([64, 128, 256, 512]):
layers.extend(discriminator_block(in_filters, out_filters, first_block=(i == 0)))
in_filters = out_filters
layers.append(nn.Conv2d(out_filters, 1, kernel_size=3, stride=1, padding=1))
self.model = nn.Sequential(*layers)
def forward(self, img):
return self.model(img)