-
Notifications
You must be signed in to change notification settings - Fork 1
/
videotransforms.py
529 lines (443 loc) · 15.5 KB
/
videotransforms.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
import numpy as np
import numbers
import random
import random
import math
import numbers
import collections
import numpy as np
import torch
from PIL import Image, ImageOps
import random
from PIL import ImageOps
class RandomCrop(object):
"""Crop the given video sequences (t x h x w) at a random location.
Args:
size (sequence or int): Desired output size of the crop. If size is an
int instead of sequence like (h, w), a square crop (size, size) is
made.
"""
def __init__(self, size):
if isinstance(size, numbers.Number):
self.size = (int(size), int(size))
else:
self.size = size
@staticmethod
def get_params(img, output_size):
"""Get parameters for ``crop`` for a random crop.
Args:
img (PIL Image): Image to be cropped.
output_size (tuple): Expected output size of the crop.
Returns:
tuple: params (i, j, h, w) to be passed to ``crop`` for random crop.
"""
t, h, w, c = img.shape
th, tw = output_size
if w == tw and h == th:
return 0, 0, h, w
i = random.randint(0, h - th) if h != th else 0
j = random.randint(0, w - tw) if w != tw else 0
return i, j, th, tw
def __call__(self, imgs):
i, j, h, w = self.get_params(imgs, self.size)
imgs = imgs[:, i:i + h, j:j + w, :]
return imgs
def __repr__(self):
return self.__class__.__name__ + '(size={0})'.format(self.size)
class Resize(object):
"""Crops the given seq Images at the center.
Args:
size (sequence or int): Desired output size of the crop. If size is an
int instead of sequence like (h, w), a square crop (size, size) is
made.
"""
def __init__(self, size):
self.size = size
def __call__(self, imgs):
"""
Args:
img (PIL Image): Image to be cropped.
Returns:
PIL Image: Cropped image.
"""
t, h, w, c = imgs.shape
if h >= w:
tw = self.size
th = int(self.size * h / w)
else:
th = self.size
tw = int(self.size * w / h)
output = list()
for i in range(t):
# print(imgs[i, :, :, :])
output[i, :, :, :] = imgs[i, :, :, :].resize((th, tw), Image.BILINEAR)
return output
def __repr__(self):
return self.__class__.__name__ + '(size={0})'.format(self.size)
class CenterCrop(object):
"""Crops the given seq Images at the center.
Args:
size (sequence or int): Desired output size of the crop. If size is an
int instead of sequence like (h, w), a square crop (size, size) is
made.
"""
def __init__(self, size):
if isinstance(size, numbers.Number):
self.size = (int(size), int(size))
else:
self.size = size
def __call__(self, imgs):
"""
Args:
img (PIL Image): Image to be cropped.
Returns:
PIL Image: Cropped image.
"""
t, h, w, c = imgs.shape
th, tw = self.size
i = int(np.round((h - th) / 2.))
j = int(np.round((w - tw) / 2.))
return imgs[:, i:i + th, j:j + tw, :]
def __repr__(self):
return self.__class__.__name__ + '(size={0})'.format(self.size)
class VideoCrop(object):
def __init__(self, size):
self.size = size
self.window_size = 3
def __call__(self, imgs):
'''
first reshape img into 256(shorter length), then clip 3 256 x 256 img in window. if need to resize to 224 x 224 ?
:param imgs:
:return:
'''
#==============================older version, can be run for 224 x 224==========
t, h, w, c = imgs.shape # batch x 256 x 340 x 3
# print(t, h, w, c)
th, tw = (self.size, self.size)
video_imgs = list()
for n in range(self.window_size):
x1 = int(round((w - tw) / self.window_size * n))
y1 = int(round((h - th) / self.window_size * n))
x2 = x1 + tw
y2 = y1 + th
# print(x1, y1, x2, y2)
img = np.resize(imgs[:, y1:y2, x1:x2, :], (t, th, tw, c)) # all img resize to th, tw ?
video_imgs.append(img)
return video_imgs
"""
# ===============================new version, for 256x256==========================
t, h, w, c = imgs.shape # batch x 256 x 340 x 3
# print(t, h, w, c)
th, tw = (self.size, self.size)
video_imgs = list()
if w > h:
for n in range(self.window_size):
x1 = int(round((w - h) / (self.window_size - 1) * n))
y1 = 0
x2 = x1 + h
y2 = h
# print("[{}:{},{}:{}]".format(y1, y2, x1, x2))
img = np.zeros((t, th, tw, c))
for i in range(t):
im = Image.fromarray(np.uint8((imgs[i, y1:y2, x1:x2, :] + 1) * 255 / 2))
img[i] = np.asarray(im.resize((th, tw), Image.ANTIALIAS))
img = 2 * (img / 255) - 1
# img = np.resize(imgs[:, y1:y2, x1:x2, :], (t, th, tw, c)) # all img resize to th, tw ?
video_imgs.append(img)
else:
for n in range(self.window_size):
x1 = 0
y1 = int(round((h - w) / (self.window_size - 1) * n))
x2 = w
y2 = y1 + w
img = np.zeros((t, th, tw, c))
for i in range(t):
im = Image.fromarray(np.uint8((imgs[i, y1:y2, x1:x2, :] + 1) * 255 / 2))
img[i] = np.asarray(im.resize((th, tw), Image.ANTIALIAS))
img = 2 * (img / 255) - 1
video_imgs.append(img)
return video_imgs
"""
def randomize_parameters(self):
if self.randomize:
self.crop_position = self.crop_positions[random.randint(
0,
len(self.crop_positions) - 1)]
class CornerCrop(object):
def __init__(self, size, crop_position=None):
self.size = size
if crop_position is None:
self.randomize = True
else:
self.randomize = False
self.crop_position = crop_position
self.crop_positions = ['c', 'tl', 'tr', 'bl', 'br']
def __call__(self, imgs):
t, h, w, c = imgs.shape
corner_imgs = list()
for n in self.crop_positions:
# print(n)
if n == 'c':
th, tw = (self.size, self.size)
x1 = int(round((w - tw) / 2.))
y1 = int(round((h - th) / 2.))
x2 = x1 + tw
y2 = y1 + th
elif n == 'tl':
x1 = 0
y1 = 0
x2 = self.size
y2 = self.size
elif n == 'tr':
x1 = w - self.size
y1 = 0
x2 = w
y2 = self.size
elif n == 'bl':
x1 = 0
y1 = h - self.size
x2 = self.size
y2 = h
elif n == 'br':
x1 = w - self.size
y1 = h - self.size
x2 = w
y2 = h
corner_imgs.append(imgs[:, y1:y2, x1:x2, :])
return corner_imgs
def randomize_parameters(self):
if self.randomize:
self.crop_position = self.crop_positions[random.randint(
0,
len(self.crop_positions) - 1)]
class RandomHorizontalFlip(object):
"""Horizontally flip the given seq Images randomly with a given probability.
Args:
p (float): probability of the image being flipped. Default value is 0.5
"""
def __init__(self, p=0.5):
self.p = p
def __call__(self, imgs):
"""
Args:
img (seq Images): seq Images to be flipped.
Returns:
seq Images: Randomly flipped seq images.
"""
if random.random() < self.p:
# t x h x w
return np.flip(imgs, axis=2).copy()
return imgs
def __repr__(self):
return self.__class__.__name__ + '(p={})'.format(self.p)
class Normalize(object):
"""Normalize an tensor image with mean and standard deviation.
Given mean: (R, G, B) and std: (R, G, B),
will normalize each channel of the torch.*Tensor, i.e.
channel = (channel - mean) / std
Args:
mean (sequence): Sequence of means for R, G, B channels respecitvely.
std (sequence): Sequence of standard deviations for R, G, B channels
respecitvely.
"""
def __init__(self, mean, std):
self.mean = mean
self.std = std
def __call__(self, tensor):
"""
Args:
tensor (Tensor): Tensor image of size (C, H, W) to be normalized.
Returns:
Tensor: Normalized image.
"""
# TODO: make efficient
# for t, m, s in zip(tensor, self.mean, self.std):
# t.sub_(m).div_(s)
xmax, xmin = tensor.max(), tensor.min()
tensor = (tensor - xmin) / (xmax - xmin)
return tensor
def randomize_parameters(self):
pass
def transform_data(data, scale_size=256, crop_size=224, random_crop=False, random_flip=False):
data = resize(data, scale_size)
width = data[0].size[0]
height = data[0].size[1]
if random_crop:
x0 = random.randint(0, width - crop_size)
y0 = random.randint(0, height - crop_size)
x1 = x0 + crop_size
y1 = y0 + crop_size
for i, img in enumerate(data):
data[i] = img.crop((x0, y0, x1, y1))
else:
x0 = int((width - crop_size) / 2)
y0 = int((height - crop_size) / 2)
x1 = x0 + crop_size
y1 = y0 + crop_size
for i, img in enumerate(data):
data[i] = img.crop((x0, y0, x1, y1))
if random_flip and random.randint(0, 1) == 0:
for i, img in enumerate(data):
data[i] = ImageOps.mirror(img)
return data
def get_10_crop(data, scale_size=256, crop_size=224):
data = resize(data, scale_size)
width = data[0].size[0]
height = data[0].size[1]
top_left = [[0, 0],
[width - crop_size, 0],
[int((width - crop_size) / 2), int((height - crop_size) / 2)],
[0, height - crop_size],
[width - crop_size, height - crop_size]]
crop_data = []
for point in top_left:
non_flip = []
flip = []
x_0 = point[0]
y_0 = point[1]
x_1 = x_0 + crop_size
y_1 = y_0 + crop_size
for img in data:
tmp = img.crop((x_0, y_0, x_1, y_1))
non_flip.append(tmp)
flip.append(ImageOps.mirror(tmp))
crop_data.append(non_flip)
crop_data.append(flip)
return crop_data
def scale(data, scale_size):
width = data[0].size[0]
height = data[0].size[1]
if (width == scale_size and height >= width) or (height == scale_size and width >= height):
return data
if width >= height:
h = scale_size
w = round((width / height) * scale_size)
else:
w = scale_size
h = round((height / width) * scale_size)
for i, image in enumerate(data):
data[i] = image.resize((w, h))
return data
def resize(data, scale_size):
width = data[0].size[0]
height = data[0].size[1]
if (width == scale_size and height >= width) or (height == scale_size and width >= height):
return data
for i, image in enumerate(data):
data[i] = image.resize((scale_size, scale_size))
return data
def video_frames_resize(data, scale_size):
"""
let the short path be 256
:param data:
:param scale_size:
:return:
"""
t, h, w, c = data.shape
if h >= scale_size and w >= scale_size:
return data
else:
data2 = data.copy()
data2.resize(t, scale_size, scale_size, c)
return data2
class Compose(object):
"""Composes several transforms together.
Args:
transforms (list of ``Transform`` objects): list of transforms to compose.
Example:
>>> transforms.Compose([
>>> transforms.CenterCrop(10),
>>> transforms.ToTensor(),
>>> ])
"""
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, img):
for t in self.transforms:
img = t(img)
return img
def __repr__(self):
format_string = self.__class__.__name__ + '('
for t in self.transforms:
format_string += '\n'
format_string += ' {0}'.format(t)
format_string += '\n)'
return format_string
class Lighting(object):
"""Lighting noise(AlexNet - style PCA - based noise)"""
def __init__(self, alphastd, eigval, eigvec):
self.alphastd = alphastd
self.eigval = torch.Tensor(eigval)
self.eigvec = torch.Tensor(eigvec)
def __call__(self, img):
if self.alphastd == 0:
return img
alpha = img.new().resize_(3).normal_(0, self.alphastd)
rgb = self.eigvec.type_as(img).clone() \
.mul(alpha.view(1, 3).expand(3, 3)) \
.mul(self.eigval.view(1, 3).expand(3, 3)) \
.sum(1).squeeze()
return img.add(rgb.view(3, 1, 1).expand_as(img))
class Grayscale(object):
def __call__(self, img):
gs = img.clone()
gs[0].mul_(0.299).add_(0.587, gs[1]).add_(0.114, gs[2])
gs[1].copy_(gs[0])
gs[2].copy_(gs[0])
return gs
class Saturation(object):
def __init__(self, var):
self.var = var
def __call__(self, img):
gs = Grayscale()(img)
alpha = random.uniform(-self.var, self.var)
return img.lerp(gs, alpha)
class Brightness(object):
def __init__(self, var):
self.var = var
def __call__(self, img):
gs = img.new().resize_as_(img).zero_()
alpha = random.uniform(-self.var, self.var)
return img.lerp(gs, alpha)
class Contrast(object):
def __init__(self, var):
self.var = var
def __call__(self, img):
gs = Grayscale()(img)
gs.fill_(gs.mean())
alpha = random.uniform(-self.var, self.var)
return img.lerp(gs, alpha)
class ColorJitter(object):
def __init__(self, brightness=0.4, contrast=0.4, saturation=0.4):
self.brightness = brightness
self.contrast = contrast
self.saturation = saturation
def __call__(self, imgs):
c, t, h, w = imgs.shape
self.transforms = []
if self.brightness != 0:
self.transforms.append(Brightness(self.brightness))
if self.contrast != 0:
self.transforms.append(Contrast(self.contrast))
if self.saturation != 0:
self.transforms.append(Saturation(self.saturation))
random.shuffle(self.transforms)
transform = Compose(self.transforms)
# print(transform)
for i in range(t):
imgs[:, i, :, :] = transform(imgs[:, i, :, :])
return imgs
class VideoToTensor(object):
"""Convert a ``numpy.ndarray`` to tensor.
Converts a numpy.ndarray (T x H x W x C)
to a torch.FloatTensor of shape (C x T x H x W)
Args:
pic (numpy.ndarray): Video to be converted to tensor.
Returns:
Tensor: Converted video.
"""
def __init__(self, alpha=0.5):
self.alpha = alpha
# return torch.from_numpy(pic)
def __call__(self, imgs):
return torch.from_numpy(imgs.transpose([3, 0, 1, 2])).type(torch.FloatTensor)