/
util.py
162 lines (118 loc) · 3.85 KB
/
util.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import math
import numpy as np
import tensorflow as tf
import errno
import os
import copy
def get_batch(X, X_, size):
# X, X_ must be nd-array
a = np.random.choice(len(X), size, replace=False)
return X[a], X_[a]
def get_sequence_batch(X, seq_length, batch_size):
# print('input dim:', len(X[0]), ', seq len:', seq_length, ', batch_size:', batch_size)
# X must be nd-array
a = np.random.choice(len(X)-seq_length, batch_size, replace=False)
a = a + seq_length
# print('index: ', a)
seq = []
for i in range(batch_size):
if a[i] < seq_length - 1:
s = np.random.normal(0.0, 0.1, [seq_length, len(X[0])])
seq.append(s)
else:
s = np.arange(a[i]-seq_length, a[i])
seq.append(X[s])
seq = np.array(seq)
return X[a], seq
def noise_validator(noise, allowed_noises):
'''Validates the noise provided'''
try:
if noise in allowed_noises:
return True
elif noise.split('-')[0] == 'mask' and float(noise.split('-')[1]):
t = float(noise.split('-')[1])
if t >= 0.0 and t <= 1.0:
return True
else:
return False
except:
return False
pass
def sigmoid_normalize(value_list):
list_max = float(max(value_list))
alist = [i/list_max for i in value_list]
alist = [1/(1+math.exp(-i)) for i in alist]
return alist
def swish(logit, name=None):
with tf.name_scope(name):
l = tf.multiply(logit, tf.nn.sigmoid(logit))
return l
def generate_samples(dim, num_inlier, num_outlier, normalize=True):
inlier = np.random.normal(0.0, 1.0, [num_inlier, dim])
sample_inlier = []
if normalize:
inlier = np.transpose(inlier)
for values in inlier:
values = sigmoid_normalize(values)
sample_inlier.append(values)
inlier = np.array(sample_inlier).transpose()
outlier = np.random.normal(1.0, 1.0, [num_outlier, dim])
sample_outlier = []
if normalize:
outlier = np.transpose(outlier)
for values in outlier:
values = sigmoid_normalize(values)
sample_outlier.append(values)
outlier = np.array(sample_outlier).transpose()
return inlier, outlier
def add_gaussian_noise(input_layer, mean, std):
if std < 0.0:
return input_layer
noise = tf.random_normal(shape=input_layer.get_shape().as_list(), mean=mean, stddev=std, dtype=tf.float32)
return tf.add(input_layer, noise)
def mkdirP(path):
"""
Create a directory and don't error if the path already exists.
If the directory already exists, don't do anything.
:param path: The directory to create.
:type path: str
"""
assert path is not None
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
class ImagePool(object):
def __init__(self, maxsize=50):
self.maxsize = maxsize
self.num_img = 0
self.images = []
def __call__(self, image):
if self.maxsize <= 0:
return image
if self.num_img < self.maxsize:
self.images.append(image)
self.num_img += 1
return image
idx = int(np.random.rand() * self.maxsize)
tmp1 = copy.copy(self.images[idx])[0]
self.images[idx][0] = image[0]
idx = int(np.random.rand() * self.maxsize)
tmp2 = copy.copy(self.images[idx])[1]
self.images[idx][1] = image[1]
if np.random.rand() > 0.1:
return [tmp1, tmp2]
else:
return image
class COLORS:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'