-
Notifications
You must be signed in to change notification settings - Fork 1
/
models_1.py
185 lines (152 loc) · 6.33 KB
/
models_1.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from __future__ import print_function, division
import scipy
from keras.datasets import mnist
from keras_contrib.layers.normalization.instancenormalization import InstanceNormalization
from keras.layers import Input, Dense, Reshape, Flatten, Dropout, Concatenate
from keras.layers import BatchNormalization, Activation, ZeroPadding2D
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.convolutional import UpSampling2D, Conv2D
from keras.layers import BatchNormalization, Activation, Embedding, ZeroPadding2D
from keras.models import Sequential, Model
from keras.optimizers import Adam
from keras.layers import Input, Dense, Reshape, Flatten, Dropout, multiply
from keras.layers import Reshape
import datetime
import sys
from keras.layers import Concatenate, Dense, LSTM, Input, concatenate
import numpy as np
import os
import random
from keras.layers import Conv2DTranspose, BatchNormalization
import tensorflow as tf
from keras.utils import to_categorical
def get_dim_conv(dim,f,p,s):
return int((dim+2*p-f)/s+1)
def build_generator_enc_dec(img_shape,gf,AU_num,channels,num_layers=4,f_size=6,tranform_layer=False):
"""U-Net Generator"""
def conv2d(layer_input, filters, f_size=f_size,strides=2):
"""Layers used during downsampling"""
d = Conv2D(filters, kernel_size=f_size, strides=strides, padding='valid')(layer_input)
d = LeakyReLU(alpha=0.2)(d)
d = InstanceNormalization()(d)
return d
def __deconv2d(layer_input, skip_input, filters, f_size=f_size, dropout_rate=0):
"""Layers used during upsampling"""
u = UpSampling2D(size=2)(layer_input)
u = Conv2D(filters, kernel_size=f_size, strides=1, padding='valid', activation='relu')(u)
if dropout_rate:
u = Dropout(dropout_rate)(u)
u = InstanceNormalization()(u)
u = Concatenate()([u, skip_input])
return u
def deconv2d(layer_input, skip_input, filters, f_size=f_size, dropout_rate=0 , output_padding=None,strides=2):
"""Layers used during upsampling"""
u = Conv2DTranspose(filters=filters, kernel_size=f_size,
strides=strides, activation='relu' , output_padding=output_padding)(layer_input)
if dropout_rate:
u = Dropout(dropout_rate)(u)
u = InstanceNormalization()(u)
u = Concatenate()([u, skip_input])
return u
# Image input
img = Input(shape=img_shape)
# Downsampling
d = img
zs = []
dims = []
_dim = img_shape[0]
for i in range(num_layers):
if i == 0:
stride = 3
else:
stride = 2
d = conv2d(d, gf*2**i,strides=stride)
zs.append(d)
_dim = get_dim_conv(_dim,f_size,0,stride)
dims.append((_dim,gf*2**i))
#print("D:",_dim,gf*2**i)
G_enc = Model(img,zs)
####
# = Input(shape=(24, 24, 32))
#d2_ = Input(shape=(12, 12, 64))
#d3_ = Input(shape=(6, 6, 128))
#d4_ = Input(shape=(3, 3, 256))
_zs = []
d_ , c_ = dims.pop()
#print(0,d_,c_)
i_ = Input(shape=(d_, d_, c_))
_zs.append(i_)
label = Input(shape=(AU_num,), dtype='float32')
label_r = Reshape((1,1,AU_num))(label)
u = concatenate([i_, label_r],axis=-1)
## transf
if tranform_layer:
tr = Flatten()(u)
tr = Dense(c_+AU_num, kernel_initializer='glorot_uniform' )(tr)
tr = LeakyReLU(alpha=0.2)(tr)
u = Reshape((1,1,c_+AU_num))(tr)
##
u = Conv2D(c_, kernel_size=1, strides=1, padding='valid')(u) ## 1x1 conv
# Upsampling
for i in range(num_layers-1):
_ch = gf*2**((num_layers-2)-i)
d_ , c_ = dims.pop()
#print(i,d_,c_)
i_ = Input(shape=(d_, d_, c_))
_zs.append(i_)
if i == 3:
u = deconv2d(u, i_, _ch,output_padding=1)
#u = deconv2d(u, i_, _ch)
else:
u = deconv2d(u, i_, _ch)
#u4 = UpSampling2D(size=2)(u)
#output_img = Conv2D(channels, kernel_size=4, strides=1, padding='same', activation='tanh')(u4)
u = Conv2DTranspose(filters=channels, kernel_size=f_size,
strides=3, activation='tanh' , output_padding=1)(u)
_zs.reverse()
_zs.append(label)
G_dec = Model(_zs,u)
return G_enc , G_dec
def build_discriminator(img_shape,df,AU_num,num_layers=4,act_multi_label='linear'):
def d_layer(layer_input, filters, f_size=4, normalization=True,strides=2):
"""Discriminator layer"""
d = Conv2D(filters, kernel_size=f_size, strides=strides, padding='valid')(layer_input)
d = LeakyReLU(alpha=0.2)(d)
if normalization:
d = InstanceNormalization()(d)
return d
img = Input(shape=img_shape)
d = img
for i in range(num_layers):
_norm = False if i == 0 else True
if i == 0:
stride = 3
else:
stride = 2
d = d_layer(d, df*2**i,normalization=_norm,f_size=6,strides=stride)
flat_repr = Flatten()(d)
#validity = Conv2D(1, kernel_size=4, strides=1, padding='same')(d4)
#print("flat_repr.get_shape().as_list():",flat_repr.get_shape().as_list())
#print("flat_repr.get_shape().as_list()[1:]:",flat_repr.get_shape().as_list()[1:])
gan_logit = Dense(df*2**(num_layers-1),kernel_initializer='glorot_uniform')(flat_repr)
gan_logit = LeakyReLU(alpha=0.2)(gan_logit)
gan_prob = Dense(1, activation='sigmoid')(gan_logit)
au_logit = Dense(df*2**(num_layers-1),kernel_initializer='glorot_uniform')(flat_repr)
au_logit = LeakyReLU(alpha=0.2)(au_logit)
au_pred = Dense(AU_num, activation=act_multi_label,kernel_initializer='glorot_uniform')(au_logit)
return Model(img, [gan_prob,au_pred])
if __name__ == '__main__':
d = build_discriminator(img_shape=(112,112,3),df=64,AU_num=17)
optimizer = Adam(0.0002, 0.5)
print("******** Discriminator/Classifier ********")
d.summary()
d.compile(loss=['binary_crossentropy','mse'],
optimizer=optimizer,
metrics=['accuracy','mean_squared_error'],
loss_weights=[1, 1])
g_enc , g_dec = build_generator_enc_dec(img_shape=(112,112,3),gf=64,
AU_num=17,channels=3,tranform_layer=True)
print("******** Generator_ENC ********")
g_enc.summary()
print("******** Generator_DEC ********")
g_dec.summary()