File size: 796 Bytes
0cb9530
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import random

from fastai.vision.image import TfmPixel

# Contributed by Rani Horev. Thank you!
def _noisify(
    x, pct_pixels_min: float = 0.001, pct_pixels_max: float = 0.4, noise_range: int = 30
):
    if noise_range > 255 or noise_range < 0:
        raise Exception("noise_range must be between 0 and 255, inclusively.")

    h, w = x.shape[1:]
    img_size = h * w
    mult = 10000.0
    pct_pixels = (
        random.randrange(int(pct_pixels_min * mult), int(pct_pixels_max * mult)) / mult
    )
    noise_count = int(img_size * pct_pixels)

    for ii in range(noise_count):
        yy = random.randrange(h)
        xx = random.randrange(w)
        noise = random.randrange(-noise_range, noise_range) / 255.0
        x[:, yy, xx].add_(noise)

    return x


noisify = TfmPixel(_noisify)