forked from marialeyvallina/generalized_contrastive_loss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasets.py
executable file
·445 lines (376 loc) · 19.6 KB
/
datasets.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
from torch.utils.data import Dataset
import json
import h5py
from scipy.spatial.distance import squareform
import torch
import numpy as np
import os
from PIL import Image
import math
import pandas as pd
from tqdm import tqdm
default_cities = {
'train': ["trondheim", "london", "boston", "melbourne", "amsterdam", "helsinki",
"tokyo", "toronto", "saopaulo", "moscow", "zurich", "paris", "bangkok",
"budapest", "austin", "berlin", "ottawa", "phoenix", "goa", "amman", "nairobi", "manila"],
'toy' :["london"],
'val': ["cph", "sf"],
'test': ["miami", "athens", "buenosaires", "stockholm", "bengaluru", "kampala"]
}
class BaseDataSet(Dataset):
def __init__(self, root_dir, idx_file, gt_file=None, ds_key="sim", transform=None):
"""
Args:
idx_file (string): Path to the idx file (.json)
gt_file (string): Path to the GT file with pairwise similarity (.h5).
ds_key (string): dataset name
transform (callable, optional): Optional transform to be applied
on a sample.
"""
self.im_paths = self.load_idx(idx_file)
self.root_dir = root_dir
self.ds_key=ds_key
if gt_file is not None:
with h5py.File(gt_file, "r") as f:
self.gt_matrix = torch.Tensor((f[ds_key][:].flatten()).astype(float))
self.transform = transform
self.n = len(self.im_paths)
@staticmethod
def load_idx(idx_file):
with open(idx_file) as f:
data = json.load(f)
root_dir = data["im_prefix"]
im_paths = data["im_paths"]
return im_paths
def __len__(self):
return len(self.im_paths)
def __getitem__(self, idx):
pass
def read_image(self, impath):
img_name = os.path.join(self.root_dir,
impath)
image = Image.open(img_name).convert('RGB')
if self.transform:
image = self.transform(image)
if image.shape[0] == 1:
image = image.repeat(3, 1, 1)
return image
class TestDataSet(BaseDataSet):
def __init__(self, root_dir, idx_file, transform=None):
super().__init__(root_dir, idx_file, None, transform=transform)
def __getitem__(self, idx_im):
return self.read_image(self.im_paths[idx_im])
class SiameseDataSet(BaseDataSet):
def __init__(self, root_dir, idx_file, gt_file, ds_key="sim", transform=None):
super().__init__(root_dir, idx_file, gt_file, ds_key, transform)
self.gt_matrix=squareform(self.gt_matrix)
def __getitem__(self, idx_im0):
if self.ds_key=="sim": #binary
s=np.random.choice([True, False], p=[0.5,0.5])#half positive, half negative
idx_im1=np.random.choice(np.where(self.gt_matrix[idx_im0,:]==s)[0])
else:#graded
s=np.random.choice(np.arange(3), p=[0.5,0.25,0.25])#half positive, quarter soft negative, quarter hard negative
if s==0:
idx_im1=np.random.choice(np.where(self.gt_matrix[idx_im0,:]>0.5)[0])
elif s==1:
idx_im1=np.random.choice(np.where(np.logical_and(self.gt_matrix[idx_im0,:]<0.5,self.gt_matrix[idx_im0,:]>0))[0])
else:
idx_im1=np.random.choice(np.where(self.gt_matrix[idx_im0,:]==0)[0])
sel_label=self.gt_matrix[idx_im0,idx_im1]
return {"im0": self.read_image(self.im_paths[idx_im0]),
"im1": self.read_image(self.im_paths[idx_im1]),
"label": float(sel_label)}
class SiameseDataSetByPairs(BaseDataSet):
def __init__(self, root_dir, idx_file, gt_file, ds_key="sim", transform=None):
super().__init__(root_dir, idx_file, gt_file, ds_key, transform)
def __len__(self):
return len(self.gt_matrix)
def __getitem__(self, idx_pair):
idx_im0, idx_im1 = self.condensed_to_square(idx_pair, self.n)
sel_label = self.gt_matrix[idx_pair]
return {"im0": self.read_image(self.im_paths[idx_im0]),
"im1": self.read_image(self.im_paths[idx_im1]),
"label": float(sel_label)}
class MSLSDataSet(Dataset):
def __init__(self, root_dir, cities, ds_key="fov", transform=None, cache_size=10000, mode="train", daynight=False):
self.cities = default_cities[cities]
self.root_dir = root_dir
self.transform=transform
self.ds_key = ds_key
self.start=0
self.cache_size=cache_size
self.daynight=daynight
if mode=="train":
self.load_cities()
if self.daynight:
self.load_daynight_idcs()
self.load_pairs_daynight()
else:
self.load_pairs()
def load_cities(self):
self.total=0
self.city_starting_idx={}
for c in self.cities:
gt_file = self.root_dir +"train_val/"+ c + "_gt.h5"
with h5py.File(gt_file, "r") as f:
gt = f["fov"]
self.city_starting_idx[self.total]=c
self.total+=gt.shape[0]
print(self.city_starting_idx)
def find_city(self, idx, city_starting_idx, total):
starting_list=list(city_starting_idx.keys())
#we figure out which one is our starting city
st=starting_list[0]
c=city_starting_idx[st]
next_st=starting_list[0]
for c_i in range(1,len(city_starting_idx.keys())):
if idx >= starting_list[c_i-1] and idx < starting_list[c_i]:
st=starting_list[c_i-1]
c=city_starting_idx[st]
next_st=starting_list[c_i]
break
st=starting_list[c_i]
c=city_starting_idx[st]
next_st=total
return st, c, next_st
def load_database_cache(self, nDescriptors=50000, nPerImage=100):
db_total=0
db_city_starting_idx={}
for c in self.cities:
gt_file = self.root_dir +"train_val/"+ c + "_gt.h5"
with h5py.File(gt_file, "r") as f:
gt = f["fov"]
db_city_starting_idx[db_total]=c
db_total+=gt.shape[1]
print(db_city_starting_idx)
#Determine how many images we're going to need
nIm=nDescriptors/nPerImage
db_idcs=sorted(torch.randperm(db_total,dtype=torch.int)[:int(nIm)])
#Get those images
st, c, next_st= self.find_city(db_idcs[0], db_city_starting_idx, db_total)
mfile = self.root_dir +"train_val/"+ c + "/database.json"
m_ims = BaseDataSet.load_idx(mfile)
if "_toy" in self.root_dir:
m_pano=np.genfromtxt(self.root_dir +"train_val/"+ c + "/database/raw_train.csv", dtype=bool, skip_header=1, delimiter=",")[:,-1]
else:
m_pano=np.genfromtxt(self.root_dir +"train_val/"+ c + "/database/raw.csv", dtype=bool, skip_header=1, delimiter=",")[:,-1]
cluster_ims=[]
for idx in tqdm(db_idcs, desc="loading clustering cache"):
city_qidx=idx-st
if idx>=next_st:
st, c, next_st= self.find_city(idx, db_city_starting_idx,db_total)
mfile = self.root_dir +"train_val/"+ c + "/database.json"
m_ims = BaseDataSet.load_idx(mfile)
if "_toy" in self.root_dir:
m_pano=np.genfromtxt(self.root_dir +"train_val/"+ c + "/database/raw_train.csv", dtype=bool, skip_header=1, delimiter=",")[:,-1]
else:
m_pano=np.genfromtxt(self.root_dir +"train_val/"+ c + "/database/raw.csv", dtype=bool, skip_header=1, delimiter=",")[:,-1]
city_qidx=idx-st
while m_pano[city_qidx]:#if we get a panorama we choose another image from the same city
city_qidx=np.random.choice(range(len(m_ims)))
cluster_ims.append(m_ims[city_qidx])
return cluster_ims
def load_daynight(self, file):
data=pd.read_csv(file)
return np.array(data["n2d"]==True),np.array(data["d2n"]==True)
def load_daynight_idcs(self):
self.d2n_idcs=[]
self.n2d_idcs=[]
for c in self.cities:
n2d,d2n=(self.load_daynight(self.root_dir+"train_val/"+ c + "/query/subtask_index.csv"))
self.d2n_idcs.extend(d2n)
self.n2d_idcs.extend(n2d)
self.d2n_idcs=np.where(self.d2n_idcs)[0]
self.n2d_idcs=np.where(self.n2d_idcs)[0]
def load_pairs_daynight(self):
#if self.start==0:
all_idcs=torch.randperm(self.total,dtype=torch.int)
self.idcs=np.hstack((self.n2d_idcs,np.random.choice(self.d2n_idcs,len(self.n2d_idcs)//2), all_idcs[:len(self.n2d_idcs)//2]))
self.idcs=self.idcs[torch.randperm(len(self.idcs))]
self.queries = []
self.matches = []
self.sims = []
queries_night=0
matches_night=0
#we get the next chunk and sort it (so that we can go by city)
cached_idcs=sorted(self.idcs)
samples=np.random.choice(np.arange(3), p=[0.5,0.25,0.25],size=len(cached_idcs))
st, c, next_st= self.find_city(cached_idcs[0], self.city_starting_idx, self.total)
qfile = self.root_dir +"train_val/"+ c + "/query.json"
mfile = self.root_dir +"train_val/"+ c + "/database.json"
gt_file = self.root_dir +"train_val/"+ c + "_gt.h5"
map_daynight,_=self.load_daynight(self.root_dir+"train_val/"+ c + "/database/subtask_index.csv")
f= h5py.File(gt_file, "r")
q_ims = BaseDataSet.load_idx(qfile)
m_ims = BaseDataSet.load_idx(mfile)
m_pano=np.genfromtxt(self.root_dir +"train_val/"+ c + "/database/raw.csv", dtype=bool, skip_header=1, delimiter=",")[:,-1]
q_pano=np.genfromtxt(self.root_dir +"train_val/"+ c + "/query/raw.csv", dtype=bool, skip_header=1, delimiter=",")[:,-1]
total_daynights=0
for idx in tqdm(cached_idcs, desc="loading cache"):
query_is_night=1 if idx in self.n2d_idcs else 0
city_qidx=idx-st
if idx>=next_st:
f.close()
st, c, next_st= self.find_city(idx, self.city_starting_idx, self.total)
gt_file = self.root_dir +"train_val/"+ c + "_gt.h5"
map_daynight,_=self.load_daynight(self.root_dir+"train_val/"+ c + "/database/subtask_index.csv")
f= h5py.File(gt_file, "r")
qfile = self.root_dir +"train_val/"+ c + "/query.json"
mfile = self.root_dir +"train_val/"+ c + "/database.json"
q_ims = BaseDataSet.load_idx(qfile)
m_ims = BaseDataSet.load_idx(mfile)
m_pano=np.genfromtxt(self.root_dir +"train_val/"+ c + "/database/raw.csv", dtype=bool, skip_header=1, delimiter=",")[:,-1]
q_pano=np.genfromtxt(self.root_dir +"train_val/"+ c + "/query/raw.csv", dtype=bool, skip_header=1, delimiter=",")[:,-1]
city_qidx=idx-st
if not q_pano[city_qidx]: #we skip panoramas
q_fovs = torch.Tensor(f[self.ds_key][city_qidx,:])
if self.ds_key=="fov":
s=np.random.choice(np.arange(3), p=[0.5,0.25,0.25])
if idx in self.d2n_idcs:##We try to select a night match
if s==0: #positive
idcs=np.where(np.logical_and(map_daynight,np.logical_and(q_fovs >= 0.5, np.logical_not(m_pano))))[0]
elif s==1: #soft negative
idcs=np.where(np.logical_and(map_daynight,np.logical_and(q_fovs < 0.5, np.logical_and(q_fovs > 0, np.logical_not(m_pano)))))[0]
elif s==2: #hard negative
idcs=np.where(np.logical_and(map_daynight,np.logical_and(q_fovs == 0, np.logical_not(m_pano))))[0]
if len(idcs) > 0:
total_daynights+=1
matches_night+=1
queries_night+=query_is_night
match_idx=np.random.choice(idcs)
self.queries.append(q_ims[city_qidx])
self.matches.append(m_ims[match_idx])
self.sims.append(q_fovs[match_idx])
else:
if s==0: #positive
idcs=np.where(np.logical_and(q_fovs >= 0.5, np.logical_not(m_pano)))[0]
elif s==1: #soft negative
idcs=np.where(np.logical_and(q_fovs < 0.5, np.logical_and(q_fovs > 0, np.logical_not(m_pano))))[0]
elif s==2: #hard negative
idcs=np.where(np.logical_and(q_fovs == 0, np.logical_not(m_pano)))[0]
if len(idcs) > 0:
match_idx=np.random.choice(idcs)
queries_night+=(query_is_night)
self.queries.append(q_ims[city_qidx])
self.matches.append(m_ims[match_idx])
self.sims.append(q_fovs[match_idx])
else:
s=np.random.choice(np.arange(2))
if s==0: #positive
idcs=np.where(np.logical_and(q_fovs == 1, np.logical_not(m_pano)))[0]
elif s==1: #negative
idcs=np.where(np.logical_and(q_fovs == 0, np.logical_not(m_pano)))[0]
if len(idcs) > 0:
match_idx=np.random.choice(idcs)
self.queries.append(q_ims[city_qidx])
self.matches.append(m_ims[match_idx])
self.sims.append(q_fovs[match_idx])
f.close()
#self.start+=self.cache_size
#if self.start>=len(self.idcs):
#print(self.start, self.idcs)
self.start=0
self.queries = np.asarray(self.queries)
self.matches = np.asarray(self.matches)
self.sims = np.asarray(self.sims)
print(len(self.queries), len(self.matches), len(self.sims))
print(queries_night, matches_night)
assert len(self.queries) == len(self.matches) == len(self.sims)
def load_pairs(self):
if self.start==0:
self.idcs=torch.randperm(self.total,dtype=torch.int)
#this every cache_size iters
self.queries = []
self.matches = []
self.sims = []
#we get the next chunk and sort it (so that we can go by city)
cached_idcs=sorted(self.idcs[self.start:self.start+self.cache_size])
samples=np.random.choice(np.arange(3), p=[0.5,0.25,0.25],size=len(cached_idcs))
st, c, next_st= self.find_city(cached_idcs[0], self.city_starting_idx, self.total)
qfile = self.root_dir +"train_val/"+ c + "/query.json"
mfile = self.root_dir +"train_val/"+ c + "/database.json"
gt_file = self.root_dir +"train_val/"+ c + "_gt.h5"
f= h5py.File(gt_file, "r")
q_ims = BaseDataSet.load_idx(qfile)
m_ims = BaseDataSet.load_idx(mfile)
if "_toy" in self.root_dir:
m_pano=np.genfromtxt(self.root_dir +"train_val/"+ c + "/database/raw_train.csv", dtype=bool, skip_header=1, delimiter=",")[:,-1]
q_pano=np.genfromtxt(self.root_dir +"train_val/"+ c + "/query/raw_train.csv", dtype=bool, skip_header=1, delimiter=",")[:,-1]
else:
m_pano=np.genfromtxt(self.root_dir +"train_val/"+ c + "/database/raw.csv", dtype=bool, skip_header=1, delimiter=",")[:,-1]
q_pano=np.genfromtxt(self.root_dir +"train_val/"+ c + "/query/raw.csv", dtype=bool, skip_header=1, delimiter=",")[:,-1]
for idx in tqdm(cached_idcs, desc="loading cache"):
city_qidx=idx-st
if idx>=next_st:
f.close()
st, c, next_st= self.find_city(idx, self.city_starting_idx, self.total)
gt_file = self.root_dir +"train_val/"+ c + "_gt.h5"
f= h5py.File(gt_file, "r")
qfile = self.root_dir +"train_val/"+ c + "/query.json"
mfile = self.root_dir +"train_val/"+ c + "/database.json"
q_ims = BaseDataSet.load_idx(qfile)
m_ims = BaseDataSet.load_idx(mfile)
if "_toy" in self.root_dir:
m_pano=np.genfromtxt(self.root_dir +"train_val/"+ c + "/database/raw_train.csv", dtype=bool, skip_header=1, delimiter=",")[:,-1]
q_pano=np.genfromtxt(self.root_dir +"train_val/"+ c + "/query/raw_train.csv", dtype=bool, skip_header=1, delimiter=",")[:,-1]
else:
m_pano=np.genfromtxt(self.root_dir +"train_val/"+ c + "/database/raw.csv", dtype=bool, skip_header=1, delimiter=",")[:,-1]
q_pano=np.genfromtxt(self.root_dir +"train_val/"+ c + "/query/raw.csv", dtype=bool, skip_header=1, delimiter=",")[:,-1]
city_qidx=idx-st
if not q_pano[city_qidx]: #we skip panoramas
q_fovs = torch.Tensor(f[self.ds_key][city_qidx,:])
if self.ds_key=="fov":
s=np.random.choice(np.arange(3), p=[0.5,0.25,0.25])
if s==0: #positive
idcs=np.where(np.logical_and(q_fovs >= 0.5, np.logical_not(m_pano)))[0]
elif s==1: #soft negative
idcs=np.where(np.logical_and(q_fovs < 0.5, np.logical_and(q_fovs > 0, np.logical_not(m_pano))))[0]
elif s==2: #hard negative
idcs=np.where(np.logical_and(q_fovs == 0, np.logical_not(m_pano)))[0]
if len(idcs) > 0:
match_idx=np.random.choice(idcs)
self.queries.append(q_ims[city_qidx])
self.matches.append(m_ims[match_idx])
self.sims.append(q_fovs[match_idx])
else:
s=np.random.choice(np.arange(2))
if s==0: #positive
idcs=np.where(np.logical_and(q_fovs == 1, np.logical_not(m_pano)))[0]
elif s==1: #negative
idcs=np.where(np.logical_and(q_fovs == 0, np.logical_not(m_pano)))[0]
if len(idcs) > 0:
match_idx=np.random.choice(idcs)
self.queries.append(q_ims[city_qidx])
self.matches.append(m_ims[match_idx])
self.sims.append(q_fovs[match_idx])
f.close()
self.start+=self.cache_size
if self.start>=self.total:
self.start=0
self.queries = np.asarray(self.queries)
self.matches = np.asarray(self.matches)
self.sims = np.asarray(self.sims)
print(len(self.queries), len(self.matches), len(self.sims))
assert len(self.queries) == len(self.matches) == len(self.sims)
def __len__(self):
return len(self.queries)
def read_image(self, impath):
img_name = os.path.join(self.root_dir,
impath)
image = Image.open(img_name).convert('RGB')
if self.transform:
image = self.transform(image)
if image.shape[0] == 1:
image = image.repeat(3, 1, 1)
return image
def __getitem__(self, idx):
sample = {"im0": self.read_image(self.queries[idx]), "im1": self.read_image(self.matches[idx]),
"label": self.sims[idx]}
return sample
class ListImageDataSet(BaseDataSet):
def __init__(self, image_list, transform=None, root_dir=None):
self.im_paths=image_list
self.transform=transform
self.root_dir=root_dir
def __getitem__(self, idx_im):
return self.read_image(self.root_dir+self.im_paths[idx_im])