-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.py
33 lines (28 loc) · 929 Bytes
/
utils.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
import tensorflow as tf
import tensorlayer as tl
from tensorlayer.prepro import *
import scipy
import numpy as np
def normalize_img(x, is_random=True):
x = imresize(x, size=[128, 128], interp='bicubic', mode=None)
x = x / (255. / 2.)
x = x - 1.
return x
def normalize_img_noresize(x, is_random=True):
x = x / (255. / 2.)
x = x - 1.
return x
def normalize_img_add_noise(Img, noiseRatio):
Img = imresize(Img, size=[128, 128], interp='bicubic', mode=None)
rows, cols, channels = Img.shape
noiseMask = np.ones((rows, cols, channels))
subNoiseNum = round(noiseRatio * cols)
for k in range(channels):
for i in range(rows):
tmp = np.random.permutation(cols)
noiseIdx = np.array(tmp[:subNoiseNum])
noiseMask[i, noiseIdx, k] = 0
corrImg = Img * noiseMask
corrImg = corrImg / (255. / 2.)
corrImg = corrImg - 1.
return corrImg