-
Notifications
You must be signed in to change notification settings - Fork 4
/
video_loader.py
152 lines (133 loc) · 5.7 KB
/
video_loader.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
from __future__ import print_function, absolute_import
import os
from PIL import Image
import numpy as np
import torch
from torch import Tensor
from torch.utils.data import Dataset
import random
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, attr=False, attr_loss="cropy", attr_lens=[], max_seq_len=200, sample_margin=10, dataset_name="mars"):
self.dataset = dataset
self.seq_len = seq_len
self.sample = sample
self.transform = transform
self.attr = attr
self.attr_loss = attr_loss
self.attr_lens = attr_lens
self.max_seq_len = max_seq_len
self.sample_margin = sample_margin
self.dataset_name = dataset_name
def __len__(self):
return len(self.dataset)
def __getitem__(self, index):
img_paths, pid, camid, attrs = self.dataset[index]
# if self.dataset_name == "mars":
# img_paths = list(img_paths)
# random.shuffle(img_paths)
# if self.sample_margin > 1 and len(img_paths) > self.seq_len * self.sample_margin:
# if self.sample == 'random':
# strat_frame = int(self.sample_margin * random.random())
# img_paths = img_paths[strat_frame::self.sample_margin]
# elif self.sample == 'dense':
# new_dense_img_paths = []
# for sp in range(self.sample_margin):
# new_dense_img_paths += img_paths[sp::self.sample_margin]
# img_paths = new_dense_img_paths
num = len(img_paths)
attributes = []
if self.attr:
if self.attr_loss == "cropy":
for a in attrs:
attributes.append(Tensor([a]).long())
if np.sum(attrs[2:9]) == 0:
attributes.append(Tensor([1]).long())
else:
attributes.append(Tensor([0]).long())
# elif self.attr_loss == "mse":
# for i, a in enumerate(attrs):
# attr = [1 if _ == a else 0 for _ in range(self.attr_lens[i])]
# attributes.append(Tensor(attr))
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 = list(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]
for index in indices:
if len(indices) >= self.seq_len:
break
indices.append(index)
indices=np.array(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, attributes,
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.
"""
cur_index=0
frame_indices = list(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:]
for index in last_seq:
if len(last_seq) >= self.seq_len:
break
last_seq.append(index)
indices_list.append(last_seq)
imgs_list=[]
for indices in indices_list:
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)
if len(imgs_list) > self.max_seq_len:
sp = int(random.random() * (len(imgs_list) - self.max_seq_len))
ep = sp + self.max_seq_len
imgs_list = imgs_list[sp:ep]
imgs_array = torch.stack(imgs_list)
return imgs_array, pid, camid, attributes, img_paths[0]
else:
raise KeyError("Unknown sample method: {}. Expected one of {}".format(self.sample, self.sample_methods))