-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdpdataset.py
211 lines (157 loc) · 6.56 KB
/
dpdataset.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
import os
os.umask(0)
from pathlib import Path
import numpy as np
os.environ["OPENCV_IO_ENABLE_OPENEXR"]="1"
import cv2
import torch
import utils.io
class DPDataset:
def __init__(self):
super().__init__()
self.dataset_name = None
# TO BE OVERRIDED
def stem_img(self, fpath: str) -> str:
return Path(fpath).stem
def read_img(self, fpath: str) -> np.ndarray:
return utils.io.read_img(fpath)
def stem_gt(self, fpath: str) -> str:
return None
def read_gt(self, fpath: str) -> np.ndarray:
return None
def get_gt_mask(self, disp: np.ndarray) -> np.ndarray:
return None
def post_process(self, imgs, gt, disp):
return imgs, gt, disp
# PUBLIC
def load(self, img1_path: str, img2_path:str, gt_path=None):
stem1 = self.stem_img(img1_path)
stem2 = self.stem_img(img2_path)
assert stem1 == stem2
stem = stem1
imgs = {}
imgs['L'] = self.read_img(img1_path)
imgs['R'] = self.read_img(img2_path)
if gt_path is not None:
assert stem == self.stem_gt(gt_path)
gt = {}
gt['d'] = self.read_gt(gt_path)
gt['conf'] = self.get_gt_mask(gt['d'])
else:
gt = None
return stem, imgs, gt
def prepare(self, imgs:dict, device:torch.device):
imgs_input = {}
scale_factor = self._scale_factor(imgs['L'].shape[1])
def _a2t(img:np.ndarray) -> torch.Tensor:
# Assert image is RGB, H W 3
assert img.ndim == 3
assert img.max() < 256
return torch.from_numpy(img/255.0).float().permute(2,0,1)
for k, v in imgs.items():
_img = cv2.resize(v, dsize=None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_LINEAR)
imgs_input[k] = _a2t(_img).unsqueeze(0).to(device)
return imgs_input
def pred_to_disp(self, predictions, origin_size):
'''origin_size = (w, h)'''
_disp = predictions['LCR']
assert isinstance(_disp, torch.Tensor)
_disp = _disp.squeeze(0).squeeze(0).cpu().numpy()
return cv2.resize(_disp, dsize=origin_size, interpolation=cv2.INTER_LINEAR) / self._scale_factor(origin_size[0])
# PRIVATE
def _scale_factor(self, w):
return 640 / w
class CanonDPDeblur(DPDataset):
def __init__(self):
super().__init__()
self.dataset_name = 'Abuolaim_ECCV_2020'
# TO BE OVERRIDED
def stem_img(self, fpath: str) -> str:
return f"{fpath.split('/')[-1].split('.')[0].split('_')[0]}"
def read_img(self, fpath: str) -> np.ndarray:
return utils.io.read_img(fpath)
def post_process(self, imgs, gt, disp):
def _crop(img, patch_size=111, stride=33):
'''
Center crop method from Punnappurath ICCP 2020 paper
'''
h, w = img.shape[:2]
m = (patch_size - 1) / 2
mids = int((stride - 1) / 2)
rowmax = np.arange(m + 1, h - m, stride).astype(int)
colmax = np.arange(m + 1, w - m, stride).astype(int)
cropped = img[rowmax[0] - mids: rowmax[-1] + mids + 1, colmax[0] - mids: colmax[-1] + mids + 1]
return cropped
for k, v in imgs.items(): imgs[k] = _crop(v)
disp = _crop(disp)
return imgs, gt, disp
class CanonDPDepth(DPDataset):
def __init__(self):
super().__init__()
self.dataset_name = 'Punnappurath_ICCP_2020'
# TO BE OVERRIDED
def stem_img(self, fpath: str) -> str:
return f"{int(fpath.split('/')[-1].split('.')[0].split('_')[0]):03d}"
def read_img(self, fpath: str) -> np.ndarray:
img = utils.io.read_img(fpath)
assert img.shape[:2] == (2940, 5180) # The original shape of data
img = img[125 : 125+32*81, 143: 143+32*148, :]
# Half size
return cv2.resize(img, dsize=(2386, 1296), interpolation=cv2.INTER_LINEAR)
def stem_gt(self, fpath: str) -> str:
return self.stem_img(fpath)
def read_gt(self, fpath: str) -> np.ndarray:
gt = (cv2.imread(fpath)[:,:,0]/255.0).astype(np.float32)
assert gt.shape[:2] == (2940, 5180) # The original shape of data
gt = gt[125 : 125+32*81, 143: 143+32*148]
# Half size
return cv2.resize(gt, dsize=(2386, 1296), interpolation=cv2.INTER_LINEAR)
def get_gt_mask(self, disp: np.ndarray) -> np.ndarray:
return np.ones_like(disp, dtype=np.float32)
def post_process(self, imgs, gt, disp):
def _crop(img, patch_size=111, stride=33):
'''
Center crop method from Punnappurath ICCP 2020 paper
'''
h, w = img.shape[:2]
m = (patch_size - 1) / 2
mids = int((stride - 1) / 2)
rowmax = np.arange(m + 1, h - m, stride).astype(int)
colmax = np.arange(m + 1, w - m, stride).astype(int)
cropped = img[rowmax[0] - mids: rowmax[-1] + mids + 1, colmax[0] - mids: colmax[-1] + mids + 1]
return cropped
for k, v in imgs.items(): imgs[k] = _crop(v)
for k, v in gt.items(): gt[k] = _crop(v)
disp = _crop(disp)
return imgs, gt, disp
class PixelDPXin(DPDataset):
def __init__(self):
super().__init__()
self.dataset_name = 'Xin_ICCV_2021'
# TO BE OVERRIDED
def stem_img(self, fpath: str) -> str:
return fpath.split('/')[-1].split('.')[0].split('_')[0]
def read_img(self, fpath: str) -> np.ndarray:
_img = cv2.imread(fpath, -1)
if _img.dtype == np.uint8:
return np.dstack((_img, _img, _img))
else:
_img = (_img.astype(np.float32) - 1024) / (2**14 -1)
_img = np.power(np.clip(_img, 0.0 , 1.0), 1/2.4)
_img = (_img * 255).astype(np.uint8)
return np.dstack((_img, _img, _img))
def stem_gt(self, fpath: str) -> str:
return self.stem_img(fpath)
def read_gt(self, fpath: str) -> np.ndarray:
return (cv2.imread(fpath)[:,:,0]/255.0).astype(np.float32)
def get_gt_mask(self, disp: np.ndarray) -> np.ndarray:
return (disp > 0).astype(np.float32)
def post_process(self, imgs, gt, disp):
def _crop(img):
offset_y = (img.shape[0] - 1008) // 2
offset_x = (img.shape[1] - 1344) // 2
return img[offset_y:offset_y+1008, offset_x:offset_x+1344]
for k, v in imgs.items(): imgs[k] = _crop(v)
for k, v in gt.items(): gt[k] = _crop(v)
disp = _crop(disp)
return imgs, gt, disp