-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstep2_data_augmentation.py
More file actions
133 lines (117 loc) · 4.42 KB
/
Copy pathstep2_data_augmentation.py
File metadata and controls
133 lines (117 loc) · 4.42 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
import numpy as np
from tqdm import tqdm
from clearml import Task
# Initialize the ClearML task
task = Task.init(
project_name='SyntaxSquad', task_type=Task.TaskTypes.data_processing,
task_name='Step 2: Perform random data augmentation on train set'
)
args = {
'data_splitting_task_id': '', # ID of the task that performed data splitting
'augmentation_frequency': 1, # Number of times to apply augmentation
}
task.connect(args)
task.execute_remotely()
def rotate(data, rotation_matrix):
frames, landmarks, _ = data.shape
center = np.array([0.5, 0.5, 0])
non_zero = np.argwhere(np.any(data[:, :, :2] != 0, axis=2))
data = data.reshape(-1, 3)
data[non_zero] -= center
data[non_zero] = np.dot(data[non_zero], rotation_matrix.T)
data[non_zero] += center
data = data.reshape(frames, landmarks, 3)
out_of_range = np.any((data[:, :, :2] < 0) | (data[:, :, :2] > 1), axis=2)
data[out_of_range] = 0
return data
def rotate_x(data):
angle = np.random.choice([np.random.uniform(-30, -10), np.random.uniform(10, 30)])
theta = np.radians(angle)
rotation_matrix = np.array([
[1, 0, 0],
[0, np.cos(theta), -np.sin(theta)],
[0, np.sin(theta), np.cos(theta)]
])
return rotate(data, rotation_matrix)
def rotate_y(data):
angle = np.random.choice([np.random.uniform(-30, -10), np.random.uniform(10, 30)])
theta = np.radians(angle)
rotation_matrix = np.array([
[np.cos(theta), 0, np.sin(theta)],
[0, 1, 0],
[-np.sin(theta), 0, np.cos(theta)]
])
return rotate(data, rotation_matrix)
def rotate_z(data):
angle = np.random.choice([np.random.uniform(-30, -10), np.random.uniform(10, 30)])
theta = np.radians(angle)
rotation_matrix = np.array([
[np.cos(theta), -np.sin(theta), 0],
[np.sin(theta), np.cos(theta), 0],
[0, 0, 1]
])
return rotate(data, rotation_matrix)
def zoom(data):
factor = np.random.uniform(0.8, 1.2)
center = np.array([0.5, 0.5])
non_zero = np.argwhere(np.any(data[:, :, :2] != 0, axis=2))
data[non_zero[:, 0], non_zero[:, 1], :2] = ((
data[non_zero[:, 0], non_zero[:, 1], :2] - center
) * factor + center)
out_of_range = np.any((data[:, :, :2] < 0) | (data[:, :, :2] > 1), axis=2)
data[out_of_range] = 0
return data
def shift(data):
x_shift = np.random.uniform(-0.2, 0.2)
y_shift = np.random.uniform(-0.2, 0.2)
non_zero = np.argwhere(np.any(data[:, :, :2] != 0, axis=2))
data[non_zero[:, 0], non_zero[:, 1], 0] += x_shift
data[non_zero[:, 0], non_zero[:, 1], 1] += y_shift
out_of_range = np.any((data[:, :, :2] < 0) | (data[:, :, :2] > 1), axis=2)
data[out_of_range] = 0
return data
def mask(data):
frames, landmarks, _ = data.shape
num_hands = int(0.3 * 42)
num_rest = int(0.6 * (landmarks - 42))
mask = np.zeros(landmarks, dtype=bool)
indices = np.concatenate([
np.random.choice(42, num_hands, replace=False),
np.random.choice(landmarks - 42, num_rest, replace=False) + 42
])
mask[indices] = True
data[:, mask] = 0
return data
def hflip(data):
data[:, :, 0] = 1 - data[:, :, 0]
return data
def speedup(data):
return data[::2]
def apply_augmentation(data):
aug_funcs = [rotate_x, rotate_y, rotate_z, zoom, shift, mask, hflip, speedup]
np.random.shuffle(aug_funcs)
count = 0
for func in aug_funcs:
if np.random.rand() < 0.5:
data = func(data)
count += 1
if count == 0: data = apply_augmentation(data)
return data
def augment(X, y, num=None):
X_aug, y_aug = X.copy(), y.copy()
for i in tqdm(range(len(y))):
for _ in range(num or np.random.choice([1, 2, 3])):
X_aug.append(apply_augmentation(X[i].copy()))
y_aug.append(y[i])
return X_aug, y_aug
data_splitting_task = Task.get_task(task_id=args['data_splitting_task_id'])
X_train = data_splitting_task.artifacts['X_train'].get()
y_train = data_splitting_task.artifacts['y_train'].get()
X_train, y_train = augment(X_train, y_train, num=args['augmentation_frequency'])
print('The Training set has', len(X_train), 'videos')
print('First video has', len(X_train[0]), 'frames')
print('Each frame has', len(X_train[0][0]), 'landmarks')
print('Each landmark has', len(X_train[0][0][0]), 'coordinates')
# Save the processed data as artifacts
task.upload_artifact('X_train', artifact_object=X_train)
task.upload_artifact('y_train', artifact_object=y_train)