-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemporal_transforms.py
More file actions
326 lines (248 loc) · 10.4 KB
/
Copy pathtemporal_transforms.py
File metadata and controls
326 lines (248 loc) · 10.4 KB
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
import random
import math
class Compose(object):
def __init__(self, transforms, num_clips=1, train=False):
self.transforms = transforms
self.num_clips = num_clips
self.train = train
def __call__(self, frame_indices):
out = []
for n in range(self.num_clips):
trans_frame_indices = frame_indices
for i, t in enumerate(self.transforms):
if isinstance(trans_frame_indices[0], list):
next_transforms = Compose(self.transforms[i:])
dst_frame_indices = [
next_transforms(clip_frame_indices)
for clip_frame_indices in trans_frame_indices
]
return dst_frame_indices
else:
trans_frame_indices = t(trans_frame_indices)
out.append(trans_frame_indices)
if self.train: out = out[0]
if len(out) == 1: out = out[0]
#print("trans_frame_indices", trans_frame_indices)
return out
class LoopPadding(object):
def __init__(self, size):
self.size = size
def __call__(self, frame_indices):
out = frame_indices
for index in out:
if len(out) >= self.size:
break
out.append(index)
return out
class LastFramePadding(object):
def __init__(self, size):
self.size = size
def __call__(self, frame_indices):
out = frame_indices
for index in out:
if len(out) >= self.size:
break
out.append(frame_indices[-1])
return out
class TemporalBeginCrop(object):
def __init__(self, size):
self.size = size
def __call__(self, frame_indices):
out = frame_indices[:self.size]
for index in out:
if len(out) >= self.size:
break
out.append(index)
return out
class TemporalCenterCrop(object):
def __init__(self, size):
self.size = size
# self.loop = LoopPadding(size)
self.loop = LastFramePadding(size)
def __call__(self, frame_indices):
if self.size == -1:
size = max(len(frame_indices), 30)
self.loop = LastFramePadding(size)
else: size = self.size
center_index = len(frame_indices) // 2
begin_index = max(0, center_index - (size // 2))
end_index = min(begin_index + size, len(frame_indices))
out = frame_indices[begin_index:end_index]
#print(begin_index, end_index)
if len(out) < size:
out = self.loop(out)
#print("video_length", len(frame_indices), "sampled", size, "len(out)", len(out))
# print(out)
return out
class TemporalRandomCrop(object):
def __init__(self, size):
self.size = size
# self.loop = LoopPadding(size)
self.loop = LastFramePadding(size)
def __call__(self, frame_indices):
rand_end = max(0, len(frame_indices) - self.size - 1)
begin_index = random.randint(0, rand_end)
end_index = min(begin_index + self.size, len(frame_indices))
out = frame_indices[begin_index:end_index]
if len(out) < self.size:
out = self.loop(out)
#print(out)
# print(len(out))
return out
class TemporalEvenCrop(object):
def __init__(self, size, n_samples=1):
self.size = size
self.n_samples = n_samples
self.loop = LoopPadding(size)
def __call__(self, frame_indices):
# print("frame_indices", frame_indices)
n_frames = len(frame_indices)
stride = max(
1, math.ceil((n_frames - 1 - self.size) / (self.n_samples - 1)))
out = []
for begin_index in frame_indices[::stride]:
if len(out) >= self.n_samples:
break
end_index = min(frame_indices[-1] + 1, begin_index + self.size)
# The following line cancels the effect of the strided sampling! I re-implemented it.
# sample = list(range(begin_index, end_index))
sample = list(frame_indices[begin_index:end_index])
if len(sample) < self.size:
out.append(self.loop(sample))
break
else:
out.append(sample)
#print("out", out)
return out
class SlidingWindow(object):
def __init__(self, size, stride=0):
self.size = size
if stride == 0:
self.stride = self.size
else:
self.stride = stride
self.loop = LoopPadding(size)
def __call__(self, frame_indices):
out = []
for begin_index in frame_indices[::self.stride]:
end_index = min(frame_indices[-1] + 1, begin_index + self.size)
sample = list(range(begin_index, end_index))
if len(sample) < self.size:
out.append(self.loop(sample))
break
else:
out.append(sample)
return out
class TemporalSubsampling(object):
def __init__(self, stride):
self.stride = stride
def __call__(self, frame_indices):
return frame_indices[::self.stride]
# Augmentations for CVPR: duration, order, composition
class AugmentDuration(object):
'''Frames are downsampled with random stride (1, 2, 4, 8, 16), so that the
sub-action duration is altered.'''
def __init__(self, strides=[1, 2, 4, 8, 16]):
print("Augment duration with sampling strides", strides)
self.strides = strides
def __call__(self, frame_indices):
stride = random.sample(self.strides,1)[0]
return frame_indices[::stride]
class AugmentOrder(object):
'''Frames are divided in segments which are randomly shuffled.
relative_segment_size: percentage of the video length that is used as segment length.'''
def __init__(self, relative_segment_size=0.1):
print("Augment order with", relative_segment_size*100, "% segment length.")
self.relative_segment_size = relative_segment_size
def __call__(self, frame_indices):
segment_size = max(8, round(self.relative_segment_size*len(frame_indices)))
print("Augment order segment_size", segment_size)
frame_indices = [
frame_indices[i:(i + segment_size)]
for i in range(0, len(frame_indices), segment_size)
]
random.shuffle(frame_indices)
#print("frame_indices", frame_indices)
frame_indices = [t for segment in frame_indices for t in segment]
return frame_indices
class AugmentComposition(object):
'''50% of frames segments are omitted in order to remove sub-actions and
break frequent (co-)occurrences.'''
def __init__(self, relative_segment_size=0.1, prob_of_omission=0.5):
print("Augment composition with", relative_segment_size*100, "% segment length", prob_of_omission, "prob. of omission")
self.relative_segment_size = relative_segment_size
self.prob_of_omission = prob_of_omission
def __call__(self, frame_indices):
segment_size = max(8, round(self.relative_segment_size*len(frame_indices)))
print("Augment composition segment_size", segment_size)
frame_indices = [
frame_indices[i:(i + segment_size)]
for i in range(0, len(frame_indices), segment_size)
]
random.shuffle(frame_indices)
frame_indices = [t for segment in frame_indices[:max(1,int(len(frame_indices)*(1-self.prob_of_omission)))]
for t in segment]
frame_indices = sorted(frame_indices)
return frame_indices
class Shuffle(object):
def __init__(self, block_size):
self.block_size = block_size
def __call__(self, frame_indices):
frame_indices = [
frame_indices[i:(i + self.block_size)]
for i in range(0, len(frame_indices), self.block_size)
]
random.shuffle(frame_indices)
frame_indices = [t for block in frame_indices for t in block]
return frame_indices
# Added as training sampling strategy
class EvenCropsSampling(object):
def __init__(self, size, sampling_stride=8, crop_size=8, augment_duration=False,
augment_order=False, augment_composition=False):
self.size = size
self.sampling_stride = sampling_stride
self.crop_size = crop_size
# self.padding = LoopPadding(size)
self.padding = LastFramePadding(size)
self.augment_duration = augment_duration
self.augment_order = augment_order
self.augment_composition = augment_composition
def __call__(self, frame_indices):
n_frames = len(frame_indices)
crop_size = self.crop_size
if self.augment_duration:
augmentation_factor = random.sample([1, 2, 4], 1)[0]
crop_size = self.crop_size*augmentation_factor
random_offset = random.randint(0, min(crop_size-1, int(n_frames/3)))
stride = max(
1, math.ceil((n_frames - self.size - random_offset) / (self.size/crop_size)))
print("(n_frames - self.size - random_offset)", (n_frames - self.size - random_offset))
print("stride", stride)
out = []
#print([i for i in range(0, n_frames, crop_size+stride)])
begin_indices = list(range(random_offset, len(frame_indices), crop_size+stride))
if self.augment_order:
# 20% prob. of being shuffled
# print(begin_indices)
n_shuffled = int((len(begin_indices)-1)*0.3)
n_not_shuffled = (len(begin_indices)-1) - n_shuffled
shuffle_prob = random.sample([0]*n_not_shuffled + [1]*n_shuffled, len(begin_indices)-1)+[0]
for idx, i in enumerate(shuffle_prob):
if i==1:
next = begin_indices[idx+1]
begin_indices[idx+1] = begin_indices[idx]
begin_indices[idx] = next
# print(shuffle_prob)
# print(begin_indices)
for begin_index in begin_indices:
# print("begin_index", begin_index)
if len(out) >= self.size:
break
end_index = min(len(frame_indices)-1, begin_index + crop_size)
# sample = list(range(begin_index, end_index))
sample = frame_indices[begin_index:end_index]
out = out + sample
if len(out) < self.size:
out = self.padding(out)
print(out)
return out