-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransforms.py
174 lines (132 loc) · 5 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
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
import torch
import random
import numpy as np
from PIL import Image, ImageEnhance
import torchvision.transforms as transforms
import copy
class ToTensor(object):
"""Convert ndarrays in sample to Tensors."""
def __call__(self, sample):
# swap color axis because
# numpy image: H x W x C
# torch image: C X H X W
img1 = sample["image"][0]
img2 = sample["image"][1]
mask = sample["label"]
img1 = np.array(img1).astype(np.float32).transpose((2, 0, 1))
img2 = np.array(img2).astype(np.float32).transpose((2, 0, 1))
# mask = np.array(mask).astype(np.float32)
mask = np.array(mask).astype(np.float32) / 255.0
img1 = torch.from_numpy(img1).float()
img2 = torch.from_numpy(img2).float()
mask = torch.from_numpy(mask).float()
return {"image": (img1, img2), "label": mask}
class RandomHorizontalFlip(object):
def __call__(self, sample):
img1 = sample["image"][0]
img2 = sample["image"][1]
mask = sample["label"]
if random.random() < 0.5:
img1 = img1.transpose(Image.FLIP_LEFT_RIGHT)
img2 = img2.transpose(Image.FLIP_LEFT_RIGHT)
mask = mask.transpose(Image.FLIP_LEFT_RIGHT)
return {"image": (img1, img2), "label": mask}
class RandomVerticalFlip(object):
def __call__(self, sample):
img1 = sample["image"][0]
img2 = sample["image"][1]
mask = sample["label"]
if random.random() < 0.5:
img1 = img1.transpose(Image.FLIP_TOP_BOTTOM)
img2 = img2.transpose(Image.FLIP_TOP_BOTTOM)
mask = mask.transpose(Image.FLIP_TOP_BOTTOM)
return {"image": (img1, img2), "label": mask}
class RandomRotate(object):
def __init__(self, degree):
self.degree = degree
def __call__(self, sample):
img1 = sample["image"][0]
img2 = sample["image"][1]
mask = sample["label"]
if random.random() < 0.5:
rotate_degree = random.uniform(-1 * self.degree, self.degree)
img1 = img1.rotate(rotate_degree, Image.BILINEAR)
img2 = img2.rotate(rotate_degree, Image.BILINEAR)
mask = mask.rotate(rotate_degree, Image.NEAREST)
return {"image": (img1, img2), "label": mask}
class Shift(object):
# def __init__(self, size):
# self.size = (size, size) # size: (h, w)
def __call__(self, sample):
img1 = sample["image"][0]
img2 = sample["image"][1]
mask = sample["label"]
Rx = random.randint(-32, 32)
Rx = Rx + 256 if Rx < 0 else Rx
Ry = random.randint(-32, 32)
Ry = Ry + 256 if Ry < 0 else Ry
img1_p = copy.deepcopy(img1)
img2_p = copy.deepcopy(img2)
mask_p = copy.deepcopy(mask)
img1.paste(img1_p, (Rx, Ry))
img1.paste(img1_p, (Rx - 256, Ry - 256))
img1.paste(img1_p, (Rx - 256, Ry))
img1.paste(img1_p, (Rx, Ry - 256))
img2.paste(img2_p, (Rx, Ry))
img2.paste(img2_p, (Rx - 256, Ry - 256))
img2.paste(img2_p, (Rx - 256, Ry))
img2.paste(img2_p, (Rx, Ry - 256))
mask.paste(mask_p, (Rx, Ry))
mask.paste(mask_p, (Rx - 256, Ry - 256))
mask.paste(mask_p, (Rx - 256, Ry))
mask.paste(mask_p, (Rx, Ry - 256))
return {"image": (img1, img2), "label": mask}
def randomColor(image):
"""
对图像进行颜色抖动
:param image: PIL的图像image
:return: 有颜色色差的图像image
"""
returnImage = image
random_factor = np.random.rand() * 0.7 + 0.8 # 随机因子 0.8~1.5
returnImage = ImageEnhance.Color(returnImage).enhance(
random_factor
) # 调整图像的饱和度
random_factor = np.random.rand() * 0.7 + 0.8 # 随机因子 0.8~1.5
returnImage = ImageEnhance.Brightness(returnImage).enhance(
random_factor
) # 调整图像的亮度
random_factor = np.random.rand() * 0.7 + 0.8 # 随机因子 0.8~1.5
returnImage = ImageEnhance.Contrast(returnImage).enhance(
random_factor
) # 调整图像对比度
random_factor = np.random.rand() * 2.2 + 0.8 # 随机因子 0.8~3.0
returnImage = ImageEnhance.Sharpness(returnImage).enhance(
random_factor
) # 调整图像锐度
return returnImage
class RandomColor(object):
# def __init__(self, size):
# self.size = (size, size) # size: (h, w)
def __call__(self, sample):
img1 = sample["image"][0]
img2 = sample["image"][1]
mask = sample["label"]
img1 = randomColor(img1)
img2 = randomColor(img2)
return {"image": (img1, img2), "label": mask}
"""
We don't use Normalize here, because it will bring negative effects.
the mask of ground truth is converted to [0,1] in ToTensor() function.
"""
train_transforms = transforms.Compose(
[
RandomHorizontalFlip(),
RandomVerticalFlip(),
RandomColor(),
Shift(),
RandomRotate(180),
ToTensor(),
]
)
test_transforms = transforms.Compose([ToTensor()])