-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch_converter.py
416 lines (375 loc) · 17.3 KB
/
patch_converter.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import torch
import torch.nn.functional as F
import torchvision.transforms.functional as vis_F
class Patch_converter(object):
def __init__(self) -> None:
"""
original size (H, W): (900, 1600)
deepint size: (448, 800)
bevfusion2 size: (448, 800)
transfusion size: (448, 800)
bevfusion size: (256, 704), from original: center bottom crop, scale 0.465
(900, 1600) * 0.465 = (418, 744)
uvtr size: (928, 1600), from original: bottom padding 28
bevformer size: (928, 1600), from original: bottom padding 28
autoalign size: (640, 1152), from original: scale to (640, 1137) then pad right to 1152
"""
self.bev_scale = 0.465
self.deepint_scale = 0.5
self.original_shape = (3, 900, 1600)
self.deepint_shape = (3, 448, 800) # also shape of bevfusion2 and transfusion
self.bevfusion_shape = (3, 256, 704)
self.uvtr_shape = (3, 928, 1600)
self.autoalign_shape = (3, 640, 1152)
self.autoalign_scale = 0.711
def bev2ori(self, patch_area):
t, l, h, w = patch_area
ori_h, ori_w = h / self.bev_scale, w / self.bev_scale
ori_t = (t + (418 - 256)) / self.bev_scale
ori_l = (l + (744 - 704) // 2) / self.bev_scale
ori_patch_area = (int(ori_t), int(ori_l), int(ori_h), int(ori_w))
return ori_patch_area
def autoalign2ori(self, patch_area):
t, l, h, w = patch_area
ori_h, ori_w = h / self.autoalign_scale, w / self.autoalign_scale
ori_t = t / self.autoalign_scale
ori_l = l / self.autoalign_scale
ori_patch_area = (int(ori_t), int(ori_l), int(ori_h), int(ori_w))
return ori_patch_area
def ori2autoalign(self, patch_area):
t, l, h, w = patch_area
return (
int(t * self.autoalign_scale),
int(l * self.autoalign_scale),
int(h * self.autoalign_scale),
int(w * self.autoalign_scale)
)
def autoalign2ori_patch(self, patch_img: torch.Tensor, mask: torch.Tensor):
assert patch_img.shape == self.autoalign_shape
patch_bind = torch.cat([mask, patch_img], dim=0).clone()
patch_bind = patch_bind[:, :, :1137]
patch_bind = vis_F.resize(patch_bind, self.original_shape[1:])
mask_out = patch_bind[[0]]
patch_out = patch_bind[1:4]
return patch_out, mask_out
def ori2autoalign_patch(self, patch_img: torch.Tensor, mask: torch.Tensor):
assert patch_img.shape == self.original_shape
patch_bind = torch.cat([mask, patch_img], dim=0).clone()
patch_bind = vis_F.resize(patch_bind, (640, 1137))
right_pad = 15
patch_bind = F.pad(patch_bind, (0, right_pad), "constant", 0)
mask_out = patch_bind[[0]]
patch_out = patch_bind[1:4]
return patch_out, mask_out
def ori2uvtr(self, patch_area):
return patch_area
def ori2uvtr_patch(self, patch_img: torch.Tensor, mask: torch.Tensor):
assert patch_img.shape == self.original_shape
patch_bind = torch.cat([mask, patch_img], dim=0).clone()
bottom_pad = 928 - 900
patch_bind = F.pad(patch_bind, (0, 0, 0, bottom_pad), "constant", 0)
mask_out = patch_bind[[0]]
patch_out = patch_bind[1:4]
return patch_out, mask_out
def uvtr2ori_patch(self, patch_img: torch.Tensor, mask: torch.Tensor):
assert patch_img.shape == self.uvtr_shape
patch_bind = torch.cat([mask, patch_img], dim=0).clone()
bottom_crop = 928 - 900
patch_bind = patch_bind[:, :-bottom_crop, :]
mask_out = patch_bind[[0]]
patch_out = patch_bind[1:4]
return patch_out, mask_out
def uvtr2ori(self, patch_area):
return patch_area
def bev2ori_patch(self, patch_img: torch.Tensor, mask: torch.Tensor):
"""
patch_img: tensor with size of (3, H, W)
mask: tensor with size of (1, H, W)
"""
assert patch_img.shape == self.bevfusion_shape
patch_bind = torch.cat([mask, patch_img], dim=0).clone()
side_pad = (744 - 704) // 2
top_pad = (418 - 256)
patch_bind = F.pad(patch_bind, (side_pad, side_pad, top_pad, 0), "constant", 0)
patch_bind = vis_F.resize(patch_bind, self.original_shape[1:])
mask_out = patch_bind[[0]]
patch_out = patch_bind[1:4]
return patch_out, mask_out
def bev2deepint_patch(self, patch_img: torch.Tensor, mask: torch.Tensor):
"""
patch_img: tensor with size of (3, H, W)
mask: tensor with size of (1, H, W)
"""
assert patch_img.shape == self.bevfusion_shape
patch_bind = torch.cat([mask, patch_img], dim=0).clone()
side_pad = (744 - 704) // 2
top_pad = (418 - 256)
patch_bind = F.pad(patch_bind, (side_pad, side_pad, top_pad, 0), "constant", 0)
patch_bind = vis_F.resize(patch_bind, self.deepint_shape[1:])
mask_out = patch_bind[[0]]
patch_out = patch_bind[1:4]
return patch_out, mask_out
def bev2uvtr_patch(self, patch_img: torch.Tensor, mask: torch.Tensor):
assert patch_img.shape == self.bevfusion_shape
patch_bind = torch.cat([mask, patch_img], dim=0).clone()
side_pad = (744 - 704) // 2
top_pad = (418 - 256)
patch_bind = F.pad(patch_bind, (side_pad, side_pad, top_pad, 0), "constant", 0)
patch_bind = vis_F.resize(patch_bind, self.original_shape[1:])
bottom_pad = 928 - 900
patch_bind = F.pad(patch_bind, (0, 0, 0, bottom_pad), "constant", 0)
mask_out = patch_bind[[0]]
patch_out = patch_bind[1:4]
return patch_out, mask_out
def deepi2uvtr_patch(self, patch_img: torch.Tensor, mask: torch.Tensor):
assert patch_img.shape == self.deepint_shape
patch_bind = torch.cat([mask, patch_img], dim=0).clone()
patch_bind = vis_F.resize(patch_bind, self.original_shape[1:])
bottom_pad = 928 - 900
patch_bind = F.pad(patch_bind, (0, 0, 0, bottom_pad), "constant", 0)
mask_out = patch_bind[[0]]
patch_out = patch_bind[1:4]
return patch_out, mask_out
def deepi2ori(self, patch_area):
t, l, h, w = patch_area
return (
int(t / self.deepint_scale),
int(l / self.deepint_scale),
int(h / self.deepint_scale),
int(w / self.deepint_scale)
)
def deepi2ori_patch(self, patch_img: torch.Tensor, mask: torch.Tensor):
"""
patch_img: tensor with size of (3, H, W)
mask: tensor with size of (1, H, W)
"""
assert patch_img.shape == self.deepint_shape
patch_out = vis_F.resize(patch_img, self.original_shape[1:])
mask_out = vis_F.resize(mask, self.original_shape[1:])
return patch_out, mask_out
def deepi2ori_pseudo_patch(self, patch_img: torch.Tensor, mask: torch.Tensor):
"""
patch_img: tensor with size of (3, H, W)
mask: tensor with size of (1, H, W)
"""
assert patch_img.shape == self.deepint_shape
t = self.original_shape[1] - self.deepint_shape[1]
b = 0
l = (self.original_shape[2] - self.deepint_shape[2]) // 2
r = l
patch_out = vis_F.pad(patch_img, [l,t,r,b])
mask_out = vis_F.pad(mask, [l,t,r,b])
return patch_out, mask_out
def ori2deepi_patch(self, patch_img: torch.Tensor, mask: torch.Tensor):
"""
patch_img: tensor with size of (3, H, W)
mask: tensor with size of (1, H, W)
"""
assert patch_img.shape == self.original_shape
patch_out = vis_F.resize(patch_img, self.deepint_shape[1:])
mask_out = vis_F.resize(mask, self.deepint_shape[1:])
return patch_out, mask_out
def ori2deepi(self, patch_area):
t, l, h, w = patch_area
return (
int(t * self.deepint_scale),
int(l * self.deepint_scale),
int(h * self.deepint_scale),
int(w * self.deepint_scale)
)
def ori2bev(self, patch_area):
t, l, h, w = patch_area
t_bev = t * self.bev_scale - (418 - 256)
l_bev = l * self.bev_scale - (744 - 704) // 2
h_bev, w_bev = h * self.bev_scale, w * self.bev_scale
return (
int(t_bev),
int(l_bev),
int(h_bev),
int(w_bev)
)
def ori2bev_patch(self, patch_img: torch.Tensor, mask: torch.Tensor):
"""
patch_img: tensor with size of (3, H, W)
mask: tensor with size of (1, H, W)
"""
assert patch_img.shape == self.original_shape
patch_bind = torch.cat([mask, patch_img], dim=0).clone()
patch_bind = vis_F.resize(patch_bind, (418, 744))
side_crop = (744 - 704) // 2
top_crop= (418 - 256)
patch_bind = patch_bind[:, top_crop:, side_crop:-side_crop]
mask_out = patch_bind[[0]]
patch_out = patch_bind[1:4]
return patch_out, mask_out
def convert_coord_from_ori(self, target, coords):
if target == 'bevfusion2' or target == 'transfusion' or target == 'deepint':
return coords * self.deepint_scale
elif target == 'bevfusion':
coord_ret = coords * self.bev_scale
coord_ret[:, 0] -= (744 - 704) // 2
coord_ret[:, 1] -= (418 - 256)
return coord_ret
elif target == 'uvtr' or target == 'bevformer' or target == 'original':
return coords
elif target == 'autoalign':
coord_ret = coords * self.autoalign_scale
return coord_ret
def convert_coord_to_ori(self, source, coords):
if source == 'bevfusion2' or source == 'transfusion' or source == 'deepint':
return coords / self.deepint_scale
elif source == 'bevfusion':
coord_ret[:, 1] += (418 - 256)
coord_ret[:, 0] += (744 - 704) // 2
coord_ret = coords / self.bev_scale
return coord_ret
elif source == 'uvtr' or source == 'bevformer' or source == 'original':
return coords
elif source == 'autoalign':
coord_ret = coords / self.autoalign_scale
return coord_ret
def convert_patch_area(self, patch_area, target:str, source:str='bevfusion'):
if source == 'bevfusion2' or source == 'transfusion':
source = 'deepint'
elif source == 'bevformer':
source = 'uvtr'
if target == 'bevfusion2' or target == 'transfusion':
target = 'deepint'
elif target == 'bevformer':
target = 'uvtr'
if source == target:
return patch_area
elif source == 'bevfusion' and target == 'original':
return self.bev2ori(patch_area)
elif source == 'bevfusion' and target == 'deepint':
return self.ori2deepi(self.bev2ori(patch_area))
elif source == 'bevfusion' and target == 'uvtr':
return self.ori2uvtr(self.bev2ori(patch_area))
elif source == 'deepint' and target == 'original':
return self.deepi2ori(patch_area)
elif source == 'deepint' and target == 'bevfusion':
return self.ori2bev(self.deepi2ori(patch_area))
elif source == 'deepint' and target == 'uvtr':
return self.ori2uvtr(self.deepi2ori(patch_area))
elif source == 'original' and target == 'bevfusion':
return self.ori2bev(patch_area)
elif source == 'original' and target == 'deepint':
return self.ori2deepi(patch_area)
elif source == 'original' and target == 'uvtr':
return self.ori2uvtr(patch_area)
elif source == 'autoalign' and target == 'original':
return self.autoalign2ori(patch_area)
elif source == 'original'and target == 'autoalign':
return self.ori2autoalign(patch_area)
else:
raise NotImplementedError()
def convert_pseudo_patch(self, patch_img, mask, target:str, source:str=None):
if source is None:
if patch_img.shape == self.original_shape:
source = 'original'
elif patch_img.shape == self.bevfusion_shape:
source = 'bevfusion'
elif patch_img.shape == self.deepint_shape:
source = 'deepint'
elif patch_img.shape == self.uvtr_shape:
source = 'uvtr'
elif patch_img.shape == self.autoalign_shape:
source = 'autoalign'
else:
raise RuntimeError("Patch image shape error.")
if target == 'bevfusion2' or target == 'transfusion':
target = 'deepint'
elif target == 'bevformer':
target = 'uvtr'
if source == target:
return patch_img, mask
transformations = {
('deepint', 'original'): self.deepi2ori_pseudo_patch,
}
try:
transform_func = transformations[(source, target)]
return transform_func(patch_img, mask)
except KeyError:
raise NotImplementedError(f"source: {source}, target: {target}")
def convert_patch(self, patch_img, mask, target:str, source:str=None):
if len(mask.shape) == 4:
patch_cvt = []
mask_cvt = []
for i in range(len(mask)):
patch_cvt_sub, mask_cvt_sub = self.convert_patch(patch_img[i], mask[i], target)
patch_cvt.append(patch_cvt_sub)
mask_cvt.append(mask_cvt_sub)
patch_cvt = torch.stack(patch_cvt)
mask_cvt = torch.stack(mask_cvt)
return patch_cvt, mask_cvt
if source is None:
if patch_img.shape == self.original_shape:
source = 'original'
elif patch_img.shape == self.bevfusion_shape:
source = 'bevfusion'
elif patch_img.shape == self.deepint_shape:
source = 'deepint'
elif patch_img.shape == self.uvtr_shape:
source = 'uvtr'
elif patch_img.shape == self.autoalign_shape:
source = 'autoalign'
else:
raise RuntimeError("Patch image shape error.")
if target == 'bevfusion2' or target == 'transfusion':
target = 'deepint'
elif target == 'bevformer':
target = 'uvtr'
if source == target:
return patch_img, mask
transformations = {
('bevfusion', 'original'): self.bev2ori_patch,
('bevfusion', 'deepint'): self.bev2deepint_patch,
('bevfusion', 'uvtr'): self.bev2uvtr_patch,
('bevfusion', 'autoalign'): lambda x, y: self.ori2autoalign_patch(*self.bev2ori_patch(x, y)),
('deepint', 'original'): self.deepi2ori_patch,
('deepint', 'bevfusion'): lambda x, y: self.ori2bev_patch(*self.deepi2ori_patch(x, y)),
('deepint', 'uvtr'): self.deepi2uvtr_patch,
('uvtr', 'bevfusion'): lambda x, y: self.ori2bev_patch(*self.uvtr2ori_patch(x, y)),
('uvtr', 'deepint'): lambda x, y: self.ori2deepi_patch(*self.uvtr2ori_patch(x, y)),
('uvtr', 'original'): self.uvtr2ori_patch,
('autoalign', 'original'): self.autoalign2ori_patch,
('original', 'autoalign'): self.ori2autoalign_patch,
('original', 'bevfusion'): self.ori2bev_patch,
('original', 'deepint'): self.ori2deepi_patch,
('original', 'uvtr'): self.ori2uvtr_patch,
}
try:
transform_func = transformations[(source, target)]
return transform_func(patch_img, mask)
except KeyError:
raise NotImplementedError(f"source: {source}, target: {target}")
# if source == target:
# return patch_img, mask
# elif source == 'bevfusion' and target == 'original':
# return self.bev2ori_patch(patch_img, mask)
# elif source == 'bevfusion' and target == 'deepint':
# return self.bev2deepint_patch(patch_img, mask)
# elif source == 'bevfusion' and target == 'uvtr':
# return self.bev2uvtr_patch(patch_img, mask)
# elif source == 'bevfusion' and target == 'autoalign':
# return self.ori2autoalign_patch(*self.bev2ori_patch(patch_img, mask))
# elif source == 'deepint' and target == 'original':
# return self.deepi2ori_patch(patch_img, mask)
# elif source == 'deepint' and target == 'bevfusion':
# return self.ori2bev_patch(*self.deepi2ori_patch(patch_img, mask))
# elif source == 'deepint' and target == 'uvtr':
# return self.deepi2uvtr_patch(patch_img, mask)
# elif source == 'uvtr' and target == 'bevfusion':
# return self.ori2bev_patch(*self.uvtr2ori_patch(patch_img, mask))
# elif source == 'uvtr' and target == 'deepint':
# return self.ori2deepi_patch(*self.uvtr2ori_patch(patch_img, mask))
# elif source == 'uvtr' and target == 'original':
# return self.uvtr2ori_patch(patch_img, mask)
# elif source == 'autoalign' and target == 'original':
# return self.autoalign2ori_patch(patch_img, mask)
# elif source == 'original' and target == 'autoalign':
# return self.ori2autoalign_patch(patch_img, mask)
# elif source == 'original' and target == 'bevfusion':
# return self.ori2bev_patch(patch_img, mask)
# else:
# raise NotImplementedError(f"source: {source}, target: {target}")