-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransforms.py
173 lines (137 loc) · 5.91 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
import numpy as np
from PIL import Image,ImageFilter
import random
import torch
from torchvision import transforms as T
from torchvision.transforms import functional as F
import cv2
def pad_if_smaller(img, size, fill=0):
min_size = min(img.size)
if min_size < size:
ow, oh = img.size
padh = size - oh if oh < size else 0
padw = size - ow if ow < size else 0
img = F.pad(img, (0, 0, padw, padh), fill=fill)
return img
class Compose(object):
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, image, target,point=None,sam_mask = None):
for t in self.transforms:
image, target, point,sam_mask = t(image, target,point,sam_mask)
ret = [image,target]
if point is not None:
ret.append(point)
if sam_mask is not None:
ret.append(sam_mask)
return tuple(ret)
class Resize(object):
def __init__(self, h, w):
self.h = h
self.w = w
def __call__(self, image, target,point=None,sam_mask=None):
image = F.resize(image, (self.h, self.w))
# If size is a sequence like (h, w), the output size will be matched to this.
# If size is an int, the smaller edge of the image will be matched to this number maintaining the aspect ratio
target = F.resize(target, (self.h, self.w), interpolation=Image.NEAREST)
return image, target,point,sam_mask
class RandomResize(object):
def __init__(self, min_size, max_size=None):
self.min_size = min_size
if max_size is None:
max_size = min_size
self.max_size = max_size
def __call__(self, image, target,point=None,sam_mask=None):
size = random.randint(self.min_size, self.max_size) # Return a random integer N such that a <= N <= b. Alias for randrange(a, b+1)
image = F.resize(image, size)
# If size is a sequence like (h, w), the output size will be matched to this.
# If size is an int, the smaller edge of the image will be matched to this number maintaining the aspect ratio
target = F.resize(target, size, interpolation=Image.NEAREST)
if sam_mask is not None:
sam_mask = F.resize(sam_mask, size, interpolation=Image.NEAREST)
return image, target,point,sam_mask
class RandomHorizontalFlip(object):
def __init__(self, p):
self.p = p
def __call__(self, image, target,point=None,sam_mask=None):
if random.random() < self.p:
h,w = target.shape
image = F.hflip(image)
target = F.hflip(target)
if point is not None:
point[0] = w-point[0]-1
if sam_mask is not None:
sam_mask = F.hflip(sam_mask)
return image, target, point, sam_mask
class RandomCrop(object):
def __init__(self, size):
self.size = size
def __call__(self, image, target,point=None,sam_mask=None):
image = pad_if_smaller(image, self.size)
target = pad_if_smaller(target, self.size, fill=255)
crop_params = T.RandomCrop.get_params(image, (self.size, self.size))
image = F.crop(image, *crop_params)
target = F.crop(target, *crop_params)
return image, target, point
class CenterCrop(object):
def __init__(self, size):
self.size = size
def __call__(self, image, target,point=None):
image = F.center_crop(image, self.size)
target = F.center_crop(target, self.size)
return image, target, point
class ToTensor(object):
def __call__(self, image, target,point=None,sam_mask=None):
image = F.to_tensor(image)
target = torch.as_tensor(np.asarray(target).copy(), dtype=torch.int64)
if sam_mask is not None:
sam_mask = torch.as_tensor(np.asarray(sam_mask).copy(), dtype=torch.int64)
return image, target, point, sam_mask
class RandomAffine(object):
def __init__(self, angle, translate, scale, shear, resample=0, fillcolor=None):
self.angle = angle
self.translate = translate
self.scale = scale
self.shear = shear
self.resample = resample
self.fillcolor = fillcolor
def __call__(self, image, target,point=None):
affine_params = T.RandomAffine.get_params(self.angle, self.translate, self.scale, self.shear, image.size)
image = F.affine(image, *affine_params)
target = F.affine(target, *affine_params)
return image, target, point
class Normalize(object):
def __init__(self, mean, std):
self.mean = mean
self.std = std
def __call__(self, image, target,point=None,sam_mask=None):
image = F.normalize(image, mean=self.mean, std=self.std)
return image, target, point,sam_mask
class RandomGaussianBlur(object):
def __init__(self,min_sigma=0.1, max_sigma=2.0,p=0.5):
self.min_sigma = min_sigma
self.max_sigma = max_sigma
self.p = p
def __call__(self, image, target,point=None,sam_mask=None):
if random.random() < self.p:
# sigma = np.random.uniform(self.min_sigma, self.max_sigma)
sigma = 1
to_tensor = T.ToTensor()
to_pil = T.ToPILImage()
image = to_pil(image)
image = image.filter(ImageFilter.GaussianBlur(radius=1))
image = to_tensor(image)
# image = F.gaussian_blur(image,kernel_size=3,sigma=sigma)
return image, target, point,sam_mask
class RandomColorJitter(object):
def __init__(self,brightness, contrast, saturation,hue, p=0.5):
self.brightness = brightness
self.contrast = contrast
self.saturation = saturation
self.hue = hue
self.p = p
def __call__(self, image, target,point=None,sam_mask=None):
if random.random() < self.p:
color_jit = T.ColorJitter(self.brightness,self.contrast,self.saturation,self.hue)
image = color_jit(image)
return image, target, point,sam_mask