-
Notifications
You must be signed in to change notification settings - Fork 0
/
augment.py
25 lines (19 loc) · 927 Bytes
/
augment.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
import torch
def add_gaussian_noise(img, mean=0., std=1.):
noise = torch.randn(img.size()) * std + mean
noisy_img = img + noise
return torch.clamp(noisy_img, 0., 1.)
class SaltPepperTransform(torch.nn.Module):
def forward(self, img, label, prob=0.05):
# Create a random matrix with the same size as the image
rand_matrix = torch.rand(img.size())
# Create masks for salt and pepper noise
salt_mask = (rand_matrix < prob / 2)
pepper_mask = ((rand_matrix >= prob / 2) & (rand_matrix < prob))
# Apply the masks to the image
noisy_img = img.clone() # Clone the original image to avoid modifying it directly
noisy_img[salt_mask] = 1.0 # Set pixels in the salt mask to white
noisy_img[pepper_mask] = 0.0 # Set pixels in the pepper mask to black
# Do some transformations
# new_img=add_salt_pepper_noise(img)
return noisy_img, label