-
Notifications
You must be signed in to change notification settings - Fork 15
/
dataset.py
154 lines (124 loc) · 5.8 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
import torch.utils.data as data
import torch
from PIL import Image
import os
import os.path
import numpy as np
from numpy.random import randint
class VideoRecord(object):
def __init__(self, row):
self._data = row
@property
def path(self):
return self._data[0]
@property
def num_frames(self):
return int(self._data[1])
@property
def label(self):
return [int(item) for item in self._data[2]]
class TSNDataSet(data.Dataset):
def __init__(self, root_path, list_file, num_file, num_class,
num_segments=3, new_length=1, modality='RGB',
image_tmpl='img_{:05d}.jpg', transform=None,
force_grayscale=False, random_shift=True, test_mode=False):
self.root_path = root_path
self.list_file = list_file
self.num_file = num_file
self.num_segments = num_segments
self.new_length = new_length
self.modality = modality
self.image_tmpl = image_tmpl
self.transform = transform
self.random_shift = random_shift
self.test_mode = test_mode
self.num_class = num_class
if self.modality == 'RGBDiff':
self.new_length += 1# Diff needs one more image to calculate diff
self._parse_list()
def _load_image(self, directory, idx):
if self.modality == 'RGB' or self.modality == 'RGBDiff':
try:
return [Image.open(os.path.join(self.root_path, directory, self.image_tmpl.format(directory,idx))).convert('RGB')]
except Exception:
print('error loading image:', os.path.join(self.root_path, directory, self.image_tmpl.format(directory,idx)))
return [Image.open(os.path.join(self.root_path, directory, self.image_tmpl.format(directory,1))).convert('RGB')]
elif self.modality == 'Flow':
try:
idx_skip = 1 + (idx-1)*5
flow = Image.open(os.path.join(self.root_path, directory, self.image_tmpl.format(directory,idx_skip))).convert('RGB')
except Exception:
print('error loading flow file:', os.path.join(self.root_path, directory, self.image_tmpl.format(directory,idx_skip)))
flow = Image.open(os.path.join(self.root_path, directory, self.image_tmpl.format(directory,1))).convert('RGB')
flow_x, flow_y, _ = flow.split()
x_img = flow_x.convert('L')
y_img = flow_y.convert('L')
return [x_img, y_img]
def _parse_list(self):
with open(self.list_file, 'r') as point:
tmp_class = [x.strip().split(' ') for x in point]
tmp_class_dict = {}
for item in tmp_class:
if item[0] in tmp_class_dict:
tmp_class_dict[item[0]].append(item[-1])
else:
tmp_class_dict[item[0]] = [item[-1]]
with open(self.num_file, 'r') as point:
tmp_num = [x.strip().split(' ') for x in point]
tmp_num_dict = {name: int(num) for name, num in tmp_num}
tmp = [[name, tmp_num_dict[name], tmp_class_dict[name]] for name in tmp_class_dict]
tmp.sort()
tmp = [item for item in tmp if item[1] >= 3]
self.video_list = [VideoRecord(item) for item in tmp]
print('video number:%d'%(len(self.video_list)))
def _sample_indices(self, record):
"""
:param record: VideoRecord
:return: list
"""
average_duration = (record.num_frames - self.new_length + 1) // self.num_segments
if average_duration > 0:
offsets = np.multiply(list(range(self.num_segments)), average_duration) + randint(average_duration, size=self.num_segments)
elif record.num_frames > self.num_segments:
offsets = np.sort(randint(record.num_frames - self.new_length + 1, size=self.num_segments))
else:
offsets = np.zeros((self.num_segments,))
return offsets + 1
def _get_val_indices(self, record):
if record.num_frames > self.num_segments + self.new_length - 1:
tick = (record.num_frames - self.new_length + 1) / float(self.num_segments)
offsets = np.array([int(tick / 2.0 + tick * x) for x in range(self.num_segments)])
else:
offsets = np.zeros((self.num_segments,))
return offsets + 1
def _get_test_indices(self, record):
tick = (record.num_frames - self.new_length + 1) / float(self.num_segments)
offsets = np.array([int(tick / 2.0 + tick * x) for x in range(self.num_segments)])
return offsets + 1
def __getitem__(self, index):
record = self.video_list[index]
# check this is a legit video folder
while not os.path.exists(os.path.join(self.root_path, record.path, self.image_tmpl.format(record.path,1))):
print(os.path.join(self.root_path, record.path, self.image_tmpl.format(record.path,1)))
index = np.random.randint(len(self.video_list))
record = self.video_list[index]
if not self.test_mode:
segment_indices = self._sample_indices(record) if self.random_shift else self._get_val_indices(record)
else:
segment_indices = self._get_test_indices(record)
return self.get(record, segment_indices)
def get(self, record, indices):
images = list()
for seg_ind in indices:
p = int(seg_ind)
for _ in range(self.new_length):
seg_imgs = self._load_image(record.path, p)
images.extend(seg_imgs)
if p < record.num_frames:
p += 1
process_data = self.transform(images)
torch_label = torch.zeros(self.num_class)
torch_label[record.label] = 1
return process_data, torch_label
def __len__(self):
return len(self.video_list)