Fabrice-TIERCELIN commited on
Commit
5e16142
·
verified ·
1 Parent(s): f16f1f2

Delete datasets

Browse files
datasets/__init__.py DELETED
File without changes
datasets/__pycache__/__init__.cpython-312.pyc DELETED
Binary file (164 Bytes)
 
datasets/__pycache__/video_transforms.cpython-312.pyc DELETED
Binary file (32.3 kB)
 
datasets/video_transforms.py DELETED
@@ -1,748 +0,0 @@
1
- import torch
2
- import random
3
- import numbers
4
- from torchvision.transforms import RandomCrop, RandomResizedCrop
5
- from PIL import Image
6
- from torchvision.utils import _log_api_usage_once
7
-
8
- def _is_tensor_video_clip(clip):
9
- if not torch.is_tensor(clip):
10
- raise TypeError("clip should be Tensor. Got %s" % type(clip))
11
-
12
- if not clip.ndimension() == 4:
13
- raise ValueError("clip should be 4D. Got %dD" % clip.dim())
14
-
15
- return True
16
-
17
-
18
- def center_crop_arr(pil_image, image_size):
19
- """
20
- Center cropping implementation from ADM.
21
- https://github.com/openai/guided-diffusion/blob/8fb3ad9197f16bbc40620447b2742e13458d2831/guided_diffusion/image_datasets.py#L126
22
- """
23
- while min(*pil_image.size) >= 2 * image_size:
24
- pil_image = pil_image.resize(
25
- tuple(x // 2 for x in pil_image.size), resample=Image.BOX
26
- )
27
-
28
- scale = image_size / min(*pil_image.size)
29
- pil_image = pil_image.resize(
30
- tuple(round(x * scale) for x in pil_image.size), resample=Image.BICUBIC
31
- )
32
-
33
- arr = np.array(pil_image)
34
- crop_y = (arr.shape[0] - image_size) // 2
35
- crop_x = (arr.shape[1] - image_size) // 2
36
- return Image.fromarray(arr[crop_y: crop_y + image_size, crop_x: crop_x + image_size])
37
-
38
-
39
- def crop(clip, i, j, h, w):
40
- """
41
- Args:
42
- clip (torch.tensor): Video clip to be cropped. Size is (T, C, H, W)
43
- """
44
- if len(clip.size()) != 4:
45
- raise ValueError("clip should be a 4D tensor")
46
- return clip[..., i : i + h, j : j + w]
47
-
48
-
49
- def resize(clip, target_size, interpolation_mode):
50
- if len(target_size) != 2:
51
- raise ValueError(f"target size should be tuple (height, width), instead got {target_size}")
52
- return torch.nn.functional.interpolate(clip, size=target_size, mode=interpolation_mode, align_corners=False)
53
-
54
- def resize_scale(clip, target_size, interpolation_mode):
55
- if len(target_size) != 2:
56
- raise ValueError(f"target size should be tuple (height, width), instead got {target_size}")
57
- H, W = clip.size(-2), clip.size(-1)
58
- scale_ = target_size[0] / min(H, W)
59
- return torch.nn.functional.interpolate(clip, scale_factor=scale_, mode=interpolation_mode, align_corners=False)
60
-
61
- def resize_with_scale_factor(clip, scale_factor, interpolation_mode):
62
- return torch.nn.functional.interpolate(clip, scale_factor=scale_factor, mode=interpolation_mode, align_corners=False)
63
-
64
- def resize_scale_with_height(clip, target_size, interpolation_mode):
65
- H, W = clip.size(-2), clip.size(-1)
66
- scale_ = target_size / H
67
- return torch.nn.functional.interpolate(clip, scale_factor=scale_, mode=interpolation_mode, align_corners=False)
68
-
69
- def resize_scale_with_weight(clip, target_size, interpolation_mode):
70
- H, W = clip.size(-2), clip.size(-1)
71
- scale_ = target_size / W
72
- return torch.nn.functional.interpolate(clip, scale_factor=scale_, mode=interpolation_mode, align_corners=False)
73
-
74
-
75
- def resized_crop(clip, i, j, h, w, size, interpolation_mode="bilinear"):
76
- """
77
- Do spatial cropping and resizing to the video clip
78
- Args:
79
- clip (torch.tensor): Video clip to be cropped. Size is (T, C, H, W)
80
- i (int): i in (i,j) i.e coordinates of the upper left corner.
81
- j (int): j in (i,j) i.e coordinates of the upper left corner.
82
- h (int): Height of the cropped region.
83
- w (int): Width of the cropped region.
84
- size (tuple(int, int)): height and width of resized clip
85
- Returns:
86
- clip (torch.tensor): Resized and cropped clip. Size is (T, C, H, W)
87
- """
88
- if not _is_tensor_video_clip(clip):
89
- raise ValueError("clip should be a 4D torch.tensor")
90
- clip = crop(clip, i, j, h, w)
91
- clip = resize(clip, size, interpolation_mode)
92
- return clip
93
-
94
-
95
- def center_crop(clip, crop_size):
96
- if not _is_tensor_video_clip(clip):
97
- raise ValueError("clip should be a 4D torch.tensor")
98
- h, w = clip.size(-2), clip.size(-1)
99
- # print(clip.shape)
100
- th, tw = crop_size
101
- if h < th or w < tw:
102
- # print(h, w)
103
- raise ValueError("height {} and width {} must be no smaller than crop_size".format(h, w))
104
-
105
- i = int(round((h - th) / 2.0))
106
- j = int(round((w - tw) / 2.0))
107
- return crop(clip, i, j, th, tw), i, j
108
-
109
-
110
- def center_crop_using_short_edge(clip):
111
- if not _is_tensor_video_clip(clip):
112
- raise ValueError("clip should be a 4D torch.tensor")
113
- h, w = clip.size(-2), clip.size(-1)
114
- if h < w:
115
- th, tw = h, h
116
- i = 0
117
- j = int(round((w - tw) / 2.0))
118
- else:
119
- th, tw = w, w
120
- i = int(round((h - th) / 2.0))
121
- j = 0
122
- return crop(clip, i, j, th, tw)
123
-
124
-
125
- def random_shift_crop(clip):
126
- '''
127
- Slide along the long edge, with the short edge as crop size
128
- '''
129
- if not _is_tensor_video_clip(clip):
130
- raise ValueError("clip should be a 4D torch.tensor")
131
- h, w = clip.size(-2), clip.size(-1)
132
-
133
- if h <= w:
134
- long_edge = w
135
- short_edge = h
136
- else:
137
- long_edge = h
138
- short_edge =w
139
-
140
- th, tw = short_edge, short_edge
141
-
142
- i = torch.randint(0, h - th + 1, size=(1,)).item()
143
- j = torch.randint(0, w - tw + 1, size=(1,)).item()
144
- return crop(clip, i, j, th, tw), i, j
145
-
146
- def random_crop(clip, crop_size):
147
- if not _is_tensor_video_clip(clip):
148
- raise ValueError("clip should be a 4D torch.tensor")
149
- h, w = clip.size(-2), clip.size(-1)
150
- th, tw = crop_size[-2], crop_size[-1]
151
-
152
- if h < th or w < tw:
153
- raise ValueError("height {} and width {} must be no smaller than crop_size".format(h, w))
154
-
155
- i = torch.randint(0, h - th + 1, size=(1,)).item()
156
- j = torch.randint(0, w - tw + 1, size=(1,)).item()
157
- clip_crop = crop(clip, i, j, th, tw)
158
- return clip_crop, i, j
159
-
160
-
161
- def to_tensor(clip):
162
- """
163
- Convert tensor data type from uint8 to float, divide value by 255.0 and
164
- permute the dimensions of clip tensor
165
- Args:
166
- clip (torch.tensor, dtype=torch.uint8): Size is (T, C, H, W)
167
- Return:
168
- clip (torch.tensor, dtype=torch.float): Size is (T, C, H, W)
169
- """
170
- _is_tensor_video_clip(clip)
171
- if not clip.dtype == torch.uint8:
172
- raise TypeError("clip tensor should have data type uint8. Got %s" % str(clip.dtype))
173
- # return clip.float().permute(3, 0, 1, 2) / 255.0
174
- return clip.float() / 255.0
175
-
176
-
177
- def normalize(clip, mean, std, inplace=False):
178
- """
179
- Args:
180
- clip (torch.tensor): Video clip to be normalized. Size is (T, C, H, W)
181
- mean (tuple): pixel RGB mean. Size is (3)
182
- std (tuple): pixel standard deviation. Size is (3)
183
- Returns:
184
- normalized clip (torch.tensor): Size is (T, C, H, W)
185
- """
186
- if not _is_tensor_video_clip(clip):
187
- raise ValueError("clip should be a 4D torch.tensor")
188
- if not inplace:
189
- clip = clip.clone()
190
- mean = torch.as_tensor(mean, dtype=clip.dtype, device=clip.device)
191
- # print(mean)
192
- std = torch.as_tensor(std, dtype=clip.dtype, device=clip.device)
193
- clip.sub_(mean[:, None, None, None]).div_(std[:, None, None, None])
194
- return clip
195
-
196
-
197
- def hflip(clip):
198
- """
199
- Args:
200
- clip (torch.tensor): Video clip to be normalized. Size is (T, C, H, W)
201
- Returns:
202
- flipped clip (torch.tensor): Size is (T, C, H, W)
203
- """
204
- if not _is_tensor_video_clip(clip):
205
- raise ValueError("clip should be a 4D torch.tensor")
206
- return clip.flip(-1)
207
-
208
-
209
- class RandomCropVideo:
210
- def __init__(self, size):
211
- if isinstance(size, numbers.Number):
212
- self.size = (int(size), int(size))
213
- else:
214
- self.size = size
215
-
216
- def __call__(self, clip):
217
- """
218
- Args:
219
- clip (torch.tensor): Video clip to be cropped. Size is (T, C, H, W)
220
- Returns:
221
- torch.tensor: randomly cropped video clip.
222
- size is (T, C, OH, OW)
223
- """
224
- i, j, h, w = self.get_params(clip)
225
- return crop(clip, i, j, h, w)
226
-
227
- def get_params(self, clip):
228
- h, w = clip.shape[-2:]
229
- th, tw = self.size
230
-
231
- if h < th or w < tw:
232
- raise ValueError(f"Required crop size {(th, tw)} is larger than input image size {(h, w)}")
233
-
234
- if w == tw and h == th:
235
- return 0, 0, h, w
236
-
237
- i = torch.randint(0, h - th + 1, size=(1,)).item()
238
- j = torch.randint(0, w - tw + 1, size=(1,)).item()
239
-
240
- return i, j, th, tw
241
-
242
- def __repr__(self) -> str:
243
- return f"{self.__class__.__name__}(size={self.size})"
244
-
245
- class CenterCropResizeVideo:
246
- '''
247
- First use the short side for cropping length,
248
- center crop video, then resize to the specified size
249
- '''
250
- def __init__(
251
- self,
252
- size,
253
- interpolation_mode="bilinear",
254
- ):
255
- if isinstance(size, tuple):
256
- if len(size) != 2:
257
- raise ValueError(f"size should be tuple (height, width), instead got {size}")
258
- self.size = size
259
- else:
260
- self.size = (size, size)
261
-
262
- self.interpolation_mode = interpolation_mode
263
-
264
-
265
- def __call__(self, clip):
266
- """
267
- Args:
268
- clip (torch.tensor): Video clip to be cropped. Size is (T, C, H, W)
269
- Returns:
270
- torch.tensor: scale resized / center cropped video clip.
271
- size is (T, C, crop_size, crop_size)
272
- """
273
- # print(clip.shape)
274
- clip_center_crop = center_crop_using_short_edge(clip)
275
- # print(clip_center_crop.shape) 320 512
276
- clip_center_crop_resize = resize(clip_center_crop, target_size=self.size, interpolation_mode=self.interpolation_mode)
277
- return clip_center_crop_resize
278
-
279
- def __repr__(self) -> str:
280
- return f"{self.__class__.__name__}(size={self.size}, interpolation_mode={self.interpolation_mode}"
281
-
282
-
283
- class SDXL:
284
- def __init__(
285
- self,
286
- size,
287
- interpolation_mode="bilinear",
288
- ):
289
- if isinstance(size, tuple):
290
- if len(size) != 2:
291
- raise ValueError(f"size should be tuple (height, width), instead got {size}")
292
- self.size = size
293
- else:
294
- self.size = (size, size)
295
-
296
- self.interpolation_mode = interpolation_mode
297
-
298
- def __call__(self, clip):
299
- """
300
- Args:
301
- clip (torch.tensor): Video clip to be cropped. Size is (T, C, H, W)
302
- Returns:
303
- torch.tensor: scale resized / center cropped video clip.
304
- size is (T, C, crop_size, crop_size)
305
- """
306
- # add aditional one pixel for avoiding error in center crop
307
- ori_h, ori_w = clip.size(-2), clip.size(-1)
308
- tar_h, tar_w = self.size[0] + 1, self.size[1] + 1
309
-
310
- # if ori_h >= tar_h and ori_w >= tar_w:
311
- # clip_tar_crop, i, j = random_crop(clip=clip, crop_size=self.size)
312
- # else:
313
- # tar_h_div_ori_h = tar_h / ori_h
314
- # tar_w_div_ori_w = tar_w / ori_w
315
- # if tar_h_div_ori_h > tar_w_div_ori_w:
316
- # clip = resize_with_scale_factor(clip=clip, scale_factor=tar_h_div_ori_h, interpolation_mode=self.interpolation_mode)
317
- # else:
318
- # clip = resize_with_scale_factor(clip=clip, scale_factor=tar_w_div_ori_w, interpolation_mode=self.interpolation_mode)
319
- # clip_tar_crop, i, j = random_crop(clip, self.size)
320
- if ori_h >= tar_h and ori_w >= tar_w:
321
- tar_h_div_ori_h = tar_h / ori_h
322
- tar_w_div_ori_w = tar_w / ori_w
323
- if tar_h_div_ori_h > tar_w_div_ori_w:
324
- clip = resize_with_scale_factor(clip=clip, scale_factor=tar_h_div_ori_h, interpolation_mode=self.interpolation_mode)
325
- else:
326
- clip = resize_with_scale_factor(clip=clip, scale_factor=tar_w_div_ori_w, interpolation_mode=self.interpolation_mode)
327
- ori_h, ori_w = clip.size(-2), clip.size(-1)
328
- clip_tar_crop, i, j = random_crop(clip, self.size)
329
- else:
330
- tar_h_div_ori_h = tar_h / ori_h
331
- tar_w_div_ori_w = tar_w / ori_w
332
- if tar_h_div_ori_h > tar_w_div_ori_w:
333
- clip = resize_with_scale_factor(clip=clip, scale_factor=tar_h_div_ori_h, interpolation_mode=self.interpolation_mode)
334
- else:
335
- clip = resize_with_scale_factor(clip=clip, scale_factor=tar_w_div_ori_w, interpolation_mode=self.interpolation_mode)
336
- clip_tar_crop, i, j = random_crop(clip, self.size)
337
- return clip_tar_crop, ori_h, ori_w, i, j
338
-
339
- def __repr__(self) -> str:
340
- return f"{self.__class__.__name__}(size={self.size}, interpolation_mode={self.interpolation_mode}"
341
-
342
-
343
- class SDXLCenterCrop:
344
- def __init__(
345
- self,
346
- size,
347
- interpolation_mode="bilinear",
348
- ):
349
- if isinstance(size, tuple):
350
- if len(size) != 2:
351
- raise ValueError(f"size should be tuple (height, width), instead got {size}")
352
- self.size = size
353
- else:
354
- self.size = (size, size)
355
-
356
- self.interpolation_mode = interpolation_mode
357
-
358
-
359
- def __call__(self, clip):
360
- """
361
- Args:
362
- clip (torch.tensor): Video clip to be cropped. Size is (T, C, H, W)
363
- Returns:
364
- torch.tensor: scale resized / center cropped video clip.
365
- size is (T, C, crop_size, crop_size)
366
- """
367
- # add aditional one pixel for avoiding error in center crop
368
- ori_h, ori_w = clip.size(-2), clip.size(-1)
369
- tar_h, tar_w = self.size[0] + 1, self.size[1] + 1
370
- tar_h_div_ori_h = tar_h / ori_h
371
- tar_w_div_ori_w = tar_w / ori_w
372
- # print('before resize', clip.shape)
373
- if tar_h_div_ori_h > tar_w_div_ori_w:
374
- clip = resize_with_scale_factor(clip=clip, scale_factor=tar_h_div_ori_h, interpolation_mode=self.interpolation_mode)
375
- # print('after h resize', clip.shape)
376
- else:
377
- clip = resize_with_scale_factor(clip=clip, scale_factor=tar_w_div_ori_w, interpolation_mode=self.interpolation_mode)
378
- # print('after resize', clip.shape)
379
- # print(clip.shape)
380
- # clip_tar_crop, i, j = random_crop(clip, self.size)
381
- clip_tar_crop, i, j = center_crop(clip, self.size)
382
- # print('after crop', clip_tar_crop.shape)
383
-
384
- return clip_tar_crop, ori_h, ori_w, i, j
385
-
386
- def __repr__(self) -> str:
387
- return f"{self.__class__.__name__}(size={self.size}, interpolation_mode={self.interpolation_mode}"
388
-
389
-
390
- class InternVideo320512:
391
- def __init__(
392
- self,
393
- size,
394
- interpolation_mode="bilinear",
395
- ):
396
- if isinstance(size, tuple):
397
- if len(size) != 2:
398
- raise ValueError(f"size should be tuple (height, width), instead got {size}")
399
- self.size = size
400
- else:
401
- self.size = (size, size)
402
-
403
- self.interpolation_mode = interpolation_mode
404
-
405
-
406
- def __call__(self, clip):
407
- """
408
- Args:
409
- clip (torch.tensor): Video clip to be cropped. Size is (T, C, H, W)
410
- Returns:
411
- torch.tensor: scale resized / center cropped video clip.
412
- size is (T, C, crop_size, crop_size)
413
- """
414
- # add aditional one pixel for avoiding error in center crop
415
- h, w = clip.size(-2), clip.size(-1)
416
- # print('before resize', clip.shape)
417
- if h < 320:
418
- clip = resize_scale_with_height(clip=clip, target_size=321, interpolation_mode=self.interpolation_mode)
419
- # print('after h resize', clip.shape)
420
- if w < 512:
421
- clip = resize_scale_with_weight(clip=clip, target_size=513, interpolation_mode=self.interpolation_mode)
422
- # print('after w resize', clip.shape)
423
- # print(clip.shape)
424
- clip_center_crop = center_crop(clip, self.size)
425
- clip_center_crop_no_subtitles = center_crop(clip, (220, 352))
426
- clip_center_resize = resize(clip_center_crop_no_subtitles, target_size=self.size, interpolation_mode=self.interpolation_mode)
427
- # print(clip_center_crop.shape)
428
- return clip_center_resize
429
-
430
- def __repr__(self) -> str:
431
- return f"{self.__class__.__name__}(size={self.size}, interpolation_mode={self.interpolation_mode}"
432
-
433
- class CenterCropVideo:
434
- '''
435
- First scale to the specified size in equal proportion to the short edge,
436
- then center cropping
437
- '''
438
- def __init__(
439
- self,
440
- size,
441
- interpolation_mode="bilinear",
442
- ):
443
- if isinstance(size, tuple):
444
- if len(size) != 2:
445
- raise ValueError(f"size should be tuple (height, width), instead got {size}")
446
- self.size = size
447
- else:
448
- self.size = (size, size)
449
-
450
- self.interpolation_mode = interpolation_mode
451
-
452
-
453
- def __call__(self, clip):
454
- """
455
- Args:
456
- clip (torch.tensor): Video clip to be cropped. Size is (T, C, H, W)
457
- Returns:
458
- torch.tensor: scale resized / center cropped video clip.
459
- size is (T, C, crop_size, crop_size)
460
- """
461
- clip_resize = resize_scale(clip=clip, target_size=self.size, interpolation_mode=self.interpolation_mode)
462
- clip_center_crop = center_crop(clip_resize, self.size)
463
- return clip_center_crop
464
-
465
- def __repr__(self) -> str:
466
- return f"{self.__class__.__name__}(size={self.size}, interpolation_mode={self.interpolation_mode}"
467
-
468
- class KineticsRandomCropResizeVideo:
469
- '''
470
- Slide along the long edge, with the short edge as crop size. And resie to the desired size.
471
- '''
472
- def __init__(
473
- self,
474
- size,
475
- interpolation_mode="bilinear",
476
- ):
477
- if isinstance(size, tuple):
478
- if len(size) != 2:
479
- raise ValueError(f"size should be tuple (height, width), instead got {size}")
480
- self.size = size
481
- else:
482
- self.size = (size, size)
483
-
484
- self.interpolation_mode = interpolation_mode
485
-
486
- def __call__(self, clip):
487
- clip_random_crop = random_shift_crop(clip)
488
- clip_resize = resize(clip_random_crop, self.size, self.interpolation_mode)
489
- return clip_resize
490
-
491
- class ResizeVideo():
492
- '''
493
- First use the short side for cropping length,
494
- center crop video, then resize to the specified size
495
- '''
496
- def __init__(
497
- self,
498
- size,
499
- interpolation_mode="bilinear",
500
- ):
501
- if isinstance(size, tuple):
502
- if len(size) != 2:
503
- raise ValueError(f"size should be tuple (height, width), instead got {size}")
504
- self.size = size
505
- else:
506
- self.size = (size, size)
507
-
508
- self.interpolation_mode = interpolation_mode
509
-
510
-
511
- def __call__(self, clip):
512
- """
513
- Args:
514
- clip (torch.tensor): Video clip to be cropped. Size is (T, C, H, W)
515
- Returns:
516
- torch.tensor: scale resized / center cropped video clip.
517
- size is (T, C, crop_size, crop_size)
518
- """
519
- clip_resize = resize(clip, target_size=self.size, interpolation_mode=self.interpolation_mode)
520
- return clip_resize
521
-
522
- def __repr__(self) -> str:
523
- return f"{self.__class__.__name__}(size={self.size}, interpolation_mode={self.interpolation_mode}"
524
-
525
- class CenterCropVideo:
526
- def __init__(
527
- self,
528
- size,
529
- interpolation_mode="bilinear",
530
- ):
531
- if isinstance(size, tuple):
532
- if len(size) != 2:
533
- raise ValueError(f"size should be tuple (height, width), instead got {size}")
534
- self.size = size
535
- else:
536
- self.size = (size, size)
537
-
538
- self.interpolation_mode = interpolation_mode
539
-
540
-
541
- def __call__(self, clip):
542
- """
543
- Args:
544
- clip (torch.tensor): Video clip to be cropped. Size is (T, C, H, W)
545
- Returns:
546
- torch.tensor: center cropped video clip.
547
- size is (T, C, crop_size, crop_size)
548
- """
549
- clip_center_crop = center_crop(clip, self.size)
550
- return clip_center_crop
551
-
552
- def __repr__(self) -> str:
553
- return f"{self.__class__.__name__}(size={self.size}, interpolation_mode={self.interpolation_mode}"
554
-
555
-
556
- class NormalizeVideo:
557
- """
558
- Normalize the video clip by mean subtraction and division by standard deviation
559
- Args:
560
- mean (3-tuple): pixel RGB mean
561
- std (3-tuple): pixel RGB standard deviation
562
- inplace (boolean): whether do in-place normalization
563
- """
564
-
565
- def __init__(self, mean, std, inplace=False):
566
- self.mean = mean
567
- self.std = std
568
- self.inplace = inplace
569
-
570
- def __call__(self, clip):
571
- """
572
- Args:
573
- clip (torch.tensor): video clip must be normalized. Size is (C, T, H, W)
574
- """
575
- return normalize(clip, self.mean, self.std, self.inplace)
576
-
577
- def __repr__(self) -> str:
578
- return f"{self.__class__.__name__}(mean={self.mean}, std={self.std}, inplace={self.inplace})"
579
-
580
-
581
- class ToTensorVideo:
582
- """
583
- Convert tensor data type from uint8 to float, divide value by 255.0 and
584
- permute the dimensions of clip tensor
585
- """
586
-
587
- def __init__(self):
588
- pass
589
-
590
- def __call__(self, clip):
591
- """
592
- Args:
593
- clip (torch.tensor, dtype=torch.uint8): Size is (T, C, H, W)
594
- Return:
595
- clip (torch.tensor, dtype=torch.float): Size is (T, C, H, W)
596
- """
597
- return to_tensor(clip)
598
-
599
- def __repr__(self) -> str:
600
- return self.__class__.__name__
601
-
602
-
603
- class RandomHorizontalFlipVideo:
604
- """
605
- Flip the video clip along the horizontal direction with a given probability
606
- Args:
607
- p (float): probability of the clip being flipped. Default value is 0.5
608
- """
609
-
610
- def __init__(self, p=0.5):
611
- self.p = p
612
-
613
- def __call__(self, clip):
614
- """
615
- Args:
616
- clip (torch.tensor): Size is (T, C, H, W)
617
- Return:
618
- clip (torch.tensor): Size is (T, C, H, W)
619
- """
620
- if random.random() < self.p:
621
- clip = hflip(clip)
622
- return clip
623
-
624
- def __repr__(self) -> str:
625
- return f"{self.__class__.__name__}(p={self.p})"
626
-
627
- class Compose:
628
- """Composes several transforms together. This transform does not support torchscript.
629
- Please, see the note below.
630
-
631
- Args:
632
- transforms (list of ``Transform`` objects): list of transforms to compose.
633
-
634
- Example:
635
- >>> transforms.Compose([
636
- >>> transforms.CenterCrop(10),
637
- >>> transforms.PILToTensor(),
638
- >>> transforms.ConvertImageDtype(torch.float),
639
- >>> ])
640
-
641
- .. note::
642
- In order to script the transformations, please use ``torch.nn.Sequential`` as below.
643
-
644
- >>> transforms = torch.nn.Sequential(
645
- >>> transforms.CenterCrop(10),
646
- >>> transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
647
- >>> )
648
- >>> scripted_transforms = torch.jit.script(transforms)
649
-
650
- Make sure to use only scriptable transformations, i.e. that work with ``torch.Tensor``, does not require
651
- `lambda` functions or ``PIL.Image``.
652
-
653
- """
654
-
655
- def __init__(self, transforms):
656
- if not torch.jit.is_scripting() and not torch.jit.is_tracing():
657
- _log_api_usage_once(self)
658
- self.transforms = transforms
659
-
660
- def __call__(self, img):
661
- for t in self.transforms:
662
- if isinstance(t, SDXLCenterCrop) or isinstance(t, SDXL):
663
- img, ori_h, ori_w, crops_coords_top, crops_coords_left = t(img)
664
- else:
665
- img = t(img)
666
- return img, ori_h, ori_w, crops_coords_top, crops_coords_left
667
-
668
- def __repr__(self) -> str:
669
- format_string = self.__class__.__name__ + "("
670
- for t in self.transforms:
671
- format_string += "\n"
672
- format_string += f" {t}"
673
- format_string += "\n)"
674
- return format_string
675
-
676
- # ------------------------------------------------------------
677
- # --------------------- Sampling ---------------------------
678
- # ------------------------------------------------------------
679
- class TemporalRandomCrop(object):
680
- """Temporally crop the given frame indices at a random location.
681
-
682
- Args:
683
- size (int): Desired length of frames will be seen in the model.
684
- """
685
-
686
- def __init__(self, size):
687
- self.size = size
688
-
689
- def __call__(self, total_frames):
690
- rand_end = max(0, total_frames - self.size - 1)
691
- begin_index = random.randint(0, rand_end)
692
- end_index = min(begin_index + self.size, total_frames)
693
- return begin_index, end_index
694
-
695
-
696
- if __name__ == '__main__':
697
- from torchvision import transforms
698
- import torchvision.io as io
699
- import numpy as np
700
- from torchvision.utils import save_image
701
- import os
702
-
703
- vframes, aframes, info = io.read_video(
704
- filename='./v_Archery_g01_c03.avi',
705
- pts_unit='sec',
706
- output_format='TCHW'
707
- )
708
-
709
- trans = transforms.Compose([
710
- ToTensorVideo(),
711
- RandomHorizontalFlipVideo(),
712
- UCFCenterCropVideo(512),
713
- # NormalizeVideo(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5], inplace=True),
714
- transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5], inplace=True)
715
- ])
716
-
717
- target_video_len = 32
718
- frame_interval = 1
719
- total_frames = len(vframes)
720
- print(total_frames)
721
-
722
- temporal_sample = TemporalRandomCrop(target_video_len * frame_interval)
723
-
724
-
725
- # Sampling video frames
726
- start_frame_ind, end_frame_ind = temporal_sample(total_frames)
727
- # print(start_frame_ind)
728
- # print(end_frame_ind)
729
- assert end_frame_ind - start_frame_ind >= target_video_len
730
- frame_indice = np.linspace(start_frame_ind, end_frame_ind - 1, target_video_len, dtype=int)
731
- print(frame_indice)
732
-
733
- select_vframes = vframes[frame_indice]
734
- print(select_vframes.shape)
735
- print(select_vframes.dtype)
736
-
737
- select_vframes_trans = trans(select_vframes)
738
- print(select_vframes_trans.shape)
739
- print(select_vframes_trans.dtype)
740
-
741
- select_vframes_trans_int = ((select_vframes_trans * 0.5 + 0.5) * 255).to(dtype=torch.uint8)
742
- print(select_vframes_trans_int.dtype)
743
- print(select_vframes_trans_int.permute(0, 2, 3, 1).shape)
744
-
745
- io.write_video('./test.avi', select_vframes_trans_int.permute(0, 2, 3, 1), fps=8)
746
-
747
- for i in range(target_video_len):
748
- save_image(select_vframes_trans[i], os.path.join('./test000', '%04d.png' % i), normalize=True, value_range=(-1, 1))