-
Notifications
You must be signed in to change notification settings - Fork 2
/
discriminator.py
158 lines (127 loc) · 6.06 KB
/
discriminator.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
import tensorflow as tf
from models.generative.ops import *
from models.generative.activations import *
from models.generative.normalization import *
display = True
def discriminator_resnet(images, layers, spectral, activation, reuse, init='xavier', regularizer=None, normalization=None, attention=None, down='downscale', label=None, label_t='cat', infoGAN=False, c_dim=None):
net = images
channels = [32, 64, 128, 256, 512, 1024]
if display:
print('DISCRIMINATOR INFORMATION:')
print('Channels: ', channels[:layers])
print('Normalization: ', normalization)
print('Activation: ', activation)
print('Attention: ', attention)
print()
with tf.variable_scope('discriminator', reuse=reuse):
for layer in range(layers):
# ResBlock.
net = residual_block(inputs=net, filter_size=3, stride=1, padding='SAME', scope=layer, is_training=True, normalization=normalization, use_bias=True,
spectral=spectral, init=init, regularizer=regularizer, activation=activation)
# Attention layer.
if attention is not None and net.shape.as_list()[1]==attention:
net = attention_block(net, spectral=True, init=init, regularizer=regularizer, scope=layers)
# Down.
net = convolutional(inputs=net, output_channels=channels[layer], filter_size=4, stride=2, padding='SAME', conv_type=down, spectral=spectral, init=init, regularizer=regularizer, scope=layer)
if normalization is not None: net = normalization(inputs=net, training=True)
net = activation(net)
# Flatten.
net = tf.layers.flatten(inputs=net)
# Dense.
net = dense(inputs=net, out_dim=channels[-1], spectral=spectral, init=init, regularizer=regularizer, scope=1)
if normalization is not None: net = normalization(inputs=net, training=True)
net = activation(net)
# Dense
logits = dense(inputs=net, out_dim=1, spectral=spectral, init=init, regularizer=regularizer, scope=2)
output = sigmoid(logits)
# Discriminator with conditional projection.
if label is not None:
batch_size, label_dim = label.shape.as_list()
embedding_size = channels[-1]
# Categorical Embedding.
if label_t == 'cat':
emb = embedding(shape=(label_dim, embedding_size), init=init, power_iterations=1)
label_emb = tf.matmul(label, emb)
# Linear conditioning, using NN to produce embedding.
else:
inter_dim = int((label_dim+net.shape.as_list()[-1])/2)
net_label = dense(inputs=net, out_dim=inter_dim, spectral=spectral, init=init, regularizer=regularizer, scope='label_nn_1')
if normalization is not None: net_label = normalization(inputs=net_label, training=True)
net_label = activation(net_label)
label_emb = dense(inputs=net_label, out_dim=embedding_size, spectral=spectral, init=init, regularizer=regularizer, scope='label_nn_2')
inner_prod = tf.reduce_sum(tf.multiply(net, label_emb), axis=-1)
output += inner_prod
if infoGAN:
mean_c_x = dense(inputs=net, out_dim=c_dim, spectral=spectral, init=init, regularizer=regularizer, scope=3)
logs2_c_x = dense(inputs=net, out_dim=c_dim, spectral=spectral, init=init, regularizer=regularizer, scope=4)
return output, logits, mean_c_x, logs2_c_x
print()
return output, logits
def encoder_resnet(images, z_dim, layers, spectral, activation, reuse, normalization=None, is_train=None, attention=None, down='downscale'):
net = images
channels = [32, 64, 128, 256, 512, 1024]
channels = [64, 128, 256, 512, 1024]
if display:
print('ENCODER INFORMATION:')
print('Channels: ', channels[:layers])
print('Normalization: ', normalization)
print('Activation: ', activation)
print('Attention: ', attention)
print()
with tf.variable_scope('encoder', reuse=reuse):
for layer in range(layers):
# ResBlock.
# if vae_dim == net.shape.as_list()[1]:
# scope = 'vae_out'
# else:
# scope = layer
net = residual_block(inputs=net, filter_size=3, stride=1, padding='SAME', scope=layer, is_training=is_train, normalization=normalization, use_bias=True,
spectral=spectral, activation=activation)
# Attention layer.
if attention is not None and net.shape.as_list()[1]==attention:
net = attention_block(net, spectral=True, scope=layers)
# if vae_dim == net.shape.as_list()[1]:
# vae_out = sigmoid(net)
# Down.
net = convolutional(inputs=net, output_channels=channels[layer], filter_size=4, stride=2, padding='SAME', conv_type=down, spectral=spectral, scope=layer)
if normalization is not None: net = normalization(inputs=net, training=is_train)
net = activation(net)
# Flatten.
net = tf.layers.flatten(inputs=net)
# Dense.
net = dense(inputs=net, out_dim=channels[-1], spectral=spectral, scope=1)
if normalization is not None: net = normalization(inputs=net, training=is_train)
net = activation(net)
# Dense.
mean_z_xi = dense(inputs=net, out_dim=z_dim, spectral=spectral, scope='mean_z_xi')
logs2_z_xi = dense(inputs=net, out_dim=z_dim, spectral=spectral, scope='logs2_z_xi')
print()
# return mean_z_xi, logs2_z_xi, vae_out
return mean_z_xi, logs2_z_xi
def discriminator(images, layers, spectral, activation, reuse, normalization=None):
net = images
channels = [32, 64, 128, 256, 512, 1024]
if display:
print('Discriminator Information.')
print('Channels: ', channels[:layers])
print('Normalization: ', normalization)
print('Activation: ', activation)
print()
with tf.variable_scope('discriminator', reuse=reuse):
# Padding = 'Same' -> H_new = H_old // Stride
for layer in range(layers):
# Down.
net = convolutional(inputs=net, output_channels=channels[layer], filter_size=5, stride=2, padding='SAME', conv_type='convolutional', spectral=spectral, scope=layer+1)
if normalization is not None: net = normalization(inputs=net, training=True)
net = activation(net)
# Flatten.
net = tf.layers.flatten(inputs=net)
# Dense.
net = dense(inputs=net, out_dim=channels[-1], spectral=spectral, scope=1)
if normalization is not None: net = normalization(inputs=net, training=True)
net = activation(net)
# Dense
logits = dense(inputs=net, out_dim=1, spectral=spectral, scope=2)
output = sigmoid(logits)
print()
return output, logits