forked from Christinaliang/cancer-cell-semantic-segmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaugmentation.py
35 lines (26 loc) · 1.08 KB
/
augmentation.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
import numpy as np
import random
from PIL import Image, ImageOps
class JointCompose(object):
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, img, mask):
for t in self.transforms:
img, mask = t(img, mask)
return img, mask
class ArrayToPIL(object):
def __call__(self, img, mask):
return Image.fromarray(img), Image.fromarray(mask)
class RandomRotateFlip(object):
def __call__(self, img, mask):
p_rot = random.random()
if 0.25 <= p_rot < 0.5:
img, mask = img.transpose(Image.ROTATE_90), mask.transpose(Image.ROTATE_90)
elif 0.5 <= p_rot < 0.75:
img, mask = img.transpose(Image.ROTATE_180), mask.transpose(Image.ROTATE_180)
elif p_rot >= 0.75:
img, mask = img.transpose(Image.ROTATE_270), mask.transpose(Image.ROTATE_270)
p_flip = random.random()
if p_flip >= 0.5:
img, mask = img.transpose(Image.FLIP_LEFT_RIGHT), mask.transpose(Image.FLIP_LEFT_RIGHT)
return img, mask