-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDataloader.py
334 lines (279 loc) · 11.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
import torch
import torchvision.transforms as T
from torch.utils.data import DataLoader
from PIL import Image, ImageFile
from torch.utils.data import Dataset
import os.path as osp
import random
import torch
import numpy as np
import math
from timm.data.random_erasing import RandomErasing
from utility import RandomIdentitySampler,RandomErasing3
from Datasets.MARS_dataset import Mars
from Datasets.iLDSVID import iLIDSVID
from Datasets.PRID_dataset import PRID
__factory = {
'Mars':Mars,
'iLIDSVID':iLIDSVID,
'PRID':PRID
}
def train_collate_fn(batch):
imgs, pids, camids,a= zip(*batch)
pids = torch.tensor(pids, dtype=torch.int64)
camids = torch.tensor(camids, dtype=torch.int64)
return torch.stack(imgs, dim=0), pids, camids, torch.stack(a, dim=0)
def val_collate_fn(batch):
imgs, pids, camids, img_paths = zip(*batch)
viewids = torch.tensor(viewids, dtype=torch.int64)
camids_batch = torch.tensor(camids, dtype=torch.int64)
return torch.stack(imgs, dim=0), pids, camids_batch, img_paths
def dataloader(Dataset_name):
train_transforms = T.Compose([
T.Resize([256, 128], interpolation=3),
T.RandomHorizontalFlip(p=0.5),
T.Pad(10),
T.RandomCrop([256, 128]),
T.ToTensor(),
T.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
])
val_transforms = T.Compose([
T.Resize([256, 128]),
T.ToTensor(),
T.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
])
dataset = __factory[Dataset_name]()
train_set = VideoDataset_inderase(dataset.train, seq_len=4, sample='intelligent',transform=train_transforms)
num_classes = dataset.num_train_pids
cam_num = dataset.num_train_cams
view_num = dataset.num_train_vids
train_loader = DataLoader(train_set, batch_size=64,sampler=RandomIdentitySampler(dataset.train, 64,4),num_workers=4, collate_fn=train_collate_fn)
q_val_set = VideoDataset(dataset.query, seq_len=4, sample='dense', transform=val_transforms)
g_val_set = VideoDataset(dataset.gallery, seq_len=4, sample='dense', transform=val_transforms)
return train_loader, len(dataset.query), num_classes, cam_num, view_num,q_val_set,g_val_set
def read_image(img_path):
"""Keep reading image until succeed.
This can avoid IOError incurred by heavy IO process."""
got_img = False
while not got_img:
try:
img = Image.open(img_path).convert('RGB')
got_img = True
except IOError:
print("IOError incurred when reading '{}'. Will redo. Don't worry. Just chill.".format(img_path))
pass
return img
class VideoDataset(Dataset):
"""Video Person ReID Dataset.
Note batch data has shape (batch, seq_len, channel, height, width).
"""
sample_methods = ['evenly', 'random', 'all']
def __init__(self, dataset, seq_len=15, sample='evenly', transform=None , max_length=40):
self.dataset = dataset
self.seq_len = seq_len
self.sample = sample
self.transform = transform
self.max_length = max_length
def __len__(self):
return len(self.dataset)
def __getitem__(self, index):
img_paths, pid, camid = self.dataset[index]
num = len(img_paths)
# if self.sample == 'restricted_random':
# frame_indices = range(num)
# chunks =
# rand_end = max(0, len(frame_indices) - self.seq_len - 1)
# begin_index = random.randint(0, rand_end)
if self.sample == 'random':
"""
Randomly sample seq_len consecutive frames from num frames,
if num is smaller than seq_len, then replicate items.
This sampling strategy is used in training phase.
"""
frame_indices = range(num)
rand_end = max(0, len(frame_indices) - self.seq_len - 1)
begin_index = random.randint(0, rand_end)
end_index = min(begin_index + self.seq_len, len(frame_indices))
indices = frame_indices[begin_index:end_index]
# print(begin_index, end_index, indices)
if len(indices) < self.seq_len:
indices=np.array(indices)
indices = np.append(indices , [indices[-1] for i in range(self.seq_len - len(indices))])
else:
indices=np.array(indices)
imgs = []
targt_cam=[]
for index in indices:
index=int(index)
img_path = img_paths[index]
img = read_image(img_path)
if self.transform is not None:
img = self.transform(img)
img = img.unsqueeze(0)
targt_cam.append(camid)
imgs.append(img)
imgs = torch.cat(imgs, dim=0)
#imgs=imgs.permute(1,0,2,3)
return imgs, pid, targt_cam
elif self.sample == 'dense':
"""
Sample all frames in a video into a list of clips, each clip contains seq_len frames, batch_size needs to be set to 1.
This sampling strategy is used in test phase.
"""
# import pdb
# pdb.set_trace()
cur_index=0
frame_indices = [i for i in range(num)]
indices_list=[]
while num-cur_index > self.seq_len:
indices_list.append(frame_indices[cur_index:cur_index+self.seq_len])
cur_index+=self.seq_len
last_seq=frame_indices[cur_index:]
# print(last_seq)
for index in last_seq:
if len(last_seq) >= self.seq_len:
break
last_seq.append(index)
indices_list.append(last_seq)
imgs_list=[]
targt_cam=[]
# print(indices_list , num , img_paths )
for indices in indices_list:
if len(imgs_list) > self.max_length:
break
imgs = []
for index in indices:
index=int(index)
img_path = img_paths[index]
img = read_image(img_path)
if self.transform is not None:
img = self.transform(img)
img = img.unsqueeze(0)
imgs.append(img)
targt_cam.append(camid)
imgs = torch.cat(imgs, dim=0)
#imgs=imgs.permute(1,0,2,3)
imgs_list.append(imgs)
imgs_array = torch.stack(imgs_list)
return imgs_array, pid, targt_cam,img_paths
#return imgs_array, pid, int(camid),trackid
elif self.sample == 'dense_subset':
"""
Sample all frames in a video into a list of clips, each clip contains seq_len frames, batch_size needs to be set to 1.
This sampling strategy is used in test phase.
"""
frame_indices = range(num)
rand_end = max(0, len(frame_indices) - self.max_length - 1)
begin_index = random.randint(0, rand_end)
cur_index=begin_index
frame_indices = [i for i in range(num)]
indices_list=[]
while num-cur_index > self.seq_len:
indices_list.append(frame_indices[cur_index:cur_index+self.seq_len])
cur_index+=self.seq_len
last_seq=frame_indices[cur_index:]
# print(last_seq)
for index in last_seq:
if len(last_seq) >= self.seq_len:
break
last_seq.append(index)
indices_list.append(last_seq)
imgs_list=[]
# print(indices_list , num , img_paths )
for indices in indices_list:
if len(imgs_list) > self.max_length:
break
imgs = []
for index in indices:
index=int(index)
img_path = img_paths[index]
img = read_image(img_path)
if self.transform is not None:
img = self.transform(img)
img = img.unsqueeze(0)
imgs.append(img)
imgs = torch.cat(imgs, dim=0)
#imgs=imgs.permute(1,0,2,3)
imgs_list.append(imgs)
imgs_array = torch.stack(imgs_list)
return imgs_array, pid, camid
elif self.sample == 'intelligent_random':
# frame_indices = range(num)
indices = []
each = max(num//seq_len,1)
for i in range(seq_len):
if i != seq_len -1:
indices.append(random.randint(min(i*each , num-1), min( (i+1)*each-1, num-1)) )
else:
indices.append(random.randint(min(i*each , num-1), num-1) )
print(len(indices))
imgs = []
for index in indices:
index=int(index)
img_path = img_paths[index]
img = read_image(img_path)
if self.transform is not None:
img = self.transform(img)
img = img.unsqueeze(0)
imgs.append(img)
imgs = torch.cat(imgs, dim=0)
#imgs=imgs.permute(1,0,2,3)
return imgs, pid, camid
else:
raise KeyError("Unknown sample method: {}. Expected one of {}".format(self.sample, self.sample_methods))
class VideoDataset_inderase(Dataset):
"""Video Person ReID Dataset.
Note batch data has shape (batch, seq_len, channel, height, width).
"""
sample_methods = ['evenly', 'random', 'all']
def __init__(self, dataset, seq_len=15, sample='evenly', transform=None , max_length=40):
self.dataset = dataset
self.seq_len = seq_len
self.sample = sample
self.transform = transform
self.max_length = max_length
self.erase = RandomErasing3(probability=0.5, mean=[0.485, 0.456, 0.406])
def __len__(self):
return len(self.dataset)
def __getitem__(self, index):
img_paths, pid, camid = self.dataset[index]
num = len(img_paths)
if self.sample != "intelligent":
frame_indices = range(num)
rand_end = max(0, len(frame_indices) - self.seq_len - 1)
begin_index = random.randint(0, rand_end)
end_index = min(begin_index + self.seq_len, len(frame_indices))
indices1 = frame_indices[begin_index:end_index]
indices = []
for index in indices1:
if len(indices1) >= self.seq_len:
break
indices.append(index)
indices=np.array(indices)
else:
# frame_indices = range(num)
indices = []
each = max(num//self.seq_len,1)
for i in range(self.seq_len):
if i != self.seq_len -1:
indices.append(random.randint(min(i*each , num-1), min( (i+1)*each-1, num-1)) )
else:
indices.append(random.randint(min(i*each , num-1), num-1) )
# print(len(indices), indices, num )
imgs = []
labels = []
targt_cam=[]
for index in indices:
index=int(index)
img_path = img_paths[index]
img = read_image(img_path)
if self.transform is not None:
img = self.transform(img)
img , temp = self.erase(img)
labels.append(temp)
img = img.unsqueeze(0)
imgs.append(img)
targt_cam.append(camid)
labels = torch.tensor(labels)
imgs = torch.cat(imgs, dim=0)
return imgs, pid, targt_cam ,labels