-
Notifications
You must be signed in to change notification settings - Fork 15
/
joint_transforms.py
96 lines (76 loc) · 3.01 KB
/
joint_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
import numbers
import random
from PIL import Image, ImageOps
Image.MAX_IMAGE_PIXELS = 1000000000
class Compose(object):
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, img, mask):
assert img.size == mask.size
for t in self.transforms:
img, mask = t(img, mask)
return img, mask
# class RandomCrop(object):
# def __init__(self, size, padding=0):
# if isinstance(size, numbers.Number):
# self.size = (int(size), int(size))
# else:
# self.size = size
# self.padding = padding
#
# def __call__(self, img, mask):
# if self.padding > 0:
# img = ImageOps.expand(img, border=self.padding, fill=0)
# mask = ImageOps.expand(mask, border=self.padding, fill=0)
#
# assert img.size == mask.size
# w, h = img.size
# th, tw = self.size
# if w == tw and h == th:
# return img, mask
# if w < tw or h < th:
# return img.resize((tw, th), Image.BILINEAR), mask.resize((tw, th), Image.NEAREST)
#
# x1 = random.randint(0, w - tw)
# y1 = random.randint(0, h - th)
# return img.crop((x1, y1, x1 + tw, y1 + th)), mask.crop((x1, y1, x1 + tw, y1 + th))
class RandomCrop(object):
def __init__(self, size,size1, padding=0):
if isinstance(size, numbers.Number):
self.size = (int(size), int(size1))
else:
self.size = size
self.padding = padding
def __call__(self, img, mask):
if self.padding > 0:
img = ImageOps.expand(img, border=self.padding, fill=0)
mask = ImageOps.expand(mask, border=self.padding, fill=0)
assert img.size == mask.size
w, h = img.size
th, tw = self.size
if w == tw and h == th:
return img, mask
if w < tw or h < th:
return img.resize((tw, th), Image.BILINEAR), mask.resize((tw, th), Image.NEAREST)
return img.resize((tw, th), Image.BILINEAR), mask.resize((tw, th), Image.NEAREST)
class RandomHorizontallyFlip(object):
def __call__(self, img, mask):
if random.random() < 0.5:
return img.transpose(Image.FLIP_LEFT_RIGHT), mask.transpose(Image.FLIP_LEFT_RIGHT)
return img, mask
class RandomHorizontallyFlip(object):
def __call__(self, img, mask):
if random.random() < 0.5:
return img.transpose(Image.FLIP_LEFT_RIGHT), mask.transpose(Image.FLIP_LEFT_RIGHT)
return img, mask
class RandomVerticallyFlip(object):
def __call__(self, img, mask):
if random.random() < 0.5:
return img.transpose(Image.FLIP_TOP_BOTTOM), mask.transpose(Image.FLIP_TOP_BOTTOM)
return img, mask
class RandomRotate(object):
def __init__(self, degree):
self.degree = degree
def __call__(self, img, mask):
rotate_degree = random.random() * 2 * self.degree - self.degree
return img.rotate(rotate_degree, Image.BILINEAR), mask.rotate(rotate_degree, Image.NEAREST)