-
Notifications
You must be signed in to change notification settings - Fork 3
/
dataio.py
executable file
·314 lines (251 loc) · 12.6 KB
/
dataio.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
import cv2
import os
import torch
import imageio
import numpy as np
from glob import glob
import data_util
import util
from collections import defaultdict
import skimage.filters
from pdb import set_trace as pdb
from itertools import combinations
from random import choice
import matplotlib.pyplot as plt
class SceneInstanceDataset(torch.utils.data.Dataset):
"""This creates a dataset class for a single object instance (such as a single car)."""
def __init__(self,
instance_idx,
instance_dir,
specific_observation_idcs=None,
input_img_sidelength=None,
img_sidelength=None,
num_images=None,
use_seg=False,
num_seg=-1,
use_depth=False,
pose2=False,
cache=None):
self.instance_idx = instance_idx
self.img_sidelength = img_sidelength
self.input_img_sidelength = input_img_sidelength
self.instance_dir = instance_dir
self.cache = cache
self.interp_mode = cv2.INTER_LINEAR
pose_dir = os.path.join(instance_dir, "pose")
color_dir = os.path.join(instance_dir, "rgb")
if not os.path.isdir(color_dir):
print("Error! root dir %s is wrong" % instance_dir)
return
self.color_paths = sorted(data_util.glob_imgs(color_dir))
self.pose_paths = sorted(glob(os.path.join(pose_dir, "*.txt")))
self.instance_name = os.path.basename(os.path.dirname(self.instance_dir))
if specific_observation_idcs is not None:
self.color_paths = util.pick(self.color_paths, specific_observation_idcs)
self.pose_paths = util.pick(self.pose_paths, specific_observation_idcs)
elif num_images is not None:
idcs = np.linspace(0, stop=len(self.color_paths), num=num_images, endpoint=False, dtype=int)
self.color_paths = util.pick(self.color_paths, idcs)
self.pose_paths = util.pick(self.pose_paths, idcs)
dummy_img = data_util.load_rgb(self.color_paths[0])
self.org_sidelength = dummy_img.shape[1]
if self.org_sidelength < self.img_sidelength:
uv = np.mgrid[0:self.img_sidelength, 0:self.img_sidelength].astype(np.int32).transpose(1, 2, 0)
self.intrinsics, _, _, _ = util.parse_intrinsics(os.path.join(self.instance_dir, "intrinsics.txt"),
trgt_sidelength=self.img_sidelength)
else:
uv = np.mgrid[0:self.org_sidelength, 0:self.org_sidelength].astype(float).transpose(1, 2, 0)
uv = cv2.resize(uv, (self.img_sidelength, self.img_sidelength), interpolation=self.interp_mode)
context_uv = cv2.resize(uv, (64,64), interpolation=self.interp_mode)
self.intrinsics, _, _, _ = util.parse_intrinsics(os.path.join(self.instance_dir, "intrinsics.txt"),
trgt_sidelength=self.org_sidelength)
uv = torch.from_numpy(np.flip(uv, axis=-1).copy()).long()
self.uv = uv.reshape(-1, 2).float()
context_uv = torch.from_numpy(np.flip(context_uv, axis=-1).copy()).long()
self.context_uv = context_uv.reshape(-1, 2).float()
self.intrinsics = torch.Tensor(self.intrinsics).float()
def set_img_sidelength(self, new_img_sidelength):
"""For multi-resolution training: Updates the image sidelength with whichimages are loaded."""
self.img_sidelength = new_img_sidelength
def __len__(self):
return min(len(self.pose_paths), len(self.color_paths))
def __getitem__(self, idx, context=False,input_context=True):
base_dir = self.color_paths
key = f'{self.instance_idx}_{idx}'
if (self.cache is not None) and (key in self.cache):
rgb, pose = self.cache[key]
else:
rgb = data_util.load_rgb(base_dir[idx])
pose = data_util.load_pose(self.pose_paths[idx])
if (self.cache is not None) and (key not in self.cache):
self.cache[key] = rgb, pose
### Context img at at 128x128
img_sidelength=self.img_sidelength
if context and input_context: img_sidelength=128
rgb = cv2.resize(rgb, (img_sidelength, img_sidelength), interpolation=self.interp_mode)
rgb = rgb.reshape(-1, 3)
sample = {
"instance_idx": torch.Tensor([self.instance_idx]).squeeze().long(),
"rgb": torch.from_numpy(rgb).float(),
"cam2world": torch.from_numpy(pose).float(),
"imgname":self.color_paths[idx],
"uv": self.uv,
"context_uv": self.context_uv,
"intrinsics": self.intrinsics,
"height_width": torch.from_numpy(np.array([self.img_sidelength, self.img_sidelength])),
"instance_name": self.instance_name
}
# Dataset optional vars
for var,name in []:
if var is not None: sample[name]=var
return sample
def get_instance_datasets(root, max_num_instances=None, specific_observation_idcs=None,
cache=None, sidelen=None, max_observations_per_instance=None):
instance_dirs = sorted(glob(os.path.join(root, "*/")))
assert (len(instance_dirs) != 0), f"No objects in the directory {root}"
if max_num_instances != None:
instance_dirs = instance_dirs[:max_num_instances]
all_instances = [SceneInstanceDataset(instance_idx=idx, instance_dir=dir,
specific_observation_idcs=specific_observation_idcs, img_sidelength=sidelen,
cache=cache, num_images=max_observations_per_instance)
for idx, dir in enumerate(instance_dirs)]
return all_instances
class SceneClassDataset(torch.utils.data.Dataset):
"""Dataset for a class of objects, where each datapoint is a SceneInstanceDataset."""
def __init__(self,
num_context, num_trgt, root_dir,
vary_context_number=False,
query_sparsity=None,
img_sidelength=None,
input_img_sidelength=None,
max_num_instances=None,
max_observations_per_instance=None,
specific_observation_idcs=None,
test=False,
test_context_idcs=None,
context_is_last=False,
context_is_first=False,
cache=None,
use_seg=False,
use_depth=False,
pose2=False,
num_seg=-1,
):
self.num_context = num_context
self.num_trgt = num_trgt
self.query_sparsity = query_sparsity
self.img_sidelength = img_sidelength
self.vary_context_number = vary_context_number
self.cache = cache
self.test = test
self.test_context_idcs = test_context_idcs
self.context_is_last = context_is_last
self.context_is_first = context_is_first
self.instance_dirs = sorted(glob(os.path.join(root_dir, "*/")))
print(f"Root dir {root_dir}, {len(self.instance_dirs)} instances")
assert (len(self.instance_dirs) != 0), "No objects in the data directory"
if max_num_instances is not None:
self.instance_dirs = self.instance_dirs[:max_num_instances]
#subdirs = set([x.split("/")[-2].split("_")[0]+"_0" for x in self.instance_dirs])
#subdirs = self.instance_dirs
#self.instance_dirs = [os.path.join(root_dir,subdir) for subdir in subdirs]
self.all_instances = [SceneInstanceDataset(instance_idx=idx,
instance_dir=dir,
specific_observation_idcs=specific_observation_idcs,
img_sidelength=img_sidelength,
input_img_sidelength=input_img_sidelength,
num_images=max_observations_per_instance,
cache=cache,
use_seg=use_seg,
use_depth=use_depth,
num_seg=num_seg,
pose2=pose2,
)
for idx, dir in enumerate(self.instance_dirs)
]
self.num_per_instance_observations = [len(obj) for obj in self.all_instances]
self.num_instances = len(self.all_instances)
def sparsify(self, dict, sparsity):
new_dict = {}
if sparsity is None:
return dict
else:
# Sample upper_limit pixel idcs at random.
rand_idcs = np.random.choice(self.img_sidelength**2, size=sparsity, replace=False)
for key in ['rgb', 'uv']:
new_dict[key] = dict[key][rand_idcs]
for key, v in dict.items():
if key not in ['rgb', 'uv']:
new_dict[key] = dict[key]
return new_dict
def set_img_sidelength(self, new_img_sidelength):
"""For multi-resolution training: Updates the image sidelength with which images are loaded."""
self.img_sidelength = new_img_sidelength
for instance in self.all_instances:
instance.set_img_sidelength(new_img_sidelength)
def __len__(self):
return self.num_instances if self.test else np.sum(self.num_per_instance_observations)
def get_instance_idx(self, idx):
if self.test:
obj_idx = 0
while idx >= 0:
idx -= self.num_per_instance_observations[obj_idx]
obj_idx += 1
return obj_idx - 1, int(idx + self.num_per_instance_observations[obj_idx - 1])
else:
return np.random.randint(self.num_instances), 0
def collate_fn(self, batch_list):
keys = batch_list[0].keys()
result = defaultdict(list)
for entry in batch_list:
# make them all into a new dict
for key in keys:
result[key].append(entry[key])
for key in keys:
try:
result[key] = torch.stack(result[key], dim=0)
except:
continue
return result
def __getitem__(self, idx,sceneidx=None):
context = []
trgt = []
post_input = []
obj_idx, det_idx = self.get_instance_idx(idx)
if sceneidx is not None:
obj_idx, det_idx = sceneidx,sceneidx
if self.vary_context_number:
num_context = np.random.randint(1, self.num_context+1)
sample_idcs = np.random.choice(len(self.all_instances[obj_idx]), replace=False,
size=self.num_trgt)
if not self.test:
for i in range(self.num_context):
idx = -(i+1) if self.context_is_last else 0 if self.context_is_first else sample_idcs[i]
sample = self.all_instances[obj_idx].__getitem__(idx,context=True,input_context=True)
context.append(sample)
for i in range(self.num_trgt):
# Mod adding context img to query imgs
if i<self.num_context:
idx = -(i+1) if self.context_is_last else 0 if self.context_is_first else sample_idcs[i]
sample = self.all_instances[obj_idx].__getitem__(idx,context=True,input_context=False)
else:
sample = self.all_instances[obj_idx][sample_idcs[i]]
post_input.append(sample)
post_input[-1]['mask'] = torch.Tensor([1.])
sub_sample = self.sparsify(sample, self.query_sparsity)
trgt.append(sub_sample)
else:
sample = self.all_instances[obj_idx].__getitem__(0,context=True,input_context=True)
context.append(sample)
for i in range(self.num_trgt):
sample = self.all_instances[obj_idx][i]
post_input.append(sample)
post_input[-1]['mask'] = torch.Tensor([1.])
sub_sample = self.sparsify(sample, self.query_sparsity)
trgt.append(sub_sample)
post_input = self.collate_fn(post_input)
trgt = self.collate_fn(trgt)
out_dict = {'query': trgt, 'post_input': post_input,'context':None}, trgt
if self.num_context:
out_dict[0]["context"] = self.collate_fn(context)
return out_dict