-
Notifications
You must be signed in to change notification settings - Fork 460
/
Copy pathcollate.py
161 lines (146 loc) · 6.55 KB
/
collate.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
import collections
from collections import defaultdict
import numpy as np
import torch
import torch.nn.functional as F
from torch.utils.data.dataloader import default_collate
from .data_container import DataContainer
def collate(batch, samples_per_gpu=1):
"""Puts each data field into a tensor/DataContainer with outer dimension
batch size.
Extend default_collate to add support for
:type:`~torchie.parallel.DataContainer`. There are 3 cases.
1. cpu_only = True, e.g., meta data
2. cpu_only = False, stack = True, e.g., images tensors
3. cpu_only = False, stack = False, e.g., gt bboxes
"""
if not isinstance(batch, collections.Sequence):
raise TypeError("{} is not supported.".format(batch.dtype))
if isinstance(batch[0], DataContainer):
assert len(batch) % samples_per_gpu == 0
stacked = []
if batch[0].cpu_only:
for i in range(0, len(batch), samples_per_gpu):
stacked.append(
[sample.data for sample in batch[i : i + samples_per_gpu]]
)
return DataContainer(
stacked, batch[0].stack, batch[0].padding_value, cpu_only=True
)
elif batch[0].stack:
for i in range(0, len(batch), samples_per_gpu):
assert isinstance(batch[i].data, torch.Tensor)
if batch[i].pad_dims is not None:
ndim = batch[i].dim()
assert ndim > batch[i].pad_dims
max_shape = [0 for _ in range(batch[i].pad_dims)]
for dim in range(1, batch[i].pad_dims + 1):
max_shape[dim - 1] = batch[i].size(-dim)
for sample in batch[i : i + samples_per_gpu]:
for dim in range(0, ndim - batch[i].pad_dims):
assert batch[i].size(dim) == sample.size(dim)
for dim in range(1, batch[i].pad_dims + 1):
max_shape[dim - 1] = max(
max_shape[dim - 1], sample.size(-dim)
)
padded_samples = []
for sample in batch[i : i + samples_per_gpu]:
pad = [0 for _ in range(batch[i].pad_dims * 2)]
for dim in range(1, batch[i].pad_dims + 1):
pad[2 * dim - 1] = max_shape[dim - 1] - sample.size(-dim)
padded_samples.append(
F.pad(sample.data, pad, value=sample.padding_value)
)
stacked.append(default_collate(padded_samples))
elif batch[i].pad_dims is None:
stacked.append(
default_collate(
[sample.data for sample in batch[i : i + samples_per_gpu]]
)
)
else:
raise ValueError("pad_dims should be either None or integers (1-3)")
else:
for i in range(0, len(batch), samples_per_gpu):
stacked.append(
[sample.data for sample in batch[i : i + samples_per_gpu]]
)
return DataContainer(stacked, batch[0].stack, batch[0].padding_value)
elif isinstance(batch[0], collections.Sequence):
transposed = zip(*batch)
return [collate(samples, samples_per_gpu) for samples in transposed]
elif isinstance(batch[0], collections.Mapping):
return {
key: collate([d[key] for d in batch], samples_per_gpu) for key in batch[0]
}
else:
return default_collate(batch)
def collate_kitti(batch_list, samples_per_gpu=1):
example_merged = collections.defaultdict(list)
for example in batch_list:
if type(example) is list:
for subexample in example:
for k, v in subexample.items():
example_merged[k].append(v)
else:
for k, v in example.items():
example_merged[k].append(v)
batch_size = len(example_merged['metadata'])
ret = {}
# voxel_nums_list = example_merged["num_voxels"]
# example_merged.pop("num_voxels")
for key, elems in example_merged.items():
if key in ["voxels", "num_points", "num_gt", "voxel_labels", "num_voxels",
"cyv_voxels", "cyv_num_points", "cyv_num_voxels"]:
ret[key] = torch.tensor(np.concatenate(elems, axis=0))
elif key in [
"gt_boxes",
]:
task_max_gts = []
for task_id in range(len(elems[0])):
max_gt = 0
for k in range(batch_size):
max_gt = max(max_gt, len(elems[k][task_id]))
task_max_gts.append(max_gt)
res = []
for idx, max_gt in enumerate(task_max_gts):
batch_task_gt_boxes3d = np.zeros((batch_size, max_gt, 7))
for i in range(batch_size):
batch_task_gt_boxes3d[i, : len(elems[i][idx]), :] = elems[i][idx]
res.append(batch_task_gt_boxes3d)
ret[key] = res
elif key == "metadata":
ret[key] = elems
elif key == "calib":
ret[key] = {}
for elem in elems:
for k1, v1 in elem.items():
if k1 not in ret[key]:
ret[key][k1] = [v1]
else:
ret[key][k1].append(v1)
for k1, v1 in ret[key].items():
ret[key][k1] = torch.tensor(np.stack(v1, axis=0))
elif key in ["coordinates", "points", "cyv_coordinates"]:
coors = []
for i, coor in enumerate(elems):
coor_pad = np.pad(
coor, ((0, 0), (1, 0)), mode="constant", constant_values=i
)
coors.append(coor_pad)
ret[key] = torch.tensor(np.concatenate(coors, axis=0))
elif key in ["anchors", "anchors_mask", "reg_targets", "reg_weights", "labels", "hm", "anno_box",
"ind", "mask", "cat"]:
ret[key] = defaultdict(list)
res = []
for elem in elems:
for idx, ele in enumerate(elem):
ret[key][str(idx)].append(torch.tensor(ele))
for kk, vv in ret[key].items():
res.append(torch.stack(vv))
ret[key] = res
elif key == 'gt_boxes_and_cls':
ret[key] = torch.tensor(np.stack(elems, axis=0))
else:
ret[key] = np.stack(elems, axis=0)
return ret