-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaugmentation.py
More file actions
36 lines (29 loc) · 1.3 KB
/
Copy pathaugmentation.py
File metadata and controls
36 lines (29 loc) · 1.3 KB
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
import torch
import torch.nn as nn
from torchvision.transforms import v2
class RandomTimeFlip(v2.Transform):
def __init__(self, p:float = 0.5):
super().__init__()
self.p = p
def forward(self, x):
if torch.rand(1).item() < self.p:
x = torch.stack((x[1], x[0]))
return x
def get_transform(dataset_name):
if dataset_name == "mgif":
transforms = v2.Compose([
v2.ToDtype(torch.uint8, scale=True),
RandomTimeFlip(0.5),
v2.RandomHorizontalFlip(0.5),
v2.RandomCrop(256),
v2.ColorJitter(hue=0.5),
v2.ToDtype(torch.float32, scale=True),])
elif dataset_name == "voxceleb":
transforms = v2.Compose([
v2.ToDtype(torch.uint8, scale=True),
RandomTimeFlip(0.5),
v2.RandomHorizontalFlip(0.5),
v2.Resize(256, antialias=True),
v2.ColorJitter(brightness=0.1, contrast=0.1, saturation=0.1, hue=0.1),
v2.ToDtype(torch.float32, scale=True),])
return transforms