-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaugment.py
More file actions
24 lines (19 loc) · 733 Bytes
/
Copy pathaugment.py
File metadata and controls
24 lines (19 loc) · 733 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import torchvision.transforms.functional as TF
import random
from PIL import Image
class LineAugment:
def __init__(self, p=0.5):
self.p = p
def __call__(self, image, mask, is_augment=True):
image = Image.fromarray(image)
mask = Image.fromarray(mask)
if random.random() < self.p:
angle = random.uniform(-15, 15)
image = TF.rotate(image, angle, fill=(0,0,0))
mask = TF.rotate(mask, angle, fill=(0))
if random.random() < self.p:
tx = random.randint(-32, 32)
ty = random.randint(-32, 32)
image = TF.affine(image, 0, (tx, ty), 1, 0)
mask = TF.affine(mask, 0, (tx, ty), 1, 0)
return image, mask