-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.py
219 lines (171 loc) · 8.63 KB
/
module.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch
import torch.nn.functional as F
# Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto. Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.
"""Custom replacement for `torch.nn.functional.grid_sample` that
supports arbitrarily high order gradients between the input and output.
Only works on 2D images and assumes
`mode='bilinear'`, `padding_mode='zeros'`, `align_corners=False`."""
#----------------------------------------------------------------------------
def grid_sample_gradfix(input, grid):
return _GridSample2dForward.apply(input, grid)
#----------------------------------------------------------------------------
class _GridSample2dForward(torch.autograd.Function):
@staticmethod
def forward(ctx, input, grid):
assert input.ndim == 4
assert grid.ndim == 4
output = torch.nn.functional.grid_sample(input=input, grid=grid, mode='bilinear', padding_mode='zeros', align_corners=False)
ctx.save_for_backward(input, grid)
return output
@staticmethod
def backward(ctx, grad_output):
input, grid = ctx.saved_tensors
grad_input, grad_grid = _GridSample2dBackward.apply(grad_output, input, grid)
return grad_input, grad_grid
#----------------------------------------------------------------------------
class _GridSample2dBackward(torch.autograd.Function):
@staticmethod
def forward(ctx, grad_output, input, grid):
op = torch._C._jit_get_operation('aten::grid_sampler_2d_backward')[0]
grad_input, grad_grid = op(grad_output, input, grid, 0, 0, False, [True, True])
ctx.save_for_backward(grid)
return grad_input, grad_grid
@staticmethod
def backward(ctx, grad2_grad_input, grad2_grad_grid):
_ = grad2_grad_grid # unused
grid, = ctx.saved_tensors
grad2_grad_output = None
grad2_input = None
grad2_grid = None
if ctx.needs_input_grad[0]:
grad2_grad_output = _GridSample2dForward.apply(grad2_grad_input, grid)
assert not ctx.needs_input_grad[2]
return grad2_grad_output, grad2_input, grad2_grid
#----------------------------------------------------------------------------
class ConvBnReLU(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, pad=1):
super(ConvBnReLU, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride=stride, padding=pad, bias=False)
self.bn = nn.BatchNorm2d(out_channels)
def forward(self, x):
return F.relu(self.bn(self.conv(x)), inplace=True)
class ConvBn(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, pad=1):
super(ConvBn, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride=stride, padding=pad, bias=False)
self.bn = nn.BatchNorm2d(out_channels)
def forward(self, x):
return self.bn(self.conv(x))
class ConvBnReLU3D(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, pad=1):
super(ConvBnReLU3D, self).__init__()
self.conv = nn.Conv3d(in_channels, out_channels, kernel_size, stride=stride, padding=pad, bias=False)
self.bn = nn.BatchNorm3d(out_channels)
def forward(self, x):
return F.relu(self.bn(self.conv(x)), inplace=True)
def homo_warping(src_fea, proj, depth_values, use_native_grid_sample=True):
# src_fea: [B, C, H, W]
# src_proj: [B, 4, 4]
# ref_proj: [B, 4, 4]
# depth_values: [B, Ndepth]
# out: [B, C, Ndepth, H, W]
batch, channels = src_fea.shape[0], src_fea.shape[1]
num_depth = depth_values.shape[1]
height, width = src_fea.shape[2], src_fea.shape[3]
with torch.no_grad():
rot = proj[:, :3, :3] # [B,3,3]
trans = proj[:, :3, 3:4] # [B,3,1]
y, x = torch.meshgrid([torch.arange(0, height, dtype=torch.float32, device=src_fea.device),
torch.arange(0, width, dtype=torch.float32, device=src_fea.device)])
y, x = y.contiguous(), x.contiguous()
y, x = y.view(height * width), x.view(height * width)
xyz = torch.stack((x, y, torch.ones_like(x))) # [3, H*W]
xyz = torch.unsqueeze(xyz, 0).repeat(batch, 1, 1) # [B, 3, H*W]
rot_xyz = torch.matmul(rot, xyz) # [B, 3, H*W]
rot_depth_xyz = rot_xyz.unsqueeze(2).repeat(1, 1, num_depth, 1) * depth_values.view(batch, 1, num_depth,
1) # [B, 3, Ndepth, H*W]
proj_xyz = rot_depth_xyz + trans.view(batch, 3, 1, 1) # [B, 3, Ndepth, H*W]
proj_xy = proj_xyz[:, :2, :, :] / proj_xyz[:, 2:3, :, :] # [B, 2, Ndepth, H*W]
proj_x_normalized = proj_xy[:, 0, :, :] / ((width - 1) / 2) - 1
proj_y_normalized = proj_xy[:, 1, :, :] / ((height - 1) / 2) - 1
proj_xy = torch.stack((proj_x_normalized, proj_y_normalized), dim=3) # [B, Ndepth, H*W, 2]
grid = proj_xy
if use_native_grid_sample:
warped_src_fea = F.grid_sample(src_fea, grid.view(batch, num_depth * height, width, 2), mode='bilinear',
padding_mode='zeros')
else:
warped_src_fea = grid_sample_gradfix(src_fea, grid.reshape(batch, num_depth * height, width, 2))
warped_src_fea = warped_src_fea.view(batch, channels, num_depth, height, width)
return warped_src_fea
def depth_regression(p, depth_values):
# p: probability volume [B, D, H, W]
# depth_values: discrete depth values [B, D]
depth_values = depth_values.view(*depth_values.shape, 1, 1)
depth = torch.sum(p * depth_values, 1)
return depth
if False:
# some testing code, just IGNORE it
from datasets import find_dataset_def
from torch.utils.data import DataLoader
import numpy as np
import cv2
MVSDataset = find_dataset_def("dtu_yao")
dataset = MVSDataset("/home/xyguo/dataset/dtu_mvs/processed/mvs_training/dtu/", '../lists/dtu/train.txt', 'train',
3, 256)
dataloader = DataLoader(dataset, batch_size=2)
item = next(iter(dataloader))
imgs = item["imgs"][:, :, :, ::4, ::4].cuda()
proj_matrices = item["proj_matrices"].cuda()
mask = item["mask"].cuda()
depth = item["depth"].cuda()
depth_values = item["depth_values"].cuda()
imgs = torch.unbind(imgs, 1)
proj_matrices = torch.unbind(proj_matrices, 1)
ref_img, src_imgs = imgs[0], imgs[1:]
ref_proj, src_projs = proj_matrices[0], proj_matrices[1:]
warped_imgs = homo_warping(src_imgs[0], src_projs[0], ref_proj, depth_values)
cv2.imwrite('../tmp/ref.png', ref_img.permute([0, 2, 3, 1])[0].detach().cpu().numpy()[:, :, ::-1] * 255)
cv2.imwrite('../tmp/src.png', src_imgs[0].permute([0, 2, 3, 1])[0].detach().cpu().numpy()[:, :, ::-1] * 255)
for i in range(warped_imgs.shape[2]):
warped_img = warped_imgs[:, :, i, :, :].permute([0, 2, 3, 1]).contiguous()
img_np = warped_img[0].detach().cpu().numpy()
cv2.imwrite('../tmp/tmp{}.png'.format(i), img_np[:, :, ::-1] * 255)
# generate gt
def tocpu(x):
return x.detach().cpu().numpy().copy()
ref_img = tocpu(ref_img)[0].transpose([1, 2, 0])
src_imgs = [tocpu(x)[0].transpose([1, 2, 0]) for x in src_imgs]
ref_proj_mat = tocpu(ref_proj)[0]
src_proj_mats = [tocpu(x)[0] for x in src_projs]
mask = tocpu(mask)[0]
depth = tocpu(depth)[0]
depth_values = tocpu(depth_values)[0]
for i, D in enumerate(depth_values):
height = ref_img.shape[0]
width = ref_img.shape[1]
xx, yy = np.meshgrid(np.arange(0, width), np.arange(0, height))
print("yy", yy.max(), yy.min())
yy = yy.reshape([-1])
xx = xx.reshape([-1])
X = np.vstack((xx, yy, np.ones_like(xx)))
# D = depth.reshape([-1])
# print("X", "D", X.shape, D.shape)
X = np.vstack((X * D, np.ones_like(xx)))
X = np.matmul(np.linalg.inv(ref_proj_mat), X)
X = np.matmul(src_proj_mats[0], X)
X /= X[2]
X = X[:2]
yy = X[0].reshape([height, width]).astype(np.float32)
xx = X[1].reshape([height, width]).astype(np.float32)
warped = cv2.remap(src_imgs[0], yy, xx, interpolation=cv2.INTER_LINEAR)
# warped[mask[:, :] < 0.5] = 0
cv2.imwrite('../tmp/tmp{}_gt.png'.format(i), warped[:, :, ::-1] * 255)