-
Notifications
You must be signed in to change notification settings - Fork 8
/
DataLoader.py
402 lines (342 loc) · 17.8 KB
/
DataLoader.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
import torch
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
import random
import numpy as np
import os
import pickle
import ipdb
from utils import read_roidb, box_id, get_box_feats
class VrdPredDataset(Dataset):
"""docstring for VrdPred"""
def __init__(self, mode = 'train', feat_mode = 'full', prior = False, ori_vgg=False, use_loc=False):
super(VrdPredDataset, self).__init__()
self.num_nodes = 21
self.num_node_types = 101
self.num_edge_types = 71
self.num_edges = 41 #41 #30 #91
if mode == 'train':
self.mode = 'train'
else:
self.mode = 'test'
self.feat_mode = feat_mode
self.prior = prior
# ----------- senmantic feature ------------- #
self.predicates_vec = np.load('./data/vrd_predicates_vec.npy')
self.objects_vec = np.load('./data/vrd_objects_vec.npy')
# ------------ original roidb feature --------#
self.roidb_read = read_roidb('./data/vrd_pred_graph_roidb.npz')
self.roidb = self.roidb_read[self.mode]
# Exclude self edges
self.off_diag_idx = np.ravel_multi_index(
np.where(np.ones((self.num_nodes, self.num_nodes)) - np.eye(self.num_nodes)),
[self.num_nodes, self.num_nodes])
# ------------ prior probability ------------- #
# shape: [100, 100, 70] sum of the last dimension is 1
f = open('./data/vrd_so_prior.pkl', 'rb')
f.seek(0)
self.rel_so_prior = pickle.load(f, encoding='bytes') #[100, 100, 70]
# ------------- prior of the existance of current [sub, obj] pair ---#
# shape: [100, 100] sum=1
self.prior_probs = np.load('./data/vrd_prior_prob.npy', encoding='bytes')
self.use_loc = use_loc
def get_adj(self, roidb_use):
bbox_coordinates = np.zeros([self.num_edges, 20])
matrix = np.eye(self.num_nodes)
rel_rec = np.zeros([self.num_edges, self.num_nodes])
rel_send = np.zeros([self.num_edges, self.num_nodes])
sub_idx = box_id(roidb_use['sub_box_gt'], roidb_use['uni_box_gt'])
obj_idx = box_id(roidb_use['obj_box_gt'], roidb_use['uni_box_gt'])
for i in range(len(sub_idx)):
sub_id = int(sub_idx[i])
obj_id = int(obj_idx[i])
rel_rec[i] = matrix[obj_id]
rel_send[i] = matrix[sub_id]
bbox_coordinates[i] = get_box_feats(roidb_use['uni_box_gt'][sub_id], roidb_use['uni_box_gt'][obj_id])
# --------- cross entropy loss ---------#
edges = np.zeros(self.num_edges) + self.num_edge_types - 1
edges[:len(roidb_use['rela_gt'])] = roidb_use['rela_gt']
edges = np.array(edges, dtype=np.int64)
node_cls = np.zeros(self.num_nodes) + self.num_node_types - 1
node_cls[:len(roidb_use['uni_gt'])] = roidb_use['uni_gt']
node_cls = np.array(node_cls, dtype=np.int64)
return edges, node_cls, rel_rec, rel_send, bbox_coordinates
def train_item(self, roidb_use):
if self.feat_mode == 'full':
# --------- node feature ------------#
feats = np.load(roidb_use['uni_fc7'])
w2vec = list(map(lambda x: self.objects_vec[int(x)], roidb_use['uni_gt']))
w2vec = np.reshape(np.array(w2vec),[-1, 300])
nodes = np.zeros([self.num_nodes, 4396])
nodes[:feats.shape[0], :4096] = feats
nodes[:feats.shape[0], 4096:] = w2vec # [self.num_nodes, 4096+300]
elif self.feat_mode == 'vis':
feats = np.load(roidb_use['uni_fc7'])
nodes = np.zeros([self.num_nodes, 4096])
nodes[:feats.shape[0]] = feats
elif self.feat_mode == 'sem':
w2vec = list(map(lambda x: self.objects_vec[int(x)], roidb_use['uni_gt']))
w2vec = np.reshape(np.array(w2vec),[-1, 300])
nodes = np.zeros([self.num_nodes, 300])
nodes[:w2vec.shape[0]] = w2vec
prior_matrix = np.zeros([self.num_edges, self.num_edge_types])-0.5/self.num_edge_types
for i in range(len(roidb_use['rela_gt'])):
sub_cls = int(roidb_use['sub_gt'][i])
obj_cls = int(roidb_use['obj_gt'][i])
current_prior = self.rel_so_prior[sub_cls, obj_cls]
# current_prior = -0.5*(current_prior+1.0/self.num_edge_types)
current_prior = -0.5*(1.0/self.num_edge_types)
prior_matrix[i, :(self.num_edge_types-1)] = current_prior
# ------ region vgg feature --- initialize edge feature ---------#
# sub_idx = box_id(roidb_use['sub_box_gt'], roidb_use['uni_box_gt'])
# obj_idx = box_id(roidb_use['obj_box_gt'], roidb_use['uni_box_gt'])
edge_feats = np.zeros([self.num_edges, 512])
pred_fc7 = np.load(roidb_use['pred_fc7'])
edge_feats[:len(roidb_use['rela_gt'])] = pred_fc7
return nodes, edge_feats, prior_matrix
def __getitem__(self, index):
roidb_use = self.roidb[index]
nodes, edge_feats, prior_matrix = self.train_item(roidb_use)
edges, node_cls, rel_rec, rel_send, bbox_coordinates = self.get_adj(roidb_use)
bbox_coordinates = torch.FloatTensor(bbox_coordinates)
nodes = torch.FloatTensor(nodes)
edges = torch.LongTensor(edges)
node_cls = torch.LongTensor(node_cls)
edge_feats = torch.FloatTensor(edge_feats)
rel_rec = torch.FloatTensor(rel_rec)
rel_send = torch.FloatTensor(rel_send)
prior_matrix = torch.FloatTensor(prior_matrix)
if self.prior:
return nodes, edges, node_cls, edge_feats, rel_rec, rel_send, bbox_coordinates, prior_matrix
else:
return nodes, edges, node_cls, edge_feats, rel_rec, rel_send
def __len__(self):
return len(self.roidb)
class VrdRelaDataset(Dataset):
"""docstring for VrdRela"""
def __init__(self, mode = 'train', feat_mode = 'full', prior=False, ori_vgg=False, use_loc=False):
super(VrdRelaDataset, self).__init__()
self.num_nodes = 21 #44 #21
self.num_edges = 41 #30 #170
self.num_node_types = 101
self.num_edge_types = 71
self.feat_mode = feat_mode
self.prior = prior
if mode == 'train':
self.mode = 'train'
else:
self.mode = 'test'
# if mode == 'test':
self.num_nodes = 96 #63
self.num_edges = self.num_nodes * (self.num_nodes-1)
# ----------- senmantic feature ------------- #
self.predicates_vec = np.load('./data/vrd_predicates_vec.npy')
self.objects_vec = np.load('./data/vrd_objects_vec.npy')
# ------------ original roidb feature --------#
self.roidb_read = read_roidb('./data/vrd_rela_graph_roidb_iou_dis_{}_{}.npz'.format(0.5*10, 0.45*10))
self.roidb = self.roidb_read[self.mode]
# Exclude self edges
self.off_diag_idx = np.ravel_multi_index(
np.where(np.ones((self.num_nodes, self.num_nodes)) - np.eye(self.num_nodes)),
[self.num_nodes, self.num_nodes])
# ------------ prior probability ------------- #
self.prior = prior
f = open('./data/vrd_so_prior.pkl', 'rb')
f.seek(0)
self.rel_so_prior = pickle.load(f, encoding='bytes') #[100, 100, 70]
self.use_loc = use_loc
def get_adj(self, roidb_use):
bbox_coordinates = np.zeros([self.num_edges, 20])
matrix = np.eye(self.num_nodes)
rel_rec = np.zeros([self.num_edges, self.num_nodes])
rel_send = np.zeros([self.num_edges, self.num_nodes])
sub_idx = box_id(roidb_use['sub_box_dete'], roidb_use['uni_box_gt'])
obj_idx = box_id(roidb_use['obj_box_dete'], roidb_use['uni_box_gt'])
for i in range(len(sub_idx)):
sub_id = int(sub_idx[i])
obj_id = int(obj_idx[i])
rel_rec[i] = matrix[obj_id]
rel_send[i] = matrix[sub_id]
bbox_coordinates[i] = get_box_feats(roidb_use['uni_box_gt'][sub_id], roidb_use['uni_box_gt'][obj_id])
edges = np.zeros(self.num_edges) + self.num_edge_types-1
edges[:len(roidb_use['rela_dete'])] = roidb_use['rela_dete']
edges = np.array(edges, dtype=np.int64)
node_cls = np.zeros(self.num_nodes) + self.num_node_types-1
node_cls[:len(roidb_use['uni_gt'])] = roidb_use['uni_gt']
node_cls = np.array(node_cls, dtype=np.int64)
return edges, node_cls, rel_rec, rel_send, bbox_coordinates
def train_item(self, roidb_use):
# --------- node feature ------------#
feats = np.load(roidb_use['uni_fc7'])
w2vec = list(map(lambda x: self.objects_vec[int(x)], roidb_use['uni_gt']))
w2vec = np.reshape(np.array(w2vec),[-1, 300])
if feats.shape[0] > self.num_nodes:
index_box = np.sort(random.sample(range(feats.shape[0]), self.num_nodes))
feats = feats[index_box, :]
w2vec = w2vec[index_box, :]
if self.feat_mode == 'full':
nodes = np.concatenate([feats, w2vec], 1) # [self.num_nodes, 4096+300]
elif self.feat_mode == 'vis':
nodes = feats
elif self.feat_mode == 'sem':
nodes = w2vec
# --------- edge feature ------------#
# edge_idx = roidb_use['edge_matrix'][index_box, :]
# edge_idx = edge_idx[:, index_box] # [self.num_nodes, self.num_nodes]
else:
if self.feat_mode == 'full':
nodes = np.zeros([self.num_nodes, 4396])
nodes[:feats.shape[0], :4096] = feats
nodes[:feats.shape[0], 4096:] = w2vec # [self.num_nodes, 4096+300]
elif self.feat_mode == 'vis':
nodes = np.zeros([self.num_nodes, 4096])
nodes[:feats.shape[0]] = feats
elif self.feat_mode == 'sem':
nodes = np.zeros([self.num_nodes, 300])
nodes[:w2vec.shape[0]] = w2vec
prior_matrix = np.zeros([self.num_edges, self.num_edge_types])-0.5/self.num_edge_types
for i in range(len(roidb_use['rela_dete'])):
sub_cls = int(roidb_use['sub_dete'][i])
obj_cls = int(roidb_use['obj_dete'][i])
current_prior = self.rel_so_prior[sub_cls, obj_cls]
# current_prior = -0.5*(current_prior+1.0/self.num_edge_types)
current_prior = -0.5*(1.0/self.num_edge_types)
prior_matrix[i, :(self.num_edge_types-1)] = current_prior
# ------ region vgg feature --- initialize edge feature ---------#
sub_idx = box_id(roidb_use['sub_box_dete'], roidb_use['uni_box_gt'])
obj_idx = box_id(roidb_use['obj_box_dete'], roidb_use['uni_box_gt'])
edge_feats = np.zeros([self.num_edges, 512])
# pred_fc7 = np.load(roidb_use['pred_fc7'])
# edge_feats[:len(roidb_use['rela_dete'])] = pred_fc7
# for i in range(len(sub_idx)):
# edge_feats[int(sub_idx[i]),int(obj_idx[i])] = pred_fc7[i]
# edge_feats = np.reshape(edge_feats, [self.num_nodes ** 2, -1])
# edge_feats = edge_feats[self.off_diag_idx]
return nodes, edge_feats, prior_matrix
def __getitem__(self, index):
roidb_use = self.roidb[index]
nodes, edge_feats, prior_matrix = self.train_item(roidb_use)
edges, node_cls, rel_rec, rel_send, bbox_coordinates = self.get_adj(roidb_use)
bbox_coordinates = torch.FloatTensor(bbox_coordinates)
nodes = torch.FloatTensor(nodes)
edges = torch.LongTensor(edges)
node_cls = torch.LongTensor(node_cls)
edge_feats = torch.FloatTensor(edge_feats)
rel_rec = torch.FloatTensor(rel_rec)
rel_send = torch.FloatTensor(rel_send)
prior_matrix = torch.FloatTensor(prior_matrix)
if self.prior:
return nodes, edges, node_cls, edge_feats, rel_rec, rel_send, bbox_coordinates, prior_matrix
else:
return nodes, edges, node_cls, edge_feats, rel_rec, rel_send
def __len__(self):
return len(self.roidb)
class VgPredDataset(Dataset):
"""docstring for VgPred"""
def __init__(self, mode = 'train', feat_mode = 'full', prior = False, ori_vgg=False, use_loc=False):
super(VgPredDataset, self).__init__()
self.num_nodes = 110 #98
self.num_edge_types = 101
self.num_node_types = 201
self.num_edges = 490 #352
if mode == 'train':
self.mode = 'train'
else:
self.mode = 'test'
self.feat_mode = feat_mode
self.prior = prior
# ----------- senmantic feature ------------- #
self.predicates_vec = np.load('./data/vg_predicates_vec.npy')
self.objects_vec = np.load('./data/vg_objects_vec.npy')
# ------------ original roidb feature --------#
self.roidb_read = read_roidb('./data/vg_pred_graph_roidb.npz')
self.roidb = self.roidb_read[self.mode]
self.rel_so_prior = np.load('./data/vg_so_prior.npy') #[201, 201, 100]
self.use_loc = use_loc
def get_adj(self, roidb_use):
bbox_coordinates = np.zeros([self.num_edges, 20])
matrix = np.eye(self.num_nodes)
rel_rec = np.zeros([self.num_edges, self.num_nodes])
rel_send = np.zeros([self.num_edges, self.num_nodes])
sub_idx = box_id(roidb_use['sub_box_gt'], roidb_use['uni_box_gt'])
obj_idx = box_id(roidb_use['obj_box_gt'], roidb_use['uni_box_gt'])
for i in range(len(sub_idx)):
sub_id = int(sub_idx[i])
obj_id = int(obj_idx[i])
rel_rec[i] = matrix[obj_id]
rel_send[i] = matrix[sub_id]
bbox_coordinates[i] = get_box_feats(roidb_use['uni_box_gt'][sub_id], roidb_use['uni_box_gt'][obj_id])
edges = np.zeros(self.num_edges) + self.num_edge_types - 1
edges[:len(roidb_use['rela_gt'])] = roidb_use['rela_gt']
edges = np.array(edges, dtype=np.int64)
node_cls = np.zeros(self.num_nodes) + self.num_node_types-1
node_cls[:len(roidb_use['uni_gt'])] = roidb_use['uni_gt']
node_cls = np.array(node_cls, dtype=np.int64)
return edges, node_cls, rel_rec, rel_send, bbox_coordinates
def train_item(self, roidb_use):
if self.feat_mode == 'full':
# --------- node feature ------------#
feats = np.load(roidb_use['uni_fc7'])
w2vec = list(map(lambda x: self.objects_vec[int(x)], roidb_use['uni_gt']))
w2vec = np.reshape(np.array(w2vec),[-1, 300])
nodes = np.zeros([self.num_nodes, 4396])
nodes[:feats.shape[0], :4096] = feats
nodes[:feats.shape[0], 4096:] = w2vec # [self.num_nodes, 4096+300]
elif self.feat_mode == 'vis':
feats = np.load(roidb_use['uni_fc7'])
nodes = np.zeros([self.num_nodes, 4096])
nodes[:feats.shape[0]] = feats
elif self.feat_mode == 'sem':
w2vec = list(map(lambda x: self.objects_vec[int(x)], roidb_use['uni_gt']))
w2vec = np.reshape(np.array(w2vec),[-1, 300])
nodes = np.zeros([self.num_nodes, 300])
nodes[:w2vec.shape[0]] = w2vec
# prior_matrix = np.zeros([self.num_edges, self.num_edge_types])
prior_matrix = np.zeros([self.num_edges, self.num_edge_types])-0.5/self.num_edge_types
for i in range(len(roidb_use['rela_gt'])):
sub_cls = int(roidb_use['sub_gt'][i])
obj_cls = int(roidb_use['obj_gt'][i])
current_prior = self.rel_so_prior[sub_cls, obj_cls]
current_prior = -0.5*(current_prior+1.0/self.num_edge_types)
# current_prior = -1.0*(current_prior+1.0/self.num_edge_types)
prior_matrix[i, :(self.num_edge_types-1)] = current_prior
# ------ region vgg feature --- initialize edge feature ---------#
# sub_idx = box_id(roidb_use['sub_box_gt'], roidb_use['uni_box_gt'])
# obj_idx = box_id(roidb_use['obj_box_gt'], roidb_use['uni_box_gt'])
edge_feats = np.zeros([self.num_edges, 512])
pred_fc7 = np.load(roidb_use['pred_fc7'])
edge_feats[:len(roidb_use['rela_gt'])] = pred_fc7
return nodes, edge_feats, prior_matrix
def __getitem__(self, index):
roidb_use = self.roidb[index]
nodes, edge_feats, prior_matrix = self.train_item(roidb_use)
edges, node_cls, rel_rec, rel_send, bbox_coordinates = self.get_adj(roidb_use)
nodes = torch.FloatTensor(nodes)
edges = torch.LongTensor(edges)
node_cls = torch.LongTensor(node_cls)
edge_feats = torch.FloatTensor(edge_feats)
rel_rec = torch.FloatTensor(rel_rec)
rel_send = torch.FloatTensor(rel_send)
prior_matrix = torch.FloatTensor(prior_matrix)
bbox_coordinates = torch.FloatTensor(bbox_coordinates)
if self.prior:
return nodes, edges, node_cls, edge_feats, rel_rec, rel_send, bbox_coordinates, prior_matrix
else:
return nodes, edges, node_cls, edge_feats, rel_rec, rel_send
def __len__(self):
return len(self.roidb)
def load_dataset(data_set='vrd', ori_vgg=False, dataset='pred', level='image', batch_size=32, eval_batch_size=32, shuffle=False, feat_mode='full', prior=False):
if data_set == 'vrd':
if dataset=='pred' and level=='image':
load_func_name = VrdPredDataset
elif dataset=='rela' and level=='image':
load_func_name = VrdRelaDataset
else:
load_func_name = VgPredDataset
train_data = load_func_name(mode='train', feat_mode = feat_mode, prior=True, ori_vgg=ori_vgg)
val_data = load_func_name(mode='test', feat_mode = feat_mode, prior=True, ori_vgg=ori_vgg)
test_data = load_func_name(mode='test', feat_mode = feat_mode, prior=True, ori_vgg=ori_vgg)
train_loader = DataLoader(train_data, shuffle=shuffle, batch_size=batch_size)
val_loader = DataLoader(val_data, shuffle=False, batch_size=eval_batch_size)
test_loader = DataLoader(test_data, shuffle=False, batch_size=eval_batch_size)
return train_loader, val_loader, test_loader