forked from cchen-cc/SIFA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layers.py
154 lines (122 loc) · 5.34 KB
/
layers.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
import tensorflow as tf
def lrelu(x, leak=0.2, name="lrelu", alt_relu_impl=False):
with tf.variable_scope(name):
if alt_relu_impl:
f1 = 0.5 * (1 + leak)
f2 = 0.5 * (1 - leak)
return f1 * x + f2 * abs(x)
else:
return tf.maximum(x, leak * x)
def instance_norm(x):
with tf.variable_scope("instance_norm"):
out = tf.contrib.layers.instance_norm(x)
return out
def batch_norm(x, is_training = True):
with tf.variable_scope("batch_norm"):
return tf.contrib.layers.batch_norm(x, is_training=is_training, decay=0.90, scale=True, center=True,
variables_collections=["internal_batchnorm_variables"],
updates_collections=None)
def general_conv2d(inputconv, o_d=64, f_h=7, f_w=7, s_h=1, s_w=1, stddev=0.01,
padding="VALID", name="conv2d", do_norm=True, do_relu=True, keep_rate=None,
relufactor=0, norm_type=None, is_training=True):
with tf.variable_scope(name):
conv = tf.contrib.layers.conv2d(
inputconv, o_d, f_w, s_w, padding,
activation_fn=None,
weights_initializer=tf.truncated_normal_initializer(
stddev=stddev
),
biases_initializer=None
)
if not keep_rate is None:
conv = tf.nn.dropout(conv, keep_rate)
if do_norm:
if norm_type is None:
print "normalization type is not specified!"
quit()
elif norm_type=='Ins':
conv = instance_norm(conv)
elif norm_type=='Batch':
conv = batch_norm(conv, is_training)
if do_relu:
if(relufactor == 0):
conv = tf.nn.relu(conv, "relu")
else:
conv = lrelu(conv, relufactor, "lrelu")
return conv
def general_conv2d_ga(inputconv, o_d=64, f_h=7, f_w=7, s_h=1, s_w=1, stddev=0.02,
padding="VALID", name="conv2d", do_norm=True, do_relu=True, keep_rate=None,
relufactor=0, norm_type=None, is_training=True):
with tf.variable_scope(name):
conv = tf.contrib.layers.conv2d(
inputconv, o_d, f_w, s_w, padding,
activation_fn=None,
weights_initializer=tf.truncated_normal_initializer(
stddev=stddev
),
biases_initializer=tf.constant_initializer(0.0)
)
if not keep_rate is None:
conv = tf.nn.dropout(conv, keep_rate)
if do_norm:
if norm_type is None:
print "normalization type is not specified!"
quit()
elif norm_type=='Ins':
conv = instance_norm(conv)
elif norm_type=='Batch':
conv = batch_norm(conv, is_training)
if do_relu:
if(relufactor == 0):
conv = tf.nn.relu(conv, "relu")
else:
conv = lrelu(conv, relufactor, "lrelu")
return conv
def dilate_conv2d(inputconv, i_d=64, o_d=64, f_h=7, f_w=7, rate=2, stddev=0.01,
padding="VALID", name="dilate_conv2d", do_norm=True, do_relu=True, keep_rate=None,
relufactor=0, norm_type=None, is_training=True):
with tf.variable_scope(name):
f_1 = tf.get_variable('weights', [f_h, f_w, i_d, o_d], initializer=tf.truncated_normal_initializer(stddev=stddev))
b_1 = tf.get_variable('biases', [o_d], initializer=tf.constant_initializer(0.0, tf.float32))
di_conv_2d = tf.nn.atrous_conv2d(inputconv, f_1, rate=rate, padding=padding)
if not keep_rate is None:
di_conv_2d = tf.nn.dropout(di_conv_2d, keep_rate)
if do_norm:
if norm_type is None:
print "normalization type is not specified!"
quit()
elif norm_type=='Ins':
di_conv_2d = instance_norm(di_conv_2d)
elif norm_type=='Batch':
di_conv_2d = batch_norm(di_conv_2d, is_training)
if do_relu:
if(relufactor == 0):
di_conv_2d = tf.nn.relu(di_conv_2d, "relu")
else:
di_conv_2d = lrelu(di_conv_2d, relufactor, "lrelu")
return di_conv_2d
def general_deconv2d(inputconv, outshape, o_d=64, f_h=7, f_w=7, s_h=1, s_w=1,
stddev=0.02, padding="VALID", name="deconv2d",
do_norm=True, do_relu=True, relufactor=0, norm_type=None, is_training=True):
with tf.variable_scope(name):
conv = tf.contrib.layers.conv2d_transpose(
inputconv, o_d, [f_h, f_w],
[s_h, s_w], padding,
activation_fn=None,
weights_initializer=tf.truncated_normal_initializer(stddev=stddev),
biases_initializer=tf.constant_initializer(0.0)
)
if do_norm:
if norm_type is None:
print "normalization type is not specified!"
quit()
elif norm_type=='Ins':
conv = instance_norm(conv)
elif norm_type=='Batch':
conv = batch_norm(conv, is_training)
if do_relu:
if(relufactor == 0):
conv = tf.nn.relu(conv, "relu")
else:
conv = lrelu(conv, relufactor, "lrelu")
return conv