-
Notifications
You must be signed in to change notification settings - Fork 0
/
gan.py
54 lines (42 loc) · 1.91 KB
/
gan.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
import tensorflow as tf
from tensorflow import keras
class Generator(keras.Model):
def __init__(self):
super(Generator, self).__init__()
self.n_f = 512
self.n_k = 4
# input z vector is [None, 100]
self.dense1 = keras.layers.Dense(3 * 3 * self.n_f)
self.conv2 = keras.layers.Conv2DTranspose(self.n_f // 2, 3, 2, 'valid')
self.bn2 = keras.layers.BatchNormalization()
self.conv3 = keras.layers.Conv2DTranspose(self.n_f // 4, self.n_k, 2, 'same')
self.bn3 = keras.layers.BatchNormalization()
self.conv4 = keras.layers.Conv2DTranspose(1, self.n_k, 2, 'same')
return
def call(self, inputs, training=None):
# [b, 100] => [b, 3, 3, 512]
x = tf.nn.leaky_relu(tf.reshape(self.dense1(inputs), shape=[-1, 3, 3, self.n_f]))
x = tf.nn.leaky_relu(self.bn2(self.conv2(x), training=training))
x = tf.nn.leaky_relu(self.bn3(self.conv3(x), training=training))
x = tf.tanh(self.conv4(x))
return x
class Discriminator(keras.Model):
def __init__(self):
super(Discriminator, self).__init__()
self.n_f = 64
self.n_k = 4
# input image is [-1, 28, 28, 1]
self.conv1 = keras.layers.Conv2D(self.n_f, self.n_k, 2, 'same')
self.conv2 = keras.layers.Conv2D(self.n_f * 2, self.n_k, 2, 'same')
self.bn2 = keras.layers.BatchNormalization()
self.conv3 = keras.layers.Conv2D(self.n_f * 4, self.n_k, 2, 'same')
self.bn3 = keras.layers.BatchNormalization()
self.flatten4 = keras.layers.Flatten()
self.dense4 = keras.layers.Dense(1)
return
def call(self, inputs, training=None):
x = tf.nn.leaky_relu(self.conv1(inputs))
x = tf.nn.leaky_relu(self.bn2(self.conv2(x), training=training))
x = tf.nn.leaky_relu(self.bn3(self.conv3(x), training=training))
x = self.dense4(self.flatten4(x))
return x