-
Notifications
You must be signed in to change notification settings - Fork 148
/
video_transforms.py
341 lines (286 loc) · 12.2 KB
/
video_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
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
327
328
329
330
331
332
333
334
335
from __future__ import division
import torch
import random
import numpy as np
import numbers
import types
import cv2
import math
import os, sys
import collections
class Compose(object):
"""Composes several video_transforms together.
Args:
transforms (List[Transform]): list of transforms to compose.
Example:
>>> video_transforms.Compose([
>>> video_transforms.CenterCrop(10),
>>> video_transforms.ToTensor(),
>>> ])
"""
def __init__(self, video_transforms):
self.video_transforms = video_transforms
def __call__(self, clips):
for t in self.video_transforms:
clips = t(clips)
return clips
class Lambda(object):
"""Applies a lambda as a transform"""
def __init__(self, lambd):
assert type(lambd) is types.LambdaType
self.lambd = lambd
def __call__(self, clips):
return self.lambd(clips)
class ToTensor(object):
"""Converts a numpy.ndarray (H x W x C) in the range
[0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0].
"""
def __call__(self, clips):
if isinstance(clips, np.ndarray):
# handle numpy array
clips = torch.from_numpy(clips.transpose((2, 0, 1)))
# backward compatibility
return clips.float().div(255.0)
class Normalize(object):
"""Given mean: (R, G, B) and std: (R, G, B),
will normalize each channel of the torch.*Tensor, i.e.
channel = (channel - mean) / std
Here, the input is a clip, not a single image. (multi-channel data)
The dimension of mean and std depends on parameter: new_length
If new_length = 1, it falls back to single image case (3 channel)
"""
def __init__(self, mean, std):
self.mean = mean
self.std = std
def __call__(self, tensor):
# TODO: make efficient
for t, m, s in zip(tensor, self.mean, self.std):
t.sub_(m).div_(s)
return tensor
class Scale(object):
""" Rescales the input numpy array to the given 'size'.
'size' will be the size of the smaller edge.
For example, if height > width, then image will be
rescaled to (size * height / width, size)
size: size of the smaller edge
interpolation: Default: cv2.INTER_LINEAR
"""
def __init__(self, size, interpolation=cv2.INTER_LINEAR):
self.size = size
self.interpolation = interpolation
def __call__(self, clips):
h, w, c = clips.shape
new_w = 0
new_h = 0
if isinstance(self.size, int):
if (w <= h and w == self.size) or (h <= w and h == self.size):
return clips
if w < h:
new_w = self.size
new_h = int(self.size * h / w)
else:
new_w = int(self.size * w / h)
new_h = self.size
else:
new_w = self.size[0]
new_h = self.size[1]
is_color = False
if c % 3 == 0:
is_color = True
if is_color:
num_imgs = int(c / 3)
scaled_clips = np.zeros((new_h,new_w,c))
for frame_id in range(num_imgs):
cur_img = clips[:,:,frame_id*3:frame_id*3+3]
scaled_clips[:,:,frame_id*3:frame_id*3+3] = cv2.resize(cur_img, (new_w, new_h), self.interpolation)
else:
num_imgs = int(c / 1)
scaled_clips = np.zeros((new_h,new_w,c))
for frame_id in range(num_imgs):
cur_img = clips[:,:,frame_id:frame_id+1]
scaled_clips[:,:,frame_id:frame_id+1] = cv2.resize(cur_img, (new_w, new_h), self.interpolation)
return scaled_clips
class CenterCrop(object):
"""Crops the given numpy array at the center to have a region of
the given size. size can be a tuple (target_height, target_width)
or an integer, in which case the target will be of a square shape (size, size)
"""
def __init__(self, size):
if isinstance(size, numbers.Number):
self.size = (int(size), int(size))
else:
self.size = size
def __call__(self, clips):
h, w, c = clips.shape
th, tw = self.size
x1 = int(round((w - tw) / 2.))
y1 = int(round((h - th) / 2.))
is_color = False
if c % 3 == 0:
is_color = True
if is_color:
num_imgs = int(c / 3)
scaled_clips = np.zeros((th,tw,c))
for frame_id in range(num_imgs):
cur_img = clips[:,:,frame_id*3:frame_id*3+3]
crop_img = cur_img[y1:y1+th, x1:x1+tw, :]
assert(crop_img.shape == (th, tw, 3))
scaled_clips[:,:,frame_id*3:frame_id*3+3] = crop_img
return scaled_clips
else:
num_imgs = int(c / 1)
scaled_clips = np.zeros((th,tw,c))
for frame_id in range(num_imgs):
cur_img = clips[:,:,frame_id:frame_id+1]
crop_img = cur_img[y1:y1+th, x1:x1+tw, :]
assert(crop_img.shape == (th, tw, 1))
scaled_clips[:,:,frame_id:frame_id+1] = crop_img
return scaled_clips
class RandomHorizontalFlip(object):
"""Randomly horizontally flips the given numpy array with a probability of 0.5
"""
def __call__(self, clips):
if random.random() < 0.5:
clips = np.fliplr(clips)
clips = np.ascontiguousarray(clips)
return clips
class RandomVerticalFlip(object):
"""Randomly vertically flips the given numpy array with a probability of 0.5
"""
def __call__(self, clips):
if random.random() < 0.5:
clips = np.flipud(clips)
clips = np.ascontiguousarray(clips)
return clips
class RandomSizedCrop(object):
"""Random crop the given numpy array to a random size of (0.08 to 1.0) of the original size
and and a random aspect ratio of 3/4 to 4/3 of the original aspect ratio
This is popularly used to train the Inception networks
size: size of the smaller edge
interpolation: Default: cv2.INTER_LINEAR
"""
def __init__(self, size, interpolation=cv2.INTER_LINEAR):
self.size = size
self.interpolation = interpolation
def __call__(self, clips):
h, w, c = clips.shape
is_color = False
if c % 3 == 0:
is_color = True
for attempt in range(10):
area = w * h
target_area = random.uniform(0.08, 1.0) * area
aspect_ratio = random.uniform(3. / 4, 4. / 3)
new_w = int(round(math.sqrt(target_area * aspect_ratio)))
new_h = int(round(math.sqrt(target_area / aspect_ratio)))
if random.random() < 0.5:
new_w, new_h = new_h, new_w
if new_w <= w and new_h <= h:
x1 = random.randint(0, w - new_w)
y1 = random.randint(0, h - new_h)
scaled_clips = np.zeros((self.size,self.size,c))
if is_color:
num_imgs = int(c / 3)
for frame_id in range(num_imgs):
cur_img = clips[:,:,frame_id*3:frame_id*3+3]
crop_img = cur_img[y1:y1+new_h, x1:x1+new_w, :]
assert(crop_img.shape == (new_h, new_w, 3))
scaled_clips[:,:,frame_id*3:frame_id*3+3] = cv2.resize(crop_img, (self.size, self.size), self.interpolation)
return scaled_clips
else:
num_imgs = int(c / 1)
for frame_id in range(num_imgs):
cur_img = clips[:,:,frame_id:frame_id+1]
crop_img = cur_img[y1:y1+new_h, x1:x1+new_w, :]
assert(crop_img.shape == (new_h, new_w, 1))
scaled_clips[:,:,frame_id:frame_id+1] = cv2.resize(crop_img, (self.size, self.size), self.interpolation)
return scaled_clips
# Fallback
scale = Scale(self.size, interpolation=self.interpolation)
crop = CenterCrop(self.size)
return crop(scale(clips))
class MultiScaleCrop(object):
"""
Description: Corner cropping and multi-scale cropping. Two data augmentation techniques introduced in:
Towards Good Practices for Very Deep Two-Stream ConvNets,
http://arxiv.org/abs/1507.02159
Limin Wang, Yuanjun Xiong, Zhe Wang and Yu Qiao
Parameters:
size: height and width required by network input, e.g., (224, 224)
scale_ratios: efficient scale jittering, e.g., [1.0, 0.875, 0.75, 0.66]
fix_crop: use corner cropping or not. Default: True
more_fix_crop: use more corners or not. Default: True
max_distort: maximum distortion. Default: 1
interpolation: Default: cv2.INTER_LINEAR
"""
def __init__(self, size, scale_ratios, fix_crop=True, more_fix_crop=True, max_distort=1, interpolation=cv2.INTER_LINEAR):
self.height = size[0]
self.width = size[1]
self.scale_ratios = scale_ratios
self.fix_crop = fix_crop
self.more_fix_crop = more_fix_crop
self.max_distort = max_distort
self.interpolation = interpolation
def fillFixOffset(self, datum_height, datum_width):
h_off = int((datum_height - self.height) / 4)
w_off = int((datum_width - self.width) / 4)
offsets = []
offsets.append((0, 0)) # upper left
offsets.append((0, 4*w_off)) # upper right
offsets.append((4*h_off, 0)) # lower left
offsets.append((4*h_off, 4*w_off)) # lower right
offsets.append((2*h_off, 2*w_off)) # center
if self.more_fix_crop:
offsets.append((0, 2*w_off)) # top center
offsets.append((4*h_off, 2*w_off)) # bottom center
offsets.append((2*h_off, 0)) # left center
offsets.append((2*h_off, 4*w_off)) # right center
offsets.append((1*h_off, 1*w_off)) # upper left quarter
offsets.append((1*h_off, 3*w_off)) # upper right quarter
offsets.append((3*h_off, 1*w_off)) # lower left quarter
offsets.append((3*h_off, 3*w_off)) # lower right quarter
return offsets
def fillCropSize(self, input_height, input_width):
crop_sizes = []
base_size = np.min((input_height, input_width))
scale_rates = self.scale_ratios
for h in range(len(scale_rates)):
crop_h = int(base_size * scale_rates[h])
for w in range(len(scale_rates)):
crop_w = int(base_size * scale_rates[w])
# append this cropping size into the list
if (np.absolute(h-w) <= self.max_distort):
crop_sizes.append((crop_h, crop_w))
return crop_sizes
def __call__(self, clips):
h, w, c = clips.shape
is_color = False
if c % 3 == 0:
is_color = True
crop_size_pairs = self.fillCropSize(h, w)
size_sel = random.randint(0, len(crop_size_pairs)-1)
crop_height = crop_size_pairs[size_sel][0]
crop_width = crop_size_pairs[size_sel][1]
if self.fix_crop:
offsets = self.fillFixOffset(h, w)
off_sel = random.randint(0, len(offsets)-1)
h_off = offsets[off_sel][0]
w_off = offsets[off_sel][1]
else:
h_off = random.randint(0, h - self.height)
w_off = random.randint(0, w - self.width)
scaled_clips = np.zeros((self.height,self.width,c))
if is_color:
num_imgs = int(c / 3)
for frame_id in range(num_imgs):
cur_img = clips[:,:,frame_id*3:frame_id*3+3]
crop_img = cur_img[h_off:h_off+crop_height, w_off:w_off+crop_width, :]
scaled_clips[:,:,frame_id*3:frame_id*3+3] = cv2.resize(crop_img, (self.width, self.height), self.interpolation)
return scaled_clips
else:
num_imgs = int(c / 1)
for frame_id in range(num_imgs):
cur_img = clips[:,:,frame_id:frame_id+1]
crop_img = cur_img[h_off:h_off+crop_height, w_off:w_off+crop_width, :]
scaled_clips[:,:,frame_id:frame_id+1] = np.expand_dims(cv2.resize(crop_img, (self.width, self.height), self.interpolation), axis=2)
return scaled_clips