-
Notifications
You must be signed in to change notification settings - Fork 1
/
augments.py
68 lines (57 loc) · 1.7 KB
/
augments.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
import numpy as np
import torch
from torch.distributions.poisson import Poisson
from torchvision.transforms import (
Compose,
ToPILImage,
ToTensor,
RandomHorizontalFlip,
RandomVerticalFlip,
RandomResizedCrop,
)
import imgaug.augmenters as iaa
# Gaussian Noise
def apply_gaussian(device, data, std=1, mean=0):
return data + torch.randn(data.size()).to(device) * std + mean
# Poisson Noise
def apply_poisson(device, data, rate=0.5):
m = Poisson(rate)
noise = m.sample(data.size()).to(device)
noise = torch.clamp(noise, 0, 1)
return data + noise
def apply_hflip(device, data):
images = []
transform = Compose([RandomHorizontalFlip(p=1)])
for d in data:
image = ToPILImage()(d)
image = transform(image)
image = ToTensor()(image)
images.append(image)
return torch.stack(images)
def apply_vflip(device, data):
images = []
transform = Compose([RandomVerticalFlip(p=1)])
for d in data:
image = ToPILImage()(d)
image = transform(image)
image = ToTensor()(image)
images.append(image)
return torch.stack(images)
def apply_random_crop(device, data, size=32):
images = []
transform = Compose([RandomResizedCrop(size=size)])
for d in data:
image = ToPILImage()(d)
image = transform(image)
image = ToTensor()(image)
images.append(image)
return torch.stack(images)
def apply_sharpen(device, data, alpha=0.5):
images = []
sharpen = iaa.Sharpen(alpha=0.5)
for d in data:
image = ToPILImage()(d)
image = sharpen.augment_image(np.array(image))
image = ToTensor()(image)
images.append(image)
return torch.stack(images)