-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbuildGenerator.py
85 lines (63 loc) · 3 KB
/
buildGenerator.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
import tensorflow as tf
OUTPUT_CHANNELS = 3
def downsample(filters, size, shape, apply_batchnorm=True):
initializer = tf.random_normal_initializer(0., 0.02)
result = tf.keras.Sequential()
result.add(
tf.keras.layers.Conv2D(filters, size, strides=2, padding='same', batch_input_shape=shape,
kernel_initializer=initializer, use_bias=False))
if apply_batchnorm:
result.add(tf.keras.layers.BatchNormalization())
result.add(tf.keras.layers.LeakyReLU())
return result
def upsample(filters, size, shape, apply_dropout=False):
initializer = tf.random_normal_initializer(0., 0.02)
result = tf.keras.Sequential()
result.add(
tf.keras.layers.Conv2DTranspose(filters, size, strides=2, batch_input_shape=shape,
padding='same',
kernel_initializer=initializer,
use_bias=False))
result.add(tf.keras.layers.BatchNormalization())
if apply_dropout:
result.add(tf.keras.layers.Dropout(0.5))
result.add(tf.keras.layers.ReLU())
return result
def buildGenerator():
inputs = tf.keras.layers.Input(shape=[256,256,3])
down_stack = [
downsample(64, 4, (None, 256, 256, 3), apply_batchnorm=False), # (bs, 128, 128, 64)
downsample(128, 4, (None, 128, 128, 64)), # (bs, 64, 64, 128)
downsample(256, 4, (None, 64, 64, 128)), # (bs, 32, 32, 256)
downsample(512, 4, (None, 32, 32, 256)), # (bs, 16, 16, 512)
downsample(512, 4, (None, 16, 16, 512)), # (bs, 8, 8, 512)
downsample(512, 4, (None, 8, 8, 512)), # (bs, 4, 4, 512)
downsample(512, 4, (None, 4, 4, 512)), # (bs, 2, 2, 512)
downsample(512, 4, (None, 2, 2, 512)), # (bs, 1, 1, 512)
]
up_stack = [
upsample(512, 4, (None, 1, 1, 512), apply_dropout=True), # (bs, 2, 2, 1024)
upsample(512, 4, (None, 2, 2, 1024), apply_dropout=True), # (bs, 4, 4, 1024)
upsample(512, 4, (None, 4, 4, 1024), apply_dropout=True), # (bs, 8, 8, 1024)
upsample(512, 4, (None, 8, 8, 1024)), # (bs, 16, 16, 1024)
upsample(256, 4, (None, 16, 16, 1024)), # (bs, 32, 32, 512)
upsample(128, 4, (None, 32, 32, 512)), # (bs, 64, 64, 256)
upsample(64, 4, (None, 64, 64, 256)), # (bs, 128, 128, 128)
]
initializer = tf.random_normal_initializer(0., 0.02)
last = tf.keras.layers.Conv2DTranspose(OUTPUT_CHANNELS, 4,
strides=2,
padding='same',
kernel_initializer=initializer,
activation='tanh') # (bs, 256, 256, 3)
x = inputs
skips = []
for down in down_stack:
x = down(x)
skips.append(x)
skips = reversed(skips[:-1])
for up, skip in zip(up_stack, skips):
x = up(x)
x = tf.keras.layers.Concatenate()([x, skip])
x = last(x)
return tf.keras.Model(inputs=inputs, outputs=x)