-
Notifications
You must be signed in to change notification settings - Fork 4
/
triple_transforms.py
45 lines (33 loc) · 1.47 KB
/
triple_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
import random
from PIL import Image
class Compose(object):
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, img, mask, depth):
assert img.size == mask.size
assert img.size == depth.size
for t in self.transforms:
img, mask, depth = t(img, mask, depth)
return img, mask, depth
class Resize(object):
def __init__(self, size):
self.size = tuple(reversed(size)) # size: (h, w)
def __call__(self, img, mask, depth):
assert img.size == mask.size
assert img.size == depth.size
return img.resize(self.size, Image.BILINEAR), mask.resize(self.size, Image.BILINEAR), depth.resize(self.size, Image.BILINEAR)
class RandomCrop(object):
def __init__(self, size):
self.size = size
def __call__(self, img, mask, depth):
assert img.size == mask.size
assert img.size == depth.size
w, h = img.size
x1 = random.randint(0, w - self.size)
y1 = random.randint(0, h - self.size)
return img.crop((x1, y1, x1 + self.size, y1 + self.size)), mask.crop((x1, y1, x1 + self.size, y1 + self.size)), depth.crop((x1, y1, x1 + self.size, y1 + self.size))
class RandomHorizontallyFlip(object):
def __call__(self, img, mask, depth):
if random.random() < 0.5:
return img.transpose(Image.FLIP_LEFT_RIGHT), mask.transpose(Image.FLIP_LEFT_RIGHT), depth.transpose(Image.FLIP_LEFT_RIGHT)
return img, mask, depth