-
Notifications
You must be signed in to change notification settings - Fork 5
/
model.py
143 lines (110 loc) · 4.67 KB
/
model.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
from keras.layers import Input, Activation, Add
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.convolutional import Conv2D, Conv2DTranspose
from keras.layers.core import Dense, Flatten, Lambda
from keras.layers.normalization import BatchNormalization
from keras.models import Model
from keras.utils import plot_model
from keras.applications import VGG19
from layer_utils import res_block, ReflectionPadding2D
# the paper defined hyper-parameter:chr
channel_rate = 64
# Note the image_shape must be multiple of patch_shape
image_shape = (512, 512, 3)
patch_shape = (channel_rate, channel_rate, 3)
ngf = 64
ndf = 64
input_nc = 3
output_nc = 3
input_shape_generator = (512, 512, input_nc)
input_shape_discriminator = (512, 512, output_nc)
n_blocks_gen = 10
def generator_model():
"""Build generator architecture."""
# Current version : ResNet block
inputs = Input(shape=image_shape)
x = ReflectionPadding2D((3, 3))(inputs)
x = Conv2D(filters=ngf, kernel_size=(7, 7), padding='valid')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
n_downsampling = 3
for i in range(n_downsampling):
mult = 2**i
x = Conv2D(filters=ngf*mult*2, kernel_size=(3, 3), strides=2, padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
mult = 2**n_downsampling
for i in range(n_blocks_gen):
x = res_block(x, ngf*mult, use_dropout=True)
for i in range(n_downsampling):
mult = 2**(n_downsampling - i)
x = Conv2DTranspose(filters=int(ngf * mult / 2), kernel_size=(3, 3), strides=2, padding='same')(x)
# x = BatchNormalization()(x)
x = Activation('relu')(x)
x = ReflectionPadding2D((3, 3))(x)
x = Conv2D(filters=output_nc, kernel_size=(7, 7), padding='valid')(x)
x = Activation('tanh')(x)
outputs = x
# outputs = Add()([x, inputs])
# outputs = Lambda(lambda z: K.clip(z, -1, 1))(x)
# outputs = Lambda(lambda z: z/2)(outputs)
model = Model(inputs=inputs, outputs=outputs, name='Generator')
plot_model(model, to_file='generator.png', show_shapes=True)
return model
def discriminator_model():
"""Build discriminator architecture."""
n_layers, use_sigmoid = 4, False
inputs = Input(shape=input_shape_discriminator)
x = Conv2D(filters=ndf, kernel_size=(4, 4), strides=2, padding='same')(inputs)
x = LeakyReLU(0.2)(x)
nf_mult, nf_mult_prev = 1, 1
for n in range(n_layers):
nf_mult_prev, nf_mult = nf_mult, min(2**n, 8)
x = Conv2D(filters=ndf*nf_mult, kernel_size=(4, 4), strides=2, padding='same')(x)
x = BatchNormalization()(x)
x = LeakyReLU(0.2)(x)
nf_mult_prev, nf_mult = nf_mult, min(2**n_layers, 8)
x = Conv2D(filters=ndf*nf_mult, kernel_size=(4, 4), strides=1, padding='same')(x)
x = BatchNormalization()(x)
x = LeakyReLU(0.2)(x)
x = Conv2D(filters=1, kernel_size=(4, 4), strides=1, padding='same')(x)
if use_sigmoid:
x = Activation('sigmoid')(x)
x = Flatten()(x)
x = Dense(1024, activation='tanh')(x)
x = Dense(1, activation='sigmoid')(x)
model = Model(inputs=inputs, outputs=x, name='Discriminator')
plot_model(model, to_file='discriminator.png', show_shapes=True)
return model
def build_vgg():
vgg = VGG19(weights="imagenet")
# Set outputs to outputs of last conv. layer in block 3
# See architecture at: https://github.com/keras-team/keras/blob/master/keras/applications/vgg19.py
vgg.outputs = [vgg.layers[9].output]
img = Input(shape=input_shape_discriminator)
# Extract image features
img_features = vgg(img)
return Model(img, img_features)
def generator_containing_discriminator(generator, discriminator):
inputs = Input(shape=image_shape)
generated_image = generator(inputs)
outputs = discriminator(generated_image)
model = Model(inputs=inputs, outputs=outputs)
return model
def generator_containing_discriminator_multiple_outputs(generator, discriminator, vgg):
inputs = Input(shape=image_shape)
x_base = Input(shape=image_shape)
generated_image = generator(inputs)
# generated_image = Add()([generated_image, x_base])
fake_features = vgg(generated_image)
outputs = discriminator(generated_image)
model = Model(inputs=[inputs, x_base], outputs=[generated_image, fake_features, outputs])
# model = Model(inputs=inputs, outputs=outputs)
return model
if __name__ == '__main__':
g = generator_model()
g.summary()
d = discriminator_model()
# please modeify the path
# g.save('/home/alyssa/PythonProjects/rain/deblur-gan-master/g_model.h5')
# d.save('/home/alyssa/PythonProjects/rain/deblur-gan-master/d_model.h5')