-
Notifications
You must be signed in to change notification settings - Fork 8
/
utils.py
executable file
·125 lines (107 loc) · 4.11 KB
/
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
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
import os
import numpy as np
import random
import tensorflow as tf
import math
def create_dirs(dirs):
"""
dirs - a list of directories to create if these directories are not found
:param dirs:
:return:
"""
try:
for dir_ in dirs:
if not os.path.exists(dir_):
os.makedirs(dir_)
except Exception as err:
print("Creating directories error: {0}".format(err))
def create_experiment_dirs(exp_dir):
"""
Create Directories of a regular tensorflow experiment directory
:param exp_dir:
:return summary_dir, checkpoint_dir:
"""
experiment_dir = os.path.realpath(os.path.join(os.path.dirname(__file__))) + "/experiments/" + exp_dir + "/"
summary_dir = experiment_dir + 'summaries/'
checkpoint_dir = experiment_dir + 'checkpoints/'
output_dir = experiment_dir + 'output/'
test_dir = experiment_dir + 'test/'
dirs = [summary_dir, checkpoint_dir, output_dir, test_dir]
try:
for dir_ in dirs:
if not os.path.exists(dir_):
os.makedirs(dir_)
print("Experiment directories created")
return experiment_dir, summary_dir, checkpoint_dir, output_dir, test_dir
except Exception as err:
print("Creating directories error: {0}".format(err))
exit(-1)
def create_list_dirs(input_dir, prefix_name, count):
dirs_path = []
for i in range(count):
dirs_path.append(input_dir + prefix_name + '-' + str(i))
create_dirs([input_dir + prefix_name + '-' + str(i)])
return dirs_path
def set_all_global_seeds(i):
try:
tf.set_random_seed(i)
np.random.seed(i)
random.seed(i)
except:
return ImportError
def find_trainable_variables(key):
with tf.variable_scope(key):
return tf.trainable_variables()
# Summaries for variables
def variable_summaries(var):
"""
Attach a lot of summaries to a Tensor (for TensorBoard visualization).
:param var: variable to be summarized
:return: None
"""
with tf.name_scope('summaries'):
mean = tf.reduce_mean(var)
tf.summary.scalar('mean', mean)
with tf.name_scope('stddev'):
stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
tf.summary.scalar('stddev', stddev)
tf.summary.scalar('max', tf.reduce_max(var))
tf.summary.scalar('min', tf.reduce_min(var))
tf.summary.histogram('histogram', var)
def get_deconv_filter(f_shape, l2_strength):
"""
The initializer for the bilinear convolution transpose filters
:param f_shape: The shape of the filter used in convolution transpose.
:param l2_strength: L2 regularization parameter.
:return weights: The initialized weights.
"""
width = f_shape[0]
height = f_shape[0]
f = math.ceil(width / 2.0)
c = (2 * f - 1 - f % 2) / (2.0 * f)
bilinear = np.zeros([f_shape[0], f_shape[1]])
for x in range(width):
for y in range(height):
value = (1 - abs(x / f - c)) * (1 - abs(y / f - c))
bilinear[x, y] = value
weights = np.zeros(f_shape)
for i in range(f_shape[2]):
for j in range(f_shape[3]):
weights[:, :, i, j] = bilinear
init = tf.constant_initializer(value=weights, dtype=tf.float32)
return variable_with_weight_decay(weights.shape, init, l2_strength)
def variable_with_weight_decay(kernel_shape, initializer, wd):
"""
Create a variable with L2 Regularization (Weight Decay)
:param kernel_shape: the size of the convolving weight kernel.
:param initializer: The initialization scheme, He et al. normal or Xavier normal are recommended.
:param wd:(weight decay) L2 regularization parameter.
:return: The weights of the kernel initialized. The L2 loss is added to the loss collection.
"""
w = tf.get_variable('weights', kernel_shape, tf.float32, initializer=initializer)
collection_name = tf.GraphKeys.REGULARIZATION_LOSSES
if wd and (not tf.get_variable_scope().reuse):
weight_decay = tf.multiply(tf.nn.l2_loss(w), wd, name='w_loss')
tf.add_to_collection(collection_name, weight_decay)
variable_summaries(w)
return w