-
Notifications
You must be signed in to change notification settings - Fork 0
/
keras_models.py
165 lines (136 loc) · 6.29 KB
/
keras_models.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
from tensorflow.python.keras.applications import ResNet50
from tensorflow.python.keras.applications import Xception as xcpt
from tensorflow.python.keras.applications import InceptionResNetV2 as irnv2
from tensorflow.python.keras.applications import DenseNet121 as dn121
from keras_vggface.vggface import VGGFace
from tensorflow.python.keras.models import Model
from tensorflow.python.keras.layers import Flatten, Dense, Activation
from tensorflow.python.keras.layers import Conv2D, BatchNormalization
from tensorflow.python.keras.layers import Dropout, Input
from tensorflow.python.keras.optimizers import Adam
from layers import GlobalCovPooling2D, Transformer
import numpy as np
def get_initial_locnet_weights(output_size):
"""
Initialize the transformation parameters to the identity transformation
"""
b = np.zeros((2, 3), dtype='float32')
b[0, 0] = 1
b[1, 1] = 1
W = np.zeros((output_size, 6), dtype='float32')
weights = [W, b.flatten()]
return weights
def RN50(input_shape=(192, 192, 3), num_classes=8, lr=0.001):
resnet50 = ResNet50(include_top=False, weights=None, input_shape=input_shape, pooling='avg')
out = resnet50.output
out = Dense(8, activation='softmax')(out)
model = Model(inputs=resnet50.input, outputs=out)
model.compile(optimizer=Adam(lr=lr),
loss='categorical_crossentropy',
metrics=['accuracy'])
return model
def RN50CovPool(input_shape=(192, 192, 3), num_classes=8, lr=0.001):
resnet50 = ResNet50(include_top=False, weights=None, input_shape=input_shape)
out = resnet50.output
out = Conv2D(256, (1, 1))(out)
out = BatchNormalization()(out)
out = Activation('relu')(out)
out = GlobalCovPooling2D(num_iter=5)(out)
out = Flatten()(out)
out = Dense(8)(out)
out = Activation('softmax')(out)
model = Model(inputs=resnet50.input, outputs=out)
model.compile(optimizer=Adam(lr=lr),
loss='categorical_crossentropy',
metrics=['accuracy'])
return model
def VGGFTrans(input_shape=(197, 197, 3), num_classes=8, lr=0.001):
vggface = VGGFace(model='resnet50', include_top=False, input_shape=input_shape, pooling='avg')
last_layer = vggface.get_layer('avg_pool').output
x = Flatten(name='flatten')(last_layer)
out = Dense(num_classes, activation='softmax', name='classifier')(x)
# Freeze the weights of the first layers
i = 0
layer = vggface.layers[i]
while layer.name != 'conv5_1_1x1_reduce':
layer.trainable = False
i += 1
layer = vggface.layers[i]
model = Model(vggface.input, out)
model.compile(optimizer=Adam(lr=lr),
loss='categorical_crossentropy',
metrics=['accuracy'])
return model
def Xception(input_shape=(192, 192, 3), num_classes=8, lr=0.001):
xception = xcpt(include_top=True, classes=8, weights=None, input_shape=input_shape, pooling='avg')
xception.compile(optimizer=Adam(lr=lr),
loss='categorical_crossentropy',
metrics=['accuracy'])
return xception
def XceptionCov(input_shape=(192, 192, 3), num_classes=8, num_iter=5):
xcptcov = xcpt(include_top=False, weights=None, input_shape=input_shape)
out = xcptcov.layers[-7].output
out = GlobalCovPooling2D(num_iter=num_iter)(out)
out = Dense(num_classes)(out)
out = Activation('softmax')(out)
model = Model(inputs=xcptcov.input, outputs=out)
return model
def InceptionResNetV2(input_shape=(192, 192, 3), num_classes=8, lr=0.001):
inres = irnv2(include_top=True, classes=8, weights=None, input_shape=input_shape, pooling='avg')
inres.compile(optimizer=Adam(lr=lr),
loss='categorical_crossentropy',
metrics=['accuracy'])
return inres
def DenseNet121(input_shape=(192, 192, 3), num_classes=8, lr=0.001):
dnet = dn121(include_top=True, classes=8, weights=None, input_shape=input_shape, pooling='avg')
dnet.compile(optimizer=Adam(lr=lr),
loss='categorical_crossentropy',
metrics=['accuracy'])
return dnet
def DenseNet121Cov(input_shape=(192, 192, 3), num_classes=8, num_iter=5):
dnetcov = dn121(include_top=False, weights=None, input_shape=input_shape)
out = dnetcov.output
out = Conv2D(512, (1, 1), kernel_initializer='he_normal')(out)
out = BatchNormalization()(out)
out = Activation('relu')(out)
out = GlobalCovPooling2D(num_iter=num_iter)(out)
out = Dense(num_classes)(out)
out = Activation('softmax')(out)
model = Model(inputs=dnetcov.input, outputs=out)
return model
def DenseNet121CovDropout(input_shape=(192, 192, 3), num_classes=8, num_iter=5):
dnetcov = dn121(include_top=False, weights=None, input_shape=input_shape)
out = dnetcov.output
out = Conv2D(512, (1, 1), kernel_initializer='he_normal')(out)
out = BatchNormalization()(out)
out = Activation('relu')(out)
out = GlobalCovPooling2D(num_iter=num_iter)(out)
out = Dropout(rate=0.5)(out)
out = Dense(num_classes)(out)
out = Activation('softmax')(out)
model = Model(inputs=dnetcov.input, outputs=out)
return model
def STNDenseNet121CovDropout(input_shape=(192, 192, 3), num_classes=8, num_iter=5):
image = Input(shape=input_shape)
# Localization network
locnet = Conv2D(8, (4, 4), strides=(2, 2), padding='same', kernel_initializer='he_normal')(image)
locnet = Activation('relu')(locnet)
locnet = Conv2D(10, (4, 4), strides=(2, 2), padding='same', kernel_initializer='he_normal')(locnet)
locnet = Activation('relu')(locnet)
locnet = Flatten()(locnet)
locnet = Dense(32)(locnet)
locnet = Activation('relu')(locnet)
locnet = Dense(6, weights=get_initial_locnet_weights(32), name='locnet_params')(locnet)
# Transformation using sampling grid
warped = Transformer((input_shape[0], input_shape[1]), name='sampler')([image, locnet])
dnetcov = dn121(include_top=False, weights=None, input_tensor=warped)
out = dnetcov.output
out = Conv2D(512, (1, 1), kernel_initializer='he_normal')(out)
out = BatchNormalization()(out)
out = Activation('relu')(out)
out = GlobalCovPooling2D(num_iter=num_iter)(out)
out = Dropout(rate=0.5)(out)
out = Dense(num_classes)(out)
out = Activation('softmax')(out)
model = Model(inputs=dnetcov.input, outputs=out)
return model