-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransformations.py
More file actions
59 lines (49 loc) · 1.8 KB
/
transformations.py
File metadata and controls
59 lines (49 loc) · 1.8 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
import random
from torchvision import transforms
class RandomTemporalSampling: #---> Good to use but does not preserve the tensor shape!
"""
Sample every other frame from the sequence, effectively halving the number of frames.
Example:
Given a sequence of 40 frames, it will return 20 frames.
"""
def __init__(self, slicing_step):
# We slice the frames with a step size of slicing_step to include more semantically important sequence frames
# This is a good trade-off between the number of frames and the semantic importance of the frames
self.slicing_step = slicing_step
def __call__(self, frames):
# Always use step size 2 if there are enough frames
if frames.size(0) > 2:
return frames[::self.slicing_step]
return frames
class RandomTemporalReverse:
"""
Randomly reverse the order of frames in the sequence.
Args:
p (float): probability of applying the reversal. Default: 0.5
"""
def __init__(self, p=0.5):
self.p = p
def __call__(self, frames):
if random.random() < self.p:
return frames.flip(0) # Reverse temporal dimension
return frames
# Combined transforms
def get_train_transforms(slicing_step):
"""
Returns the composition of transforms for training.
"""
return transforms.Compose([
# Spatial augmentations
transforms.RandomHorizontalFlip(p=0.5),
transforms.RandomRotation(25),
# Temporal augmentations
RandomTemporalSampling(slicing_step),
RandomTemporalReverse(p=0.3),
])
def get_test_transforms(slicing_step):
"""
Returns the composition of transforms for testing.
"""
return transforms.Compose([
RandomTemporalSampling(slicing_step)
])