-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.py
142 lines (105 loc) · 5.47 KB
/
module.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
import tensorflow as tf
import tensorflow.contrib.slim as slim
import sys
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)
def instance_norm(input, name='instance_norm'):
with tf.variable_scope(name):
depth = input.get_shape()[3]
scale = tf.get_variable('scale', [depth], initializer=tf.random_normal_initializer(
1.0, 0.02, dtype=tf.float32))
offset = tf.get_variable(
'offset', [depth], initializer=tf.constant_initializer(0.0))
mean, variance = tf.nn.moments(input, axes=[1, 2], keep_dims=True)
epsilon = 1e-5
inv = tf.rsqrt(variance + epsilon)
normalized = (input-mean)*inv
return scale*normalized + offset
def conv2d(input_, output_dim, ks=3, s=1, stddev=0.02, padding='SAME', name='conv2d'):
with tf.variable_scope(name):
return slim.conv2d(input_, output_dim, ks, s, padding=padding, activation_fn=None,
weights_initializer=tf.truncated_normal_initializer(
stddev=stddev),
biases_initializer=None)
def deconv2d(input_, output_dim, ks=4, s=2, stddev=0.02, name='deconv2d'):
with tf.variable_scope(name):
return slim.conv2d_transpose(input_, output_dim, ks, s, padding='SAME', activation_fn=None,
weights_initializer=tf.truncated_normal_initializer(
stddev=stddev),
biases_initializer=None)
def relu(x, name='relu'):
return tf.nn.relu(x)
def lrelu(x, leak=0.2, name='lrelu'):
return tf.maximum(x, leak*x)
def tanh(x):
return tf.nn.tanh(x)
def generator(images, options, reuse=False, name='gen'):
# reuse or not
with tf.variable_scope(name):
if reuse:
tf.get_variable_scope().reuse_variables()
else:
assert tf.get_variable_scope().reuse is False
# down sampling
x = relu(instance_norm(conv2d(images, options.nf, ks=7, s=1, name='gen_ds_conv1'), 'in1_1'))
x = relu(instance_norm(conv2d(x, 2*options.nf, ks=4, s=2, name='gen_ds_conv2'), 'in1_2'))
x = relu(instance_norm(conv2d(x, 4*options.nf, ks=4, s=2, name='gen_ds_conv3'), 'in1_3'))
# bottleneck
x = relu(instance_norm(conv2d(x, 4*options.nf, ks=3, s=1, name='gen_bn_conv1'), 'in2_1'))
x = relu(instance_norm(conv2d(x, 4*options.nf, ks=3, s=1, name='gen_bn_conv2'), 'in2_2'))
x = relu(instance_norm(conv2d(x, 4*options.nf, ks=3, s=1, name='gen_bn_conv3'), 'in2_3'))
x = relu(instance_norm(conv2d(x, 4*options.nf, ks=3, s=1, name='gen_bn_conv4'), 'in2_4'))
x = relu(instance_norm(conv2d(x, 4*options.nf, ks=3, s=1, name='gen_bn_conv5'), 'in2_5'))
x = relu(instance_norm(conv2d(x, 4*options.nf, ks=3, s=1, name='gen_bn_conv6'), 'in2_6'))
# up sampling
x = relu(instance_norm(deconv2d(x, 2*options.nf, ks=4, s=2, name='gen_us_deconv1'), 'in3_1'))
x = relu(instance_norm(deconv2d(x, options.nf, ks=4, s=2, name='gen_us_deconv2'), 'in3_2'))
# ここのdeconvの第二引数がgeneratorのチャンネルに当たる
x = tanh(deconv2d(x, 1, ks=7, s=1, name='gen_us_dwconv3'))
return x
def discriminator(images, options, reuse=False, name='disc'):
# reuse or not
with tf.variable_scope(name):
if reuse:
tf.get_variable_scope().reuse_variables()
else:
assert tf.get_variable_scope().reuse is False
# input & hidden layer
x = lrelu(conv2d(images, options.nf, ks=4, s=2, name='disc_conv1'))
x1 = lrelu(conv2d(x, 2*options.nf, ks=4, s=2, name='disc_conv2'))
x2 = lrelu(conv2d(x1, 4*options.nf, ks=4, s=2, name='disc_conv3'))
x3 = lrelu(conv2d(x2, 8*options.nf, ks=4, s=2, name='disc_conv4'))
x4 = lrelu(conv2d(x3, 16*options.nf, ks=4, s=2, name='disc_conv5'))
x5 = lrelu(conv2d(x4, 32*options.nf, ks=4, s=2, name='disc_conv6'))
# (batch, h/64, w/64, 2048)
# output layer
x6 = conv2d(x5, 1+options.n_label, ks=1, s=1,
name='disc_conv7') # (batch, h/64, w/64, 1+n)
x7 = tf.reshape(tf.reduce_mean(
x6, axis=[1, 2]), [-1, 1+options.n_label]) # (batch, 1+n)
src = x7[:, 0]
cls = x7[:, 1:]
return src, cls, x6
def wgan_gp_loss(real_img, fake_img, options, epsilon):
hat_img = epsilon * real_img + (1.-epsilon) * fake_img
gradients = tf.gradients(discriminator(
hat_img, options, reuse=True, name='disc')[0], xs=[hat_img])[0]
slopes = tf.sqrt(tf.reduce_sum(tf.square(gradients), axis=[1, 2, 3]))
gradient_penalty = tf.reduce_mean(tf.square(slopes - 1.))
return options.lambda_gp * gradient_penalty
def gan_loss(logits, labels):
return tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=labels))
def cls_loss(logits, labels):
return tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=labels))
def recon_loss(image1, image2):
return tf.reduce_mean(tf.abs(image1 - image2))
def feature_loss(feats_real, feats_fake):
losses = 0
feat_real_mean = tf.map_fn(calc_mean, feats_real)
feat_fake_mean = tf.map_fn(calc_mean, feats_fake)
l2 = (feat_real_mean - feat_fake_mean) ** 2
loss = tf.reduce_mean(l2)
losses = tf.reduce_sum(loss)
return losses
def calc_mean(tensor):
return tf.reduce_mean(tensor)