-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathtransforms.py
134 lines (112 loc) · 4.82 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
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
import torch
import torchvision.transforms as transforms
import numpy as np
def image_transforms(mode='train', augment_parameters=[0.8, 1.2, 0.5, 2.0, 0.8, 1.2],
do_augmentation=True, transformations=None, size=(256, 512)):
if mode == 'train':
data_transform = transforms.Compose([
ResizeImage(train=True, size=size),
RandomFlip(do_augmentation),
ToTensor(train=True),
AugmentImagePair(augment_parameters, do_augmentation)
])
return data_transform
elif mode == 'test':
data_transform = transforms.Compose([
ResizeImage(train=False, size=size),
ToTensor(train=False),
DoTest(),
])
return data_transform
elif mode == 'custom':
data_transform = transforms.Compose(transformations)
return data_transform
else:
print('Wrong mode')
class ResizeImage(object):
def __init__(self, train=True, size=(256, 512)):
self.train = train
self.transform = transforms.Resize(size)
def __call__(self, sample):
if self.train:
left_image = sample['left_image']
right_image = sample['right_image']
new_right_image = self.transform(right_image)
new_left_image = self.transform(left_image)
sample = {'left_image': new_left_image, 'right_image': new_right_image}
else:
left_image = sample
new_left_image = self.transform(left_image)
sample = new_left_image
return sample
class DoTest(object):
def __call__(self, sample):
new_sample = torch.stack((sample, torch.flip(sample, [2])))
return new_sample
class ToTensor(object):
def __init__(self, train):
self.train = train
self.transform = transforms.ToTensor()
def __call__(self, sample):
if self.train:
left_image = sample['left_image']
right_image = sample['right_image']
new_right_image = self.transform(right_image)
new_left_image = self.transform(left_image)
sample = {'left_image': new_left_image,
'right_image': new_right_image}
else:
left_image = sample
sample = self.transform(left_image)
return sample
class RandomFlip(object):
def __init__(self, do_augmentation):
self.transform = transforms.RandomHorizontalFlip(p=1)
self.do_augmentation = do_augmentation
def __call__(self, sample):
left_image = sample['left_image']
right_image = sample['right_image']
k = np.random.uniform(0, 1, 1)
if self.do_augmentation:
if k > 0.5:
fliped_left = self.transform(right_image)
fliped_right = self.transform(left_image)
sample = {'left_image': fliped_left, 'right_image': fliped_right}
else:
sample = {'left_image': left_image, 'right_image': right_image}
return sample
class AugmentImagePair(object):
def __init__(self, augment_parameters, do_augmentation):
self.do_augmentation = do_augmentation
self.gamma_low = augment_parameters[0] # 0.8
self.gamma_high = augment_parameters[1] # 1.2
self.brightness_low = augment_parameters[2] # 0.5
self.brightness_high = augment_parameters[3] # 2.0
self.color_low = augment_parameters[4] # 0.8
self.color_high = augment_parameters[5] # 1.2
def __call__(self, sample):
left_image = sample['left_image']
right_image = sample['right_image']
p = np.random.uniform(0, 1, 1)
if self.do_augmentation:
if p > 0.5:
# randomly shift gamma
random_gamma = np.random.uniform(self.gamma_low, self.gamma_high)
left_image_aug = left_image ** random_gamma
right_image_aug = right_image ** random_gamma
# randomly shift brightness
random_brightness = np.random.uniform(self.brightness_low, self.brightness_high)
left_image_aug = left_image_aug * random_brightness
right_image_aug = right_image_aug * random_brightness
# randomly shift color
random_colors = np.random.uniform(self.color_low, self.color_high, 3)
for i in range(3):
left_image_aug[i, :, :] *= random_colors[i]
right_image_aug[i, :, :] *= random_colors[i]
# saturate
left_image_aug = torch.clamp(left_image_aug, 0, 1)
right_image_aug = torch.clamp(right_image_aug, 0, 1)
sample = {'left_image': left_image_aug, 'right_image': right_image_aug}
else:
sample = {'left_image': left_image, 'right_image': right_image}
return sample