File size: 7,451 Bytes
89ce6b3 |
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
import tensorflow as tf
# import tensorflow.contrib.slim as slim
import tf_slim as slim
import math
import pprint
pp = pprint.PrettyPrinter()
get_stddev = lambda x, k_h, k_w: 1 / math.sqrt(k_w * k_h * x.get_shape()[-1])
# import tensorflow.contrib as tf_contrib
# weight_init = tf_contrib.layers.xavier_initializer()
weight_init = tf.initializers.GlorotUniform()
weight_regularizer = None
def batch_norm(x, name="batch_norm"):
# return tf.contrib.layers.batch_norm(
# x, decay=0.9, updates_collections=None, epsilon=1e-5, scale=True, scope=name
# )
return tf.keras.layers.BatchNormalization(
momentum=0.9, epsilon=1e-5, scale=True, name=name
)(x)
def instance_norm(input, name="instance_norm"):
with tf.compat.v1.variable_scope(name):
depth = input.get_shape()[3]
scale = tf.compat.v1.get_variable(
"scale",
[depth],
initializer=tf.keras.initializers.RandomNormal(
mean=1.0, stddev=0.02, seed=None
),
)
offset = tf.compat.v1.get_variable(
"offset", [depth], initializer=tf.constant_initializer(0.0)
)
mean, variance = tf.nn.moments(input, axes=[1, 2], keepdims=True)
epsilon = 1e-5
inv = tf.math.rsqrt(variance + epsilon)
normalized = (input - mean) * inv
return scale * normalized + offset
def conv2d(input_, output_dim, ks=4, s=2, stddev=0.02, padding="SAME", name="conv2d"):
with tf.compat.v1.variable_scope(name):
return slim.conv2d(
input_,
output_dim,
ks,
s,
padding=padding,
activation_fn=None,
weights_initializer=tf.keras.initializers.TruncatedNormal(stddev=stddev),
biases_initializer=None,
)
def deconv2d(input_, output_dim, ks=4, s=2, stddev=0.02, name="deconv2d"):
with tf.compat.v1.variable_scope(name):
return slim.conv2d_transpose(
input_,
output_dim,
ks,
s,
padding="SAME",
activation_fn=None,
weights_initializer=tf.keras.initializers.TruncatedNormal(stddev=stddev),
biases_initializer=None,
)
def dilated_conv2d(
input_, output_dim, ks=3, s=2, stddev=0.02, padding="SAME", name="conv2d"
):
with tf.compat.v1.variable_scope(name):
batch, in_height, in_width, in_channels = [int(d) for d in input_.get_shape()]
filter = tf.compat.v1.get_variable(
"filter",
[ks, ks, in_channels, output_dim],
dtype=tf.float32,
initializer=tf.random_normal_initializer(0, stddev),
)
conv = tf.nn.atrous_conv2d(input_, filter, rate=s, padding=padding, name=name)
return conv
def one_step(x, ch, kernel, stride, name):
return lrelu(
instance_norm(
conv2d(x, ch, kernel, stride, name=name + "_first_c"), name + "_first_bn"
)
)
def one_step_dilated(x, ch, kernel, stride, name):
return lrelu(
instance_norm(
dilated_conv2d(x, ch, kernel, stride, name=name + "_first_c"),
name + "_first_bn",
)
)
def num_steps(x, ch, kernel, stride, num_steps, name):
for i in range(num_steps):
x = lrelu(
instance_norm(
conv2d(x, ch, kernel, stride, name=name + "_c_" + str(i)),
name + "_bn_" + str(i),
)
)
return x
def one_step_noins(x, ch, kernel, stride, name):
return lrelu(conv2d(x, ch, kernel, stride, name=name + "_first_c"))
def num_steps_noins(x, ch, kernel, stride, num_steps, name):
for i in range(num_steps):
x = lrelu(conv2d(x, ch, kernel, stride, name=name + "_c_" + str(i)))
return x
def dis_down(images, kernel_size, stride, n_scale, ch, name):
backpack = images[0]
for i in range(n_scale):
if i == n_scale - 1:
images[i] = num_steps(
backpack, ch, kernel_size, stride, n_scale, name + str(i)
)
else:
images[i] = one_step_dilated(
images[i + 1], ch, kernel_size, 1, name + str(i)
)
return images
def dis_down_noins(images, kernel_size, stride, n_scale, ch, name):
backpack = images[0]
for i in range(n_scale):
if i == n_scale - 1:
images[i] = num_steps_noins(
backpack, ch, kernel_size, stride, n_scale, name + str(i)
)
else:
images[i] = one_step_noins(images[i + 1], ch, kernel_size, 1, name + str(i))
return images
def final_conv(images, n_scale, name):
for i in range(n_scale):
images[i] = conv2d(images[i], 1, s=1, name=name + str(i))
return images
def lrelu(x, leak=0.2, name="lrelu"):
return tf.maximum(x, leak * x)
def linear(input_, output_size, scope=None, stddev=0.02, bias_start=0.0, with_w=False):
with tf.compat.v1.variable_scope(scope or "Linear"):
matrix = tf.get_variable(
"Matrix",
[input_.get_shape()[-1], output_size],
tf.float32,
tf.random_normal_initializer(stddev=stddev),
)
bias = tf.get_variable(
"bias", [output_size], initializer=tf.constant_initializer(bias_start)
)
if with_w:
return tf.matmul(input_, matrix) + bias, matrix, bias
else:
return tf.matmul(input_, matrix) + bias
def get_ones_like(logit):
target = []
for i in range(len(logit)):
target.append(tf.ones_like(logit[i]))
return target
def get_zeros_like(logit):
target = []
for i in range(len(logit)):
target.append(tf.zeros_like(logit[i]))
return target
def conv(
x,
channels,
kernel=4,
stride=2,
pad=0,
pad_type="zero",
use_bias=True,
scope="conv_0",
):
with tf.compat.v1.variable_scope(scope):
if pad_type == "zero":
x = tf.pad(x, [[0, 0], [pad, pad], [pad, pad], [0, 0]])
if pad_type == "reflect":
x = tf.pad(x, [[0, 0], [pad, pad], [pad, pad], [0, 0]], mode="REFLECT")
x = tf.layers.conv2d(
inputs=x,
filters=channels,
kernel_size=kernel,
kernel_initializer=weight_init,
kernel_regularizer=weight_regularizer,
strides=stride,
use_bias=use_bias,
)
return x
def reduce_sum(input_tensor, axis=None, keepdims=False):
try:
return tf.reduce_sum(input_tensor, axis=axis, keepdims=keepdims)
except:
return tf.reduce_sum(input_tensor, axis=axis, keep_dims=keepdims)
def get_shape(inputs, name=None):
name = "shape" if name is None else name
with tf.name_scope(name):
static_shape = inputs.get_shape().as_list()
dynamic_shape = tf.shape(inputs)
shape = []
for i, dim in enumerate(static_shape):
dim = dim if dim is not None else dynamic_shape[i]
shape.append(dim)
return shape
def show_all_variables():
model_vars = tf.trainable_variables()
slim.model_analyzer.analyze_vars(model_vars, print_info=True)
|