-
Notifications
You must be signed in to change notification settings - Fork 0
/
finalldmkarch.py
254 lines (202 loc) · 9.9 KB
/
finalldmkarch.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 21 16:00:48 2018
@author: ck807
"""
import numpy as np
import tensorflow as tf
from keras.models import Model
import matplotlib.pyplot as plt
import keras
from keras import layers
from keras.layers.convolutional import Conv2D, UpSampling2D, SeparableConv2D
from keras.layers.pooling import MaxPooling2D, GlobalAveragePooling2D, AveragePooling2D
from keras.layers import Input, Dropout, Dense, BatchNormalization
from keras.regularizers import l2
from keras.optimizers import SGD, RMSprop, Adam
from keras.metrics import mean_absolute_error
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
from frozenresidualblockwithlayernames import ResidualR
from frozenresidualblockwithlayernames import initial_conv_block1, Residual2, Residual3, Residual4, Residual5, Residual6
from frozenresidualblockwithlayernames import Residual7, Residual8, Residual9, Residual10, Residual11, Residual12
from frozenresidualblockwithlayernames import Residual13, Residual14, Residual15, Residual16, Residual17, Residual18, Residual19
from se import squeeze_excite_block
from layers import initial_conv_block, bottleneck_block_with_se
import keras.backend as K
trainData = np.load('trainDataRegressor.npy')
trainLabel = np.load('trainLabelRegressor.npy')
valData = np.load('valDataRegressor.npy')
valLabel = np.load('valLabelRegressor.npy')
#trainData = trainData[0:3500,:,:,:]
#trainLabel = trainLabel[0:3500,:]
#valData = valData[0:500,:,:,:]
#valLabel = valLabel[0:500,:]
def huber_loss(y_true, y_pred, clip_delta=1.0):
error = y_true - y_pred
cond = K.abs(error) < clip_delta
squared_loss = 0.5 * K.square(error)
linear_loss = clip_delta * (K.abs(error) - 0.5 * clip_delta)
return tf.where(cond, squared_loss, linear_loss)
def huber_loss_mean(y_true, y_pred, clip_delta=1.0):
return K.mean(huber_loss(y_true, y_pred, clip_delta))
w = 10.0
e = 2.0
c = w - w * K.log(1 + (w/e))
#print('Wing Loss Parameters:')
#print('w = ', w)
#print('e = ', e)
#sess=tf.Session()
#print('c = ', sess.run(c))
def wingLoss(y_true, y_pred, w=w, e=e, c=c):
error = y_true - y_pred
cond = K.abs(error) < w
true = w * (K.log(1 + (K.abs(error)/e)))
otherwise = K.abs(error) - c
return tf.where(cond, true, otherwise)
with tf.device('/device:GPU:2'):
input = Input((192, 192, 3), name='Input')
conv1 = initial_conv_block1(input)
conv1 = Residual2(16, 32, conv1)
pool1 = MaxPooling2D(pool_size=(2, 2), name='MaxPool1')(conv1)
conv2 = Residual3(32, 32, pool1)
conv2 = Residual4(32, 64, conv2)
pool2 = MaxPooling2D(pool_size=(2, 2), name='MaxPool2')(conv2)
conv3 = Residual5(64, 64, pool2)
conv3 = Residual6(64, 128, conv3)
pool3 = MaxPooling2D(pool_size=(2, 2), name='MaxPool3')(conv3)
conv4 = Residual7(128, 128, pool3)
conv4 = Residual8(128, 256, conv4)
drop4 = Dropout(0.2, name='Dropout1')(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2), name='MaxPool4')(drop4)
conv5 = Residual9(256, 256, pool4)
conv5 = Residual10(256, 128, conv5)
drop5 = Dropout(0.2, name='Dropout2')(conv5)
up6 = Conv2D(128, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal', name='UpConv1')(UpSampling2D(size = (2,2), name='Up1')(drop5))
merge6 = keras.layers.Concatenate(name='Concat1')([drop4,up6])
conv6 = Residual11(384, 128, merge6)
conv6_1 = Residual12(128, 64, conv6)
up7 = Conv2D(64, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal', name='UpConv2')(UpSampling2D(size = (2,2), name='Up2')(conv6_1))
merge7 = keras.layers.Concatenate(name='Concat2')([conv3,up7])
conv7 = Residual13(192, 64, merge7)
conv7_1 = Residual14(64, 32, conv7)
up8 = Conv2D(32, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal', name='UpConv3')(UpSampling2D(size = (2,2), name='Up3')(conv7_1))
merge8 = keras.layers.Concatenate(name='Concat3')([conv2,up8])
conv8 = Residual15(96, 32, merge8)
conv8_1 = Residual16(32, 16, conv8)
up9 = Conv2D(16, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal', name='UpConv4')(UpSampling2D(size = (2,2), name='Up4')(conv8_1))
merge9 = keras.layers.Concatenate(name='Concat4')([conv1,up9])
conv9 = Residual17(48, 16, merge9)
conv10 = Residual18(16, 2, conv9)
conv10 = Residual19(2, 1, conv10)
conv11 = Conv2D(1, 1, activation = 'sigmoid', name='Output')(conv10)
with tf.device('/device:GPU:3'):
init = initial_conv_block(input, weight_decay=5e-4)
#x1 = ResidualR(32, 64, init) #192x192x64
#x1 = ResidualR(64, 64, x1)
#x1 = ResidualR(64, 64, x1) #192x192x64
x1 = Conv2D(64, (3,3), padding='same', kernel_initializer='he_normal')(init)
x1 = BatchNormalization()(x1)
x1 = layers.LeakyReLU()(x1)
x1concat = keras.layers.Concatenate()([x1, conv9]) #192x192x80
x1se = squeeze_excite_block(x1concat)
x1conv1 = SeparableConv2D(80, (1,1), padding = 'same', kernel_initializer = 'he_normal')(x1se)
x1conv1 = layers.LeakyReLU()(x1conv1)
x1conv2 = Conv2D(64, (1,1), padding = 'same', kernel_initializer = 'he_normal')(x1conv1)
x1conv2 = layers.LeakyReLU()(x1conv2)
x1pool = MaxPooling2D(pool_size=(2,2))(x1conv2)
#x2 = ResidualR(64, 96, x1pool) #96x96x96
#x2 = ResidualR(96, 96, x2)
#x2 = ResidualR(96, 96, x2) #96x96x96
x2 = Conv2D(96, (3,3), padding='same', kernel_initializer='he_normal')(x1pool)
x2 = BatchNormalization()(x2)
x2 = layers.LeakyReLU()(x2)
x2concat = keras.layers.Concatenate()([x2, conv8]) #96x96x128
x2se = squeeze_excite_block(x2concat)
x2conv1 = SeparableConv2D(128, (1,1), padding = 'same', kernel_initializer = 'he_normal')(x2se)
x2conv1 = layers.LeakyReLU()(x2conv1)
x2conv2 = Conv2D(96, (1,1), padding = 'same', kernel_initializer = 'he_normal')(x2conv1)
x2conv2 = layers.LeakyReLU()(x2conv2)
x2pool = MaxPooling2D(pool_size=(2,2))(x2conv2)
#with tf.device('/device:GPU:2'):
#x3 = ResidualR(96, 128, x2pool) #48x48x128
#x3 = ResidualR(128, 128, x3)
#x3 = ResidualR(128, 128, x3) #48x48x128
x3 = Conv2D(128, (3,3), padding='same', kernel_initializer='he_normal')(x2pool)
x3 = BatchNormalization()(x3)
x3 = layers.LeakyReLU()(x3)
x3concat = keras.layers.Concatenate()([x3, conv7]) #48x48x192
x3se = squeeze_excite_block(x3concat)
x3conv1 = SeparableConv2D(192, (1,1), padding = 'same', kernel_initializer = 'he_normal')(x3se)
x3conv1 = layers.LeakyReLU()(x3conv1)
x3conv2 = Conv2D(128, (1,1), padding = 'same', kernel_initializer = 'he_normal')(x3conv1)
x3conv2 = layers.LeakyReLU()(x3conv2)
x3pool = MaxPooling2D(pool_size=(2,2))(x3conv2)
#x4 = ResidualR(128, 256, x3pool) #24x24x256
#x4 = ResidualR(256, 256, x4)
#x4 = ResidualR(256, 256, x4) #24x24x256
x4 = Conv2D(256, (3,3), padding='same', kernel_initializer='he_normal')(x3pool)
x4 = BatchNormalization()(x4)
x4 = layers.LeakyReLU()(x4)
x4concat = keras.layers.Concatenate()([x4, conv6]) #24x24x384
x4se = squeeze_excite_block(x4concat)
x4conv1 = SeparableConv2D(384, (1,1), padding = 'same', kernel_initializer = 'he_normal')(x4se)
x4conv1 = layers.LeakyReLU()(x4conv1)
x4conv2 = Conv2D(256, (1,1), padding = 'same', kernel_initializer = 'he_normal')(x4conv1)
x4conv2 = layers.LeakyReLU()(x4conv2)
x4pool = MaxPooling2D(pool_size=(2,2))(x4conv2)
#with tf.device('/device:GPU:3'):
#x5 = ResidualR(256, 256, x4pool) #12x12x256
#x5 = ResidualR(256, 256, x5)
#x5 = ResidualR(256, 256, x5) #12x12x256
x5 = Conv2D(512, (3,3), padding='same', kernel_initializer='he_normal')(x4pool)
x5 = BatchNormalization()(x5)
x5 = layers.LeakyReLU()(x5)
#x5pool = MaxPooling2D(pool_size=(2,2))(x5)
#x6 = ResidualR(256, 512, x5pool) #6x6x512
#x6 = ResidualR(512, 512, x6)
#x6 = ResidualR(512, 512, x6) #6x6x512
block_shape = K.int_shape(x5)
xpool = AveragePooling2D(pool_size=(block_shape[1], block_shape[2]), strides=(1,1))(x5)
#xpool = MaxPooling2D(pool_size=(2,2))(x6)
flatten = layers.Flatten()(xpool)
#dense1 = layers.Dense(512)(flatten)
#dense1 = layers.LeakyReLU()(dense1)
#dense1 = layers.Dropout(0.5)(dense1)
#dense2 = layers.Dense(1024)(dense1)
#dense2 = layers.LeakyReLU()(dense2)
#dense2 = layers.Dropout(0.5)(dense2)
output = Dense(20, use_bias=False, kernel_regularizer=l2(5e-4), kernel_initializer='he_normal', activation='linear')(flatten)
model = Model(input, output)
model.load_weights('val_loss_Residual_checkpoint.h5', by_name=True)
model.summary()
model.compile(loss='mse', optimizer=SGD(lr=0.001, momentum=0.9, nesterov=True), metrics=[mean_absolute_error])
#SGD(lr=0.03, momentum=0.9, nesterov=True)
#'RMSprop'
#Adam(lr=0.001)
callbacks = [
EarlyStopping(monitor='val_loss', patience=10, verbose=1),
ModelCheckpoint("val_loss__final_ldmk_checkpoint.h5", monitor='val_loss', verbose=1, save_best_only=True, mode='auto'),
ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=5, verbose=1, mode='auto', epsilon=0.01, cooldown=0, min_lr=1e-7)
]
history = model.fit(trainData, trainLabel, validation_data=(valData, valLabel), batch_size=16, epochs=100, verbose=1, shuffle=True, callbacks=callbacks)
model.save('final_ldmk_model.h5')
plt.figure(0)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Loss')
plt.xlabel('epoch')
plt.legend(['loss', 'val_loss'], loc='upper right')
plt.savefig('final_ldmk_loss.png')
plt.figure(1)
plt.plot(history.history['lr'])
plt.title('Learning Rate')
plt.xlabel('epoch')
plt.savefig('final_ldmk_lr.png')
plt.figure(2)
plt.plot(history.history['mean_absolute_error'])
plt.plot(history.history['val_mean_absolute_error'])
plt.title('Mean Absolute Error Accuracy')
plt.xlabel('epoch')
plt.legend(['mean_absolute_error', 'val_mean_absolute_error'], loc='upper right')
plt.savefig('final_ldmk_metric.png')