-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
266 lines (225 loc) · 10.1 KB
/
dataset.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
import os
from os.path import join, exists
from scipy.io import loadmat
import numpy as np
from random import randint, random
from collections import namedtuple
from PIL import Image
import torch
import torchvision.transforms as transforms
import torch.utils.data as data
from sklearn.neighbors import NearestNeighbors
import h5py
import random
from skimage import feature
import cv2
from matplotlib import pyplot as plt
# from utils.utils import *
def input_transform():
return transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
])
class KITTI(data.Dataset):
def __init__(self, root="/mnt/share_disk/KITTI/dataset", seq="02", n_neg=10, resize_shape=(800, 128)):
super().__init__()
self.root = root
self.n_neg = n_neg
self.distThr = 2
self.resize_shape = resize_shape
pose_path = os.path.join(root, "poses", seq+".txt")
self.poses = np.loadtxt(pose_path)
self.poses = np.array([self.poses[:,3], self.poses[:,7]]).transpose()
self.pose_q = []
self.pose_db = []
self.img_q = []
self.img_db = []
for i in range(3000):
self.img_q.append('sequences/'+seq+'/image_2/'+str(1000000+i)[1:]+'.png')
# self.img_q.append('sequences/'+seq+'/rgb/'+str(1000000+i)[1:]+'.npy')
self.pose_q.append(self.poses[i])
lidarn = "lidar3"
for i in range(3000):
self.img_db.append('sequences/'+seq+'/'+lidarn+'/'+str(1000000+i)[1:]+'.tiff')
self.pose_db.append(self.poses[i])
print(lidarn)
self.pose_q = np.array(self.pose_q)
self.pose_db = np.array(self.pose_db)
def __getitem__(self, index):
resize_shape = self.resize_shape
# query image
q = np.array(Image.open(os.path.join(self.root, self.img_q[index]))).astype(np.float32)[165:]
# q = np.load(os.path.join(self.root, self.img_q[index])).astype(np.float32).transpose([1,2,0])
q = cv2.resize(q, resize_shape)
# plt.imsave("crop.png", q, cmap='jet')
q = input_transform()(q)
# positive lidar
pos = np.array(Image.open(os.path.join(self.root, self.img_db[index]))).astype(np.float32)
# pos = np.load(os.path.join(self.root, self.img_db[index])).astype(np.float32)
pos = np.array([pos]).transpose([1,2,0]).repeat(3,2).astype(np.float32)
# pos[pos<0] = 0
pos = pos*255
pos = cv2.resize(pos, resize_shape)
pos = input_transform()(pos) #[3, 55, 400]
# negtive lidar
hard_mine = 1
neg = []
diff = self.pose_q[index] - self.pose_db
diff = np.linalg.norm(diff, 2, axis=1)
rang = np.arange(len(diff))[diff > self.distThr]
choice = np.random.choice(rang, 10)
for i in range(self.n_neg-hard_mine):
neg_one = np.array(Image.open(os.path.join(self.root, self.img_db[choice[i]]))).astype(np.float32)
# neg_one = np.load(os.path.join(self.root, self.img_db[choice[i]]))
neg_one = np.array([neg_one]).transpose([1,2,0]).repeat(3,2).astype(np.float32)
# neg_one[neg_one<0] = 0
neg_one = neg_one*255
neg_one = cv2.resize(neg_one, resize_shape)
neg_one = input_transform()(neg_one) #[3, 55, 400]
neg.append(neg_one)
rang = rang[np.argsort(diff[diff > self.distThr])]
for i in range(hard_mine): # hard mining
neg_one = np.array(Image.open(os.path.join(self.root, self.img_db[rang[i]]))).astype(np.float32)
# neg_one = np.load(os.path.join(self.root, self.img_db[rang[i]]))
neg_one = np.array([neg_one]).transpose([1,2,0]).repeat(3,2).astype(np.float32)
# neg_one[neg_one<0] = 0
neg_one = neg_one*255
neg_one = cv2.resize(neg_one, resize_shape)
neg_one = input_transform()(neg_one) #[3, 55, 400]
neg.append(neg_one)
neg = np.stack(neg) # [10, 3, 55, 400]
return q, pos, neg
def __len__(self):
return len(self.img_q)
class HaomoData(data.Dataset):
def __init__(self, root="/mnt/share_disk/HaomoData", n_neg=10, resize_shape=(800, 128)):
super().__init__()
self.root = root
self.n_neg = n_neg
self.distThr = 2
self.resize_shape = resize_shape
pose_path = os.path.join(root, "pose_kitti_fmt.json")
self.poses = np.loadtxt(pose_path)
self.poses = np.array([self.poses[:,4], self.poses[:,4]]).transpose()
self.poses = self.poses - np.min(self.poses, axis=0)
pair_path = os.path.join(root, "pair.txt")
with open(pair_path, 'r') as f:
pair = f.readlines()
for i in range(len(pair)):
pair[i] = pair[i].strip().split()
self.pair = pair
self.pose_q = []
self.pose_db = []
self.img_q = []
self.img_db = []
for i in range(len(self.pair)):
self.img_q.append(self.root+'/cam/'+self.pair[i][0])
self.pose_q.append(self.poses[int(self.pair[i][1][:5])])
for i in range(len(self.pair)):
self.img_db.append(self.root+'/lidar3/'+self.pair[i][1])
self.pose_db = self.pose_q
def __getitem__(self, index):
resize_shape = self.resize_shape
# query image
q = np.array(Image.open(self.img_q[index])).astype(np.float32)[700:-650, :]
# q = np.load(os.path.join(self.root, self.img_q[index])).astype(np.float32).transpose([1,2,0])
q = cv2.resize(q, resize_shape)
# plt.imsave("crop.png", q, cmap='jet')
q = input_transform()(q)
# positive lidar
pos = np.array(Image.open(self.img_db[index][:-3]+"tiff")).astype(np.float32)
# pos = np.load(os.path.join(self.root, self.img_db[index])).astype(np.float32)
pos = np.array([pos]).transpose([1,2,0]).repeat(3,2).astype(np.float32)
# pos[pos<0] = 0
pos = pos*255
pos = cv2.resize(pos, resize_shape)
pos = input_transform()(pos) #[3, 55, 400]
# negtive lidar
neg = []
diff = self.pose_q[index] - self.pose_db
diff = np.linalg.norm(diff, 2, axis=1)
rang = np.arange(len(diff))[diff > self.distThr]
choice = np.random.choice(rang, 10)
for i in range(self.n_neg):
neg_one = np.array(Image.open(self.img_db[choice[i]][:-3]+"tiff")).astype(np.float32)
# neg_one = np.load(os.path.join(self.root, self.img_db[choice[i]]))
neg_one = np.array([neg_one]).transpose([1,2,0]).repeat(3,2).astype(np.float32)
# neg_one[neg_one<0] = 0
neg_one = neg_one*255
neg_one = cv2.resize(neg_one, resize_shape)
neg_one = input_transform()(neg_one) # [3, 55, 400]
neg.append(neg_one)
neg = np.stack(neg) # [10, 3, 55, 400]
return q, pos, neg
def __len__(self):
return len(self.img_q)
class KITTIPRE(data.Dataset):
def __init__(self, root="/mnt/share_disk/KITTI/dataset", seq="00", n_neg=10, resize_shape=(800, 128)):
super().__init__()
self.root = root
self.n_neg = n_neg
self.distThr = 2
self.resize_shape = resize_shape
pose_path = os.path.join(root, "poses", seq+".txt")
self.poses = np.loadtxt(pose_path)
self.poses = np.array([self.poses[:,3], self.poses[:,7]]).transpose()
self.pose_q = []
self.pose_db = []
self.img_q = []
self.img_db = []
for i in range(3000):
self.img_q.append('sequences/'+seq+'/rgb/'+str(1000000+i)[1:]+'.npy')
self.pose_q.append(self.poses[i])
for i in range(3000):
self.img_db.append('sequences/'+seq+'/lidar/'+str(1000000+i)[1:]+'.npy')
self.pose_db.append(self.poses[i])
self.pose_q = np.array(self.pose_q)
self.pose_db = np.array(self.pose_db)
def __getitem__(self, index):
resize_shape = self.resize_shape
# query image
q = np.load(os.path.join(self.root, self.img_q[index])).astype(np.float32)
q = q.transpose([1,2,0])
q = cv2.resize(q, resize_shape)
q = input_transform()(q)
# positive lidar
pos = np.load(os.path.join(self.root, self.img_db[index]))
pos = np.array([pos]).transpose([1,2,0]).repeat(3,2).astype(np.float32)
pos[pos<0]=0
pos = pos/60.0*255
pos = cv2.resize(pos, resize_shape)
pos = input_transform()(pos) #[3, 55, 400]
# negtive lidar
hard_mine = 1
neg = []
diff = self.pose_q[index] - self.pose_db
diff = np.linalg.norm(diff, 2, axis=1)
rang = np.arange(len(diff))[diff > self.distThr]
choice = np.random.choice(rang, 10)
for i in range(self.n_neg-hard_mine):
neg_one = np.load(os.path.join(self.root, self.img_db[choice[i]]))
neg_one = np.array([neg_one]).transpose([1,2,0]).repeat(3,2).astype(np.float32)
neg_one[neg_one==-1]=0
neg_one = neg_one/60.0*255
neg_one = cv2.resize(neg_one, resize_shape)
neg_one = input_transform()(neg_one) #[3, 55, 400]
neg.append(neg_one)
rang = rang[np.argsort(diff[diff > self.distThr])]
for i in range(hard_mine): # hard mining
neg_one = np.load(os.path.join(self.root, self.img_db[rang[i]]))
neg_one = np.array([neg_one]).transpose([1,2,0]).repeat(3,2).astype(np.float32)
neg_one[neg_one==-1]=0
neg_one = neg_one/60.0*255
neg_one = cv2.resize(neg_one, resize_shape)
neg_one = input_transform()(neg_one) #[3, 55, 400]
neg.append(neg_one)
neg = np.stack(neg) # [10, 3, 55, 400]
return q, pos, neg
def __len__(self):
return len(self.img_q)
if __name__ == "__main__":
dataset = KITTI()
q, pos, neg = dataset.__getitem__(1)
print(len(dataset))
# print(q)