-
Notifications
You must be signed in to change notification settings - Fork 3
/
transforms.py
94 lines (69 loc) · 2.16 KB
/
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
from pl_bolts.utils import _OPENCV_AVAILABLE, _TORCHVISION_AVAILABLE
from pl_bolts.utils.warnings import warn_missing_pkg
from augmentations.augmentations import RandomCuboid
if _TORCHVISION_AVAILABLE:
from torchvision import transforms as transforms
else: # pragma: no cover
warn_missing_pkg('torchvision')
# Self-supervised
class SimCLRTrainDataTransform(object):
"""
Transforms for SimCLR
"""
def __init__(self, data_transforms) -> None:
augmentations = transforms.Compose([
RandomCuboid(p=1),
*data_transforms
])
self.data_transforms = augmentations
# TODO: Parameterize the crop size
self.online_transform = transforms.Compose([
RandomCuboid(p=1),
])
def __call__(self, sample):
transform = self.data_transforms
xi = transform(sample)
xj = transform(sample)
return xi, xj, self.online_transform(sample)
class SimCLREvalDataTransform(SimCLRTrainDataTransform):
"""
Transforms for SimCLR
"""
def __init__(self, data_transforms) -> None:
super().__init__(data_transforms)
def __call__(self, sample):
transform = self.data_transforms
xi = transform(sample)
xj = transform(sample)
return xi, xj, sample
# Fine-tuning
class FineTuningTrainDataTransform(object):
"""
Transforms for SimCLR
"""
def __init__(self, data_transforms) -> None:
self.data_transforms = transforms.Compose([
*data_transforms
])
def __call__(self, sample):
transform = self.data_transforms
xi = transform(sample)
return xi
class FineTuningEvalDataTransform(object):
"""
Transforms for SimCLR
"""
def __call__(self, sample):
return sample
class FineTuningTestDataTransform(object):
"""
Test transforms for SimCLR fine tuning module.
"""
def __init__(self, data_transforms) -> None:
self.data_transforms = transforms.Compose([
*data_transforms
])
def __call__(self, sample):
transform = self.data_transforms
xi = transform(sample)
return xi