-
Notifications
You must be signed in to change notification settings - Fork 51
/
video_transforms.py
586 lines (499 loc) · 22.3 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
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
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 ToTensor3(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((3, 2, 0, 1)))
# backward compatibility
return clips.float().div(255.0)
class ToTensor2(object):
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(1.0)
class Reset(object):
def __init__(self, mask_prob, num_seg):
self.mask_prob = mask_prob
self.num_seg =num_seg
def __call__(self, clips):
mask=np.random.binomial(1, self.mask_prob, self.num_seg).repeat(3)
return clips*mask
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
torch_mean = torch.tensor([[self.mean]]).view(-1,1,1).float()
torch_std = torch.tensor([[self.std]]).view(-1,1,1).float()
tensor2 = (tensor - torch_mean) / torch_std
# for t, m, s in zip(tensor, self.mean, self.std):
# t.sub_(m).div_(s)
return tensor2
class DeNormalize(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
torch_mean = torch.tensor([[self.mean]]).view(-1,1,1).float()
torch_std = torch.tensor([[self.std]]).view(-1,1,1).float()
tensor2 = (tensor * torch_std) + torch_mean
# for t, m, s in zip(tensor, self.mean, self.std):
# t.sub_(m).div_(s)
return tensor2
class Normalize3(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):
torch_mean = torch.tensor([[self.mean]]).view(1,-1,1,1)
torch_std = torch.tensor([[self.std]]).view(1,-1,1,1)
tensor2 = (tensor - torch_mean) / torch_std
return tensor2
class Normalize2(object):
def __init__(self, mean, std, num_seg):
self.mean = mean
self.std = std
self.num_seg = num_seg
def __call__(self, tensor, num_seg):
# TODO: make efficient
mean = self.mean * self.num_seg
std = self.std * self.num_seg
for t, m, s in zip(tensor, mean, 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, selectedRegionOutput=False):
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)
if not selectedRegionOutput:
return scaled_clips
else:
return scaled_clips, off_sel
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)
if not selectedRegionOutput:
return scaled_clips
else:
return scaled_clips, off_sel
class MultiScaleFixedCrop(object):
def __init__(self, size, interpolation=cv2.INTER_LINEAR):
self.height = size[0]
self.width = size[1]
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
return offsets
def __call__(self, clips, selectedRegionOutput=False):
h, w, c = clips.shape
is_color = False
if c % 3 == 0:
is_color = True
crop_height = 224
crop_width = 224
offsets = self.fillFixOffset(h, w)
scaled_clips_list = []
for offset in offsets:
h_off = offset[0]
w_off = offset[1]
scaled_clips = np.zeros((self.height,self.width,c))
scaled_clips_flips = 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)
scaled_clips_flips = scaled_clips[:,::-1,:].copy()
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)
scaled_clips_flips = scaled_clips[:,::-1,:].copy()
scaled_clips_list.append(np.expand_dims(scaled_clips,-1))
scaled_clips_list.append(np.expand_dims(scaled_clips_flips,-1))
return np.concatenate(scaled_clips_list,axis=-1)
class rawPoseAugmentation(object):
def __init__(self, scale_ratios):
self.possible_scale_tuples = []
self.scale_ratios = scale_ratios
for i in range(len(scale_ratios)):
for j in range(len(scale_ratios)):
if np.abs(i-j) < 2:
scale_ration_height = self.scale_ratios[i]
scale_ration_width = self.scale_ratios[j]
self.possible_scale_tuples.append((scale_ration_height, scale_ration_width))
self.length_possible_scale_tuples = len(self.possible_scale_tuples)
def __call__(self, poses):
selected_random_scale_tuple_index = np.random.randint(self.length_possible_scale_tuples)
selected_scale_height = self.possible_scale_tuples[selected_random_scale_tuple_index][0]
selected_scale_width = self.possible_scale_tuples[selected_random_scale_tuple_index][1]
random_crop_height_start = np.random.uniform(0,1-selected_scale_height)
random_crop_width_start = np.random.uniform(0,1-selected_scale_width)
# pos_not_touched = poses.copy()
check_width = poses[:,:,0,:] > random_crop_width_start + selected_scale_width
check_height = poses[:,:,1,:] > random_crop_height_start + selected_scale_height
check = np.logical_or(check_width,check_height)
check = np.expand_dims(check, 2)
check = np.concatenate((check,check),2)
poses[check] = 0
poses[:,:,0,:] -= random_crop_width_start
poses[:,:,1,:] -= random_crop_height_start
poses[poses < 0] = None
poses[:,:,0,:] /= selected_scale_width
poses[:,:,1,:] /= selected_scale_height
if len(poses[poses>1]) > 0:
print('basdasd')
return poses
class pose_one_hot_decoding(object):
def __init__(self,length):
self.space = 0.1
self.number_of_people = 1
self.total_bins = self.number_of_people * 25
self.one_hot_vector_length_per_joint = (1/self.space ) ** 2
self.one_hot_vector_length = int(self.total_bins * self.one_hot_vector_length_per_joint + 1)
self.one_hot = np.zeros(self.one_hot_vector_length)
self.length = length
self.onehot_multiplication = np.repeat(range(self.total_bins), length).reshape(self.total_bins,length)
def __call__(self, poses):
poses = poses.reshape(-1,2,self.length)
dim1 = np.floor(poses[:,0,:] / self.space)
dim2 = np.floor(poses[:,1,:] / self.space)
one_hot_values = (1/self.space ) * dim1 + dim2
one_hot_values[np.isnan(one_hot_values)] = self.one_hot_vector_length_per_joint
one_hot_values = one_hot_values * self.onehot_multiplication + one_hot_values
one_hot_values[np.isnan(one_hot_values)] = self.one_hot_vector_length + 1
return poses
class pose_one_hot_decoding2(object):
def __init__(self,length):
self.space = 1/32
self.bin_number = int((1/self.space))
self.number_of_people = 1
self.total_bins = self.number_of_people * 25
self.one_hot_vector_length = self.bin_number ** 2
self.one_hot = np.zeros(self.one_hot_vector_length)
self.length = length
self.position_matrix = np.zeros([self.bin_number + 1, self.bin_number + 1, self.length])
def __call__(self, poses):
poses = poses.reshape(-1,2,self.length)
dim1 = np.floor(poses[:,0,:] / self.space)
dim2 = np.floor(poses[:,1,:] / self.space)
dim1[np.isnan(dim1)] = self.bin_number
dim2[np.isnan(dim2)] = self.bin_number
dim1 = dim1.astype(np.int)
dim2 = dim2.astype(np.int)
for i in range(self.length):
try:
self.position_matrix[dim1[:,i], dim2[:,i], i] = 1
except:
print('hasdasd')
one_hot_encoding = self.position_matrix[:self.bin_number, :self.bin_number, :]
one_hot_encoding = one_hot_encoding.reshape(-1,self.length)
one_hot_encoding_torch = torch.from_numpy(one_hot_encoding.transpose((1,0))).float()
return one_hot_encoding_torch
class ToTensorPose(object):
def __call__(self, clips):
if isinstance(clips, np.ndarray):
# handle numpy ar
clips = clips - 0.5
clips[np.isnan(clips)] = 0
clips = torch.from_numpy(clips.transpose((3,0,1,2))).float()
# backward compatibility
return clips