-
Notifications
You must be signed in to change notification settings - Fork 21
/
joint_transforms.py
37 lines (29 loc) · 1 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
"""
@Time : 2021/7/6 11:05
@Author : Haiyang Mei
@E-mail : [email protected]
@Project : CVPR2021_PFNet
@File : joint_transforms.py
@Function: Transforms for both image and mask
"""
import random
from PIL import Image
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 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 Resize(object):
def __init__(self, size):
self.size = tuple(reversed(size)) # size: (h, w) PIL: (w, h)
def __call__(self, img, mask):
assert img.size == mask.size
return img.resize(self.size, Image.BILINEAR), mask.resize(self.size, Image.NEAREST)