-
Notifications
You must be signed in to change notification settings - Fork 0
/
augs.py
157 lines (120 loc) · 4.37 KB
/
augs.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
import cv2
import numpy as np
import albumentations as A
import random
import copy
class Original:
def __call__(self, video):
return video
class GaussianBlur:
def __init__(self, kernel_size=5):
self.kernel_size = kernel_size
def __call__(self, video):
video = copy.deepcopy(video)
transformed_video = []
for img in video:
img = cv2.GaussianBlur(img, (self.kernel_size, self.kernel_size), 0)
transformed_video.append(img)
transformed_video = np.stack(transformed_video)
return transformed_video
class Cutout:
"""
Adapted from https://github.com/uoguelph-mlrg/Cutout/blob/master/util/cutout.py
"""
def __init__(self, n_holes=4, length=16):
self.n_holes = n_holes
self.length = length
def __call__(self, video):
video = copy.deepcopy(video)
h = video.shape[1]
w = video.shape[2]
hole_points = []
for _ in range(self.n_holes):
y = np.random.randint(h)
x = np.random.randint(w)
hole_points.append([x, y])
transformed_video = []
for img in video:
mask = np.ones((h, w), np.float32)
for n in range(self.n_holes):
x, y = hole_points[n]
y1 = np.clip(y - self.length // 2, 0, h)
y2 = np.clip(y + self.length // 2, 0, h)
x1 = np.clip(x - self.length // 2, 0, w)
x2 = np.clip(x + self.length // 2, 0, w)
mask[y1:y2, x1:x2] = 0.0
mask = np.expand_dims(mask, axis=-1).repeat(3, axis=-1)
img = img * mask
transformed_video.append(img)
transformed_video = np.stack(transformed_video)
return transformed_video
class CutoutColor:
def __init__(self, n_holes=4, length=16, color=None):
self.n_holes = n_holes
self.length = length
self.color = self.random_color() if color is None else color
def random_color(self):
rgbl = [255, 0, 0]
random.shuffle(rgbl)
return tuple(rgbl)
def __call__(self, video):
video = copy.deepcopy(video)
h = video.shape[1]
w = video.shape[2]
hole_points = []
for _ in range(self.n_holes):
y = np.random.randint(h)
x = np.random.randint(w)
hole_points.append([x, y])
transformed_video = []
for img in video:
for n in range(self.n_holes):
x, y = hole_points[n]
y1 = np.clip(y - self.length // 2, 0, h)
y2 = np.clip(y + self.length // 2, 0, h)
x1 = np.clip(x - self.length // 2, 0, w)
x2 = np.clip(x + self.length // 2, 0, w)
img[y1:y2, x1:x2, :] = self.color
transformed_video.append(img)
transformed_video = np.stack(transformed_video)
return transformed_video
class Rotate:
def __call__(self, video):
video = copy.deepcopy(video)
transformed_video = []
for img in video:
img = np.rot90(img)
transformed_video.append(img)
transformed_video = np.stack(transformed_video)
return transformed_video
class Flip:
def __call__(self, video):
video = copy.deepcopy(video)
transformed_video = []
for img in video:
img = np.fliplr(img)
transformed_video.append(img)
transformed_video = np.stack(transformed_video)
return transformed_video
class CenterCrop:
def __init__(self, height=180, width=320):
self.transform = A.Compose([A.CenterCrop(height=height, width=width, p=1.0)])
def __call__(self, video):
video = copy.deepcopy(video)
transformed_video = []
for img in video:
img = self.transform(image=img)["image"]
transformed_video.append(img)
transformed_video = np.stack(transformed_video)
return transformed_video
class Grayscale:
def __init__(self, height=180, width=320):
self.transform = A.Compose([A.ToGray(p=1.0)])
def __call__(self, video):
video = copy.deepcopy(video)
transformed_video = []
for img in video:
img = self.transform(image=img)["image"]
transformed_video.append(img)
transformed_video = np.stack(transformed_video)
return transformed_video