forked from kenshohara/3D-ResNets-PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
temporal_transforms.py
172 lines (118 loc) · 4.21 KB
/
temporal_transforms.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
import random
import math
class Compose(object):
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, frame_indices):
for i, t in enumerate(self.transforms):
if isinstance(frame_indices[0], list):
next_transforms = Compose(self.transforms[i:])
dst_frame_indices = [
next_transforms(clip_frame_indices)
for clip_frame_indices in frame_indices
]
return dst_frame_indices
else:
frame_indices = t(frame_indices)
return frame_indices
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 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
def __call__(self, frame_indices):
center_index = len(frame_indices) // 2
begin_index = max(0, center_index - (self.size // 2))
end_index = min(begin_index + self.size, len(frame_indices))
out = frame_indices[begin_index:end_index]
for index in out:
if len(out) >= self.size:
break
out.append(index)
return out
class TemporalRandomCrop(object):
def __init__(self, size):
self.size = size
self.loop = LoopPadding(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)
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):
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)
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 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]
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